AchievementLogic.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. namespace addons\BossAchievement\common\logic;
  3. use addons\BossAchievement\common\enums\BossAchievementEnum;
  4. use addons\BossAchievement\common\models\BossAchievementLevelSetting;
  5. use addons\BossAchievement\common\models\BossAchievementMonth;
  6. use addons\BossAchievement\common\models\BossAchievementTeam;
  7. use addons\BossAchievement\common\models\BossAchievementUser;
  8. use common\enums\StatusEnum;
  9. use common\enums\UserWalletEnum;
  10. use common\models\mall\Mall;
  11. use common\models\user\UserPartitionRelationship;
  12. use common\traits\ErrorTrait;
  13. use Exception;
  14. use services\common\UserWalletService;
  15. use yii;
  16. use yii\helpers\Json;
  17. class AchievementLogic
  18. {
  19. use ErrorTrait;
  20. /**
  21. * 统计处理
  22. * @Author: lun
  23. * @DateTime: 2022/7/10 0010 16:03
  24. * @Copyright: copyright (c) 2021 广东七件事集团
  25. * @param $order
  26. */
  27. public function statistics($order)
  28. {
  29. $trans = Yii::$app->db->beginTransaction();
  30. try {
  31. // 获取用户所有上级
  32. $parent_ids = UserPartitionRelationship::getParentIdArr($order['user_id']);
  33. array_push($parent_ids, $order['user_id']);
  34. // 检查用户上级是否存在插件中
  35. $boss_parent_ids = BossAchievementUser::find()->select('user_id')->where(['user_id' => $parent_ids, 'status' => StatusEnum::ENABLED])->column();
  36. if (empty($boss_parent_ids)) return true;
  37. // 获取所有的业绩商品
  38. $goods_ids = BossAchievementLevelSetting::getAllAchievementGoodsIds($order['mall_id']);
  39. $month = date('Ym', time());
  40. foreach ($order['detail'] as $item) {
  41. // 判断订单商品是否在业绩商品中
  42. if (in_array($item['goods_id'], $goods_ids)) {
  43. // 增加个人消费业绩记录
  44. $res = BossAchievementTeam::setData(['mall_id' => $order['mall_id'], 'user_id' => $order['user_id'], 'goods_id' => $item['goods_id'], 'goods_price' => $item['goods_price']]);
  45. if ($res === false) throw new Exception(BossAchievementTeam::getStaticError());
  46. // 增加上级所有团队业绩当月业绩
  47. $res = BossAchievementMonth::batchSetData($boss_parent_ids, ['mall_id' => $order['mall_id'], 'month' => $month, 'goods_price' => $item['goods_price']]);
  48. if ($res === false) throw new Exception(BossAchievementMonth::getStaticError());
  49. }
  50. }
  51. $trans->commit();
  52. return true;
  53. } catch (Exception $e) {
  54. $trans->rollBack();
  55. return false;
  56. }
  57. }
  58. /**
  59. * 定时任务执行
  60. * @Author: lun
  61. * @DateTime: 2022/7/10 0010 18:19
  62. * @Copyright: copyright (c) 2021 广东七件事集团
  63. * @return bool
  64. */
  65. public function exec()
  66. {
  67. try {
  68. $mall_list = Mall::getEnableMallList(true);
  69. if(!empty($mall_list)){
  70. foreach ($mall_list as $mall){
  71. $res = $this->settlement($mall['id']);
  72. if ($res === false) throw new Exception($this->getError());
  73. }
  74. }
  75. return true;
  76. } catch (Exception $e) {
  77. $this->setError($e->getMessage());
  78. return false;
  79. }
  80. }
  81. /**
  82. * 业绩结算
  83. * @Author: lun
  84. * @DateTime: 2022/7/10 0010 16:33
  85. * @Copyright: copyright (c) 2021 广东七件事集团
  86. * @param int $mall_id
  87. * @param bool $month 执行指定月份
  88. */
  89. public function settlement($mall_id, $month = '')
  90. {
  91. $trans = Yii::$app->db->beginTransaction();
  92. try {
  93. $day = date('d', time());
  94. if ($day != 1 && empty($month)) return true;
  95. // 上个月月份
  96. $month = $month ?: date("Ym", strtotime("-1 month"));
  97. $insert_wallet = [];
  98. // 获取所有等级设置
  99. $level_arr = BossAchievementLevelSetting::find()->where(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED])->indexBy('level')->asArray()->all();
  100. // 获取每个等级对应的所有商品
  101. $achievement_goods_ids = [];
  102. foreach ($level_arr as $item) {
  103. $achievement_goods_ids[$item['level']] = Json::decode($item['achievement_goods_ids'], true);
  104. }
  105. foreach ($achievement_goods_ids as $level => $goods_ids) {
  106. // 获取对应等级下的总商品业绩
  107. $settlement_money = BossAchievementTeam::find()->select('sum(finish_order_money) as total_money')->where(['mall_id' => $mall_id, 'goods_id' => $goods_ids, 'month' => $month, 'is_settlement' => StatusEnum::DISABLED, 'status' => StatusEnum::ENABLED])->scalar();
  108. if (empty($settlement_money)) continue;// 结算金额为0直接跳过
  109. // 获取该等级结算的用户
  110. $users = BossAchievementUser::find()->where(['mall_id' => $mall_id, 'level' => $level, 'status' => StatusEnum::ENABLED])->indexBy('user_id')->asArray()->all();
  111. if (!empty($users)) {
  112. // 获取达到业绩的用户
  113. $all_user_ids = array_column($users, 'user_id');
  114. $standard_user_ids = BossAchievementMonth::find()->select('user_id')
  115. ->where(['mall_id' => $mall_id, 'user_id' => $all_user_ids, 'month' => $month, 'status' => StatusEnum::ENABLED])
  116. ->andWhere(['>=', 'achievement', $level_arr[$level]['standard_money']])
  117. ->column();
  118. // 该等级结算人数
  119. $settlement_num = count($standard_user_ids);
  120. // 计算平分的金额,保留两位小数向下取整
  121. $percent = $level_arr[$level]['prize'];
  122. if (empty($settlement_num) || empty($percent)) {
  123. $bouns = 0;
  124. } else {
  125. $bouns = $settlement_money * $percent / 100 / $settlement_num;
  126. $bouns = sprintf("%.2f",substr(sprintf("%.3f", $bouns), 0, -1));
  127. }
  128. // 用户结算
  129. foreach ($users as $user_id => $item) {
  130. if (in_array($user_id, $standard_user_ids)) {
  131. // 已达标用户处理
  132. $res = BossAchievementMonth::setData([
  133. 'mall_id' => $mall_id,
  134. 'user_id' => $user_id,
  135. 'month' => $month,
  136. 'bonus' => $bouns,
  137. 'settlement_money' => $settlement_money,
  138. 'settlement_num' => $settlement_num,
  139. 'standard_money' => $level_arr[$level]['standard_money'],
  140. 'is_settlement' => StatusEnum::ENABLED
  141. ]);
  142. if ($res === false) throw new Exception(BossAchievementMonth::getStaticError());
  143. // 修改用户累计分红
  144. $res = BossAchievementUser::updateAllCounters(['commission' => $bouns], ['user_id' => $user_id, 'status' => StatusEnum::ENABLED]);
  145. if ($res == false) throw new Exception(BossAchievementMonth::getStaticError());
  146. $insert_wallet[] = [
  147. 'mall_id' => $mall_id,
  148. 'user_id' => $user_id,
  149. 'is_frozen' => false,
  150. 'wallet_type' => 'commission',
  151. 'money' => $bouns,
  152. 'desc' => '股东业绩分红',
  153. 'source_table' => 'addons_boss_achievement_month',
  154. 'source_table_id' => '0',
  155. 'from_type' => BossAchievementEnum::ADDONS_NAME,
  156. 'capital_type' => UserWalletEnum::BOUNS,
  157. ];
  158. } else {
  159. // 未达标用户处理
  160. $res = BossAchievementMonth::setData([
  161. 'mall_id' => $mall_id,
  162. 'user_id' => $user_id,
  163. 'month' => $month,
  164. 'standard_money' => $level_arr[$level]['standard_money'],
  165. 'is_settlement' => StatusEnum::ENABLED
  166. ]);
  167. if ($res === false) throw new Exception(BossAchievementMonth::getStaticError());
  168. }
  169. }
  170. }
  171. // 将当月的商品改为已结算
  172. $res = BossAchievementTeam::updateAll(['is_settlement' => StatusEnum::ENABLED], 'goods_id in('.implode(',', $goods_ids).') and month='.$month);
  173. if ($res === false) throw new Exception(BossAchievementTeam::getStaticError());
  174. }
  175. // 插入分红队列
  176. if ($insert_wallet) {
  177. $res = UserWalletService::batchHandleByQueue($insert_wallet);
  178. if (empty($res)) throw new \Exception(UserWalletService::getStaticError());
  179. }
  180. $trans->commit();
  181. return true;
  182. } catch (Exception $e) {
  183. $trans->rollBack();
  184. Yii::error('股东业绩执行失败:' . $e->getMessage());
  185. return false;
  186. }
  187. }
  188. }