添加:订单完成解冻兑换额度

添加:订单退款扣除兑换额度
This commit is contained in:
wuhui_zzw 2024-01-14 15:14:08 +08:00
parent ede44b963f
commit 60f5d2030a
5 changed files with 124 additions and 5 deletions

View File

@ -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) {

View File

@ -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' => [],

View File

@ -0,0 +1,71 @@
<?php
namespace app\listener\exchangeQuota;
use app\common\model\user\ExchangeQuota;
use app\common\model\user\ExchangeQuotaRecord;
use app\common\repositories\store\order\StoreRefundProductRepository;
use think\facade\Log;
class OrderAgreeRefundEvent{
public $groupOrder;
public function handle($data){
try{
$refund = $data['refund'];
// Log::info('订单进入退款成功 - 兑换额度相关处理 - 开始:'.var_export(['id' => $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));
}
}
}

View File

@ -58,7 +58,7 @@ class OrderPaySuccessEvent{
'change_quantity' => (float)$orderProductInfo->product_price,
'change_front' => $changeFront,
'change_after' => (float)$userHoldInfo->surplus_quota,
'remark' => "购买商品赠送兑换额度",
'remark' => "购买商品赠送",
];
}
}

View File

@ -0,0 +1,42 @@
<?php
namespace app\listener\exchangeQuota;
use app\common\model\user\ExchangeQuota;
use app\common\model\user\ExchangeQuotaRecord;
use think\facade\Log;
class OrderTakeEvent{
public $groupOrder;
public function handle($data){
try{
$order = $data['order'];
// Log::info('订单进入待评价 - 兑换额度相关处理 - 开始: '.var_export(['order_id' => $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());
}
}
}