TeamReward.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. <?php
  2. namespace addons\CloudStock\common\logic\reward;
  3. use addons\CloudStock\common\enums\CloudStockEnum;
  4. use addons\CloudStock\common\models\CloudStockAgent;
  5. use addons\CloudStock\common\models\CloudStockBonusPoolUser;
  6. use addons\CloudStock\common\models\CloudStockFillStatistics;
  7. use addons\CloudStock\common\models\CloudStockIncreaseDetail;
  8. use addons\CloudStock\common\models\CloudStockIncreaseByPlatformLog;
  9. use common\enums\AddonsEnum;
  10. use common\enums\StatusEnum;
  11. use common\helpers\DateHelper;
  12. use common\models\user\User;
  13. use common\enums\UserWalletEnum;
  14. class TeamReward
  15. {
  16. //云仓个人团队补货奖励开发
  17. public static function index($mall_id,$time,$start=null,$end=null)
  18. {
  19. $configs = \Yii::$app->services->setting->addons->get($mall_id, AddonsEnum::ADDONS_NAME_CLOUD_STOCK, 'basic');
  20. $award_proportion_settlement_type = $configs['award_proportion_settlement_type'] ?? 0;
  21. $is_upgrade_bag_give_goods = $configs['is_upgrade_bag_give_goods'] ?? 0;
  22. $is_equal_2_step = $configs['is_equal_2_step'] ?? 0;
  23. $award_proportion_reward_type = $configs['award_proportion_reward_type'] ?? 0;
  24. //清零方式
  25. $award_proportion_clear_type = $configs['award_proportion_clear_type'] ?? 0;
  26. //计算方式
  27. $award_proportion_calculate_type = $configs['award_proportion_calculate_type'] ?? 0;
  28. $cloud_stock_level_arr = [];
  29. $cloud_stock_bonus_level_arr = [];
  30. $award_proportion = [];
  31. if(empty($configs['team_reward_goods_id'])){
  32. $team_reward_goods_id = [];
  33. }else{
  34. $team_reward_goods_id = json_decode($configs['team_reward_goods_id'],true);
  35. }
  36. if (!empty($configs['award_proportion'])) {
  37. $award_proportion = json_decode($configs['award_proportion'], true);
  38. }
  39. if (!empty($configs['cloud_stock_level_arr'])) {
  40. $cloud_stock_level_arr = json_decode($configs['cloud_stock_level_arr'], true);
  41. }
  42. if (!empty($configs['cloud_stock_bonus_level_arr'])) {
  43. $cloud_stock_bonus_level_arr = json_decode($configs['cloud_stock_bonus_level_arr'], true);
  44. }
  45. $day = date('d', $time);
  46. if ($day != '01') return false;
  47. switch ($award_proportion_settlement_type) {
  48. case 0:
  49. if(!isset($start)||!isset($end)){
  50. $res = DateHelper::lastMonth();
  51. $start = $res['start'];
  52. $end = $res['end'];
  53. }
  54. break;
  55. case 1:
  56. $month = date('m', $time);
  57. if(!in_array($month,['01','04','07','10'])){
  58. return false;
  59. }
  60. if(!isset($start)||!isset($end)) {
  61. $res = DateHelper::lastSeason();
  62. $start = $res['start'];
  63. $end = $res['end'];
  64. }
  65. break;
  66. // case 2:
  67. // if(!isset($start)||!isset($end)){
  68. // $res = DateHelper::lastYear();
  69. // $start = $res['start'];
  70. // $end = $res['end'];
  71. // }
  72. // break;
  73. }
  74. if (empty($start) || empty($end)) return false;
  75. if($award_proportion_reward_type==1){
  76. if($award_proportion_calculate_type == 1){
  77. $select = 'user_id,sum(num * fill_unit_price) as num';
  78. }else{
  79. $select = 'user_id,sum(num) as num';
  80. }
  81. $changes_type = [CloudStockEnum::CHANGE_TYPE_DEDUTION_FILL];
  82. if ($is_upgrade_bag_give_goods) $changes_type[] = CloudStockEnum::STOCK_CHANGE_TYPE105;
  83. $list = CloudStockIncreaseByPlatformLog::lists([
  84. 'where' => [
  85. ['>=', 'created_at', $start],
  86. ['<=', 'created_at', $end],
  87. ['changes_type' => $changes_type],
  88. ['status' => StatusEnum::ENABLED],
  89. ['mall_id' => $mall_id],
  90. ['goods_id' => $team_reward_goods_id],
  91. ],
  92. 'group' => 'user_id',
  93. 'select' => $select,
  94. 'index' => 'user_id',
  95. ]);
  96. }else{
  97. $changes_type = [CloudStockEnum::STOCK_CHANGE_TYPE100];
  98. if ($is_upgrade_bag_give_goods) $changes_type[] = CloudStockEnum::STOCK_CHANGE_TYPE105;
  99. $list = CloudStockIncreaseDetail::lists([
  100. 'where' => [
  101. ['>=', 'created_at', $start],
  102. ['<=', 'created_at', $end],
  103. ['changes_type' => $changes_type],
  104. ['status' => StatusEnum::ENABLED],
  105. ['mall_id' => $mall_id],
  106. ['goods_id' => $team_reward_goods_id],
  107. ],
  108. 'group' => 'user_id',
  109. 'select' => 'user_id,sum(num) as num',
  110. 'index' => 'user_id',
  111. ]);
  112. }
  113. if (empty($list)) return false;
  114. //清零时间
  115. $clear_time = false;
  116. $year_start = null;
  117. $year_end = null;
  118. if($award_proportion_reward_type == 1){
  119. switch ($award_proportion_clear_type) {
  120. case 0:
  121. $year = date("Y",$time);
  122. $year_before = date("Y",$start);
  123. $month = date('m', $time);
  124. if($year != $year_before && $month != '01') $clear_time = true;
  125. $year_start = strtotime($year_before.'-01-01 00:00:00');
  126. $year_end = strtotime($year_before.'-12-31 23:59:59');
  127. break;
  128. }
  129. }
  130. $t = \Yii::$app->db->beginTransaction();
  131. try {
  132. $user_ids = array_column($list, 'user_id');
  133. $agent_list = CloudStockAgent::lists([
  134. 'where' => [
  135. ['status' => StatusEnum::ENABLED],
  136. ['mall_id' => $mall_id],
  137. ['user_id' => $user_ids],
  138. ],
  139. 'index' => 'user_id',
  140. ]);
  141. $bonus_pool_agent_list = CloudStockBonusPoolUser::lists([
  142. 'where' => [
  143. ['status' => StatusEnum::ENABLED],
  144. ['mall_id' => $mall_id],
  145. ['user_id' => $user_ids],
  146. ],
  147. 'index' => 'user_id',
  148. ]);
  149. $direct_list = [];
  150. $indirect_list = [];
  151. if (!empty($is_equal_2_step)) {
  152. $user_list = User::lists([
  153. 'where' => [
  154. ['status' => StatusEnum::ENABLED],
  155. ['mall_id' => $mall_id],
  156. ['id' => $user_ids],
  157. ],
  158. ]);
  159. foreach ($user_list as $value) {
  160. $direct_list[$value['parent_id']][] = $value['id'];
  161. $indirect_list[$value['second_parent_id']][] = $value['id'];
  162. }
  163. }
  164. foreach ($list as $item) {
  165. if(empty($agent_list[$item['user_id']])) continue;
  166. if(!in_array($agent_list[$item['user_id']]['level'],$cloud_stock_level_arr)) continue;
  167. if(!empty($cloud_stock_bonus_level_arr)){
  168. if(empty($bonus_pool_agent_list[$item['user_id']])) continue;
  169. if(!in_array($bonus_pool_agent_list[$item['user_id']]['level'],$cloud_stock_bonus_level_arr)) continue;
  170. }
  171. $model = CloudStockFillStatistics::findOne([
  172. 'mall_id' => $mall_id,
  173. 'user_id' => $item['user_id'],
  174. 'start' => $start,
  175. 'end' => $end,
  176. ]);
  177. if (!empty($model)) continue;
  178. $self_num = $item['num'];
  179. $direct_num = 0;
  180. $indirect_num = 0;
  181. $direct_money = 0;
  182. $indirect_money = 0;
  183. if (isset($direct_list[$item['user_id']])) {
  184. foreach ($direct_list[$item['user_id']] as $user_id) {
  185. if (empty($agent_list[$item['user_id']]) || empty($agent_list[$user_id])) {
  186. continue;
  187. }
  188. if ($agent_list[$item['user_id']]['level'] != $agent_list[$user_id]['level']) continue;
  189. $temp_num = $list[$user_id]['num'] ?? 0;
  190. $temp_money = self::getRewardMoney($temp_num, $award_proportion,$award_proportion_reward_type,$user_id,$award_proportion_calculate_type,$clear_time,$year_start,$year_end);
  191. $direct_money += $temp_money;
  192. $direct_num += $temp_num;
  193. }
  194. }
  195. if (isset($indirect_list[$item['user_id']])) {
  196. foreach ($indirect_list[$item['user_id']] as $user_id) {
  197. if (empty($agent_list[$item['user_id']]) || empty($agent_list[$user_id])) {
  198. continue;
  199. }
  200. if ($agent_list[$item['user_id']]['level'] != $agent_list[$user_id]['level']) continue;
  201. $temp_num = $list[$user_id]['num'] ?? 0;
  202. $temp_money = self::getRewardMoney($temp_num, $award_proportion,$award_proportion_reward_type,$user_id,$award_proportion_calculate_type,$clear_time,$year_start,$year_end);
  203. $indirect_money += $temp_money;
  204. $indirect_num += $temp_num;
  205. }
  206. }
  207. $total_num = $self_num + $direct_num + $indirect_num;
  208. $money = self::getRewardMoney($total_num, $award_proportion,$award_proportion_reward_type,$item['user_id'],$award_proportion_calculate_type,$clear_time,$year_start,$year_end);
  209. $money = $money - $direct_money - $indirect_money;
  210. $add_data = [
  211. 'mall_id' => $mall_id,
  212. 'user_id' => $item['user_id'],
  213. 'level' => $agent_list[$item['user_id']]['level']??0,
  214. 'self_num' => $self_num,
  215. 'direct_num' => $direct_num,
  216. 'indirect_num' => $indirect_num,
  217. 'start' => $start,
  218. 'end' => $end,
  219. 'money' => $money,
  220. 'award_proportion_settlement_type' => $award_proportion_settlement_type,
  221. 'is_upgrade_bag_give_goods' => $is_upgrade_bag_give_goods,
  222. 'is_equal_2_step' => $is_equal_2_step,
  223. 'award_proportion' => json_encode($award_proportion),
  224. 'award_proportion_calculate_type' => $award_proportion_calculate_type,
  225. ];
  226. $model = CloudStockFillStatistics::setData($add_data);
  227. if ($money > 0) {
  228. $money = bcadd($money, 0, 2);
  229. $insert_wallet[] = ['mall_id' => $mall_id, 'user_id' => $item['user_id'],
  230. 'is_frozen' => false,
  231. 'wallet_type' => 'commission', 'money' => $money,
  232. 'desc' => '云仓个人团队补货奖励',
  233. 'source_table' => CloudStockFillStatistics::tableName(),
  234. 'source_table_id' => $model['id'] ?? 0,
  235. 'from_type' => AddonsEnum::ADDONS_NAME_CLOUD_STOCK,
  236. 'capital_type' => UserWalletEnum::CLOUD_STOCK_FILL_ACHIEVEMENT,
  237. ];
  238. }
  239. }
  240. $t->commit();
  241. if (!empty($insert_wallet)){
  242. // var_dump('TeamReward:'.json_encode($insert_wallet));
  243. CloudStockAgent::sendUserWalletQueue($insert_wallet);
  244. }
  245. return true;
  246. } catch (\Exception $e) {
  247. $t->rollBack();
  248. var_dump("TeamReward::index" . $e->getMessage().$e->getLine().$e->getFile());
  249. return false;
  250. }
  251. }
  252. protected static function getRewardMoney($num, $award_proportion,$award_proportion_reward_type,$user_id,$award_proportion_calculate_type,$clear_time,$year_start,$year_end)
  253. {
  254. $column = array_column($award_proportion, 'num');
  255. array_multisort($column, SORT_DESC, $award_proportion);//升序
  256. $money = 0;//奖励金额
  257. //结算数量
  258. $settle_num = $num;
  259. if(!empty($award_proportion_reward_type) && !$clear_time){
  260. $before = CloudStockFillStatistics::find()
  261. ->select('sum(self_num + direct_num + indirect_num) as num')
  262. ->where(['user_id'=>$user_id,'award_proportion_calculate_type'=>$award_proportion_calculate_type])
  263. ->andWhere(['>=','start',$year_start])
  264. ->andWhere(['<=','end',$year_end])
  265. ->asArray()
  266. ->one();
  267. if(!empty($before['num'])){
  268. $settle_num += $before['num'];
  269. }
  270. }
  271. foreach ($award_proportion as $k => $v) {
  272. if ($settle_num >= $v['num']) {
  273. $money = $v['money'];
  274. break;
  275. }
  276. }
  277. if($award_proportion_calculate_type == 1){
  278. $money = $money / 100;
  279. }
  280. return $money*$num;
  281. }
  282. }