CalculatedAmount.php 3.6 KB
<?php

namespace app\common\library;

use app\api\model\Level;
use app\api\model\MoneyLog;
use app\api\model\order\Order;
use app\api\model\promoter\PromoterLast;
use app\api\model\User;
use app\api\model\WithdrawalLog;
use EasyWeChat\Factory;


/**
 * 计算方法
 */
class CalculatedAmount {


	/**
	 * 计算可提现金额
	 */
	public static function calculation( $user_id ) {
		$moneyModel      = new MoneyLog();
		$withdrawalModel = new WithdrawalLog();
		$userModel       = new User();

		//计算退回直推佣金
		$one = $moneyModel->where( ['user_id' => $user_id, 'type' => 'out', 'commission_type' => 1] )->sum( 'money' );
		//计算退回推广佣金
		$two = $moneyModel->where( ['user_id' => $user_id, 'type' => 'out', 'commission_type' => 2] )->sum( 'money' );

		//①计算用户获得的直推佣金
		$direct_get_money = $moneyModel->where( ['user_id' => $user_id, 'type' => 'get', 'commission_type' => 1] )->sum( 'money' );
		//②计算用户获得的推广佣金
		$wide_get_money = $moneyModel->where( ['user_id' => $user_id, 'type' => 'get', 'commission_type' => 2] )->sum( 'money' );
		//③计算用户的退回佣金
		$out_money = $moneyModel->where( ['user_id' => $user_id, 'type' => 'out'] )->sum( 'money' );
		//④计算用户的提现总额
		$total_money = $withdrawalModel->where( ['user_id' => $user_id, 'status' => 1] )->sum( 'withdrawal_price' );

		//计算用户可提现金额
		$user_total_money = $direct_get_money + $wide_get_money - $out_money - $total_money;

		//更新用户信息
		$userModel->where( 'id', $user_id )->update( [
			'withdrawable_cash_price'     => $user_total_money,  //可提现金额
			'cumulative_withdrawal_price' => $total_money,       //累计提现金额
			'direct_commission'           => $direct_get_money - $one,  //累计直推佣金
			'promotion_commission'        => $wide_get_money - $two,    //累计推广佣金
		] );
	}


	/**
	 * 计算用户级别
	 */
	public static function calculation_level( $user_id ) {
		$userModel       = new User();
		$orderModel      = new Order();
		$levelModel      = new Level();
		$last_user_model = new PromoterLast();


		$level = $levelModel->select();

		//用户的下级用户
		$last_user_ids = $last_user_model->where( 'user_id', $user_id )->column( 'last_user_id' );

		//用户成为推广员时间
		$user_become_time = $userModel->where( 'id', $user_id )->value( 'become_promoter_time' );

		//成为推广员后的订单才算
		$where['createtime'] = ['>', $user_become_time];
		$where['user_id']    = ['=', $user_id];
		$where['status']     = ['=', '2'];

		//计算用户订单总金额
		$user_total_order = $orderModel->where( $where )->sum( 'pay_price' );

		//计算用户购买成功单量
		$user_count_order = $orderModel->where( $where )->count();

		//计算用户下一级订单总金额
		$last_total_order = $orderModel->whereIn( 'user_id', $last_user_ids )->sum( 'pay_price' );

		//计算用户下一级购买成功单量
		$last_count_order = $orderModel->whereIn( 'user_id', $last_user_ids )->count();

		//个人及直属下一级的累积金额
		$total_money = $user_total_order + $last_total_order;
		//个人及直属下一级的累积单量
		$total_order = $user_count_order + $last_count_order;


		//用户等级
		$user_level = 0;
		//判断用户是否满足升级条件
		foreach ( $level as &$item ) {
			if ( $item['cumulative_amount'] <= $user_total_order && $item['cumulative_order'] <= $user_count_order && $item['last_cumulative_amount'] <= $total_money && $item['last_cumulative_order'] <= $total_order ) {
				$user_level = $item['level'];
			} else {
				break;
			}

		}
		$userModel->where( 'id', $user_id )->update( ['level' => $user_level] );

	}

}