From 0ab8d0fb666abb789ead25b57b842d0a96fcd831 Mon Sep 17 00:00:00 2001 From: wuhui_zzw <1760308791@qq.com> Date: Tue, 2 Apr 2024 15:56:32 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=EF=BC=9A=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E5=88=86=E9=94=80=E4=BD=A3=E9=87=91=E5=90=8C=E4=B8=80=E7=AC=94?= =?UTF-8?q?=E8=AE=A2=E5=8D=95=20=E7=BB=93=E7=AE=97=E5=A4=9A=E6=AC=A1?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98=20=E4=BF=AE=E5=A4=8D=EF=BC=9A?= =?UTF-8?q?=E8=8F=9C=E5=8D=A1=E9=A2=9D=E5=BA=A6=E6=9C=AA=E8=A7=A3=E5=86=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../repositories/user/UserBillRepository.php | 35 ++++--- .../exchangeQuota/OrderVerifyEvent.php | 97 ++++++++++++++----- 2 files changed, 93 insertions(+), 39 deletions(-) diff --git a/app/common/repositories/user/UserBillRepository.php b/app/common/repositories/user/UserBillRepository.php index 9214405..64ebfd0 100644 --- a/app/common/repositories/user/UserBillRepository.php +++ b/app/common/repositories/user/UserBillRepository.php @@ -120,18 +120,28 @@ class UserBillRepository extends BaseRepository * @author xaboy * @day 2020-05-07 */ - public function bill(int $uid, string $category, string $type, int $pm, array $data) - { - $data['category'] = $category; - $data['type'] = $type; - $data['uid'] = $uid; - $data['pm'] = $pm; - if (isset($data['with_goods_mer_id']) && $data['with_goods_mer_id'] !== '') $data['with_goods_mer_id'] = $data['with_goods_mer_id'] ?? 0; - $bill = $this->dao->create($data); - if($category == 'now_money'){ - Queue::push(SendSmsJob::class,['tempId' => 'USER_BALANCE_CHANGE','id' => $bill->bill_id]); + public function bill(int $uid, string $category, string $type, int $pm, array $data){ + // 判断:当前佣金是否已经结算 + $isHas = $this->dao->getSearch([ + 'category' => $category, + 'type' => $type, + 'uid' => $uid, + 'pm' => $pm, + 'link_id' => $data['link_id'], + ])->findOrEmpty(); + if((int)$isHas->bill_id <= 0){ + $data['category'] = $category; + $data['type'] = $type; + $data['uid'] = $uid; + $data['pm'] = $pm; + if (isset($data['with_goods_mer_id']) && $data['with_goods_mer_id'] !== '') $data['with_goods_mer_id'] = $data['with_goods_mer_id'] ?? 0; + $bill = $this->dao->create($data); + if($category == 'now_money') Queue::push(SendSmsJob::class,['tempId' => 'USER_BALANCE_CHANGE','id' => $bill->bill_id]); + + return $bill; } - return $bill; + + return $isHas; } /** @@ -143,8 +153,7 @@ class UserBillRepository extends BaseRepository * @author xaboy * @day 2020-05-07 */ - public function incBill(int $uid, string $category, string $type, array $data) - { + public function incBill(int $uid, string $category, string $type, array $data){ return $this->bill($uid, $category, $type, 1, $data); } diff --git a/app/listener/exchangeQuota/OrderVerifyEvent.php b/app/listener/exchangeQuota/OrderVerifyEvent.php index 3397fc2..82a8d4b 100644 --- a/app/listener/exchangeQuota/OrderVerifyEvent.php +++ b/app/listener/exchangeQuota/OrderVerifyEvent.php @@ -16,17 +16,44 @@ class OrderVerifyEvent{ public $groupOrder; public function handle($data){ + $order = $data['order']; + // Log::info('订单核销 - 额度相关处理 - 开始: '.var_export(['order_id' => $order->order_id,'uid' => $order->uid],1)); + // 判断:如果存在上级id 则使用上级id + $orderId = $order->main_id > 0 ? $order->main_id : $order->order_id; + + // 酒水卡额度解冻 + $this->wineCard($orderId, $order); + // 菜卡额度解冻 + $this->dish($orderId, $order); + + + + } + /** + * Common: 获取订单总数量 + * Author: wu-hui + * Time: 2024/01/17 16:03 + * @param $orderId + * @return mixed + */ + public function getRate($orderId){ + return (float)app()->make(StoreOrderRepository::class) + ->getSearch([]) + ->where(function($query) use ($orderId){ + $query->where('order_id',$orderId)->whereOr('main_id',$orderId); + })->sum('total_num'); + } + + + // 酒水卡额度解冻 + public function wineCard($orderId, $order){ try{ - $order = $data['order']; - // Log::info('订单核销 - 酒卡额度相关处理 - 开始: '.var_export(['order_id' => $order->order_id,'uid' => $order->uid],1)); - // 判断:如果存在上级id 则使用上级id - $orderId = $order->main_id > 0 ? $order->main_id : $order->order_id; # 获取变更记录 条件:order_id=当前订单id、变更类型=增加 $sum = (float)app()->make(ExchangeQuotaRecordRepository::class)->searchModel([ - 'order_id' => $orderId, - 'change_type' => 1, - 'source' => 0,// 仅查询购买赠送 进行解冻 - ])->sum('change_quantity'); + 'order_id' => $orderId, + 'change_type' => 1, + 'source' => 0,// 仅查询购买赠送 进行解冻 + ])->sum('change_quantity'); $refundSum = (float)app()->make(ExchangeQuotaRecordRepository::class) ->searchModel([ 'order_id' => $orderId, @@ -37,8 +64,8 @@ class OrderVerifyEvent{ $surplusQuota = (float)sprintf("%.2f",$sum - $refundSum); if($surplusQuota > 0){ // 核销订单 会进行拆分操作 需要获取当前订单占总订单数量的比例 - $totalNum = (int)$this->getRate($orderId); - $currentTotalNum = (int)$order->total_num; + $totalNum = (float)$this->getRate($orderId); + $currentTotalNum = (float)$order->total_num; $rate = (float)sprintf("%.2f",$currentTotalNum / $totalNum * 100); $surplusQuota = (float)sprintf("%.2f",$surplusQuota * $rate / 100); // 解冻操作 @@ -51,19 +78,40 @@ class OrderVerifyEvent{ Log::info('订单核销 - 酒卡额度相关处理 - 错误: '.$e->getMessage()); } } - /** - * Common: 获取订单总数量 - * Author: wu-hui - * Time: 2024/01/17 16:03 - * @param $orderId - * @return mixed - */ - public function getRate($orderId){ - return app()->make(StoreOrderRepository::class) - ->getSearch([]) - ->where(function($query) use ($orderId){ - $query->where('order_id',$orderId)->whereOr('main_id',$orderId); - })->sum('total_num'); + // 酒水卡额度解冻 + public function dish($orderId, $order){ + try{ + # 获取变更记录 条件:order_id=当前订单id、变更类型=增加 + $sum = (float)app()->make(ExchangeQuotaRecordRepository::class)->searchModel([ + 'order_id' => $orderId, + 'change_type' => 1, + 'source' => 0,// 仅查询购买赠送 进行解冻 + 'quota_type' => 2, + ])->sum('change_quantity'); + $refundSum = (float)app()->make(ExchangeQuotaRecordRepository::class) + ->searchModel([ + 'order_id' => $orderId, + 'change_type' => 0, + 'source' => 1,// 仅查询订单退款 减少内容 + 'quota_type' => 2, + ])->sum('change_quantity'); + // 剩余数量 解冻 + $surplusQuota = (float)sprintf("%.2f",$sum - $refundSum); + if($surplusQuota > 0){ + // 核销订单 会进行拆分操作 需要获取当前订单占总订单数量的比例 + $totalNum = (float)$this->getRate($orderId); + $currentTotalNum = (float)$order->total_num; + $rate = (float)sprintf("%.2f",$currentTotalNum / $totalNum * 100); + $surplusQuota = (float)sprintf("%.2f",$surplusQuota * $rate / 100); + // 解冻操作 + $hold = app()->make(ExchangeQuotaRepository::class)->searchModel(['uid'=>$order->uid,'quota_type'=>2])->findOrEmpty(); + $freezeQuota = sprintf("%.2f",$hold->freeze_quota - $surplusQuota); + $hold->freeze_quota = $freezeQuota < 0 ? 0 : $freezeQuota; + $hold->save(); + } + }catch(\Exception $e){ + Log::info('订单核销 - 菜卡额度相关处理 - 错误: '.$e->getMessage()); + } } @@ -72,7 +120,4 @@ class OrderVerifyEvent{ - - - } \ No newline at end of file