mall_id = $mall_id; $this->user_id = $user_id; $this->parent_agent_list = $parent_agent_list; $this->stock_log = $stock_log; $this->basic_conf = $basic_conf; } /** * 计算并记录奖励,所有上级可获得的极差奖及团队奖 * * @return float|int|mixed */ public function sendReward() { $config = $this->getLevelConfig(); if (!isset($this->basic_conf['team_prize_deep'])) $this->basic_conf['team_prize_deep'] = 0; if (!isset($this->basic_conf['equal_prize_deep'])) $this->basic_conf['equal_prize_deep'] = 0; if (!isset($this->basic_conf['equal_settle_type'])) $this->basic_conf['equal_settle_type'] = 0; if (!isset($this->basic_conf['calculation_basis'])) $this->basic_conf['calculation_basis'] = 0; if($this->stock_log['order_type'] == CloudStockTwoEnum::STOCK_ORDER_TYPE_USER){ $order = CloudStockTwoBuyingOrder::findOne(['id'=>$this->stock_log['order_id']]); $money = ($order['after_distribution_price'] - $order['reward_unit_price']) / $order['num']; }else{ $order = CloudStockTwoFillOrder::findOne(['id'=>$this->stock_log['order_id']]); //减去直推金额再结算 $money = ($order['pay_money'] - $order['reward_unit_price']) / $order['num']; $agent = CloudStockTwoAgent::find()->where(['user_id' => $this->user_id, 'mall_id' => $this->mall_id,'status'=>StatusEnum::ENABLED])->asArray()->one(); array_unshift($this->parent_agent_list,$agent); } $multiple = $this->stock_log['num']; $all_agent_num = count($this->parent_agent_list); // 初始化团队奖代理 // if (empty($this->team_prize_agents)) { // $current_agent_level = 0; // foreach ($this->parent_agent_list as $key => $agent) { // if ($agent->level > $current_agent_level) { // $this->team_prize_agents[$key] = $agent; // $current_agent_level = $agent->level; // } // } // } // $team_prize_num = 0; $team_prize_list = []; // $log_total_price = 0; // $equal_settle_type = 0; $current_agent = []; $is_over = 0;//平级/越级奖是否已发放 // $first_agent = null;//第一个拿极差的代理 // $next_team_agent = null;//第二个拿极差代理 // $equal_prize_num = 0; // 平级奖已领取层级数量 $wait_deduct_equal_money = 0;//平级/越级奖金额 $last_diff = 0;//上个极差奖励 $replensh_price = 0;//上个极差奖代理价 // $is_diff = 0; foreach ($this->parent_agent_list as $key => $team_agent) { if($team_agent['level'] > $current_agent['level']){ //计算极差奖励 todo if(!$current_agent){ $before_money = $money; }else{ $before_money = CloudStockTwoGoods::getReplenshPrice($this->mall_id,$current_agent['level'],[$this->stock_log['goods_id']]) ?? 0; if($before_money > $money){ $before_money = $money; } } //代理价 $replensh_price = CloudStockTwoGoods::getReplenshPrice($this->mall_id,$team_agent['level'],[$this->stock_log['goods_id']]) ?? 0; //极差奖 $diff_price = 0; $diff_price = $before_money - $replensh_price; if($diff_price > 0){ $team_prize_list[$team_agent['user_id']][UserWalletEnum::DIFF] = [ 'unit_goods_price' => $diff_price, 'diff_price' => $diff_price, 'num' => 0, 'agent_id' => $team_agent['user_id'], 'agent_level' => $team_agent['level'], ]; $last_diff = $diff_price; // 推荐人等级小于被推荐人,拿越级奖,只判断第一个拿极差奖的上级是否会有越级奖 // if(!$first_agent){ // $first_agent = $team_agent; // } // $equal_prize_num = 0; // 下一个拿极差,重置平级奖已领取层级数量 $team_prize_list[$team_agent['user_id']][UserWalletEnum::DIFF]['unit_goods_price'] -= $wait_deduct_equal_money; if($team_prize_list[$team_agent['user_id']][UserWalletEnum::DIFF]['unit_goods_price'] <= 0){ unset($team_prize_list[$team_agent['user_id']][UserWalletEnum::DIFF]); } $wait_deduct_equal_money = 0;//平级奖金额 //获得极差 重置设置 $is_over = 0; } $current_agent = $team_agent; // $is_diff = 1; //获得极差奖 不再往下执行 continue; } //平级/越级奖 只往上个获得极差奖代理往下找一个 if(!$is_over && $this->basic_conf['is_equal'] && (isset($team_prize_list[$current_agent['user_id']][UserWalletEnum::DIFF]) || ($current_agent && $current_agent['user_id'] == $this->user_id && $replensh_price <= $money))){ if($team_agent['level'] == $current_agent['level']){ $equal_prize = $this->computing($config[$team_agent['level']]['commission'], $replensh_price, 'equal'); if($equal_prize > 0){ $team_prize_list[$team_agent['user_id']][UserWalletEnum::EQUAL] = [ 'unit_goods_price' => $equal_prize, 'num' => 0, 'agent_id' => $team_agent['user_id'], 'agent_level' => $team_agent['level'], ]; $wait_deduct_equal_money = $equal_prize; $is_over = 1; } }elseif($team_agent['level'] < $current_agent['level']){ $over_prize = $this->computing($config[$team_agent['level']]['commission'], $replensh_price, 'over'); if($over_prize > 0){ $team_prize_list[$team_agent['user_id']][UserWalletEnum::OVER] = [ 'unit_goods_price' => $over_prize, 'num' => 0, 'agent_id' => $team_agent['user_id'], 'agent_level' => $team_agent['level'], ]; $wait_deduct_equal_money = $over_prize; $is_over = 1; } } } } //删除下单用户获得极差奖 if(isset($team_prize_list[$this->user_id])){ unset($team_prize_list[$this->user_id]); } //如无上级代理结算平级/越级奖,由平台/销售公司结算 $this->wait_deduct_equal_money = $wait_deduct_equal_money; return $team_prize_list; } /** * @desc:计算 * @Author: hua * @Date: 2021/10/27 * @Time: 18:28 * @Copyright: copyright (c) 2021 广东七件事集团 * @param $config * @param $money * @param $type * @param int $multiple * @return float|int */ public function computing($config, $money, $type, $multiple = 1) { $is_percent_key = "is_percent_{$type}"; $agent_prize_key = "agent_{$type}_prize"; $num = 0; if (isset($config[$is_percent_key]) && isset($config[$agent_prize_key])) { $num = $config[$is_percent_key] == 0 ? $money * $config["agent_{$type}_prize"] / 100 * $multiple : $config[$agent_prize_key] * $multiple; } return $num; } /** * 获取等级配置 */ public function getLevelConfig(){ $params['where'] = [['mall_id' => $this->mall_id], ['status' => StatusEnum::ENABLED]]; $params['index'] = 'level'; $level_list = CloudStockTwoLevel::lists($params, true); if(empty($level_list)){ return []; } foreach($level_list as &$list){ $list['commission'] = json_decode($list['commission'],true); $list['score'] = json_decode($list['score'],true); $list['commission']['is_percent_over'] = 0; $list['score']['is_percent_over'] = 1; } unset($list); return $level_list; } }