IntegralJob.php 21 KB

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