IntegralJob.php 21 KB

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