IntegralJob.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <?php
  2. namespace app\jobs\integral;
  3. use app\services\user\level\UserLevelServices;
  4. use app\services\user\UserAwardIntegralServices;
  5. use app\services\user\UserBillServices;
  6. use app\services\user\UserBrokerageServices;
  7. use app\services\user\UserServices;
  8. use crmeb\basic\BaseJobs;
  9. use crmeb\traits\QueueTrait;
  10. use think\facade\Log;
  11. /**
  12. * 订单消息队列
  13. * Class OrderJob
  14. * @package app\jobs
  15. */
  16. class IntegralJob extends BaseJobs
  17. {
  18. use QueueTrait;
  19. public function dealOrderIntegral($order)
  20. {
  21. Log::debug(date('Y-m-d H:i:s') . '开始处理' . $order['id'] . '积分');
  22. /** @var UserAwardIntegralServices $awardIntegralService */
  23. $awardIntegralService = app()->make(UserAwardIntegralServices::class);
  24. /** @var UserServices $userService */
  25. $userService = app()->make(UserServices::class);
  26. /** @var UserLevelServices $levelServices */
  27. $levelServices = app()->make(UserLevelServices::class);
  28. $user = $userService->getUserInfo($order['uid']);
  29. $integral_price = $awardIntegralService->getPrice($order['id']);
  30. $total_price = $order['total_price'];
  31. $award_price = bcsub($total_price, $order['cost'], 2);
  32. return $awardIntegralService->transaction(function () use ($award_price, $levelServices, $awardIntegralService, $userService, $user, $integral_price, $total_price, $order) {
  33. try {
  34. //添加静态积分
  35. $static_integral_ratio = sys_config('static_integral_ratio', 0);
  36. $give_static_integral = bcdiv(bcmul($award_price, $static_integral_ratio), 100, 2);
  37. // if ($give_static_integral > 0) {
  38. $rate = sys_config('static_integral_rate', 3);
  39. $extract_sum = bcmul($award_price, $rate, 2);
  40. $mark = '购买商品,获得消费分';
  41. $awardIntegralService->incIntegral($order['uid'], $integral_price, $give_static_integral, $total_price, 0, $extract_sum, $order['id'], $mark);
  42. // }
  43. //添加奖池
  44. $lake_ratio = sys_config('lake_ratio', 0);
  45. $add_lake = bcdiv(bcmul($award_price, $lake_ratio), 100, 2);
  46. if ($add_lake > 0) {
  47. $mark = '用户' . $order['uid'] . '购买商品,添加资金池';
  48. $awardIntegralService->addLake($add_lake, $order['id'], $mark);
  49. }
  50. //TODO 加动态积分
  51. //推荐奖
  52. $rate = sys_config('action_integral_rate', 3);
  53. $spread = $userService->getUserInfo($user['spread_uid']);
  54. if ($spread) {
  55. if ($awardIntegralService->getPaySum($spread['uid']) >= 1000 || $awardIntegralService->getHourExtractPaySum($spread['uid'], 24) >= 1000) {
  56. $award_ratio = sys_config('recommend_integral', 0);
  57. $give_action_integral = bcdiv(bcmul($award_price, $award_ratio), 100, 2);
  58. if ($give_action_integral > 0) {
  59. $extract_sum = bcmul($give_action_integral, $rate, 2);
  60. $mark = '推荐用户' . $user['nickname'] . "({$user['uid']})" . '购买商品,获得推荐积分';
  61. $awardIntegralService->incIntegral($spread['uid'], $integral_price, $give_action_integral, $total_price, 1, $extract_sum, $order['id'], $mark);
  62. }
  63. }
  64. if ($awardIntegralService->getPaySum($spread['uid']) > 0) {
  65. $award_ratio = sys_config('recommend_speed_integral', 0);
  66. $give_action_integral = bcdiv(bcmul($award_price, $award_ratio), 100, 2);
  67. if ($give_action_integral > 0) {
  68. $first = $awardIntegralService->getFirstStaticIntegral($spread['uid']);
  69. $mark = ',推荐用户' . $user['nickname'] . "({$user['uid']})" . '购买商品,获得加速积分';
  70. $awardIntegralService->incUpdateIntegral($first['id'], $integral_price, $give_action_integral, $mark);
  71. }
  72. }
  73. }
  74. $levelServices->detection((int)$user['uid']);
  75. $pass = [$user['uid']];
  76. $send = 0;
  77. while ($spread && !in_array($spread['uid'], $pass)) {
  78. //先发奖,再结算等级
  79. $level = $levelServices->getUserLevel($spread['uid']);
  80. $ratio = $level['levelInfo']['group_award'] ?? 0;
  81. $give_action_integral = bcdiv(bcmul($award_price, $ratio), 100, 2);
  82. if ($give_action_integral > $send) {
  83. $real_send = bcsub($give_action_integral, $send, 2);
  84. // if ($awardIntegralService->getHourExtractPaySum($spread['uid'], 24) > 0) {
  85. $extract_sum = bcmul($real_send, $rate, 2);
  86. $mark = '团队用户' . $user['nickname'] . "({$user['uid']})" . '购买商品,获得团队级差积分';
  87. $awardIntegralService->incIntegral($spread['uid'], $integral_price, $real_send, $total_price, 1, $extract_sum, $order['id'], $mark);
  88. // }
  89. $send = $give_action_integral;
  90. }
  91. $levelServices->detection((int)$spread['uid']);
  92. $pass[] = $spread['uid'];
  93. $spread = $userService->getUserInfo($spread['spread_uid']);
  94. }
  95. $this->autoExtract($awardIntegralService->getPrice());
  96. Log::debug(date('Y-m-d H:i:s') . '结束处理' . $order['id'] . '积分');
  97. return true;
  98. } catch (\Throwable $e) {
  99. Log::error('处理积分奖池失败,失败原因:' . $e->getMessage());
  100. Log::debug(date('Y-m-d H:i:s') . '结束处理' . $order['id'] . '积分');
  101. return false;
  102. }
  103. });
  104. }
  105. public function extract($id)
  106. {
  107. /** @var UserAwardIntegralServices $awardIntegralService */
  108. $awardIntegralService = app()->make(UserAwardIntegralServices::class);
  109. /** @var UserBrokerageServices $brokerageService */
  110. $brokerageService = app()->make(UserBrokerageServices::class);
  111. /** @var UserBillServices $billService */
  112. $billService = app()->make(UserBillServices::class);
  113. /** @var UserServices $userService */
  114. $userService = app()->make(UserServices::class);
  115. $info = $awardIntegralService->getIntegral($id);
  116. return $awardIntegralService->transaction(function () use ($info, $awardIntegralService, $brokerageService, $billService, $userService) {
  117. if ($info['status'] != 0) {
  118. $awardIntegralService->update($info['id'], ['handle' => 0]);
  119. return true;
  120. }
  121. $price = $awardIntegralService->getPrice();
  122. $sum = bcmul($price, $info['num'], 2);
  123. if ($sum > $info['extract_sum']) {
  124. $sum = $info['extract_sum'];
  125. }
  126. $res = true;
  127. $real_out = bcdiv(bcmul($sum, sys_config('extract_ratio', 0)), 100, 2);
  128. if ($real_out > 0) {
  129. $user = $userService->getUserInfo($info['uid']);
  130. $mark = '用户积分出局,出局时价格' . $price;
  131. $res = $res && $awardIntegralService->addLake(-$real_out, $info['id'], $mark);
  132. $to_brokerage = bcdiv(bcmul($real_out, sys_config('extract_brokerage_ratio', 0)), 100, 2);
  133. $balance = bcadd($user['brokerage_price'], $to_brokerage, 2);
  134. $res = $res && $brokerageService->income('extract_integral', $info['uid'], [
  135. 'type' => $info['type'] ? '贡献分' : '消费分',
  136. 'price' => round($price, 2),
  137. 'integral_num' => $info['num'],
  138. 'number' => floatval($to_brokerage),
  139. 'frozen_time' => 0
  140. ], $balance, $info['id']);
  141. // 添加用户佣金
  142. $res = $res && $userService->bcInc($info['uid'], 'brokerage_price', $to_brokerage, 'uid');
  143. $to_energy = bcsub($real_out, $to_brokerage, 2);
  144. $balance = bcadd($user['energy'], $to_energy, 2);
  145. $res = $res && $billService->income('extract_integral', $info['uid'], [
  146. 'type' => $info['type'] ? '贡献分' : '消费分',
  147. 'price' => round($price, 2),
  148. 'integral_num' => $info['num'],
  149. 'number' => floatval($to_energy),
  150. ], $balance, $info['id']);
  151. // 添加用户佣金
  152. $res = $res && $userService->bcInc($info['uid'], 'energy', $to_energy, 'uid');
  153. $res = $res && $awardIntegralService->update($info['id'], ['handle' => 0, 'extract_sum_real' => $sum, 'status' => 1, 'extract_time' => time()]);
  154. }
  155. return $res && $this->autoExtract($awardIntegralService->getPrice());
  156. });
  157. }
  158. public function autoExtract($price)
  159. {
  160. /** @var UserAwardIntegralServices $awardIntegralService */
  161. $awardIntegralService = app()->make(UserAwardIntegralServices::class);
  162. /** @var UserBrokerageServices $brokerageService */
  163. $brokerageService = app()->make(UserBrokerageServices::class);
  164. /** @var UserBillServices $billService */
  165. $billService = app()->make(UserBillServices::class);
  166. /** @var UserServices $userService */
  167. $userService = app()->make(UserServices::class);
  168. $infos = $awardIntegralService->getIntegralsOverExtract($price);
  169. $res = true;
  170. foreach ($infos as $info) {
  171. if ($info['status'] != 0) {
  172. $awardIntegralService->update($info['id'], ['handle' => 0]);
  173. continue;
  174. }
  175. $sum = bcmul($price, $info['num'], 2);
  176. if ($sum > $info['extract_sum']) {
  177. $sum = $info['extract_sum'];
  178. }
  179. $real_out = bcdiv(bcmul($sum, sys_config('extract_ratio', 0)), 100, 2);
  180. if ($real_out > 0) {
  181. $user = $userService->getUserInfo($info['uid']);
  182. $mark = '用户积分出局,出局时价格' . $price;
  183. $res = $res && $awardIntegralService->addLake(-$real_out, $info['id'], $mark);
  184. $to_brokerage = bcdiv(bcmul($real_out, sys_config('extract_brokerage_ratio', 0)), 100, 2);
  185. $balance = bcadd($user['brokerage_price'], $to_brokerage, 2);
  186. $res = $res && $brokerageService->income('extract_integral', $info['uid'], [
  187. 'type' => $info['type'] ? '贡献分' : '消费分',
  188. 'price' => round($price, 2),
  189. 'integral_num' => $info['num'],
  190. 'number' => floatval($to_brokerage),
  191. 'frozen_time' => 0
  192. ], $balance, $info['id']);
  193. // 添加用户佣金
  194. $res = $res && $userService->bcInc($info['uid'], 'brokerage_price', $to_brokerage, 'uid');
  195. $to_energy = bcsub($real_out, $to_brokerage, 2);
  196. $balance = bcadd($user['energy'], $to_energy, 2);
  197. $res = $res && $billService->income('extract_integral', $info['uid'], [
  198. 'type' => $info['type'] ? '贡献分' : '消费分',
  199. 'price' => round($price, 2),
  200. 'integral_num' => $info['num'],
  201. 'number' => floatval($to_energy),
  202. ], $balance, $info['id']);
  203. // 添加用户佣金
  204. $res = $res && $userService->bcInc($info['uid'], 'energy', $to_energy, 'uid');
  205. $res = $res && $awardIntegralService->update($info['id'], ['handle' => 0, 'extract_sum_real' => $sum, 'status' => 1, 'extract_time' => time()]);
  206. }
  207. }
  208. if (count($infos) > 0) return $res && $this->autoExtract($awardIntegralService->getPrice());
  209. return $res;
  210. }
  211. /**
  212. * 减积分
  213. * @param int $uid 用户
  214. * @param float $static 静态积分
  215. * @param float $action 动态积分
  216. * @param float $cash 资金池
  217. * @param int $link_id 关联ID
  218. * @param string $mark 备注
  219. */
  220. public function decIntegral(int $uid, float $static, float $action, float $cash, int $link_id = 0, string $mark = '')
  221. {
  222. }
  223. }