AgentReward.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. namespace addons\CloudStockTwo\common\logic\reward;
  3. use addons\CloudStockTwo\common\models\CloudStockTwoFillOrder;
  4. use addons\CloudStockTwo\common\models\CloudStockTwoBuyingOrder;
  5. use addons\CloudStockTwo\common\models\CloudStockTwoLevel;
  6. use addons\CloudStockTwo\common\models\CloudStockTwoGoods;
  7. use addons\CloudStockTwo\common\enums\CloudStockTwoEnum;
  8. use common\enums\AddonsEnum;
  9. use common\models\order\Order;
  10. use common\enums\StatusEnum;
  11. use common\enums\UserWalletEnum;
  12. use addons\CloudStockTwo\common\models\CloudStockTwoAgent;
  13. class AgentReward
  14. {
  15. protected $user_id; //用户id
  16. protected $parent_agent_list; //上级代理
  17. protected $stock_log; //云库存订单
  18. protected $team_prize_agents = [];
  19. protected $basic_conf;
  20. public $mall_id;
  21. public $wait_deduct_equal_money;
  22. public function __construct($mall_id,$user_id,$parent_agent_list,$stock_log,$basic_conf)
  23. {
  24. $this->mall_id = $mall_id;
  25. $this->user_id = $user_id;
  26. $this->parent_agent_list = $parent_agent_list;
  27. $this->stock_log = $stock_log;
  28. $this->basic_conf = $basic_conf;
  29. }
  30. /**
  31. * 计算并记录奖励,所有上级可获得的极差奖及团队奖
  32. *
  33. * @return float|int|mixed
  34. */
  35. public function sendReward()
  36. {
  37. $config = $this->getLevelConfig();
  38. if (!isset($this->basic_conf['team_prize_deep'])) $this->basic_conf['team_prize_deep'] = 0;
  39. if (!isset($this->basic_conf['equal_prize_deep'])) $this->basic_conf['equal_prize_deep'] = 0;
  40. if (!isset($this->basic_conf['equal_settle_type'])) $this->basic_conf['equal_settle_type'] = 0;
  41. if (!isset($this->basic_conf['calculation_basis'])) $this->basic_conf['calculation_basis'] = 0;
  42. if($this->stock_log['order_type'] == CloudStockTwoEnum::STOCK_ORDER_TYPE_USER){
  43. $order = CloudStockTwoBuyingOrder::findOne(['id'=>$this->stock_log['order_id']]);
  44. $money = ($order['after_distribution_price'] - $order['reward_unit_price']) / $order['num'];
  45. }else{
  46. $order = CloudStockTwoFillOrder::findOne(['id'=>$this->stock_log['order_id']]);
  47. //减去直推金额再结算
  48. $money = ($order['pay_money'] - $order['reward_unit_price']) / $order['num'];
  49. $agent = CloudStockTwoAgent::find()->where(['user_id' => $this->user_id, 'mall_id' => $this->mall_id,'status'=>StatusEnum::ENABLED])->asArray()->one();
  50. array_unshift($this->parent_agent_list,$agent);
  51. }
  52. $multiple = $this->stock_log['num'];
  53. $all_agent_num = count($this->parent_agent_list);
  54. // 初始化团队奖代理
  55. // if (empty($this->team_prize_agents)) {
  56. // $current_agent_level = 0;
  57. // foreach ($this->parent_agent_list as $key => $agent) {
  58. // if ($agent->level > $current_agent_level) {
  59. // $this->team_prize_agents[$key] = $agent;
  60. // $current_agent_level = $agent->level;
  61. // }
  62. // }
  63. // }
  64. // $team_prize_num = 0;
  65. $team_prize_list = [];
  66. // $log_total_price = 0;
  67. // $equal_settle_type = 0;
  68. $current_agent = [];
  69. $is_over = 0;//平级/越级奖是否已发放
  70. // $first_agent = null;//第一个拿极差的代理
  71. // $next_team_agent = null;//第二个拿极差代理
  72. // $equal_prize_num = 0; // 平级奖已领取层级数量
  73. $wait_deduct_equal_money = 0;//平级/越级奖金额
  74. $last_diff = 0;//上个极差奖励
  75. $replensh_price = 0;//上个极差奖代理价
  76. // $is_diff = 0;
  77. foreach ($this->parent_agent_list as $key => $team_agent) {
  78. if($team_agent['level'] > $current_agent['level']){
  79. //计算极差奖励 todo
  80. if(!$current_agent){
  81. $before_money = $money;
  82. }else{
  83. $before_money = CloudStockTwoGoods::getReplenshPrice($this->mall_id,$current_agent['level'],[$this->stock_log['goods_id']]) ?? 0;
  84. if($before_money > $money){
  85. $before_money = $money;
  86. }
  87. }
  88. //代理价
  89. $replensh_price = CloudStockTwoGoods::getReplenshPrice($this->mall_id,$team_agent['level'],[$this->stock_log['goods_id']]) ?? 0;
  90. //极差奖
  91. $diff_price = 0;
  92. $diff_price = $before_money - $replensh_price;
  93. if($diff_price > 0){
  94. $team_prize_list[$team_agent['user_id']][UserWalletEnum::DIFF] = [
  95. 'unit_goods_price' => $diff_price,
  96. 'diff_price' => $diff_price,
  97. 'num' => 0,
  98. 'agent_id' => $team_agent['user_id'],
  99. 'agent_level' => $team_agent['level'],
  100. ];
  101. $last_diff = $diff_price;
  102. // 推荐人等级小于被推荐人,拿越级奖,只判断第一个拿极差奖的上级是否会有越级奖
  103. // if(!$first_agent){
  104. // $first_agent = $team_agent;
  105. // }
  106. // $equal_prize_num = 0; // 下一个拿极差,重置平级奖已领取层级数量
  107. $team_prize_list[$team_agent['user_id']][UserWalletEnum::DIFF]['unit_goods_price'] -= $wait_deduct_equal_money;
  108. if($team_prize_list[$team_agent['user_id']][UserWalletEnum::DIFF]['unit_goods_price'] <= 0){
  109. unset($team_prize_list[$team_agent['user_id']][UserWalletEnum::DIFF]);
  110. }
  111. $wait_deduct_equal_money = 0;//平级奖金额
  112. //获得极差 重置设置
  113. $is_over = 0;
  114. }
  115. $current_agent = $team_agent;
  116. // $is_diff = 1;
  117. //获得极差奖 不再往下执行
  118. continue;
  119. }
  120. //平级/越级奖 只往上个获得极差奖代理往下找一个
  121. 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))){
  122. if($team_agent['level'] == $current_agent['level']){
  123. $equal_prize = $this->computing($config[$team_agent['level']]['commission'], $replensh_price, 'equal');
  124. if($equal_prize > 0){
  125. $team_prize_list[$team_agent['user_id']][UserWalletEnum::EQUAL] = [
  126. 'unit_goods_price' => $equal_prize,
  127. 'num' => 0,
  128. 'agent_id' => $team_agent['user_id'],
  129. 'agent_level' => $team_agent['level'],
  130. ];
  131. $wait_deduct_equal_money = $equal_prize;
  132. $is_over = 1;
  133. }
  134. }elseif($team_agent['level'] < $current_agent['level']){
  135. $over_prize = $this->computing($config[$team_agent['level']]['commission'], $replensh_price, 'over');
  136. if($over_prize > 0){
  137. $team_prize_list[$team_agent['user_id']][UserWalletEnum::OVER] = [
  138. 'unit_goods_price' => $over_prize,
  139. 'num' => 0,
  140. 'agent_id' => $team_agent['user_id'],
  141. 'agent_level' => $team_agent['level'],
  142. ];
  143. $wait_deduct_equal_money = $over_prize;
  144. $is_over = 1;
  145. }
  146. }
  147. }
  148. }
  149. //删除下单用户获得极差奖
  150. if(isset($team_prize_list[$this->user_id])){
  151. unset($team_prize_list[$this->user_id]);
  152. }
  153. //如无上级代理结算平级/越级奖,由平台/销售公司结算
  154. $this->wait_deduct_equal_money = $wait_deduct_equal_money;
  155. return $team_prize_list;
  156. }
  157. /**
  158. * @desc:计算
  159. * @Author: hua
  160. * @Date: 2021/10/27
  161. * @Time: 18:28
  162. * @Copyright: copyright (c) 2021 广东七件事集团
  163. * @param $config
  164. * @param $money
  165. * @param $type
  166. * @param int $multiple
  167. * @return float|int
  168. */
  169. public function computing($config, $money, $type, $multiple = 1)
  170. {
  171. $is_percent_key = "is_percent_{$type}";
  172. $agent_prize_key = "agent_{$type}_prize";
  173. $num = 0;
  174. if (isset($config[$is_percent_key]) && isset($config[$agent_prize_key])) {
  175. $num = $config[$is_percent_key] == 0 ? $money * $config["agent_{$type}_prize"] / 100 * $multiple : $config[$agent_prize_key] * $multiple;
  176. }
  177. return $num;
  178. }
  179. /**
  180. * 获取等级配置
  181. */
  182. public function getLevelConfig(){
  183. $params['where'] = [['mall_id' => $this->mall_id], ['status' => StatusEnum::ENABLED]];
  184. $params['index'] = 'level';
  185. $level_list = CloudStockTwoLevel::lists($params, true);
  186. if(empty($level_list)){
  187. return [];
  188. }
  189. foreach($level_list as &$list){
  190. $list['commission'] = json_decode($list['commission'],true);
  191. $list['score'] = json_decode($list['score'],true);
  192. $list['commission']['is_percent_over'] = 0;
  193. $list['score']['is_percent_over'] = 1;
  194. }
  195. unset($list);
  196. return $level_list;
  197. }
  198. }