IntegralJob.php 21 KB

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