From b1d3c95c4fe3a329a1f1ff79939d97805e260918 Mon Sep 17 00:00:00 2001 From: wuhui_zzw <1760308791@qq.com> Date: Wed, 3 Jan 2024 11:31:23 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=9A=E9=80=80=E6=AC=BE?= =?UTF-8?q?=E6=88=90=E5=8A=9F=20-=20=E5=90=88=E4=BC=99=E4=BA=BA=E4=BD=A3?= =?UTF-8?q?=E9=87=91=E5=A4=84=E7=90=86=20=E6=B7=BB=E5=8A=A0=EF=BC=9A?= =?UTF-8?q?=E9=80=80=E6=AC=BE=E6=88=90=E5=8A=9F=20-=20=E8=B1=86=E8=B1=86?= =?UTF-8?q?=E5=92=8C=E7=A7=AF=E5=88=86=E5=A4=84=E7=90=86=20=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=EF=BC=9A=E5=BD=93=E6=97=A5=E8=B1=86=E8=B1=86=E5=92=8C?= =?UTF-8?q?=E7=A7=AF=E5=88=86=E8=AE=A1=E7=AE=97=20-=20=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E5=B7=B2=E9=80=80=E6=AC=BE=E4=BF=A1=E6=81=AF=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../platformCommission/ComputeIntegralJob.php | 16 ++-- .../HandleCommissionMoneyJob.php | 83 ++++++++++++++++++ .../HandleCommissionPartnerMoneyJob.php | 84 ++++++++++++++++++ .../refundOrderHandle/HandleLegumesJob.php | 87 +++++++++++++++++++ .../HandlePlatformCommissionRecordJob.php | 50 +++++++++++ .../HandleWeightValueJob.php | 87 +++++++++++++++++++ .../OrderAgreeRefundEvent.php | 62 ++++++++++--- 7 files changed, 450 insertions(+), 19 deletions(-) create mode 100644 app/jobs/store/platformCommission/refundOrderHandle/HandleCommissionMoneyJob.php create mode 100644 app/jobs/store/platformCommission/refundOrderHandle/HandleCommissionPartnerMoneyJob.php create mode 100644 app/jobs/store/platformCommission/refundOrderHandle/HandleLegumesJob.php create mode 100644 app/jobs/store/platformCommission/refundOrderHandle/HandlePlatformCommissionRecordJob.php create mode 100644 app/jobs/store/platformCommission/refundOrderHandle/HandleWeightValueJob.php diff --git a/app/jobs/store/platformCommission/ComputeIntegralJob.php b/app/jobs/store/platformCommission/ComputeIntegralJob.php index 8413388..c1fb113 100644 --- a/app/jobs/store/platformCommission/ComputeIntegralJob.php +++ b/app/jobs/store/platformCommission/ComputeIntegralJob.php @@ -23,12 +23,17 @@ class ComputeIntegralJob implements JobInterface{ try{ Log::info("全平台豆豆计算 - 开始处理: ".var_export($data,1)); $page = $data['page'] ?? 0; + $ids = $data['ids'] ?? []; $limit = 1000; $set = app()->make(RecordRepository::class)->getBaseConfig(); // 获取当前页数据 有效记录、已获取积分低于订单金额 - $model = LegumesLog::field('id,order_money,get_legumes,get_integral') - ->whereIn('status',[0,1]) - ->where('get_integral < order_money'); + $model = LegumesLog::field('id,(order_money - refund_order_money) as order_money,(get_legumes - refund_get_legumes) as get_legumes,get_integral') + ->when(count($ids) > 0,function($query) use ($ids){ + $query->whereIn('id',$ids); + },function($query){ + $query->where('get_integral < order_money'); + }) + ->whereIn('status',[0,1]); $count = $model->count(); $list = $model->page($page,$limit)->select()->toArray(); if(count($list) <= 0) throw new \Exception('无处理数据!'); @@ -45,9 +50,8 @@ class ComputeIntegralJob implements JobInterface{ // 判断:是否存在下一页 $currentLimit = $page * $limit; if($currentLimit < $count){ - Queue::push(ComputeIntegralJob::class,[ - 'page' => $page + 1 - ]); + $data['page'] = $page + 1; + Queue::push(ComputeIntegralJob::class,$data); } } catch(\Exception $e){ diff --git a/app/jobs/store/platformCommission/refundOrderHandle/HandleCommissionMoneyJob.php b/app/jobs/store/platformCommission/refundOrderHandle/HandleCommissionMoneyJob.php new file mode 100644 index 0000000..edd98e5 --- /dev/null +++ b/app/jobs/store/platformCommission/refundOrderHandle/HandleCommissionMoneyJob.php @@ -0,0 +1,83 @@ +field(['id','uid','mer_id','order_id','commission_merchants_money','commission_promoter_money']) + ->with([ + 'mer' => function($query){ + $query->field('mer_id,spread_uid as mer_spread_uid')->bind(['mer_spread_uid']); + }, + 'user' => function($query){ + $query->field('uid,spread_uid')->bind(['spread_uid']); + }, + ])->find(); + if(!$recordInfo) return false; + $recordInfo = $recordInfo->toArray(); + $userBillRepository = app()->make(UserBillRepository::class); + // 处理招商员佣金 + if((int)$recordInfo['mer_spread_uid'] > 0){ + // 获取冻结中佣金信息 + $reduce = (float)sprintf("%.2f",$recordInfo['commission_merchants_money'] * $data['refund_rate'] / 100);// 减少数量 + // 添加减少记录 + $userBillRepository->decBill($recordInfo['mer_spread_uid'], 'brokerage', 'commission_merchants', [ + 'link_id' => $recordInfo['order_id'], + 'status' => 1, + 'title' => '减少招商佣金', + 'number' => $reduce, + 'mark' => '订单退款,减少招商佣金' . floatval($reduce), + 'balance' => 0 + ]); + } + // 处理推广员佣金 + if((int)$recordInfo['spread_uid'] > 0){ + // 获取冻结中佣金信息 + $reduce = (float)sprintf("%.2f",$recordInfo['commission_promoter_money'] * $data['refund_rate'] / 100);// 减少数量 + // 添加减少记录 + $userBillRepository->decBill($recordInfo['spread_uid'], 'brokerage', 'commission_promoter', [ + 'link_id' => $recordInfo['order_id'], + 'status' => 1, + 'title' => '减少推广佣金', + 'number' => $reduce, + 'mark' => '订单退款,减少推广佣金' . floatval($reduce), + 'balance' => 0 + ]); + } + } + catch(\Exception $e){ + $data['error_msg'] = $e->getMessage(); + Log::info('订单退款成功 - 招商员佣金&推广员佣金处理 - 失败: '.var_export($data,1)); + } + $job->delete(); + } + + public function failed($data){ + Log::info('订单退款成功 - 招商员佣金&推广员佣金处理 - 失败(failed): '.var_export($data,1)); + } + + + + + + + + + + + +} diff --git a/app/jobs/store/platformCommission/refundOrderHandle/HandleCommissionPartnerMoneyJob.php b/app/jobs/store/platformCommission/refundOrderHandle/HandleCommissionPartnerMoneyJob.php new file mode 100644 index 0000000..31c1832 --- /dev/null +++ b/app/jobs/store/platformCommission/refundOrderHandle/HandleCommissionPartnerMoneyJob.php @@ -0,0 +1,84 @@ +find(); + // 待结算-结算时处理,已结算-减少用户已获得佣金,2、3-无处理 + if($platformCommissionRecord->commission_partner_status == 1){ + // 获取:获取当前抽成结算的归属周期 + $createTime = strtotime($platformCommissionRecord->create_time); + $cycleInfo = PartnerSettlementCycle::where('start_time','<',$createTime) + ->where('end_time','>',$createTime) + ->find(); + if($cycleInfo){ + // 计算总退款的合伙人佣金 + $reduceCommissionMoney = (float)sprintf("%.2f",$platformCommissionRecord['commission_partner_money'] * $data['refund_rate'] / 100);// 减少数量 + // 获取退款的用户列表 + $userList = PartnerSettlement::field('id,uid,money,proportion,cycle_id') + ->where('cycle_id',$cycleInfo->id) + ->select() + ->toArray(); + $bills = []; + foreach($userList as $partnerInfo){ + // 计算当前用户应扣除合伙人佣金 + $reduce = (float)sprintf("%.2f",$reduceCommissionMoney * $partnerInfo['proportion'] / 100); + // 记录账单 + $bills[] = [ + 'uid' => $partnerInfo['uid'], + 'link_id' => $partnerInfo['cycle_id'], + 'pm' => 0, + 'title' => '订单退款-扣除合伙人佣金', + 'category' => 'brokerage', + 'type' => 'commission_partner', + 'number' => $reduce, + 'balance' => 0, + 'mark' => '订单退款,扣除合伙人佣金'.floatval($reduce).'元', + 'mer_id' => 0, + 'status' => 1 + ]; + } + // 记录账单变化信息 + if (count($bills) > 0) app()->make(UserBillRepository::class)->insertAll($bills); + } + } + } + catch(\Exception $e){ + $data['error_msg'] = $e->getMessage(); + Log::info('订单退款成功 - 合伙人佣金处理 - 失败: '.var_export($data,1)); + } + $job->delete(); + } + + public function failed($data){ + Log::info('订单退款成功 - 合伙人佣金处理 - 失败(failed): '.var_export($data,1)); + } + + + + + + + + + + + +} diff --git a/app/jobs/store/platformCommission/refundOrderHandle/HandleLegumesJob.php b/app/jobs/store/platformCommission/refundOrderHandle/HandleLegumesJob.php new file mode 100644 index 0000000..5bacd59 --- /dev/null +++ b/app/jobs/store/platformCommission/refundOrderHandle/HandleLegumesJob.php @@ -0,0 +1,87 @@ +find(); + // 判断:当前抽成豆豆信息是否已经结算,未结算-无操作处理(结算时处理退款内容);已结算-处理退款相关内容 + $maxEndTime = Legumes::max('end_time'); + $createTime = strtotime($platformCommissionRecord->create_time); + if($createTime < $maxEndTime){ + // 已结算 - 处理退款相关内容 + $legumesId = (int)Legumes::where('start_time','<',$createTime)->where('end_time','>',$createTime)->value('id'); + if($legumesId > 0){ + // 获取需要修改的信息列表 + $correlationList = LegumesLog::field('id,uid,order_money,refund_order_money,get_legumes,refund_get_legumes,get_integral') + ->where('legumes_id',$legumesId) + ->select() + ->toArray(); + // 循环处理 + $updateData = []; + foreach($correlationList as $correlationInfo){ + // 计算 退款金额 + $reduceOrderMoney = (float)sprintf("%.2f",$correlationInfo['order_money'] * $data['refund_rate'] / 100); + $refundOrderMoney = bcadd($correlationInfo['refund_order_money'],$reduceOrderMoney,2); + if($refundOrderMoney > $correlationInfo['order_money']) $refundOrderMoney = $correlationInfo['order_money']; + // 计算 退款豆豆 + $reduceGetLegumes = (float)sprintf("%.2f",$correlationInfo['get_legumes'] * $data['refund_rate'] / 100); + $refundGetLegumes = bcadd($correlationInfo['refund_get_legumes'],$reduceGetLegumes,3); + if($refundGetLegumes > $correlationInfo['get_legumes']) $refundGetLegumes = $correlationInfo['get_legumes']; + // 记录修改信息 + $updateData[] = [ + 'id' => $correlationInfo['id'], + 'refund_order_money' => $refundOrderMoney, + 'refund_get_legumes' => $refundGetLegumes + ]; + } + + if(count($updateData) > 0) { + LegumesLog::batchUpdate(array_values($updateData)); + Queue::push(ComputeIntegralJob::class,[ + 'ids' => array_column($correlationList,'id') + ]); + } + } + } + } + catch(\Exception $e){ + $data['error_msg'] = $e->getMessage(); + Log::info('订单退款成功 - 豆豆和积分 - 失败: '.var_export($data,1)); + } + $job->delete(); + } + + public function failed($data){ + Log::info('订单退款成功 - 豆豆和积分 - 失败(failed): '.var_export($data,1)); + } + + + + + + + + + + + +} diff --git a/app/jobs/store/platformCommission/refundOrderHandle/HandlePlatformCommissionRecordJob.php b/app/jobs/store/platformCommission/refundOrderHandle/HandlePlatformCommissionRecordJob.php new file mode 100644 index 0000000..8f335f2 --- /dev/null +++ b/app/jobs/store/platformCommission/refundOrderHandle/HandlePlatformCommissionRecordJob.php @@ -0,0 +1,50 @@ +find(); + $platformCommissionRecord->refund_ratio += $data['refund_rate']; + if($platformCommissionRecord->refund_ratio >= 100){ + $platformCommissionRecord->refund_ratio = 100; + $platformCommissionRecord->commission_partner_status = 3; + $platformCommissionRecord->commission_merchants_status = 3; + $platformCommissionRecord->commission_promoter_status = 3; + } + $platformCommissionRecord->save(); + } + catch(\Exception $e){ + $data['error_msg'] = $e->getMessage(); + Log::info('订单退款成功 - 平台抽成记录处理 - 失败: '.var_export($data,1)); + } + $job->delete(); + } + + public function failed($data){ + Log::info('订单退款成功 - 平台抽成记录处理 - 失败(failed): '.var_export($data,1)); + } + + + + + + + + + + + +} diff --git a/app/jobs/store/platformCommission/refundOrderHandle/HandleWeightValueJob.php b/app/jobs/store/platformCommission/refundOrderHandle/HandleWeightValueJob.php new file mode 100644 index 0000000..1be7332 --- /dev/null +++ b/app/jobs/store/platformCommission/refundOrderHandle/HandleWeightValueJob.php @@ -0,0 +1,87 @@ +where('change_type',1) + ->where('source',0) + ->select() + ->toArray(); + $insertLogData = []; + foreach($weightValueLogList as $weightValueLogInfo){ + // 获取用户持有信息 + $holdInfo = WeightValue::where('uid',$weightValueLogInfo['uid']) + ->where('brokerage_level',$weightValueLogInfo['brokerage_level']) + ->find(); + // 获取已经减少数量 + $reduced = WeightValueLog::where('order_product_id',$data['order_product_id']) + ->where('change_type',0) + ->where('source',1) + ->where('uid',$weightValueLogInfo['uid']) + ->sum('change_quantity'); + // 计算减少信息,总减少数量不能超过获取数量 + $reduce = (float)sprintf("%.2f",$weightValueLogInfo['change_quantity'] * $data['refund_rate'] / 100); + $totalReduce = (float)sprintf("%.2f",$reduced * $reduce); + if($totalReduce > $weightValueLogInfo['change_quantity']) { + $reduce = (float)sprintf("%.2f",$weightValueLogInfo['change_quantity'] - $reduced); + } + $changeFront = $holdInfo->quantity; + $holdInfo->quantity = (float)sprintf("%.2f",$holdInfo->quantity - $reduce); + $holdInfo->save(); + // 记录变更记录 + $insertLogData[] = [ + 'uid' => $weightValueLogInfo['uid'], + 'brokerage_level' => $weightValueLogInfo['brokerage_level'], + 'product_id' => $weightValueLogInfo['product_id'], + 'order_id' => $weightValueLogInfo['order_id'], + 'order_product_id' => $weightValueLogInfo['order_product_id'], + 'change_type' => 0, + 'change_quantity' => $reduce, + 'change_front' => $changeFront, + 'change_after' => (float)$holdInfo->quantity, + 'remark' => '商品退款,减少权重值', + 'source' => 1, + ]; + } + + if(count($insertLogData) > 0) WeightValueLog::insertAll($insertLogData); + } + catch(\Exception $e){ + $data['error_msg'] = $e->getMessage(); + Log::info('订单退款成功 - 权重值相关处理 - 失败: '.var_export($data,1)); + } + $job->delete(); + } + + public function failed($data){ + Log::info('订单退款成功 - 权重值相关处理 - 失败(failed): '.var_export($data,1)); + } + + + + + + + + + + + +} diff --git a/app/listener/platformCommission/OrderAgreeRefundEvent.php b/app/listener/platformCommission/OrderAgreeRefundEvent.php index a655c03..44bfe51 100644 --- a/app/listener/platformCommission/OrderAgreeRefundEvent.php +++ b/app/listener/platformCommission/OrderAgreeRefundEvent.php @@ -3,7 +3,14 @@ namespace app\listener\platformCommission; +use app\common\repositories\store\order\StoreRefundProductRepository; +use app\jobs\store\platformCommission\refundOrderHandle\HandleCommissionMoneyJob; +use app\jobs\store\platformCommission\refundOrderHandle\HandleCommissionPartnerMoneyJob; +use app\jobs\store\platformCommission\refundOrderHandle\HandleLegumesJob; +use app\jobs\store\platformCommission\refundOrderHandle\HandlePlatformCommissionRecordJob; +use app\jobs\store\platformCommission\refundOrderHandle\HandleWeightValueJob; use think\facade\Log; +use think\facade\Queue; /** * Common: 订单退款申请成功(同意退款) @@ -19,21 +26,50 @@ class OrderAgreeRefundEvent{ try{ $refund = $data['refund']; Log::info('订单进入退款成功 - 平台抽成相关处理 - 开始:'.var_export(['id' => $data['id']],1)); - - - - - - - - - - + $refundProductList = app()->make(StoreRefundProductRepository::class) + ->getSearch([]) + ->field('order_product_id,refund_price,refund_num,refund_legumes_integral') + ->with([ + 'product' => function($query){ + $query->field('order_product_id,order_id,product_num,use_legumes_integral'); + } + ]) + ->where('refund_order_id',$data['id']) + ->select() + ->toArray(); + foreach($refundProductList as $refundProductInfo){ + // 判断:当前商品是否存在豆豆积分抵扣情况 + if((float)$refundProductInfo['product']['use_legumes_integral'] <= 0) continue; + if((int)$refundProductInfo['refund_num'] <= 0) continue; + $refundRate = (float)sprintf("%.2f",(int)$refundProductInfo['refund_num'] / (int)$refundProductInfo['product']['product_num'] * 100); + // 退款成功 计算平台抽成相关退款内容 + Queue::push(HandlePlatformCommissionRecordJob::class,[ + 'order_product_id' => $refundProductInfo['order_product_id'], + 'refund_rate' => $refundRate + ]); + // 退款成功 权重值相关处理 + Queue::push(HandleWeightValueJob::class,[ + 'order_product_id' => $refundProductInfo['order_product_id'], + 'refund_rate' => $refundRate + ]); + // 招商员佣金 & 推广员佣金处理 + Queue::push(HandleCommissionMoneyJob::class,[ + 'order_product_id' => $refundProductInfo['order_product_id'], + 'refund_rate' => $refundRate + ]); + // 合伙人佣金处理 + Queue::push(HandleCommissionPartnerMoneyJob::class,[ + 'order_product_id' => $refundProductInfo['order_product_id'], + 'refund_rate' => $refundRate + ]); + // 积分和豆豆处理 + Queue::push(HandleLegumesJob::class,[ + 'order_product_id' => $refundProductInfo['order_product_id'], + 'refund_rate' => $refundRate + ]); + } }catch(\Exception $e){ Log::info('订单进入退款成功 - 平台抽成相关处理 - 错误:'.$e->getMessage()); } } - - - } \ No newline at end of file