From 377f780ab543bfbe79b433d49e4d7027a70ca160 Mon Sep 17 00:00:00 2001 From: ctexthuang Date: Tue, 25 Mar 2025 15:54:34 +0800 Subject: [PATCH] feat : spu --- .../Pay/GoodOrderPayFinishConsumer.php | 61 +++++++ .../Refund/GoodOrderRefundFinishConsumer.php | 62 +++++++ .../Pay/GoodOrderPayFinishProducer.php | 26 +++ .../Refund/GoodOrderRefundFinishProducer.php | 26 +++ app/Controller/Api/DriverController.php | 27 +++ .../Amqp/Pay/PayGoodOrderFinishService.php | 81 +++++++++ .../Refund/RefundGoodOrderFinishService.php | 63 +++++++ app/Service/Api/Driver/BaseDriverService.php | 56 ++++++ app/Service/Api/Driver/GetSiteListService.php | 166 ++++++++++++++++++ .../Pay/Wx/WxJsRechargeOrderService.php | 65 ++++--- .../ServiceTrait/Api/GoodOrderTrait.php | 2 +- .../ServiceTrait/Api/RefundOrderTrait.php | 25 +-- 12 files changed, 619 insertions(+), 41 deletions(-) create mode 100644 app/Amqp/Consumer/Pay/GoodOrderPayFinishConsumer.php create mode 100644 app/Amqp/Consumer/Refund/GoodOrderRefundFinishConsumer.php create mode 100644 app/Amqp/Producer/Pay/GoodOrderPayFinishProducer.php create mode 100644 app/Amqp/Producer/Refund/GoodOrderRefundFinishProducer.php create mode 100644 app/Controller/Api/DriverController.php create mode 100644 app/Service/Amqp/Pay/PayGoodOrderFinishService.php create mode 100644 app/Service/Amqp/Refund/RefundGoodOrderFinishService.php create mode 100644 app/Service/Api/Driver/BaseDriverService.php create mode 100644 app/Service/Api/Driver/GetSiteListService.php diff --git a/app/Amqp/Consumer/Pay/GoodOrderPayFinishConsumer.php b/app/Amqp/Consumer/Pay/GoodOrderPayFinishConsumer.php new file mode 100644 index 0000000..d76eb45 --- /dev/null +++ b/app/Amqp/Consumer/Pay/GoodOrderPayFinishConsumer.php @@ -0,0 +1,61 @@ +log->error('GoodOrderPayFinishConsumer:error:NoData:'.json_encode($data)); + return Result::ACK; + } + + $this->log->callbackLog('GoodOrderPayFinishConsumer:success:'.json_encode($data)); + + try { + $service = new PayGoodOrderFinishService(); + + $service->orderSno = $data['order_sno']; + $service->payType = (int)$data['pay_type']; + + $service->handle(); + } catch (Exception|ErrException $e) { + $this->log->error('GoodOrderPayFinishConsumer:error:'.$e->getMessage()); + } + + return Result::ACK; + } +} diff --git a/app/Amqp/Consumer/Refund/GoodOrderRefundFinishConsumer.php b/app/Amqp/Consumer/Refund/GoodOrderRefundFinishConsumer.php new file mode 100644 index 0000000..5bac860 --- /dev/null +++ b/app/Amqp/Consumer/Refund/GoodOrderRefundFinishConsumer.php @@ -0,0 +1,62 @@ +log->error('GoodOrderRefundFinishConsumer:error:NoData:'.json_encode($data)); + return Result::ACK; + } + + $this->log->callbackLog('GoodOrderRefundFinishConsumer:success:'.json_encode($data)); + + try { + $service = new RefundGoodOrderFinishService(); + + $service->orderSno = $data['order_sno']; + $service->payType = (int)$data['pay_type']; + + $service->handle(); + } catch (Exception|ErrException $e) { + $this->log->error('GoodOrderRefundFinishConsumer:error:'.$e->getMessage()); + } + + return Result::ACK; + } +} diff --git a/app/Amqp/Producer/Pay/GoodOrderPayFinishProducer.php b/app/Amqp/Producer/Pay/GoodOrderPayFinishProducer.php new file mode 100644 index 0000000..0418da1 --- /dev/null +++ b/app/Amqp/Producer/Pay/GoodOrderPayFinishProducer.php @@ -0,0 +1,26 @@ + {"order_sno":"out_trade_no","pay_type":"PayType"} + */ + $this->payload = $data; + } +} diff --git a/app/Amqp/Producer/Refund/GoodOrderRefundFinishProducer.php b/app/Amqp/Producer/Refund/GoodOrderRefundFinishProducer.php new file mode 100644 index 0000000..cad6e9a --- /dev/null +++ b/app/Amqp/Producer/Refund/GoodOrderRefundFinishProducer.php @@ -0,0 +1,26 @@ + {"refund_order_sno":"out_trade_no","pay_type":"PayType"} + */ + $this->payload = $data; + } +} diff --git a/app/Controller/Api/DriverController.php b/app/Controller/Api/DriverController.php new file mode 100644 index 0000000..7397b78 --- /dev/null +++ b/app/Controller/Api/DriverController.php @@ -0,0 +1,27 @@ +handle(); + } +} diff --git a/app/Service/Amqp/Pay/PayGoodOrderFinishService.php b/app/Service/Amqp/Pay/PayGoodOrderFinishService.php new file mode 100644 index 0000000..64eada4 --- /dev/null +++ b/app/Service/Amqp/Pay/PayGoodOrderFinishService.php @@ -0,0 +1,81 @@ +checkCallBackOrder(); + + Db::transaction(function (){ + $this->manageOrder(); + + $this->manageWxOrder(); +// + $this->isCatering = $this->manageAddCateringLog(); + }); + + //已经截单 自动退款 + if (!$this->isCatering) $this->joinRefundQueue($this->orderInfo->id,RefundCode::FULL_GOOD_REFUND,'已截单,系统自动退款'); + } +} \ No newline at end of file diff --git a/app/Service/Amqp/Refund/RefundGoodOrderFinishService.php b/app/Service/Amqp/Refund/RefundGoodOrderFinishService.php new file mode 100644 index 0000000..2693a82 --- /dev/null +++ b/app/Service/Amqp/Refund/RefundGoodOrderFinishService.php @@ -0,0 +1,63 @@ +checkRefundOrder(); + + Db::transaction(function (){ + $this->manageRefundOrder(); + + $this->manageOrderByRefund(); + + $this->manageSubCateringLog(); + + if ($this->orderInfo->coupin_id > 0) $this->rollbackCoupon(OrderCode::ORDER_TYPE_GOOD,$this->orderInfo); + }); + +// $this->sendStockMq($this->orderInfo->id,$this->orderInfo->status); + } +} \ No newline at end of file diff --git a/app/Service/Api/Driver/BaseDriverService.php b/app/Service/Api/Driver/BaseDriverService.php new file mode 100644 index 0000000..9e2075d --- /dev/null +++ b/app/Service/Api/Driver/BaseDriverService.php @@ -0,0 +1,56 @@ +cycleId = $this->initTodayCycleId(); + + $this->adminInfo = $this->adminUserModel->getAdminInfoByBindUserId($this->userId); + if ($this->adminInfo->role_id != RoleCode::DRIVER) throw new ErrException('暂无权限,请联系客服并提供相关信息绑定账号'); + } + + abstract public function handle(); +} \ No newline at end of file diff --git a/app/Service/Api/Driver/GetSiteListService.php b/app/Service/Api/Driver/GetSiteListService.php new file mode 100644 index 0000000..0ce8252 --- /dev/null +++ b/app/Service/Api/Driver/GetSiteListService.php @@ -0,0 +1,166 @@ +siteModel + ->where('delivered_id',$this->adminInfo->id) + ->orderBy('sequence') + ->get(); + if ($siteArr->isEmpty()) throw new ErrException('该司机并未绑定站点'); + $siteArr = array_column($siteArr->toArray(),null,'id'); + $this->siteIds = array_keys($siteArr); + + $optionArr = $this->getOptionData(); + + $mealArr = $this->getMealData(); + + $res = []; + foreach ($siteArr as $site) { + if (empty($res[$site['id']])) { + $res[$site['id']] = [ + 'site_id' => $site['id'], + 'site_name' => $site['name'], + 'name' => '', + 'meal_order_quantity' => 0, + 'meal_add_staple_food_num' => 0, + 'option_order_quantity' => 0, + 'option_add_staple_food_num' => 0, + ]; + } + } + } + + /** + * @return array + */ + private function getMealData(): array + { + $mealArr = $this->orderMealCateringLogModel + ->where('cycle_id',$this->cycleId) + ->where('status',CateringCode::CATERING_STATUS_FINISH) + ->whereIn('site_id', $this->siteIds) + ->select(['quantity','site_id','sku_id']) + ->get(); + if ($mealArr->isEmpty()) $mealArr = []; + $mealArr = $mealArr->toArray(); + + $skuIds = array_column($mealArr, 'sku_id'); + $skuArr = $this->skuModel->getDataArrByIds($skuIds); + $skuArr = array_column($skuArr,null,'id'); + + + $res = []; + foreach ($mealArr as $oneLog) { + if (empty($res[$oneLog['site_id']])) { + $res[$oneLog['site_id']] = [ + 'total_copies' => 0, + 'add_staple_food_num' => 0, + 'sku' => [] + ]; + } + + if (empty($res[$oneLog['site_id']]['sku'][$oneLog['sku_id']])) { + $res[$oneLog['site_id']]['sku'][$oneLog['sku_id']] = [ + 'sku_id' => $oneLog['sku_id'], + 'sku_name' => $skuArr[$oneLog['sku_id']]['title'] ?? '', + 'quantity' => 0, + ]; + } + + if ($skuArr[$oneLog['sku_id']]['is_add_staple_food'] == GoodCode::IS_ADD_STAPLE_FOOD) { + $res[$oneLog['site_id']]['add_staple_food_num'] += $oneLog['quantity']; + } else { + $res[$oneLog['site_id']]['total_copies'] += $oneLog['quantity']; + } + + $res[$oneLog['site_id']]['sku'][$oneLog['sku_id']]['quantity'] += $oneLog['quantity']; + } + + return $res; + } + + /** + * @return array + */ + private function getOptionData(): array + { + $optionArr = $this->orderOptionCateringLogModel + ->where('cycle_id',$this->cycleId) + ->where('status',CateringCode::CATERING_STATUS_FINISH) + ->whereIn('site_id', $this->siteIds) + ->select(['quantity','site_id','add_staple_food_num']) + ->get(); + if ($optionArr->isEmpty()) $optionArr = []; + $optionArr = $optionArr->toArray(); + + $res = []; + foreach ($optionArr as $oneSite) { + if (empty($res[$oneSite['site_id']])) { + $res[$oneSite['site_id']] = [ + 'copies' => 0, + 'add_staple_food_num' => 0 + ]; + } + + $res[$oneSite['site_id']]['copies'] = $oneSite['quantity'] + $res[$oneSite['site_id']]['copies']; + $res[$oneSite['site_id']]['add_staple_food_num'] = $oneSite['add_staple_food_num'] + $res[$oneSite['site_id']]['add_staple_food_num']; + } + + return $res; + } +} diff --git a/app/Service/Common/Pay/Wx/WxJsRechargeOrderService.php b/app/Service/Common/Pay/Wx/WxJsRechargeOrderService.php index 3ceab8e..0f0fa73 100644 --- a/app/Service/Common/Pay/Wx/WxJsRechargeOrderService.php +++ b/app/Service/Common/Pay/Wx/WxJsRechargeOrderService.php @@ -2,6 +2,8 @@ namespace App\Service\Common\Pay\Wx; +use App\Amqp\Producer\Pay\GoodOrderPayFinishProducer; +use App\Amqp\Producer\Refund\GoodOrderRefundFinishProducer; use App\Constants\Common\OrderCode; use App\Constants\Common\PayCode; use App\Constants\Common\RefundCode; @@ -13,7 +15,9 @@ use App\Service\ServiceTrait\Api\OrderTrait; use App\Service\ServiceTrait\Api\PayFinishTrait; use App\Service\ServiceTrait\Api\RefundOrderTrait; use App\Service\ServiceTrait\Common\OrderChangeStatusTrait; +use Hyperf\Amqp\Producer; use Hyperf\DbConnection\Db; +use Hyperf\Di\Annotation\Inject; use Psr\Container\ContainerExceptionInterface; use Psr\Container\NotFoundExceptionInterface; use Psr\Http\Message\ResponseInterface; @@ -68,6 +72,12 @@ class WxJsRechargeOrderService extends WxJsRechargeBaseService */ protected bool $isCatering = true; + /** + * @var Producer + */ + #[Inject] + protected Producer $producer; + /** * @return ResponseInterface * @throws ContainerExceptionInterface @@ -80,18 +90,25 @@ class WxJsRechargeOrderService extends WxJsRechargeBaseService $this->checkWxCallBackOrder(); - $this->checkCallBackOrder(); + $message = new GoodOrderPayFinishProducer([ + 'order_sno' => $this->orderNo, + 'pay_type' => self::PayType, + ]); + $this->producer->produce($message); - Db::transaction(function (){ - $this->manageOrder(); - $this->manageWxOrder(); +// $this->checkCallBackOrder(); // - $this->isCatering = $this->manageAddCateringLog(); - }); - - //已经截单 自动退款 - if (!$this->isCatering) $this->joinRefundQueue($this->orderId,RefundCode::FULL_GOOD_REFUND,'已截单,系统自动退款'); +// Db::transaction(function (){ +// $this->manageOrder(); +// +// $this->manageWxOrder(); +//// +// $this->isCatering = $this->manageAddCateringLog(); +// }); +// +// //已经截单 自动退款 +// if (!$this->isCatering) $this->joinRefundQueue($this->orderId,RefundCode::FULL_GOOD_REFUND,'已截单,系统自动退款'); //todo 发送订阅通知 // $this->sendGainCoinMsg(); @@ -118,19 +135,25 @@ class WxJsRechargeOrderService extends WxJsRechargeBaseService $this->checkWxRefundCallBackOrder(); - $this->checkRefundOrder(); + $message = new GoodOrderRefundFinishProducer([ + 'refund_order_sno' => $this->orderNo, + 'pay_type' => self::PayType, + ]); + $this->producer->produce($message); - Db::transaction(function (){ - $this->manageRefundOrder(); - - $this->manageOrderByRefund(); - - $this->manageSubCateringLog(); - - if ($this->orderInfo->coupin_id > 0) $this->rollbackCoupon(OrderCode::ORDER_TYPE_GOOD,$this->orderInfo); - }); - - $this->sendStockMq($this->orderInfo->id,$this->orderInfo->status); +// $this->checkRefundOrder(); +// +// Db::transaction(function (){ +// $this->manageRefundOrder(); +// +// $this->manageOrderByRefund(); +// +// $this->manageSubCateringLog(); +// +// if ($this->orderInfo->coupin_id > 0) $this->rollbackCoupon(OrderCode::ORDER_TYPE_GOOD,$this->orderInfo); +// }); +// +// $this->sendStockMq($this->orderInfo->id,$this->orderInfo->status); return $this->returnSuccess(); } diff --git a/app/Service/ServiceTrait/Api/GoodOrderTrait.php b/app/Service/ServiceTrait/Api/GoodOrderTrait.php index 681205f..33fc7d5 100644 --- a/app/Service/ServiceTrait/Api/GoodOrderTrait.php +++ b/app/Service/ServiceTrait/Api/GoodOrderTrait.php @@ -53,7 +53,7 @@ trait GoodOrderTrait protected function checkCallBackOrder(): void { $this->orderInfo = $this->orderModel->getInfoByOrderSno($this->orderNo); - $this->payInfo = $this->payOrderModel->getInfoByOrderIdAndTypeAndRType($this->orderInfo->id,self::OrderType,self::PayType); + $this->payInfo = $this->payOrderModel->getInfoByOrderIdAndTypeAndRType($this->orderInfo->id,$this->orderType,$this->payType); if (empty($this->orderInfo) || empty($this->payInfo)) { $this->log->debug(__CLASS__.':订单不存在,订单号:'.$this->orderNo); diff --git a/app/Service/ServiceTrait/Api/RefundOrderTrait.php b/app/Service/ServiceTrait/Api/RefundOrderTrait.php index 01eeadf..a00fafa 100644 --- a/app/Service/ServiceTrait/Api/RefundOrderTrait.php +++ b/app/Service/ServiceTrait/Api/RefundOrderTrait.php @@ -14,8 +14,6 @@ trait RefundOrderTrait { /** * @return void - * @throws ContainerExceptionInterface - * @throws NotFoundExceptionInterface */ protected function checkRefundOrder(): void { @@ -23,26 +21,15 @@ trait RefundOrderTrait $this->orderInfo = $this->orderModel->getInfoById($this->refundInfo->order_id); $this->payInfo = $this->payOrderModel->getInfoByOrderIdAndTypeAndRType($this->orderInfo->id,self::OrderType,self::PayType); - if (empty($this->orderInfo) || empty($this->payInfo) || empty($this->refundInfo)) { - $this->log->debug(__CLASS__.':订单不存在,订单号:'.$this->orderNo); - throw new ErrException('订单不存在'); - } + if (empty($this->orderInfo) || empty($this->payInfo) || empty($this->refundInfo)) throw new ErrException('订单不存在'); - if ($this->refundInfo->refund_status != RefundCode::WAIT_BY_PAY_TOOL) { - $this->log->debug(__CLASS__.':订单已退款成功,订单信息:'.json_encode($this->refundInfo->toArray())); - throw new ErrException('订单已退款成功'); - } + if ($this->refundInfo->refund_status != RefundCode::WAIT_BY_PAY_TOOL) throw new ErrException('订单已退款成功'); - if ($this->refundInfo->refund_type != PayCode::WECHAT_PAY) { - $this->log->debug(__CLASS__.':订单退款渠道错误,订单信息:'.json_encode($this->refundInfo->toArray())); - throw new ErrException('订单退款渠道错误'); - } + if ($this->refundInfo->refund_type != PayCode::WECHAT_PAY) throw new ErrException('订单退款渠道错误'); } /** * @return void - * @throws ContainerExceptionInterface - * @throws NotFoundExceptionInterface */ protected function manageRefundOrder(): void { @@ -56,8 +43,8 @@ trait RefundOrderTrait if (!$this->refundInfo->save()) throw new Exception('更新退款订单失败'); }catch (Exception $e) { - $this->log->error(__CLASS__.':Function:manageGoodOrder:'.$e->getMessage().':orderId:'.$this->orderInfo->id); - throw new ErrException($e->getMessage()); +// $this->log->error('Function:RefundFinish:'.$e->getMessage().':orderId:'.$this->orderInfo->id); + throw new ErrException('Function:RefundFinish:'.$e->getMessage().':orderId:'.$this->orderInfo->id); } } @@ -84,7 +71,7 @@ trait RefundOrderTrait }catch (Exception $e) { $this->log->error(__CLASS__.':Function:manageGoodOrder:'.$e->getMessage().':orderId:'.$this->orderInfo->id); - throw new ErrException($e->getMessage()); + throw new ErrException('Function:RefundFinish:'.$e->getMessage().':orderId:'.$this->orderInfo->id); } } } \ No newline at end of file