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