IntegralJob.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. }
  65. $levelServices->detection((int)$user['uid']);
  66. $pass = [$user['uid']];
  67. $send = 0;
  68. while ($spread && !in_array($spread['uid'], $pass)) {
  69. //先发奖,再结算等级
  70. $level = $levelServices->getUserLevel($spread['uid']);
  71. $ratio = $level['levelInfo']['group_award'] ?? 0;
  72. $give_action_integral = bcdiv(bcmul($award_price, $ratio), 100, 2);
  73. if ($give_action_integral > $send) {
  74. $real_send = bcsub($give_action_integral, $send, 2);
  75. if ($real_send < 0) $real_send = 0;
  76. $extract_sum = bcmul($real_send, $rate, 2);
  77. $mark = '团队用户' . $user['nickname'] . "({$user['uid']})" . '购买商品,获得团队级差积分';
  78. $awardIntegralService->incIntegral($spread['uid'], $integral_price, $real_send, $total_price, 1, $extract_sum, $order['id'], $mark);
  79. if ($real_send > 0) $send = $give_action_integral;
  80. }
  81. $levelServices->detection((int)$spread['uid']);
  82. $pass[] = $spread['uid'];
  83. $spread = $userService->getUserInfo($spread['spread_uid']);
  84. }
  85. $this->autoExtract($awardIntegralService->getPrice());
  86. Log::debug(date('Y-m-d H:i:s') . '结束处理' . $order['id'] . '积分');
  87. return true;
  88. } catch (\Throwable $e) {
  89. Log::error('处理积分奖池失败,失败原因:' . $e->getMessage());
  90. Log::debug(date('Y-m-d H:i:s') . '结束处理' . $order['id'] . '积分');
  91. return false;
  92. }
  93. });
  94. }
  95. public function extract($id)
  96. {
  97. /** @var UserAwardIntegralServices $awardIntegralService */
  98. $awardIntegralService = app()->make(UserAwardIntegralServices::class);
  99. /** @var UserBrokerageServices $brokerageService */
  100. $brokerageService = app()->make(UserBrokerageServices::class);
  101. /** @var UserBillServices $billService */
  102. $billService = app()->make(UserBillServices::class);
  103. /** @var UserServices $userService */
  104. $userService = app()->make(UserServices::class);
  105. $info = $awardIntegralService->getIntegral($id);
  106. return $awardIntegralService->transaction(function () use ($info, $awardIntegralService, $brokerageService, $billService, $userService) {
  107. if ($info['status'] != 0) {
  108. $awardIntegralService->update($info['id'], ['handle' => 0]);
  109. return true;
  110. }
  111. $price = $awardIntegralService->getPrice();
  112. $sum = bcmul($price, $info['num'], 2);
  113. if ($sum > $info['extract_sum']) {
  114. $sum = $info['extract_sum'];
  115. }
  116. $res = true;
  117. $real_out = bcdiv(bcmul($sum, sys_config('extract_ratio', 0)), 100, 2);
  118. if ($real_out > 0) {
  119. $user = $userService->getUserInfo($info['uid']);
  120. $mark = '用户积分出局,出局时价格' . $price;
  121. $res = $res && $awardIntegralService->addLake(-$real_out, $info['id'], $mark);
  122. $to_brokerage = bcdiv(bcmul($real_out, sys_config('extract_brokerage_ratio', 0)), 100, 2);
  123. $balance = bcadd($user['brokerage_price'], $to_brokerage, 2);
  124. $res = $res && $brokerageService->income('extract_integral', $info['uid'], [
  125. 'type' => $info['type'] ? '贡献分' : '消费分',
  126. 'price' => round($price, 2),
  127. 'integral_num' => $info['num'],
  128. 'number' => floatval($to_brokerage),
  129. 'frozen_time' => 0
  130. ], $balance, $info['id']);
  131. // 添加用户佣金
  132. $res = $res && $userService->bcInc($info['uid'], 'brokerage_price', $to_brokerage, 'uid');
  133. $to_energy = bcsub($real_out, $to_brokerage, 2);
  134. $balance = bcadd($user['energy'], $to_energy, 2);
  135. $res = $res && $billService->income('extract_integral', $info['uid'], [
  136. 'type' => $info['type'] ? '贡献分' : '消费分',
  137. 'price' => round($price, 2),
  138. 'integral_num' => $info['num'],
  139. 'number' => floatval($to_energy),
  140. ], $balance, $info['id']);
  141. // 添加用户佣金
  142. $res = $res && $userService->bcInc($info['uid'], 'energy', $to_energy, 'uid');
  143. $res = $res && $awardIntegralService->update($info['id'], ['handle' => 0, 'extract_sum_real' => $sum, 'status' => 1, 'extract_time' => time()]);
  144. }
  145. return $res && $this->autoExtract($awardIntegralService->getPrice());
  146. });
  147. }
  148. public function autoExtract($price)
  149. {
  150. /** @var UserAwardIntegralServices $awardIntegralService */
  151. $awardIntegralService = app()->make(UserAwardIntegralServices::class);
  152. /** @var UserBrokerageServices $brokerageService */
  153. $brokerageService = app()->make(UserBrokerageServices::class);
  154. /** @var UserBillServices $billService */
  155. $billService = app()->make(UserBillServices::class);
  156. /** @var UserServices $userService */
  157. $userService = app()->make(UserServices::class);
  158. $infos = $awardIntegralService->getIntegralsOverExtract($price);
  159. $res = true;
  160. foreach ($infos as $info) {
  161. if ($info['status'] != 0) {
  162. $awardIntegralService->update($info['id'], ['handle' => 0]);
  163. continue;
  164. }
  165. $sum = bcmul($price, $info['num'], 2);
  166. if ($sum > $info['extract_sum']) {
  167. $sum = $info['extract_sum'];
  168. }
  169. $real_out = bcdiv(bcmul($sum, sys_config('extract_ratio', 0)), 100, 2);
  170. if ($real_out > 0) {
  171. $user = $userService->getUserInfo($info['uid']);
  172. $mark = '用户积分出局,出局时价格' . $price;
  173. $res = $res && $awardIntegralService->addLake(-$real_out, $info['id'], $mark);
  174. $to_brokerage = bcdiv(bcmul($real_out, sys_config('extract_brokerage_ratio', 0)), 100, 2);
  175. $balance = bcadd($user['brokerage_price'], $to_brokerage, 2);
  176. $res = $res && $brokerageService->income('extract_integral', $info['uid'], [
  177. 'type' => $info['type'] ? '贡献分' : '消费分',
  178. 'price' => round($price, 2),
  179. 'integral_num' => $info['num'],
  180. 'number' => floatval($to_brokerage),
  181. 'frozen_time' => 0
  182. ], $balance, $info['id']);
  183. // 添加用户佣金
  184. $res = $res && $userService->bcInc($info['uid'], 'brokerage_price', $to_brokerage, 'uid');
  185. $to_energy = bcsub($real_out, $to_brokerage, 2);
  186. $balance = bcadd($user['energy'], $to_energy, 2);
  187. $res = $res && $billService->income('extract_integral', $info['uid'], [
  188. 'type' => $info['type'] ? '贡献分' : '消费分',
  189. 'price' => round($price, 2),
  190. 'integral_num' => $info['num'],
  191. 'number' => floatval($to_energy),
  192. ], $balance, $info['id']);
  193. // 添加用户佣金
  194. $res = $res && $userService->bcInc($info['uid'], 'energy', $to_energy, 'uid');
  195. $res = $res && $awardIntegralService->update($info['id'], ['handle' => 0, 'extract_sum_real' => $sum, 'status' => 1, 'extract_time' => time()]);
  196. }
  197. }
  198. if (count($infos) > 0) return $res && $this->autoExtract($awardIntegralService->getPrice());
  199. return $res;
  200. }
  201. /**
  202. * 减积分
  203. * @param int $uid 用户
  204. * @param float $static 静态积分
  205. * @param float $action 动态积分
  206. * @param float $cash 资金池
  207. * @param int $link_id 关联ID
  208. * @param string $mark 备注
  209. */
  210. public function decIntegral(int $uid, float $static, float $action, float $cash, int $link_id = 0, string $mark = '')
  211. {
  212. }
  213. }