| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213 |
- <?php
- namespace addons\CloudStockTwo\common\logic\reward;
- use addons\CloudStockTwo\common\models\CloudStockTwoFillOrder;
- use addons\CloudStockTwo\common\models\CloudStockTwoBuyingOrder;
- use addons\CloudStockTwo\common\models\CloudStockTwoLevel;
- use addons\CloudStockTwo\common\models\CloudStockTwoGoods;
- use addons\CloudStockTwo\common\enums\CloudStockTwoEnum;
- use common\enums\AddonsEnum;
- use common\models\order\Order;
- use common\enums\StatusEnum;
- use common\enums\UserWalletEnum;
- use addons\CloudStockTwo\common\models\CloudStockTwoAgent;
- class AgentReward
- {
- protected $user_id; //用户id
- protected $parent_agent_list; //上级代理
- protected $stock_log; //云库存订单
- protected $team_prize_agents = [];
- protected $basic_conf;
- public $mall_id;
- public $wait_deduct_equal_money;
- public function __construct($mall_id,$user_id,$parent_agent_list,$stock_log,$basic_conf)
- {
- $this->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;
- }
- }
|