IntegralJob.php 16 KB

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