PoolReward.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. namespace addons\CloudStock\common\logic\reward;
  3. use addons\CloudStock\common\models\CloudStockBonusPoolLevel;
  4. use addons\CloudStock\common\models\CloudStockBonusPoolLog;
  5. use addons\CloudStock\common\models\CloudStockBonusPoolLogDetail;
  6. use addons\CloudStock\common\models\CloudStockBonusPoolUser;
  7. use common\enums\AddonsEnum;
  8. use common\enums\RabbitMqEnum;
  9. use common\enums\StatusEnum;
  10. use common\helpers\DateHelper;
  11. use services\common\UserWalletService;
  12. class PoolReward
  13. {
  14. //分红池统计,结算
  15. public static function index($mall_id,$time,$start=null,$end=null)
  16. {
  17. $configs = \Yii::$app->services->setting->addons->get($mall_id, AddonsEnum::ADDONS_NAME_CLOUD_STOCK, 'basic');
  18. $is_upgrade_bag_give_goods_pool = $configs['is_upgrade_bag_give_goods_pool'] ?? 0;
  19. $is_superposition = $configs['is_superposition'] ?? 0;
  20. $pool_settlement_type = $configs['pool_settlement_type'] ?? 0;
  21. $day = date('d', $time);
  22. if ($day != '01') return false;
  23. switch ($pool_settlement_type) {
  24. case 0:
  25. if(!isset($start)||!isset($end)) {
  26. $res = DateHelper::lastMonth();
  27. $start = $res['start'];
  28. $end = $res['end'];
  29. }
  30. break;
  31. case 1:
  32. $month = date('m', time());
  33. if (!in_array($month, ['01', '04', '07', '10'])) {
  34. return false;
  35. }
  36. if(!isset($start)||!isset($end)) {
  37. $res = DateHelper::lastSeason();
  38. $start = $res['start'];
  39. $end = $res['end'];
  40. }
  41. break;
  42. }
  43. if (empty($start) || empty($end)) return false;
  44. $list_level = CloudStockBonusPoolLevel::lists([
  45. 'where' => [
  46. ['mall_id' => $mall_id],
  47. ['status' => StatusEnum::ENABLED],
  48. ],
  49. 'order' => 'level asc',
  50. ], false);
  51. if (empty($list_level)) return false;
  52. $time = time();
  53. $t = \Yii::$app->db->beginTransaction();
  54. try {
  55. $pool_log_id_arr = [];
  56. foreach ($list_level as $item) {
  57. $query = CloudStockBonusPoolUser::find()->where(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED]);
  58. if ($is_superposition) {
  59. $query->andWhere(['>=', 'level', $item['level']]);
  60. } else {
  61. $query->andWhere(['=', 'level', $item['level']]);
  62. }
  63. $query_copy = clone $query;
  64. $counts = $query->count();
  65. if (empty($counts)) continue;
  66. $pool_money = $item['money'];
  67. $average_money = $item['money'] / $counts;
  68. $add_data = [
  69. 'mall_id' => $mall_id,
  70. 'level' => $item['level'],
  71. 'start' => $start,
  72. 'end' => $end,
  73. 'money' => $pool_money,
  74. 'dispatch_money' => $pool_money,
  75. 'people' => $counts,
  76. 'is_upgrade_bag_give_goods_pool' => $is_upgrade_bag_give_goods_pool,
  77. 'is_superposition' => $is_superposition,
  78. 'pool_settlement_type' => $pool_settlement_type,
  79. ];
  80. $model = CloudStockBonusPoolLog::create($add_data);
  81. $pool_log_id_arr[] = $model['id'];
  82. $item->money = 0;
  83. $item->save();
  84. if ($average_money <= 0) continue;
  85. $field = ['mall_id', 'pool_log_id', 'user_id', 'level', 'money', 'is_settled', 'status', 'created_at', 'updated_at'];
  86. $i = 1;
  87. $page_size = 1000;
  88. while ($user_list = $query_copy->offset(($i - 1) * $page_size)->limit($page_size)->all()) {
  89. $rows = [];
  90. foreach ($user_list as $user) {
  91. $user->pool_income += $average_money;
  92. $user->save();
  93. $rows[] = [
  94. $mall_id,
  95. $model['id'],
  96. $user['user_id'],
  97. $user['level'],
  98. $average_money,
  99. 0,
  100. 1,
  101. $time,
  102. $time
  103. ];
  104. }
  105. $res = \Yii::$app->db->createCommand()->batchInsert(CloudStockBonusPoolLogDetail::tableName(), $field, $rows)->execute();
  106. if ($res == false) throw new \Exception("批量插入奖励出错");
  107. $i++;
  108. var_dump($i);
  109. }
  110. }
  111. $t->commit();
  112. $data = [
  113. 'mall_id' => $mall_id,
  114. 'pool_log_id_arr' => $pool_log_id_arr,
  115. ];
  116. var_dump($data);
  117. \Yii::$app->services->rabbitMq->push(RabbitMqEnum::EXCHANGE_TASK, RabbitMqEnum::TASK_QUEUE, $data, PoolReward::class, 'sendUserWallet');
  118. return true;
  119. } catch (\Exception $e) {
  120. $t->rollBack();
  121. var_dump("PoolReward::index" . $e->getMessage());
  122. return false;
  123. }
  124. }
  125. public static function sendUserWallet($params)
  126. {
  127. $pool_log_id_arr = $params['pool_log_id_arr'];
  128. $mall_id = $params['mall_id'];
  129. $pool_log_list = CloudStockBonusPoolLog::lists(['where' => [['id' => $pool_log_id_arr], ['mall_id' => $mall_id]], 'index' => 'id']);
  130. $level_arr = array_column($pool_log_list, 'level');
  131. $level_list = CloudStockBonusPoolLevel::lists([
  132. 'where' => [
  133. ['mall_id' => $mall_id],
  134. ['level' => $level_arr],
  135. ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]
  136. ],
  137. 'index' => 'level'
  138. ]);
  139. $i = 1;
  140. $page_size = 1000;
  141. while ($detail_list = CloudStockBonusPoolLogDetail::find()->where(['pool_log_id' => $pool_log_id_arr])->offset(($i - 1) * $page_size)->limit($page_size)->all()) {
  142. $desc = '';
  143. $insert_wallet = [];
  144. $id_arr = [];
  145. foreach ($detail_list as $detail) {
  146. $id_arr[] = $detail['id'];
  147. if (isset($pool_log_list[$detail['pool_log_id']])) {
  148. $level = $pool_log_list[$detail['pool_log_id']]['level'];
  149. if (isset($level_list[$level])) {
  150. $desc = $level_list[$level]['name'] . '分红池分红';
  151. }
  152. }
  153. $insert_wallet[] = [
  154. 'mall_id' => $detail['mall_id'],
  155. 'user_id' => $detail['user_id'],
  156. 'is_frozen' => false,
  157. 'wallet_type' => 'commission',
  158. 'money' => $detail['money'],
  159. 'desc' => $desc,
  160. 'source_table' => CloudStockBonusPoolLogDetail::tableName(),
  161. 'source_table_id' => $detail['id'],
  162. 'from_type' => AddonsEnum::ADDONS_NAME_CLOUD_STOCK,
  163. 'capital_type' => 'bouns',
  164. ];
  165. }
  166. $res = UserWalletService::batchHandleByQueue($insert_wallet);
  167. if ($res) {
  168. CloudStockBonusPoolLogDetail::updateAll(['is_settled' => 1], ['id' => $id_arr]);
  169. }
  170. $i++;
  171. }
  172. }
  173. public static function inPool($mall_id, $goods_id, $num, $fill_unit_price)
  174. {
  175. $list = CloudStockBonusPoolLevel::lists([
  176. 'where' => [
  177. ['mall_id' => $mall_id],
  178. ['status' => StatusEnum::ENABLED]
  179. ],
  180. 'order' => 'level asc'
  181. ], false);
  182. if (empty($list)) return false;
  183. foreach ($list as $item) {
  184. $goods_setting = json_decode($item['goods_setting'], true);
  185. if(!is_array($goods_setting)) continue;
  186. $goods = array_column($goods_setting,null,'goods_id');
  187. if (!isset($goods[$goods_id])) continue;
  188. if ($goods[$goods_id]['is_percent'] == 0) {
  189. $money = bcmul($num, $goods[$goods_id]['money'], 2);
  190. } else {
  191. $money = bcdiv($fill_unit_price * $num * $goods[$goods_id]['money'], 100, 2);
  192. }
  193. $item->money += $money;
  194. $item->total_money += $money;
  195. $item->save();
  196. }
  197. }
  198. }