From c7d0a222ab6b35d70d780bca978a6710c41794de Mon Sep 17 00:00:00 2001 From: ctexthuang Date: Wed, 26 Mar 2025 17:48:48 +0800 Subject: [PATCH] feat : spu --- .../Statement/RefundFinishConsumer.php | 40 +++++ .../Statement/RefundFinishProducer.php | 27 +++ .../Amqp/Statement/RefundStatementService.php | 159 ++++++++++++++++++ 3 files changed, 226 insertions(+) create mode 100644 app/Amqp/Consumer/Statement/RefundFinishConsumer.php create mode 100644 app/Amqp/Producer/Statement/RefundFinishProducer.php create mode 100644 app/Service/Amqp/Statement/RefundStatementService.php diff --git a/app/Amqp/Consumer/Statement/RefundFinishConsumer.php b/app/Amqp/Consumer/Statement/RefundFinishConsumer.php new file mode 100644 index 0000000..0695801 --- /dev/null +++ b/app/Amqp/Consumer/Statement/RefundFinishConsumer.php @@ -0,0 +1,40 @@ +log->error('RefundFinishConsumer:error:NoData:'.json_encode($data)); + return Result::ACK; + } + + + + return Result::ACK; + } +} diff --git a/app/Amqp/Producer/Statement/RefundFinishProducer.php b/app/Amqp/Producer/Statement/RefundFinishProducer.php new file mode 100644 index 0000000..2eba830 --- /dev/null +++ b/app/Amqp/Producer/Statement/RefundFinishProducer.php @@ -0,0 +1,27 @@ + {"order_id":"order_id","refund_order_id":"refund_order_id"} + */ + $this->payload = $data; + } +} diff --git a/app/Service/Amqp/Statement/RefundStatementService.php b/app/Service/Amqp/Statement/RefundStatementService.php new file mode 100644 index 0000000..85aee98 --- /dev/null +++ b/app/Service/Amqp/Statement/RefundStatementService.php @@ -0,0 +1,159 @@ +refundOrderInfo = $this->refundOrderModel->find($this->refundOrderId); + if (empty($this->refundOrderInfo) || $this->refundOrderInfo->status != RefundCode::REFUND_SUCCESS) throw new Exception('退款信息不存在'); + + $this->orderInfo = $this->orderModel->find($this->orderId); + if (empty($this->orderInfo) || !in_array($this->orderInfo->status,[OrderCode::FINISH_REFUND,OrderCode::UNCOMPLETED_REFUND])) throw new Exception('订单信息不存在'); + + $this->orderGoodIds = $this->orderGoodModel->where('order_id',$this->orderId)->pluck('id')->toArray(); + if (empty($this->orderGoodIds)) throw new Exception('订单商品信息不存在'); + + $this->rollBackChefData(); + + $this->createRefundData(); + } + + /** + * @return void + * @throws Exception + */ + private function rollBackChefData(): void + { + $refundOrderGoodIds = []; + switch ($this->refundOrderInfo->type) { + case RefundCode::PARTIAL_GOOD_REFUND: + $refundOrderGoodIds = json_decode($this->refundOrderInfo->good_ids,true); + break; + case RefundCode::FULL_GOOD_REFUND: + $isRefundIds = $this->refundOrderModel + ->where('order_id',$this->orderInfo->id) + ->where('order_type',OrderCode::ORDER_TYPE_GOOD) + ->where('type',RefundCode::PARTIAL_GOOD_REFUND) + ->where('status',RefundCode::REFUND_SUCCESS) + ->pluck('good_ids') + ->toArray(); + + if (empty($isRefundIds)) { + $refundOrderGoodIds = $this->orderGoodIds; + break; + } + + $isRefundArr = []; + foreach ($isRefundIds as $oneDay) { + array_merge($isRefundArr,json_decode($oneDay,true)); + } + + if (empty($isRefundArr)) { + $refundOrderGoodIds = $this->orderGoodIds; + break; + } + + $refundOrderGoodIds = array_diff($this->orderGoodIds,$isRefundArr); + break; + } + + if (empty($refundOrderGoodIds)) throw new Exception('订单商品信息获取失败-01'); + + $skuList = $this->orderGoodModel->whereIn('id',$refundOrderGoodIds)->selectRaw('SUM(`quantity`) as quantity')->select('sku_id')->groupBy('sku_id')->get(); + if ($skuList->isEmpty()) throw new Exception('订单商品信息获取失败-02'); + $skuList = $skuList->toArray(); + + foreach ($skuList as $skuInfo) { + $info = $this->chefStatementModel->where('sku_id',$skuInfo['sku_id'])->first(); + + if (empty($info)) continue; + + $info->sale -= $skuInfo['quantity']; + $info->refund += $skuInfo['quantity']; + + if (!$info->save()) throw new Exception('更新报表信息失败-03'); + } + } + + private function createRefundData() + { + //todo + } +} \ No newline at end of file