IntegralJob.php 21 KB

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