IntegralJob.php 19 KB

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