From 60f5d2030aadc1e22b4395aefa90aebaee68e650 Mon Sep 17 00:00:00 2001 From: wuhui_zzw <1760308791@qq.com> Date: Sun, 14 Jan 2024 15:14:08 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=9A=E8=AE=A2=E5=8D=95?= =?UTF-8?q?=E5=AE=8C=E6=88=90=E8=A7=A3=E5=86=BB=E5=85=91=E6=8D=A2=E9=A2=9D?= =?UTF-8?q?=E5=BA=A6=20=E6=B7=BB=E5=8A=A0=EF=BC=9A=E8=AE=A2=E5=8D=95?= =?UTF-8?q?=E9=80=80=E6=AC=BE=E6=89=A3=E9=99=A4=E5=85=91=E6=8D=A2=E9=A2=9D?= =?UTF-8?q?=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/common/dao/store/order/StoreOrderDao.php | 2 +- app/event.php | 12 +++- .../exchangeQuota/OrderAgreeRefundEvent.php | 71 +++++++++++++++++++ .../exchangeQuota/OrderPaySuccessEvent.php | 2 +- app/listener/exchangeQuota/OrderTakeEvent.php | 42 +++++++++++ 5 files changed, 124 insertions(+), 5 deletions(-) create mode 100644 app/listener/exchangeQuota/OrderAgreeRefundEvent.php create mode 100644 app/listener/exchangeQuota/OrderTakeEvent.php diff --git a/app/common/dao/store/order/StoreOrderDao.php b/app/common/dao/store/order/StoreOrderDao.php index 2261837..79d62ec 100644 --- a/app/common/dao/store/order/StoreOrderDao.php +++ b/app/common/dao/store/order/StoreOrderDao.php @@ -291,7 +291,7 @@ class StoreOrderDao extends BaseDao ->when(isset($where['uid']) && $where['uid'] !== '', function ($query) use ($where) { $query->where('uid', $where['uid']); }) - ->when($where['activity_type'] == 20, function ($query) use ($where) { + ->when(isset($where['activity_type']) && $where['activity_type'] !== '', function ($query) use ($where) { $query->where('mer_id', $where['mer_id'] ?? 0); }) ->when(isset($where['is_user']) && $where['is_user'] !== '', function ($query) use ($where) { diff --git a/app/event.php b/app/event.php index 42a975b..2de66f4 100644 --- a/app/event.php +++ b/app/event.php @@ -55,13 +55,19 @@ return [ 'pay_success_meal' => [\crmeb\listens\pay\MealSuccessListen::class], // 订单支付成功事件触发 'order.paySuccess' => [ - // 支付成功 赠送兑换额度 + // 赠送兑换额度 'app\listener\exchangeQuota\OrderPaySuccessEvent' ], // 订单完成事件触发(进入待评价) - 'order.take' => [], + 'order.take' => [ + // 兑换额度解冻 + 'app\listener\exchangeQuota\OrderTakeEvent' + ], // 订单退款事件 - 'refund.agree' => [], + 'refund.agree' => [ + // 兑换额度相关处理 + 'app\listener\exchangeQuota\OrderAgreeRefundEvent' + ], ], 'subscribe' => [], diff --git a/app/listener/exchangeQuota/OrderAgreeRefundEvent.php b/app/listener/exchangeQuota/OrderAgreeRefundEvent.php new file mode 100644 index 0000000..a5d1205 --- /dev/null +++ b/app/listener/exchangeQuota/OrderAgreeRefundEvent.php @@ -0,0 +1,71 @@ + $data['id'],'uid' => $refund['uid']],1)); + $refundProductList = app()->make(StoreRefundProductRepository::class) + ->getSearch([]) + ->field('order_product_id,refund_price,refund_num') + ->with([ + 'product' => function($query){ + $query->field('product_id,order_product_id,order_id,product_num'); + } + ]) + ->where('refund_order_id',$data['id']) + ->select() + ->toArray(); + foreach($refundProductList as $refundProductInfo){ + // 获取当前商品退款比例 + if((int)$refundProductInfo['refund_num'] <= 0) continue; + $refundRate = (float)sprintf("%.2f",(int)$refundProductInfo['refund_num'] / (int)$refundProductInfo['product']['product_num'] * 100); + // 计算减少额度 + $sum = (float)ExchangeQuotaRecord::where('order_product_id',$refundProductInfo['order_product_id']) + ->where('change_type',1) + ->sum('change_quantity'); + $refundQuota = (float)sprintf("%.2f",$sum * $refundRate / 100); + if($refundQuota > 0){ + // 修改用户持有信息 + $userHoldInfo = ExchangeQuota::where('uid',$refund['uid'])->findOrEmpty(); + $changeFront = (float)$userHoldInfo->surplus_quota; + $userHoldInfo->total_quota -= (float)$refundQuota;// 总额度 + $userHoldInfo->surplus_quota -= (float)$refundQuota;// 剩余额度 + $userHoldInfo->freeze_quota -= (float)$refundQuota;// 冻结额度 + $userHoldInfo->save(); + // 添加变更记录 + ExchangeQuotaRecord::insert([ + 'uid' => $refund['uid'], + 'product_id' => $refundProductInfo['product']['product_id'], + 'order_id' => $refundProductInfo['product']['order_id'], + 'order_product_id' => $refundProductInfo['order_product_id'], + 'change_type' => 0, + 'change_quantity' => (float)$refundQuota, + 'change_front' => $changeFront, + 'change_after' => (float)$userHoldInfo->surplus_quota, + 'remark' => "订单退款减少", + 'source' => 1, + ]); + } + } + }catch(\Exception $e){ + $error = [ + 'id' => $data['id'], + 'msg' => $e->getMessage() + ]; + + Log::info('订单进入退款成功 - 兑换额度相关处理 - 错误:'.var_export($error,1)); + } + } +} \ No newline at end of file diff --git a/app/listener/exchangeQuota/OrderPaySuccessEvent.php b/app/listener/exchangeQuota/OrderPaySuccessEvent.php index 63919d6..dbe2835 100644 --- a/app/listener/exchangeQuota/OrderPaySuccessEvent.php +++ b/app/listener/exchangeQuota/OrderPaySuccessEvent.php @@ -58,7 +58,7 @@ class OrderPaySuccessEvent{ 'change_quantity' => (float)$orderProductInfo->product_price, 'change_front' => $changeFront, 'change_after' => (float)$userHoldInfo->surplus_quota, - 'remark' => "购买商品赠送兑换额度", + 'remark' => "购买商品赠送", ]; } } diff --git a/app/listener/exchangeQuota/OrderTakeEvent.php b/app/listener/exchangeQuota/OrderTakeEvent.php new file mode 100644 index 0000000..3fecbfb --- /dev/null +++ b/app/listener/exchangeQuota/OrderTakeEvent.php @@ -0,0 +1,42 @@ + $order->order_id,'uid' => $order->uid],1)); + # 获取变更记录 条件:order_id=当前订单id、变更类型=增加 + $sum = (float)ExchangeQuotaRecord::where('order_id',$order->order_id) + ->where('change_type',1) + ->where('source',0) // 仅查询购买赠送 进行解冻 + ->sum('change_quantity'); + $refundSum = (float)ExchangeQuotaRecord::where('order_id',$order->order_id) + ->where('change_type',0) + ->where('source',1) // 仅查询订单退款 减少内容 + ->sum('change_quantity'); + // 剩余数量 解冻 + $surplusQuota = (float)sprintf("%.2f",$sum - $refundSum); + if($surplusQuota > 0){ + $hold = ExchangeQuota::where('uid',$order->uid)->findOrEmpty(); + $freezeQuota = sprintf("%.2f",$hold->freeze_quota - $surplusQuota); + $hold->freeze_quota = $freezeQuota < 0 ? 0 : $freezeQuota; + $hold->save(); + } + }catch(\Exception $e){ + Log::info('订单进入待评价 - 兑换额度相关处理 - 错误: '.$e->getMessage()); + } + } + + + +} \ No newline at end of file