From fb582550f9d1d9ffdfbfc7a93ef2467653e81bec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E6=99=AF=E7=AB=8B?= Date: Mon, 15 Mar 2021 16:50:54 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BB=A3=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/data/command/UserBalance.php | 2 +- app/data/controller/UserBalance.php | 4 +- app/data/service/RebateCurrentService.php | 2 +- app/data/service/UserUpgradeService.php | 74 +++++++++---------- .../service/payment/BalancePyamentService.php | 4 +- 5 files changed, 43 insertions(+), 43 deletions(-) diff --git a/app/data/command/UserBalance.php b/app/data/command/UserBalance.php index 0596db4a1..28e17a2b0 100644 --- a/app/data/command/UserBalance.php +++ b/app/data/command/UserBalance.php @@ -33,7 +33,7 @@ class UserBalance extends Command [$total, $count] = [$this->app->db->name('DataUser')->count(), 0]; foreach ($this->app->db->name('DataUser')->field('id')->cursor() as $user) { $this->queue->message($total, ++$count, "正在计算用户 [{$user['id']}] 的余额"); - UserUpgradeService::instance()->balance($user['id']); + UserUpgradeService::instance()->syncBalance($user['id']); $this->queue->message($total, $count, "完成计算用户 [{$user['id']}] 的余额", 1); } } catch (\Exception $exception) { diff --git a/app/data/controller/UserBalance.php b/app/data/controller/UserBalance.php index e4c2d22ed..cdff4a258 100644 --- a/app/data/controller/UserBalance.php +++ b/app/data/controller/UserBalance.php @@ -91,7 +91,7 @@ class UserBalance extends Controller protected function _form_result(bool $state, array $data) { if ($state && isset($data['uid'])) { - UserUpgradeService::instance()->balance($data['uid']); + UserUpgradeService::instance()->syncBalance($data['uid']); } } @@ -116,7 +116,7 @@ class UserBalance extends Controller $ids = str2arr(input('id', '')); $query = $this->app->db->name($this->table); foreach ($query->whereIn('id', $ids)->cursor() as $vo) { - UserUpgradeService::instance()->balance($vo['uid']); + UserUpgradeService::instance()->syncBalance($vo['uid']); } } } diff --git a/app/data/service/RebateCurrentService.php b/app/data/service/RebateCurrentService.php index b019a5b3c..3fa2e4550 100644 --- a/app/data/service/RebateCurrentService.php +++ b/app/data/service/RebateCurrentService.php @@ -165,7 +165,7 @@ class RebateCurrentService extends Service 'remark' => "来自订单{$this->order['order_no']}的余额充值", 'amount' => $this->order['reward_balance'], ], 'code'); - return UserUpgradeService::instance()->balance($this->order['uid']); + return UserUpgradeService::instance()->syncBalance($this->order['uid']); } /** diff --git a/app/data/service/UserUpgradeService.php b/app/data/service/UserUpgradeService.php index 27b838e53..12ad88daf 100644 --- a/app/data/service/UserUpgradeService.php +++ b/app/data/service/UserUpgradeService.php @@ -13,39 +13,16 @@ class UserUpgradeService extends Service { /** - * 同步刷新用户返利 - * @param integer $uuid - * @return array [total, count, lock] + * 获取用户等级数据 + * @return array + * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException */ - public function syncRebate(int $uuid): array + public function levels(): array { - $total = abs($this->app->db->name('DataUserRebate')->where("uid='{$uuid}' and status=1 and amount>0 and deleted=0")->sum('amount')); - $count = abs($this->app->db->name('DataUserRebate')->where("uid='{$uuid}' and status=1 and amount<0 and deleted=0")->sum('amount')); - $lockd = abs($this->app->db->name('DataUserRebate')->where("uid='{$uuid}' and status=0 and amount<0 and deleted=0")->sum('amount')); - $this->app->db->name('DataUser')->where(['id' => $uuid])->update([ - 'rebate_total' => $total, 'rebate_used' => $count, 'rebate_lock' => $lockd, - ]); - return [$total, $count, $lockd]; - } - - /** - * 同步刷新用户余额 - * @param int $uuid 用户UID - * @param array $nots 排除的订单 - * @return array [total, count] - * @throws \think\db\exception\DbException - */ - public function balance(int $uuid, array $nots = []): array - { - $total = abs($this->app->db->name('DataUserBalance')->where("uid='{$uuid}' and status=1 and amount>0 and deleted=0")->sum('amount')); - $count = abs($this->app->db->name('DataUserBalance')->where("uid='{$uuid}' and status=1 and amount<0 and deleted=0")->sum('amount')); - if (empty($nots)) { - $this->app->db->name('DataUser')->where(['id' => $uuid])->update(['balance_total' => $total, 'balance_used' => $count]); - } else { - $count -= $this->app->db->name('DataUserBalance')->whereRaw("uid={$uuid}")->whereIn('code', $nots)->sum('amount'); - } - return [$total, $count]; + $query = $this->app->db->name('DataUserUpgrade'); + return $query->where(['status' => 1])->order('number asc')->select()->toArray(); } /** @@ -85,16 +62,39 @@ class UserUpgradeService extends Service } /** - * 获取用户等级数据 - * @return array - * @throws \think\db\exception\DataNotFoundException + * 同步刷新用户返利 + * @param integer $uuid + * @return array [total, count, lock] * @throws \think\db\exception\DbException - * @throws \think\db\exception\ModelNotFoundException */ - public function levels(): array + public function syncRebate(int $uuid): array { - $query = $this->app->db->name('DataUserUpgrade'); - return $query->where(['status' => 1])->order('number asc')->select()->toArray(); + $total = abs($this->app->db->name('DataUserRebate')->where("uid='{$uuid}' and status=1 and amount>0 and deleted=0")->sum('amount')); + $count = abs($this->app->db->name('DataUserRebate')->where("uid='{$uuid}' and status=1 and amount<0 and deleted=0")->sum('amount')); + $lockd = abs($this->app->db->name('DataUserRebate')->where("uid='{$uuid}' and status=0 and amount<0 and deleted=0")->sum('amount')); + $this->app->db->name('DataUser')->where(['id' => $uuid])->update([ + 'rebate_total' => $total, 'rebate_used' => $count, 'rebate_lock' => $lockd, + ]); + return [$total, $count, $lockd]; + } + + /** + * 同步刷新用户余额 + * @param int $uuid 用户UID + * @param array $nots 排除的订单 + * @return array [total, count] + * @throws \think\db\exception\DbException + */ + public function syncBalance(int $uuid, array $nots = []): array + { + $total = abs($this->app->db->name('DataUserBalance')->where("uid='{$uuid}' and status=1 and amount>0 and deleted=0")->sum('amount')); + $count = abs($this->app->db->name('DataUserBalance')->where("uid='{$uuid}' and status=1 and amount<0 and deleted=0")->sum('amount')); + if (empty($nots)) { + $this->app->db->name('DataUser')->where(['id' => $uuid])->update(['balance_total' => $total, 'balance_used' => $count]); + } else { + $count -= $this->app->db->name('DataUserBalance')->whereRaw("uid={$uuid}")->whereIn('code', $nots)->sum('amount'); + } + return [$total, $count]; } /** diff --git a/app/data/service/payment/BalancePyamentService.php b/app/data/service/payment/BalancePyamentService.php index c02b40f7e..7cda314a6 100644 --- a/app/data/service/payment/BalancePyamentService.php +++ b/app/data/service/payment/BalancePyamentService.php @@ -56,7 +56,7 @@ class BalancePyamentService extends PaymentService // 创建支付行为 $this->createPaymentAction($orderNo, $paymentTitle, $paymentAmount); // 扣减用户余额 - [$total, $count] = UserUpgradeService::instance()->balance($order['uid'], [$orderNo]); + [$total, $count] = UserUpgradeService::instance()->syncBalance($order['uid'], [$orderNo]); if ($paymentAmount > $total - $count) throw new Exception("可抵扣余额不足"); $this->app->db->name('ShopOrder')->where(['order_no' => $orderNo])->update(['payment_balance' => $paymentAmount]); // 扣除余额金额 @@ -70,7 +70,7 @@ class BalancePyamentService extends PaymentService // 更新支付行为 $this->updatePaymentAction($orderNo, CodeExtend::uniqidDate(20), $paymentAmount, '账户余额支付'); // 刷新用户余额 - UserUpgradeService::instance()->balance($order['uid']); + UserUpgradeService::instance()->syncBalance($order['uid']); return ['info' => '余额支付完成']; } } \ No newline at end of file