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