IntegralJob.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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 UserMoneyServices $moneyService */
  120. $moneyService = app()->make(UserMoneyServices::class);
  121. /** @var UserServices $userService */
  122. $userService = app()->make(UserServices::class);
  123. $info = $awardIntegralService->getIntegral($id);
  124. return $awardIntegralService->transaction(function () use ($info, $awardIntegralService, $brokerageService, $billService, $userService, $moneyService) {
  125. if ($info['status'] != 0) {
  126. $awardIntegralService->update($info['id'], ['handle' => 0]);
  127. return true;
  128. }
  129. $price = $awardIntegralService->getPrice();
  130. $sum = bcmul($price, $info['num'], 2);
  131. if ($sum > $info['extract_sum']) {
  132. $sum = $info['extract_sum'];
  133. }
  134. $res = true;
  135. $extract_ratio = sys_config('extract_ratio', 0);
  136. if ($info['type'] == 1) $extract_ratio = sys_config('extract_ratio_active', 0);
  137. $real_out = bcdiv(bcmul($sum, $extract_ratio), 100, 2);
  138. if ($real_out > 0) {
  139. $user = $userService->getUserInfo($info['uid']);
  140. $mark = '用户积分出局,出局时价格' . $price;
  141. $res = $res && $awardIntegralService->addLake(-$real_out, $info['id'], $mark);
  142. $to_brokerage = bcdiv(bcmul($real_out, sys_config('extract_brokerage_ratio', 0)), 100, 2);
  143. $to_energy = bcsub($real_out, $to_brokerage, 2);
  144. if ($to_energy > 0) {
  145. $balance = bcadd($user['energy'], $to_energy, 2);
  146. $res = $res && $billService->income('extract_integral', $info['uid'], [
  147. 'type' => $info['type'] ? '贡献分' : '消费分',
  148. 'price' => round($price, 2),
  149. 'integral_num' => $info['num'],
  150. 'number' => floatval($to_energy),
  151. ], $balance, $info['id']);
  152. // 添加用户佣金
  153. $res = $res && $userService->bcInc($info['uid'], 'energy', $to_energy, 'uid');
  154. }
  155. $to_now_money = 0;
  156. $starts_return_time = sys_config('starts_return_time', 0);
  157. if ($starts_return_time > 0) {
  158. $count = $awardIntegralService->search(['uid' => $user['uid'], 'type' => 0, 'status' => 1])->count();
  159. if ($count < $starts_return_time) {
  160. $to_now_money = $info['order_price'];
  161. if ($to_now_money > $to_brokerage) {
  162. $to_now_money = $to_brokerage;
  163. }
  164. $to_brokerage = bcsub($to_brokerage, $to_now_money, 2);
  165. }
  166. }
  167. if ($to_now_money > 0) {
  168. $balance = bcadd($user['now_money'], $to_now_money, 2);
  169. $res = $res && $moneyService->income('extract_integral', $info['uid'], [
  170. 'type' => $info['type'] ? '贡献分' : '消费分',
  171. 'price' => round($price, 2),
  172. 'integral_num' => $info['num'],
  173. 'number' => floatval($to_now_money),
  174. 'frozen_time' => 0
  175. ], $balance, $info['id']);
  176. // 添加用户佣金
  177. $res = $res && $userService->bcInc($info['uid'], 'now_money', $to_now_money, 'uid');
  178. }
  179. if ($to_brokerage > 0) {
  180. $balance = bcadd($user['brokerage_price'], $to_brokerage, 2);
  181. $res = $res && $brokerageService->income('extract_integral', $info['uid'], [
  182. 'type' => $info['type'] ? '贡献分' : '消费分',
  183. 'price' => round($price, 2),
  184. 'integral_num' => $info['num'],
  185. 'number' => floatval($to_brokerage),
  186. 'frozen_time' => 0
  187. ], $balance, $info['id']);
  188. // 添加用户佣金
  189. $res = $res && $userService->bcInc($info['uid'], 'brokerage_price', $to_brokerage, 'uid');
  190. }
  191. $res = $res && $awardIntegralService->update($info['id'], ['handle' => 0, 'extract_sum_real' => $sum, 'status' => 1, 'extract_time' => time()]);
  192. }
  193. return $res && $this->autoExtract($awardIntegralService->getPrice());
  194. });
  195. }
  196. public function autoExtract($price)
  197. {
  198. /** @var UserAwardIntegralServices $awardIntegralService */
  199. $awardIntegralService = app()->make(UserAwardIntegralServices::class);
  200. /** @var UserBrokerageServices $brokerageService */
  201. $brokerageService = app()->make(UserBrokerageServices::class);
  202. /** @var UserMoneyServices $moneyService */
  203. $moneyService = app()->make(UserMoneyServices::class);
  204. /** @var UserBillServices $billService */
  205. $billService = app()->make(UserBillServices::class);
  206. /** @var UserServices $userService */
  207. $userService = app()->make(UserServices::class);
  208. $infos = $awardIntegralService->getIntegralsOverExtract($price);
  209. $res = true;
  210. foreach ($infos as $info) {
  211. if ($info['status'] != 0) {
  212. $awardIntegralService->update($info['id'], ['handle' => 0]);
  213. continue;
  214. }
  215. $sum = bcmul($price, $info['num'], 2);
  216. if ($sum > $info['extract_sum']) {
  217. $sum = $info['extract_sum'];
  218. }
  219. $extract_ratio = sys_config('extract_ratio', 0);
  220. if ($info['type'] == 1) $extract_ratio = sys_config('extract_ratio_active', 0);
  221. $real_out = bcdiv(bcmul($sum, $extract_ratio), 100, 2);
  222. $left = bcsub($info['num'], bcdiv(bcmul($info['num'], $extract_ratio), 100, 2), 5);
  223. $to_top = bcdiv(bcmul($left, sys_config('extract_speed_ratio', 0)), 100, 5);
  224. $extract_speed_num = sys_config('extract_speed_num', 0);
  225. if ($extract_speed_num > 0) {
  226. $to_top = bcdiv($to_top, $extract_speed_num, 5);
  227. } else {
  228. $to_top = 0;
  229. }
  230. $to_spread = 0;
  231. if ($info['type'] == 0) {
  232. $to_spread = bcdiv(bcmul($info['num'], sys_config('extract_spread_ratio', 0)), 100, 2);
  233. }
  234. $user = $userService->getUserInfo($info['uid']);
  235. if ($real_out > 0) {
  236. $mark = '用户积分出局,出局时价格' . $price;
  237. $res = $res && $awardIntegralService->addLake(-$real_out, $info['id'], $mark);
  238. $to_brokerage = bcdiv(bcmul($real_out, sys_config('extract_brokerage_ratio', 0)), 100, 2);
  239. $to_energy = bcsub($real_out, $to_brokerage, 2);
  240. if ($to_energy > 0) {
  241. $balance = bcadd($user['energy'], $to_energy, 2);
  242. $res = $res && $billService->income('extract_integral', $info['uid'], [
  243. 'type' => $info['type'] ? '贡献分' : '消费分',
  244. 'price' => round($price, 2),
  245. 'integral_num' => $info['num'],
  246. 'number' => floatval($to_energy),
  247. ], $balance, $info['id']);
  248. // 添加用户佣金
  249. $res = $res && $userService->bcInc($info['uid'], 'energy', $to_energy, 'uid');
  250. }
  251. $to_now_money = 0;
  252. $starts_return_time = sys_config('starts_return_time', 0);
  253. if ($starts_return_time > 0) {
  254. $count = $awardIntegralService->search(['uid' => $user['uid'], 'type' => 0, 'status' => 1])->count();
  255. if ($count < $starts_return_time) {
  256. $to_now_money = $info['order_price'];
  257. if ($to_now_money > $to_brokerage) {
  258. $to_now_money = $to_brokerage;
  259. }
  260. $to_brokerage = bcsub($to_brokerage, $to_now_money, 2);
  261. }
  262. }
  263. if ($to_now_money > 0) {
  264. $balance = bcadd($user['now_money'], $to_now_money, 2);
  265. $res = $res && $moneyService->income('extract_integral', $info['uid'], [
  266. 'type' => $info['type'] ? '贡献分' : '消费分',
  267. 'price' => round($price, 2),
  268. 'integral_num' => $info['num'],
  269. 'number' => floatval($to_now_money),
  270. 'frozen_time' => 0
  271. ], $balance, $info['id']);
  272. // 添加用户佣金
  273. $res = $res && $userService->bcInc($info['uid'], 'now_money', $to_now_money, 'uid');
  274. }
  275. if ($to_brokerage > 0) {
  276. $balance = bcadd($user['brokerage_price'], $to_brokerage, 2);
  277. $res = $res && $brokerageService->income('extract_integral', $info['uid'], [
  278. 'type' => $info['type'] ? '贡献分' : '消费分',
  279. 'price' => round($price, 2),
  280. 'integral_num' => $info['num'],
  281. 'number' => floatval($to_brokerage),
  282. 'frozen_time' => 0
  283. ], $balance, $info['id']);
  284. // 添加用户佣金
  285. $res = $res && $userService->bcInc($info['uid'], 'brokerage_price', $to_brokerage, 'uid');
  286. }
  287. $res = $res && $awardIntegralService->update($info['id'], ['handle' => 0, 'extract_sum_real' => $sum, 'status' => 1, 'extract_time' => time()]);
  288. }
  289. if ($to_top > 0) {
  290. $id = 0;
  291. for ($i = 0; $i < $extract_speed_num; $i++) {
  292. $first = $awardIntegralService->getTopStaticIntegral($id);
  293. if ($first) {
  294. $mark = ',用户' . $user['nickname'] . "({$user['uid']})" . '积分出局,获得加速积分';
  295. $awardIntegralService->incUpdateIntegral($first['id'], 0, $to_top, $mark);
  296. } else {
  297. break;
  298. }
  299. $id = $first['id'];
  300. }
  301. }
  302. if ($to_spread > 0) {
  303. $spread = $userService->getUserInfo($user['spread_uid']);
  304. if ($spread) {
  305. $first = $awardIntegralService->getFirstStaticIntegral($spread['uid']);
  306. if ($first) {
  307. $mark = ',推荐用户' . $user['nickname'] . "({$user['uid']})" . '消费分出局,获得加速积分';
  308. $awardIntegralService->incUpdateIntegral($first['id'], 0, $to_spread, $mark);
  309. }
  310. }
  311. }
  312. }
  313. if (count($infos) > 0) return $res && $this->autoExtract($awardIntegralService->getPrice());
  314. return $res;
  315. }
  316. /**
  317. * 减积分
  318. * @param int $uid 用户
  319. * @param float $static 静态积分
  320. * @param float $action 动态积分
  321. * @param float $cash 资金池
  322. * @param int $link_id 关联ID
  323. * @param string $mark 备注
  324. */
  325. public function decIntegral(int $uid, float $static, float $action, float $cash, int $link_id = 0, string $mark = '')
  326. {
  327. }
  328. }