TeamAchievement.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. namespace addons\Agent\common\logic\reward;
  3. use addons\Agent\common\models\Agent;
  4. use addons\Agent\common\models\AgentCommissionLevelSetting;
  5. use addons\Agent\common\models\AgentTeamAchievement;
  6. use common\enums\AddonsEnum;
  7. use common\enums\RedisKeyEnum;
  8. use common\enums\StatusEnum;
  9. use common\enums\UserWalletEnum;
  10. use common\helpers\LockHelper;
  11. use common\models\mall\Mall;
  12. use services\common\UserWalletService;
  13. use yii\db\Exception;
  14. /**
  15. * @desc 渠道奖励
  16. * Class AgentReward
  17. * @Author: hua
  18. * @DateTime: 2021/10/27 15:24
  19. * @Copyright: copyright (c) 2021 广东七件事集团
  20. * @package addons\Agent\common\logic\reward
  21. */
  22. class TeamAchievement
  23. {
  24. public function teamAchievementExtra(){
  25. //结算按月结算渠道业绩额外奖励
  26. $lock_key = RedisKeyEnum::suffix(RedisKeyEnum::LOCK_AGENT_TEAM_ACHIEVEMENT);
  27. $identification = uniqid();
  28. $t = \Yii::$app->db->beginTransaction();
  29. try {
  30. if (!LockHelper::lock($lock_key, $identification, 30, 60)) throw new \Exception('业绩进入待结算,当前访问过多');
  31. $commission_type = 'commission';
  32. $text = $commission_type == 'commission' ? '佣金' : '积分';
  33. //加锁
  34. $time = time();
  35. $day = date('d', $time);
  36. if ($day != '01') throw new Exception('现在不是每月1号');
  37. $last_month = date('Y-m', strtotime("-1 month"));
  38. // $last_month ='2022-07';//调测使用
  39. $mall_list = Mall::getEnableMallList(true);
  40. foreach ($mall_list as $mall) {
  41. $mall_id = $mall['id'];
  42. while (true){
  43. $agent_team_achievement_list = AgentTeamAchievement::find()->where(['mall_id' =>$mall_id, 'month' => $last_month,'is_settlement'=>0])->orderBy('created_at asc')->all();
  44. if(empty($agent_team_achievement_list)) break;
  45. $user_ids = array_column($agent_team_achievement_list, 'user_id');
  46. $agent_list = Agent::lists(['where' => [['user_id' => $user_ids], ['status' => StatusEnum::ENABLED]], 'index' => 'user_id'],false);
  47. $level_arr = array_column($agent_list, 'level');
  48. $where = [];
  49. $where[] = ['mall_id' => $mall_id];
  50. $where[] = ['level' => $level_arr];
  51. $where[] = ['status' => StatusEnum::ENABLED];
  52. $where[] = ['commission_type' => $commission_type];
  53. $agent_commission_level_setting = AgentCommissionLevelSetting::lists(['where' => $where, 'index' => 'level']);
  54. foreach ($agent_team_achievement_list as $agent_team_achievement) {
  55. $insert_wallet = [];
  56. $total_performance = $agent_team_achievement['personal_performance'] + $agent_team_achievement['team_performance'];
  57. if (isset($agent_list[$agent_team_achievement['user_id']])) {
  58. $agent = $agent_list[$agent_team_achievement['user_id']];
  59. if (isset($agent_commission_level_setting[$agent['level']])) {
  60. $team_achievement_rate = $agent_commission_level_setting[$agent['level']]['team_achievement'];
  61. $commission_nums = bcdiv($total_performance * $team_achievement_rate, 100, 2);
  62. $agent_team_achievement->rate = $team_achievement_rate;
  63. $agent_team_achievement->level = $agent['level'];
  64. if ($commission_nums >0){
  65. $capital_type = UserWalletEnum::TEAM_ACHIEVEMENT_EXTRA;
  66. $prize_name = $type_map[$capital_type] ?? '渠道业绩额外奖励';
  67. $desc = '用户(' . $agent['user_id'] . '),获得奖励,奖励类型:' . $prize_name . ',' . $text . ':' . $commission_nums;
  68. // echo $desc.PHP_EOL;
  69. $insert_wallet [] = ['mall_id' => $mall_id, 'user_id' => $agent['user_id'],
  70. 'is_frozen' => false,
  71. 'wallet_type' => $commission_type,
  72. 'money' => $commission_nums,
  73. 'desc' => $desc,
  74. 'source_table' => AgentTeamAchievement::tableName(),
  75. 'source_table_id' => $agent_team_achievement['id'],
  76. 'from_type' => AddonsEnum::ADDONS_NAME_AGENT,
  77. 'capital_type' => $capital_type,
  78. 'order_no' => $agent_team_achievement['id'],
  79. 'contributor_name' => '',
  80. 'contributor_id' => 0,
  81. 'contributor_money' => $total_performance,
  82. ];
  83. $res = UserWalletService::batchHandleByQueue($insert_wallet);
  84. if (empty($res)) throw new \Exception(UserWalletService::getStaticError());
  85. $field = 'total_' . $commission_type;
  86. $agent->$field += $commission_nums;
  87. $res = $agent->save();
  88. if ($res == false) throw new \Exception(Agent::getStaticError());
  89. }
  90. }
  91. }
  92. $agent_team_achievement->is_settlement = 1;
  93. $res = $agent_team_achievement->save();
  94. var_dump($res);
  95. if (empty($res)) throw new \Exception($agent_team_achievement->getErrorMessage());
  96. }
  97. }
  98. }
  99. LockHelper::uLock($lock_key,$identification);
  100. $t->commit();
  101. return true;
  102. } catch (Exception $e) {
  103. var_dump($e->getMessage());
  104. $t->rollBack();
  105. LockHelper::uLock($lock_key,$identification);
  106. return false;
  107. }
  108. }
  109. }