IntegralJob.php 20 KB

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