123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406 |
- <?php
- namespace app\jobs\integral;
- use app\services\user\AwardIntegralServices;
- use app\services\user\level\UserLevelServices;
- use app\services\user\UserAwardIntegralServices;
- use app\services\user\UserBillServices;
- use app\services\user\UserBrokerageServices;
- use app\services\user\UserMoneyServices;
- use app\services\user\UserServices;
- use crmeb\basic\BaseJobs;
- use crmeb\traits\QueueTrait;
- use think\exception\ValidateException;
- use think\facade\Log;
- /**
- * 订单消息队列
- * Class OrderJob
- * @package app\jobs
- */
- class IntegralJob extends BaseJobs
- {
- use QueueTrait;
- public function dealOrderIntegral($order)
- {
- Log::debug(date('Y-m-d H:i:s') . '开始处理' . $order['id'] . '积分');
- /** @var AwardIntegralServices $awardIntegralService */
- // $awardIntegralService = app()->make(UserAwardIntegralServices::class);
- $awardIntegralService = app()->make(AwardIntegralServices::class);
- /** @var UserServices $userService */
- $userService = app()->make(UserServices::class);
- /** @var UserLevelServices $levelServices */
- $levelServices = app()->make(UserLevelServices::class);
- $user = $userService->getUserInfo($order['uid']);
- // $integral_price = $awardIntegralService->getPrice($order['id']);
- $total_price = $order['award_price'];
- // $award_price = bcsub($total_price, $order['cost'], 2);
- return $awardIntegralService->transaction(function () use ($levelServices, $awardIntegralService, $userService, $user, $total_price, $order) {
- try {
- $oldAwardIntegralService = app()->make(UserAwardIntegralServices::class);
- // //添加静态积分
- // $static_integral_ratio = sys_config('static_integral_ratio', 0);
- $rate = sys_config('static_integral_rate', 3);
- $award_order_limit = sys_config('award_order_limit');
- $give_static_integral = bcmul($total_price, $rate);
- if ($give_static_integral > 0) {
- // $extract_sum = bcmul($total_price, $rate, 2);
- $mark = '购买商品,获得参考分';
- $integral_info = $awardIntegralService->incIntegral($order['uid'], $give_static_integral, $total_price, $order['id'], $mark);
- }
- //添加奖池
- // $lake_ratio = sys_config('lake_ratio', 0);
- // $add_lake = bcdiv(bcmul($award_price, $lake_ratio), 100, 2);
- // if ($add_lake > 0) {
- // $mark = '用户' . $order['uid'] . '购买商品,添加资金池';
- // $awardIntegralService->addLake($add_lake, $order['id'], $mark);
- // }
- //TODO 加动态积分
- //推荐奖
- $rate = sys_config('action_integral_rate', 3);
- $spread = $userService->getUserInfo($user['spread_uid']);
- if ($spread && $spread['is_promoter']) {
- if ($awardIntegralService->getPaySum($spread['uid']) >= $award_order_limit) {
- $award_ratio = sys_config('recommend_integral', 0);
- if (($integral_info['link_id'] ?? 0) > 0) {
- $award_ratio = sys_config('reorder_recommend_integral', 0);
- }
- $give_action_integral = bcdiv(bcmul($total_price, $award_ratio), 100, 2);
- if ($give_action_integral > 0) {
- $extract_sum = bcmul($give_action_integral, $rate, 2);
- $mark = '推荐用户' . $user['nickname'] . "({$user['uid']})" . '购买商品,获得推荐积分';
- $this->sendAward($extract_sum, $spread['uid'], $mark, $order['id']);
- $oldAwardIntegralService->incIntegral($spread['uid'], 0, $give_action_integral, $total_price, 1, $extract_sum, $order['id'], $mark);
- }
- }
- if ($awardIntegralService->getPaySum($spread['uid']) >= $award_order_limit) {
- $award_ratio = sys_config('recommend_speed_integral', 0);
- $give_action_integral = bcdiv(bcmul($total_price, $award_ratio), 100, 2);
- if ($give_action_integral > 0) {
- $first = $awardIntegralService->getFirstStaticIntegral($spread['uid']);
- if ($first) {
- // $mark = ',推荐用户' . $user['nickname'] . "({$user['uid']})" . '购买商品,释放加速';
- $awardIntegralService->upSpeedIntegral($first['id'], $give_action_integral);
- }
- }
- }
- }
- $levelServices->detection((int)$user['uid']);
- $pass = [$user['uid']];
- $send = 0;
- while ($spread && !in_array($spread['uid'], $pass)) {
- //先发奖,再结算等级
- $level = $levelServices->getUserLevel($spread['uid']);
- $ratio = $level['levelInfo']['group_award'] ?? 0;
- $give_action_integral = bcdiv(bcmul($total_price, $ratio), 100, 2);
- if ($give_action_integral > $send && $spread['is_promoter']) {
- $real_send = bcsub($give_action_integral, $send, 2);
- if ($awardIntegralService->getPaySum($spread['uid']) >= $award_order_limit || $spread['award_switch']) {
- $extract_sum = bcmul($real_send, $rate, 2);
- $mark = '团队用户' . $user['nickname'] . "({$user['uid']})" . '购买商品,获得团队级差积分';
- $this->sendAward($extract_sum, $spread['uid'], $mark, $order['id']);
- $oldAwardIntegralService->incIntegral($spread['uid'], 0, $give_action_integral, $total_price, 1, $extract_sum, $order['id'], $mark);
- }
- $send = $give_action_integral;
- }
- try {
- $levelServices->detection((int)$spread['uid']);
- } catch (ValidateException $e) {
- }
- $pass[] = $spread['uid'];
- $spread = $userService->getUserInfo($spread['spread_uid']);
- }
- // $this->autoExtract($awardIntegralService->getPrice());
- Log::debug(date('Y-m-d H:i:s') . '结束处理' . $order['id'] . '积分');
- return true;
- } catch (\Throwable $e) {
- Log::error('处理积分奖池失败,失败原因:' . $e->getMessage());
- Log::debug(date('Y-m-d H:i:s') . '结束处理' . $order['id'] . '积分');
- return false;
- }
- });
- }
- public function sendAward($sum, $uid, $mark, $order_id)
- {
- /** @var UserBrokerageServices $brokerageService */
- $brokerageService = app()->make(UserBrokerageServices::class);
- /** @var UserBillServices $billService */
- $billService = app()->make(UserBillServices::class);
- /** @var UserMoneyServices $moneyService */
- $moneyService = app()->make(UserMoneyServices::class);
- /** @var UserServices $userService */
- $userService = app()->make(UserServices::class);
- $res = true;
- $extract_ratio = sys_config('extract_ratio_active', 0);
- $real_out = bcdiv(bcmul($sum, $extract_ratio), 100, 2);
- if ($real_out > 0) {
- $user = $userService->getUserInfo($uid);
- $to_brokerage = bcdiv(bcmul($real_out, sys_config('extract_brokerage_ratio_active', 0)), 100, 2);
- $to_energy = bcsub($real_out, $to_brokerage, 2);
- if ($to_energy > 0) {
- $balance = bcadd($user['energy'], $to_energy, 2);
- $res = $res && $billService->income('extract_integral_new', $uid, [
- 'mark' => $mark,
- 'number' => floatval($to_energy),
- ], $balance, $order_id);
- // 添加用户佣金
- $res = $res && $userService->bcInc($uid, 'energy', $to_energy, 'uid');
- }
- if ($to_brokerage > 0) {
- $balance = bcadd($user['brokerage_price'], $to_brokerage, 2);
- $res = $res && $brokerageService->income('extract_integral_new', $uid, [
- 'mark' => $mark,
- 'number' => floatval($to_brokerage),
- ], $balance, $order_id);
- // 添加用户佣金
- $res = $res && $userService->bcInc($uid, 'brokerage_price', $to_brokerage, 'uid');
- }
- }
- return $res;
- }
- public function extract($id)
- {
- return true;
- /** @var UserAwardIntegralServices $awardIntegralService */
- $awardIntegralService = app()->make(UserAwardIntegralServices::class);
- /** @var UserBrokerageServices $brokerageService */
- $brokerageService = app()->make(UserBrokerageServices::class);
- /** @var UserBillServices $billService */
- $billService = app()->make(UserBillServices::class);
- /** @var UserMoneyServices $moneyService */
- $moneyService = app()->make(UserMoneyServices::class);
- /** @var UserServices $userService */
- $userService = app()->make(UserServices::class);
- $info = $awardIntegralService->getIntegral($id);
- return $awardIntegralService->transaction(function () use ($info, $awardIntegralService, $brokerageService, $billService, $userService, $moneyService) {
- if ($info['status'] != 0) {
- $awardIntegralService->update($info['id'], ['handle' => 0]);
- return true;
- }
- $price = $awardIntegralService->getPrice();
- $sum = bcmul($price, $info['num'], 2);
- if ($sum > $info['extract_sum']) {
- $sum = $info['extract_sum'];
- }
- $res = true;
- $extract_ratio = sys_config('extract_ratio', 0);
- if ($info['type'] == 1) $extract_ratio = sys_config('extract_ratio_active', 0);
- $real_out = bcdiv(bcmul($sum, $extract_ratio), 100, 2);
- if ($real_out > 0) {
- $user = $userService->getUserInfo($info['uid']);
- $mark = '用户积分出局,出局时价格' . $price;
- $res = $res && $awardIntegralService->addLake(-$real_out, $info['id'], $mark);
- $to_brokerage = bcdiv(bcmul($real_out, sys_config('extract_brokerage_ratio', 0)), 100, 2);
- if ($info['type'] == 1) $to_brokerage = bcdiv(bcmul($real_out, sys_config('extract_brokerage_ratio_active', 0)), 100, 2);
- $to_energy = bcsub($real_out, $to_brokerage, 2);
- if ($to_energy > 0) {
- $balance = bcadd($user['energy'], $to_energy, 2);
- $res = $res && $billService->income('extract_integral', $info['uid'], [
- 'type' => $info['type'] ? '贡献分' : '消费分',
- 'price' => round($price, 2),
- 'integral_num' => $info['num'],
- 'number' => floatval($to_energy),
- ], $balance, $info['id']);
- // 添加用户佣金
- $res = $res && $userService->bcInc($info['uid'], 'energy', $to_energy, 'uid');
- }
- $to_now_money = 0;
- $starts_return_time = sys_config('starts_return_time', 0);
- if ($starts_return_time > 0 && $info['type'] == 0) {
- $count = $awardIntegralService->search(['uid' => $user['uid'], 'type' => 0, 'status' => 1])->count();
- if ($count < $starts_return_time) {
- $to_now_money = $info['order_price'];
- if ($to_now_money > $to_brokerage) {
- $to_now_money = $to_brokerage;
- }
- $to_brokerage = bcsub($to_brokerage, $to_now_money, 2);
- }
- }
- if ($to_now_money > 0) {
- $balance = bcadd($user['now_money'], $to_now_money, 2);
- $res = $res && $moneyService->income('extract_integral', $info['uid'], [
- 'type' => $info['type'] ? '贡献分' : '消费分',
- 'price' => round($price, 2),
- 'integral_num' => $info['num'],
- 'number' => floatval($to_now_money),
- 'frozen_time' => 0
- ], $balance, $info['id']);
- // 添加用户佣金
- $res = $res && $userService->bcInc($info['uid'], 'now_money', $to_now_money, 'uid');
- }
- if ($to_brokerage > 0) {
- $balance = bcadd($user['brokerage_price'], $to_brokerage, 2);
- $res = $res && $brokerageService->income('extract_integral', $info['uid'], [
- 'type' => $info['type'] ? '贡献分' : '消费分',
- 'price' => round($price, 2),
- 'integral_num' => $info['num'],
- 'number' => floatval($to_brokerage),
- 'frozen_time' => 0
- ], $balance, $info['id']);
- // 添加用户佣金
- $res = $res && $userService->bcInc($info['uid'], 'brokerage_price', $to_brokerage, 'uid');
- }
- $res = $res && $awardIntegralService->update($info['id'], ['handle' => 0, 'extract_sum_real' => $sum, 'status' => 1, 'extract_time' => time()]);
- }
- return $res && $this->autoExtract($awardIntegralService->getPrice());
- });
- }
- public function autoExtract($price)
- {
- return true;
- /** @var UserAwardIntegralServices $awardIntegralService */
- $awardIntegralService = app()->make(UserAwardIntegralServices::class);
- /** @var UserBrokerageServices $brokerageService */
- $brokerageService = app()->make(UserBrokerageServices::class);
- /** @var UserMoneyServices $moneyService */
- $moneyService = app()->make(UserMoneyServices::class);
- /** @var UserBillServices $billService */
- $billService = app()->make(UserBillServices::class);
- /** @var UserServices $userService */
- $userService = app()->make(UserServices::class);
- $infos = $awardIntegralService->getIntegralsOverExtract($price);
- $res = true;
- foreach ($infos as $info) {
- if ($info['status'] != 0) {
- $awardIntegralService->update($info['id'], ['handle' => 0]);
- continue;
- }
- $sum = bcmul($price, $info['num'], 2);
- if ($sum > $info['extract_sum']) {
- $sum = $info['extract_sum'];
- }
- $extract_ratio = sys_config('extract_ratio', 0);
- if ($info['type'] == 1) $extract_ratio = sys_config('extract_ratio_active', 0);
- $real_out = bcdiv(bcmul($sum, $extract_ratio), 100, 2);
- $left = bcsub($info['num'], bcdiv(bcmul($info['num'], $extract_ratio), 100, 2), 5);
- $to_top = bcdiv(bcmul($left, sys_config('extract_speed_ratio', 0)), 100, 5);
- $extract_speed_num = sys_config('extract_speed_num', 0);
- if ($extract_speed_num > 0) {
- $to_top = bcdiv($to_top, $extract_speed_num, 5);
- } else {
- $to_top = 0;
- }
- $to_spread = 0;
- if ($info['type'] == 0) {
- $to_spread = bcdiv(bcmul($info['num'], sys_config('extract_spread_ratio', 0)), 100, 2);
- }
- $user = $userService->getUserInfo($info['uid']);
- if ($real_out > 0) {
- $mark = '用户积分出局,出局时价格' . $price;
- $res = $res && $awardIntegralService->addLake(-$real_out, $info['id'], $mark);
- $to_brokerage = bcdiv(bcmul($real_out, sys_config('extract_brokerage_ratio', 0)), 100, 2);
- $to_energy = bcsub($real_out, $to_brokerage, 2);
- if ($to_energy > 0) {
- $balance = bcadd($user['energy'], $to_energy, 2);
- $res = $res && $billService->income('extract_integral', $info['uid'], [
- 'type' => $info['type'] ? '贡献分' : '消费分',
- 'price' => round($price, 2),
- 'integral_num' => $info['num'],
- 'number' => floatval($to_energy),
- ], $balance, $info['id']);
- // 添加用户佣金
- $res = $res && $userService->bcInc($info['uid'], 'energy', $to_energy, 'uid');
- }
- $to_now_money = 0;
- $starts_return_time = sys_config('starts_return_time', 0);
- if ($starts_return_time > 0 && $info['type'] == 0) {
- $count = $awardIntegralService->search(['uid' => $user['uid'], 'type' => 0, 'status' => 1])->count();
- if ($count < $starts_return_time) {
- $to_now_money = $info['order_price'];
- if ($to_now_money > $to_brokerage) {
- $to_now_money = $to_brokerage;
- }
- $to_brokerage = bcsub($to_brokerage, $to_now_money, 2);
- }
- }
- if ($to_now_money > 0) {
- $balance = bcadd($user['now_money'], $to_now_money, 2);
- $res = $res && $moneyService->income('extract_integral', $info['uid'], [
- 'type' => $info['type'] ? '贡献分' : '消费分',
- 'price' => round($price, 2),
- 'integral_num' => $info['num'],
- 'number' => floatval($to_now_money),
- 'frozen_time' => 0
- ], $balance, $info['id']);
- // 添加用户佣金
- $res = $res && $userService->bcInc($info['uid'], 'now_money', $to_now_money, 'uid');
- }
- if ($to_brokerage > 0) {
- $balance = bcadd($user['brokerage_price'], $to_brokerage, 2);
- $res = $res && $brokerageService->income('extract_integral', $info['uid'], [
- 'type' => $info['type'] ? '贡献分' : '消费分',
- 'price' => round($price, 2),
- 'integral_num' => $info['num'],
- 'number' => floatval($to_brokerage),
- 'frozen_time' => 0
- ], $balance, $info['id']);
- // 添加用户佣金
- $res = $res && $userService->bcInc($info['uid'], 'brokerage_price', $to_brokerage, 'uid');
- }
- $res = $res && $awardIntegralService->update($info['id'], ['handle' => 0, 'extract_sum_real' => $sum, 'status' => 1, 'extract_time' => time()]);
- }
- if ($to_top > 0) {
- $id = 0;
- for ($i = 0; $i < $extract_speed_num; $i++) {
- $first = $awardIntegralService->getTopStaticIntegral($id);
- if ($first) {
- $mark = ',用户' . $user['nickname'] . "({$user['uid']})" . '积分出局,获得加速积分';
- $awardIntegralService->incUpdateIntegral($first['id'], 0, $to_top, $mark);
- } else {
- break;
- }
- $id = $first['id'];
- }
- }
- if ($to_spread > 0) {
- $spread = $userService->getUserInfo($user['spread_uid']);
- if ($spread) {
- $first = $awardIntegralService->getFirstStaticIntegral($spread['uid']);
- if ($first) {
- $mark = ',推荐用户' . $user['nickname'] . "({$user['uid']})" . '消费分出局,获得加速积分';
- $awardIntegralService->incUpdateIntegral($first['id'], 0, $to_spread, $mark);
- }
- }
- }
- }
- if (count($infos) > 0) return $res && $this->autoExtract($awardIntegralService->getPrice());
- return $res;
- }
- /**
- * 减积分
- * @param int $uid 用户
- * @param float $static 静态积分
- * @param float $action 动态积分
- * @param float $cash 资金池
- * @param int $link_id 关联ID
- * @param string $mark 备注
- */
- public function decIntegral(int $uid, float $static, float $action, float $cash, int $link_id = 0, string $mark = '')
- {
- }
- }
|