From efb659107bf6447f915df6d13de67dc0ba623240 Mon Sep 17 00:00:00 2001 From: monanxiao Date: Wed, 15 Mar 2023 15:14:08 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8B=E5=8D=95=E6=B5=81=E7=A8=8B=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=EF=BC=8C=E6=9C=AA=E5=AE=8C=E5=96=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../order/StoreOrderCreateRepository.php | 2 +- .../user/UserExtractRepository.php | 109 +++++++++++++----- .../repositories/user/UserRepository.php | 23 ++++ app/controller/api/user/User.php | 92 ++++++++++----- app/controller/api/user/UserExtract.php | 4 +- crmeb/jobs/SupplyChainOrderBrokerAgeJob.php | 7 +- 6 files changed, 169 insertions(+), 68 deletions(-) diff --git a/app/common/repositories/store/order/StoreOrderCreateRepository.php b/app/common/repositories/store/order/StoreOrderCreateRepository.php index 4bb7769c..0a165a65 100644 --- a/app/common/repositories/store/order/StoreOrderCreateRepository.php +++ b/app/common/repositories/store/order/StoreOrderCreateRepository.php @@ -1154,7 +1154,7 @@ class StoreOrderCreateRepository extends StoreOrderRepository $_order = [ 'cartInfo' => $merchantCart, 'activity_type' => $orderInfo['order_type'], - 'commission_rate' => $rate, + 'commission_rate' => $makePrice, 'order_type' => $merchantCart['order']['isTake'] ? 1 : 0, 'is_virtual' => $order_model ? 1 : 0, 'order_sn' => $this->getNewOrderId() . ($k + 1), diff --git a/app/common/repositories/user/UserExtractRepository.php b/app/common/repositories/user/UserExtractRepository.php index a23e307d..a5d69873 100644 --- a/app/common/repositories/user/UserExtractRepository.php +++ b/app/common/repositories/user/UserExtractRepository.php @@ -45,7 +45,7 @@ class UserExtractRepository extends BaseRepository * TODO * @param $id * @return bool - * @author Qinii + * @author QiniiuserTotalExtract * @day 2020-06-16 */ public function getWhereCount($id) @@ -97,41 +97,86 @@ class UserExtractRepository extends BaseRepository * @author Qinii * @day 2020-06-16 */ - public function create($user,$data) - { - event('user.extract.before',compact('user','data')); - $userExtract = Db::transaction(function()use($user,$data){ + public function create($user, $data, $source = 2) + { + // 如果是小组服务提现 + if($source == 1) + { + event('user.extract.before',compact('user','data')); + $userExtract = Db::transaction(function()use($user,$data,$source){ - if($user['brokerage_price'] < (systemConfig('user_extract_min'))) - throw new ValidateException('可提现金额不足'); - if($data['extract_price'] < (systemConfig('user_extract_min'))) - throw new ValidateException('提现金额不得小于最低额度'); - if($user['brokerage_price'] < $data['extract_price']) - throw new ValidateException('提现金额不足'); - if($data['extract_type'] == 3) { - $make = app()->make(WechatUserRepository::class); - $openid = $make->idByOpenId((int)$user['wechat_user_id']); - if (!$openid){ - $openid = $make->idByRoutineId((int)$user['wechat_user_id']); - if(!$openid) throw new ValidateException('openID获取失败,请确认是微信用户'); + if($user['supply_brokerage_price'] < (systemConfig('user_extract_min'))) + throw new ValidateException('可提现金额不足'); + if($data['extract_price'] < (systemConfig('user_extract_min'))) + throw new ValidateException('提现金额不得小于最低额度'); + if($user['supply_brokerage_price'] < $data['extract_price']) + throw new ValidateException('提现金额不足'); + if($data['extract_type'] == 3) { + $make = app()->make(WechatUserRepository::class); + $openid = $make->idByOpenId((int)$user['wechat_user_id']); + if (!$openid){ + $openid = $make->idByRoutineId((int)$user['wechat_user_id']); + if(!$openid) throw new ValidateException('openID获取失败,请确认是微信用户'); + } } - } - $brokerage_price = bcsub($user['brokerage_price'],$data['extract_price'],2); - $user->brokerage_price = $brokerage_price; - $user->save(); + $supply_brokerage_price = bcsub($user['supply_brokerage_price'],$data['extract_price'],2); + $user->supply_brokerage_price = $supply_brokerage_price; + $user->save(); - $data['extract_sn'] = $this->createSn(); - $data['uid'] = $user['uid']; - $data['balance'] = $brokerage_price; + $data['extract_sn'] = $this->createSn(); + $data['uid'] = $user['uid']; + $data['balance'] = $supply_brokerage_price; + $data['source'] = $source; - return $this->dao->create($data); - }); - event('user.extract',compact('userExtract')); - SwooleTaskService::admin('notice', [ - 'type' => 'extract', - 'title' => '您有一条新的提醒申请', - 'id' => $userExtract->extract_id - ]); + return $this->dao->create($data); + }); + event('user.extract',compact('userExtract')); + SwooleTaskService::admin('notice', [ + 'type' => 'extract', + 'title' => '您有一条新的提醒申请', + 'id' => $userExtract->extract_id + ]); + } + + // 分销推广提现 或者 余额提现 + if($source == 2) + { + event('user.extract.before',compact('user','data')); + $userExtract = Db::transaction(function()use($user,$data,$source){ + + if($user['brokerage_price'] < (systemConfig('user_extract_min'))) + throw new ValidateException('可提现金额不足'); + if($data['extract_price'] < (systemConfig('user_extract_min'))) + throw new ValidateException('提现金额不得小于最低额度'); + if($user['brokerage_price'] < $data['extract_price']) + throw new ValidateException('提现金额不足'); + if($data['extract_type'] == 3) { + $make = app()->make(WechatUserRepository::class); + $openid = $make->idByOpenId((int)$user['wechat_user_id']); + if (!$openid){ + $openid = $make->idByRoutineId((int)$user['wechat_user_id']); + if(!$openid) throw new ValidateException('openID获取失败,请确认是微信用户'); + } + } + $brokerage_price = bcsub($user['brokerage_price'],$data['extract_price'],2); + $user->brokerage_price = $brokerage_price; + $user->save(); + + $data['extract_sn'] = $this->createSn(); + $data['uid'] = $user['uid']; + $data['balance'] = $brokerage_price; + $data['source'] = $source; + + return $this->dao->create($data); + }); + event('user.extract',compact('userExtract')); + SwooleTaskService::admin('notice', [ + 'type' => 'extract', + 'title' => '您有一条新的提醒申请', + 'id' => $userExtract->extract_id + ]); + } + } public function switchStatus($id,$data) diff --git a/app/common/repositories/user/UserRepository.php b/app/common/repositories/user/UserRepository.php index 6adcc069..ed0876f3 100644 --- a/app/common/repositories/user/UserRepository.php +++ b/app/common/repositories/user/UserRepository.php @@ -856,6 +856,29 @@ class UserRepository extends BaseRepository return User::getDB()->where('uid', $uid)->value('nickname'); } + /** + * @param $uid + * @param $inc + * @param string $type + * @author xaboy + * @day 2020/6/22 + * 供应链佣金增加 + */ + public function incSupplyBrokerage($uid, $inc, $type = '+') + { + $moneyKey = 'b_top_' . date('Y-m'); + $weekKey = 'b_top_' . monday(); + //TODO 佣金周榜 + $brokerage = Cache::zscore($weekKey, $uid); + $brokerage = $type == '+' ? bcadd($brokerage, $inc, 2) : bcsub($brokerage, $inc, 2); + Cache::zadd($weekKey, $brokerage, $uid); + + //TODO 佣金月榜 + $brokerage = Cache::zscore($moneyKey, $uid); + $brokerage = $type == '+' ? bcadd($brokerage, $inc, 2) : bcsub($brokerage, $inc, 2); + Cache::zadd($moneyKey, $brokerage, $uid); + } + /** * @param $uid * @param $inc diff --git a/app/controller/api/user/User.php b/app/controller/api/user/User.php index 4a9f20b1..28ee1501 100644 --- a/app/controller/api/user/User.php +++ b/app/controller/api/user/User.php @@ -35,17 +35,20 @@ use think\db\exception\ModelNotFoundException; use think\exception\ValidateException; use think\facade\Db; use app\common\model\user\UserBill; +use app\common\dao\user\UserExtractDao as dao; class User extends BaseController { protected $repository; protected $user; + protected $dao; - public function __construct(App $app, UserRepository $repository) + public function __construct(App $app, UserRepository $repository, dao $dao) { parent::__construct($app); $this->repository = $repository; $this->user = $this->request->userInfo(); + $this->dao = $dao; } /** @@ -78,35 +81,35 @@ class User extends BaseController public function spread_info() { - $user = $this->user; - $make = app()->make(UserBrokerageRepository::class); - $user->append(['one_level_count', 'lock_brokerage', 'two_level_count', 'spread_total', 'yesterday_brokerage', 'total_extract', 'total_brokerage', 'total_brokerage_price']); - - $show_brokerage = (bool)$make->search(['type' => 0])->count(); - $source = $this->request->param('source'); $source = !isset($source)?2:$source; // 默认来源为2 普通商品订单 + $user = $this->user; + $make = app()->make(UserBrokerageRepository::class); + // 小组采购佣金另外处理 if($source == 1) { + $user->append([ + 'one_level_count', + 'two_level_count', + 'spread_total', + ]); + $data = [ - // 'total_brokerage_price' => $this->totalBrokerage($user->uid, $source), // 可提现金额 - 'total_brokerage_price' => $user->total_brokerage, // 可提现金额 - 'total_extract' => $this->lockBrokerage($user->uid, $source), // 已提现金额 - 'yesterday_brokerage' => $this->yesterdayBrokerage($user->uid, $source), // 昨日提现收益 - 'lock_brokerage' => $this->lockBrokerage($user->uid, $source), // 冻结中金额 - 'spread_total' => $user->spread_total, - 'total_brokerage' => $user->total_brokerage, - 'brokerage_price' => $user->brokerage_price, - 'show_brokerage' => $show_brokerage, - 'broken_day' => (int)systemConfig('lock_brokerage_timer'), - 'user_extract_min' => (int)systemConfig('user_extract_min'), + 'total_brokerage_price' => $this->totalBrokeragePrice($user['uid'], $source, $user['supply_brokerage_price']), + 'lock_brokerage' => $this->lockBrokerage($user['uid'], 0, $source), // 冻结 待确定 + 'yesterday_brokerage' => $this->yesterdayBrokerage($user['uid'], $source), + 'total_extract' => $this->userTotalExtract($user['uid'], $source), + 'total_brokerage' => $this->totalBrokerage($user['uid'], $source), ]; return app('json')->success($data); } + $user->append(['one_level_count', 'lock_brokerage', 'two_level_count', 'spread_total', 'yesterday_brokerage', 'total_extract', 'total_brokerage', 'total_brokerage_price']); + $show_brokerage = (bool)$make->search(['type' => 0])->count(); + $data = [ 'total_brokerage_price' => $user->total_brokerage_price, 'lock_brokerage' => $user->lock_brokerage, @@ -126,30 +129,40 @@ class User extends BaseController return app('json')->success($data); } - public function lockBrokerage($uid, $source) + public function totalBrokeragePrice($uid, $source, $supply_brokerage_price) { - $lst = UserBill::getDB()->where('category', 'brokerage') + return bcadd($this->lockBrokerage($uid, 0, $source), $supply_brokerage_price, 2); + } + + // 计算佣金 + public function lockBrokerage($uid, $status, $source) + { + $lst = UserBill::getDB() + ->where('category', 'brokerage') ->whereIn('type', ['order_one', 'order_two']) ->where('uid', $uid) + ->where('status', $status) ->where('source', $source) - ->where('status', 0) ->field('link_id,number') - ->select()->toArray(); + ->select() + ->toArray(); + $refundPrice = 0; + if (count($lst)) { - $refundPrice = -1 * UserBill::getDB()->whereIn('link_id', array_column($lst, 'link_id'))->where('uid', $uid) - ->where('category', 'brokerage')->whereIn('type', ['refund_two', 'refund_one'])->sum('number'); + $refundPrice = -1 * UserBill::getDB() + ->whereIn('link_id', array_column($lst, 'link_id')) + ->where('uid', $uid) + ->where('category', 'brokerage') + ->whereIn('type', ['refund_two', 'refund_one']) + ->sum('number'); } + foreach ($lst as $bill) { $refundPrice = bcadd($refundPrice, $bill['number'], 2); } - return $refundPrice; - } - public function yesterdayBrokerage($uid, $source) - { - return getModelTime(UserBill::getDB()->where('category', 'brokerage') - ->whereIn('type', ['order_one', 'order_two'])->where('source', $source)->where('uid', $uid), 'yesterday')->sum('number'); + return $refundPrice; } public function totalBrokerage($uid, $source) @@ -157,7 +170,24 @@ class User extends BaseController return bcsub(UserBill::getDB()->where('category', 'brokerage') ->whereIn('type', ['order_one', 'order_two'])->where('source', $source)->where('uid', $uid)->sum('number'), UserBill::getDB()->where('uid', $uid) - ->where('category', 'brokerage')->whereIn('type', ['refund_two', 'refund_one'])->sum('number'), 2); + ->where('category', 'brokerage')->where('source', $source)->whereIn('type', ['refund_two', 'refund_one'])->sum('number'), 2); + } + + // 已提现金额 + public function userTotalExtract($uid, $source) + { + return $this->dao->search(['status' => 1, 'uid' => $uid, 'source' => $source])->sum('extract_price'); + } + + // 昨日佣金 + public function yesterdayBrokerage($uid, $source) + { + return getModelTime(UserBill::getDB() + ->where('category', 'brokerage') + ->where('source', $source) + ->whereIn('type', ['order_one', 'order_two']) + ->where('uid', $uid), 'yesterday') + ->sum('number'); } public function brokerage_all() diff --git a/app/controller/api/user/UserExtract.php b/app/controller/api/user/UserExtract.php index 6d8dec70..5895b081 100644 --- a/app/controller/api/user/UserExtract.php +++ b/app/controller/api/user/UserExtract.php @@ -48,8 +48,10 @@ class UserExtract extends BaseController { $data = $this->checkParams($validate); $user = $this->request->userInfo(); + $source = $this->request->param('source'); // 1 小组服务 2 普通来源 + $source = !isset($source)?2:$source; // 默认来源为2 普通商品订单 if($data['extract_type'] == 3 && !systemConfig('sys_extension_type') ) return app('json')->fail('未开启付款到零钱'); - $this->repository->create($user,$data); + $this->repository->create($user,$data,$source); return app('json')->success('申请已提交'); } diff --git a/crmeb/jobs/SupplyChainOrderBrokerAgeJob.php b/crmeb/jobs/SupplyChainOrderBrokerAgeJob.php index 0f050fc2..1d08fbe1 100644 --- a/crmeb/jobs/SupplyChainOrderBrokerAgeJob.php +++ b/crmeb/jobs/SupplyChainOrderBrokerAgeJob.php @@ -44,13 +44,14 @@ class SupplyChainOrderBrokerAgeJob implements JobInterface if($supplyTeam) { // 可获得金额 - $usrPrice = sprintf("%.2f", $price/100*$supplyLevel['rate']); + $usrPrice = floatval(($price/100)*$supplyLevel['rate']); } // 分润数据 $dataArr = [ 'user_id' => $data['uid'], // 用户ID 'pay_price' => $data['inc'], // 订单金额 + 'price' => $price, // 订单分佣金额 'order_sn' => $data['order_sn'], // 订单编号 'supply_sn' => $data['order_sn'], // 订单编号 'order_id' => $data['order_id'], // 订单ID @@ -62,7 +63,7 @@ class SupplyChainOrderBrokerAgeJob implements JobInterface ]; // 储存小组服务分佣记录 - $status = SupplyChainBorkerage::create($dataArr); + SupplyChainBorkerage::create($dataArr); // 写入冻结佣金 $userBillRepository = app()->make(UserBillRepository::class); @@ -81,7 +82,7 @@ class SupplyChainOrderBrokerAgeJob implements JobInterface $userRepository = app()->make(UserRepository::class); // 增加 - $userRepository->incBrokerage($data['uid'], $price); + $userRepository->incSupplyBrokerage($data['uid'], $price); // 增加小组服务佣金明细 // $onePrice = '0.00'; // 一级佣金