diff --git a/app/Amqp/Consumer/Refund/GoodOrderRefundFinishConsumer.php b/app/Amqp/Consumer/Refund/GoodOrderRefundFinishConsumer.php index 309a0f9..ab20543 100644 --- a/app/Amqp/Consumer/Refund/GoodOrderRefundFinishConsumer.php +++ b/app/Amqp/Consumer/Refund/GoodOrderRefundFinishConsumer.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace App\Amqp\Consumer\Refund; +use App\Constants\Common\OrderCode; use App\Exception\ErrException; use App\Lib\Log; use App\Service\Amqp\Refund\RefundGoodOrderFinishService; @@ -53,6 +54,8 @@ class GoodOrderRefundFinishConsumer extends ConsumerMessage $service->orderSno = $data['order_sno']; $service->payType = (int)$data['pay_type']; + $service->callbackData = $data['callback_data']; + $service->orderType = OrderCode::ORDER_TYPE_GOOD; $service->handle(); } catch (Exception|ErrException $e) { diff --git a/app/Listener/RefundGoodOrderFinishStatementListener.php b/app/Listener/RefundGoodOrderFinishStatementListener.php index 83d0ea7..5917e8f 100644 --- a/app/Listener/RefundGoodOrderFinishStatementListener.php +++ b/app/Listener/RefundGoodOrderFinishStatementListener.php @@ -17,6 +17,7 @@ use Exception; use Hyperf\Event\Annotation\Listener; use Psr\Container\ContainerInterface; use Hyperf\Event\Contract\ListenerInterface; +use function Symfony\Component\String\b; #[Listener] class RefundGoodOrderFinishStatementListener implements ListenerInterface @@ -34,7 +35,7 @@ class RefundGoodOrderFinishStatementListener implements ListenerInterface public function listen(): array { return [ - RefundGoodOrderFinishEvent::class, +// RefundGoodOrderFinishEvent::class, ]; } @@ -43,13 +44,21 @@ class RefundGoodOrderFinishStatementListener implements ListenerInterface */ protected Order $orderInfo; + /** + * @var RefundOrder + */ + protected RefundOrder $refundOrderInfo; + + /** + * @var array + */ protected array $orderGoodIds; public function process(object $event): void { try { - $refundOrderInfo = $this->refundOrderModel->find($event->refundOrderId); - if (empty($refundOrderInfo) || $refundOrderInfo->status != RefundCode::REFUND_SUCCESS) throw new Exception('退款信息不存在'); + $this->refundOrderInfo = $this->refundOrderModel->find($event->refundOrderId); + if (empty($this->refundOrderInfo) || $this->refundOrderInfo->status != RefundCode::REFUND_SUCCESS) throw new Exception('退款信息不存在'); $this->orderInfo = $this->orderModel->find($event->orderId); if (empty($this->orderInfo) || !in_array($this->orderInfo->status,[OrderCode::FINISH_REFUND,OrderCode::UNCOMPLETED_REFUND])) throw new Exception('订单信息不存在'); @@ -67,6 +76,44 @@ class RefundGoodOrderFinishStatementListener implements ListenerInterface 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(); + + $allIds = $this->orderGoodModel->where('order_id',$this->orderInfo->id)->pluck('id')->toArray(); + + if (empty($isRefundIds)) { + $refundOrderGoodIds = $allIds; + break; + } + + $isRefundArr = []; + foreach ($isRefundIds as $oneDay) { + array_merge($isRefundArr,json_decode($oneDay,true)); + } + + if (empty($isRefundArr)) { + $refundOrderGoodIds = $allIds; + break; + } + + $refundOrderGoodIds = array_diff($allIds,$isRefundArr); + } + + if (empty($refundOrderGoodIds)) throw new Exception('订单商品信息获取失败'); + + $skuIds = $this->orderGoodModel->whereIn('id',$refundOrderGoodIds)->pluck('quantity','sku_id')->toArray(); + } diff --git a/app/Model/RefundOrder.php b/app/Model/RefundOrder.php index 15d4e2c..5a1887d 100644 --- a/app/Model/RefundOrder.php +++ b/app/Model/RefundOrder.php @@ -12,9 +12,11 @@ use Hyperf\DbConnection\Model\Model; /** * @property int $id * @property int $user_id - * @property int $order_type + * @property int $order_type + * @property int $type * @property int $order_id - * @property int $pay_id + * @property int $pay_id + * @property string $good_ids * @property int $refund_status * @property string $refund_money * @property string $refund_time @@ -98,9 +100,9 @@ class RefundOrder extends Model /** * @param string $orderSno * @param int $type - * @return \Hyperf\Database\Model\Model|null + * @return \Hyperf\Database\Model\Model|RefundOrder|null */ - public function getInfoByOrderSnoAndType(string $orderSno, int $type): \Hyperf\Database\Model\Model|null + public function getInfoByOrderSnoAndType(string $orderSno, int $type): \Hyperf\Database\Model\Model|null|RefundOrder { return $this->where('refund_order_sno',$orderSno)->where('refund_type',$type)->first(); } diff --git a/app/Service/Amqp/Refund/BaseRefundOrderService.php b/app/Service/Amqp/Refund/BaseRefundOrderService.php index 16a82c7..7d25d49 100644 --- a/app/Service/Amqp/Refund/BaseRefundOrderService.php +++ b/app/Service/Amqp/Refund/BaseRefundOrderService.php @@ -213,13 +213,15 @@ abstract class BaseRefundOrderService $this->refundInfo = new RefundOrder(); $this->refundInfo->user_id = $this->orderInfo->user_id; - $this->refundInfo->order_type = $this->type; + $this->refundInfo->order_type = OrderCode::ORDER_TYPE_GOOD; $this->refundInfo->order_id = $this->orderId; $this->refundInfo->pay_id = $this->payInfo->id; + $this->refundInfo->type = $this->type; + if ($this->type == RefundCode::PARTIAL_GOOD_REFUND) $this->refundInfo->good_ids = json_encode($this->orderGoodIds); $this->refundInfo->refund_status = RefundCode::WAIT_REFUND; $this->refundInfo->refund_money = $this->refundAmount; $this->refundInfo->refund_type = $this->payInfo->recharge_type; - $this->refundInfo->refund_order_sno = $this->generateRefundOrderNo($this->type, $this->orderInfo->user_id); + $this->refundInfo->refund_order_sno = $this->generateRefundOrderNo(OrderCode::ORDER_TYPE_GOOD, $this->orderInfo->user_id); $this->refundInfo->reason = $this->reason; if (!$this->refundInfo->save()) throw new Exception('退款订单创建失败'); diff --git a/app/Service/Amqp/Refund/RefundGoodOrderFinishService.php b/app/Service/Amqp/Refund/RefundGoodOrderFinishService.php index 267bf96..9afa89a 100644 --- a/app/Service/Amqp/Refund/RefundGoodOrderFinishService.php +++ b/app/Service/Amqp/Refund/RefundGoodOrderFinishService.php @@ -10,7 +10,8 @@ declare(strict_types=1); namespace App\Service\Amqp\Refund; -use App\Constants\Common\OrderCode; +use App\Event\RefundGoodOrderFinishEvent; +use App\Lib\Log; use App\Model\Order; use App\Model\PayOrder; use App\Model\RefundOrder; @@ -19,8 +20,10 @@ use App\Service\ServiceTrait\Api\CouponTrait; use App\Service\ServiceTrait\Api\OrderTrait; use App\Service\ServiceTrait\Api\RefundOrderTrait; use Hyperf\DbConnection\Db; +use Hyperf\Di\Annotation\Inject; use Psr\Container\ContainerExceptionInterface; use Psr\Container\NotFoundExceptionInterface; +use Psr\EventDispatcher\EventDispatcherInterface; class RefundGoodOrderFinishService { @@ -44,6 +47,11 @@ class RefundGoodOrderFinishService */ public int $orderType; + /** + * @var array + */ + public array $callbackData; + /** * @var Order */ @@ -59,23 +67,47 @@ class RefundGoodOrderFinishService */ protected RefundOrder $refundInfo; + /** + * @var EventDispatcherInterface + */ + #[Inject] + private EventDispatcherInterface $eventDispatcher; + + /** + * @var Log + */ + #[Inject] + protected Log $log; + + /** + * @var bool + */ + protected bool $rollBackStockFlag; + /** * @return void + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface */ public function handle(): void { $this->checkRefundOrder(); + $this->rollBackStockFlag = $this->orderInfo->cycle_id == $this->initTodayCycleId(); + 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); + if ($this->rollBackStockFlag) { + $this->sendStockMq($this->orderInfo->id,$this->orderInfo->status); + } + + $this->eventDispatcher->dispatch(new RefundGoodOrderFinishEvent($this->orderInfo->id, $this->payInfo->id, $this->refundInfo->id)); + } } \ No newline at end of file diff --git a/app/Service/Api/System/SystemConfigService.php b/app/Service/Api/System/SystemConfigService.php index d601ec1..fd18794 100644 --- a/app/Service/Api/System/SystemConfigService.php +++ b/app/Service/Api/System/SystemConfigService.php @@ -35,6 +35,8 @@ class SystemConfigService extends BaseService { $res = [ 'logo' => $this->configCache->getConfigValueByKey(ConfigCode::APP_LOGO), + 'sundry_unit_price' => $this->configCache->getConfigValueByKey(ConfigCode::SUNDRY_UNIT_PRICE), + 'today_cut_off_time' => $this->configCache->getConfigValueByKey(ConfigCode::TODAY_CUT_OFF_TIME_KEY), ]; return $this->return->success('success', $res); diff --git a/app/Service/ServiceTrait/Api/CateringTrait.php b/app/Service/ServiceTrait/Api/CateringTrait.php index 100db36..71d38b5 100644 --- a/app/Service/ServiceTrait/Api/CateringTrait.php +++ b/app/Service/ServiceTrait/Api/CateringTrait.php @@ -9,12 +9,15 @@ use App\Model\OrderMealCateringLog; use App\Model\OrderOptionCateringLog; use App\Model\Sku; use App\Model\Spu; +use App\Service\ServiceTrait\Common\CycleTrait; use Hyperf\Di\Annotation\Inject; use Psr\Container\ContainerExceptionInterface; use Psr\Container\NotFoundExceptionInterface; trait CateringTrait { + use CycleTrait; + /** * @var OrderOptionCateringLog */ @@ -54,6 +57,8 @@ trait CateringTrait */ private function manageSubCateringLog(): void { + if (!$this->rollBackStockFlag) return; + match ($this->orderInfo->type) { OrderCode::ORDER_TYPE_MEAL => $this->manageSubMealCateringLog(), OrderCode::ORDER_TYPE_OPTIONAL => $this->manageSubOptionCateringLog(), diff --git a/app/Service/ServiceTrait/Api/RefundOrderTrait.php b/app/Service/ServiceTrait/Api/RefundOrderTrait.php index a00fafa..21dc81a 100644 --- a/app/Service/ServiceTrait/Api/RefundOrderTrait.php +++ b/app/Service/ServiceTrait/Api/RefundOrderTrait.php @@ -6,7 +6,9 @@ use App\Constants\Common\OrderCode; use App\Constants\Common\PayCode; use App\Constants\Common\RefundCode; use App\Exception\ErrException; +use App\Model\OrderGood; use Exception; +use Hyperf\Di\Annotation\Inject; use Psr\Container\ContainerExceptionInterface; use Psr\Container\NotFoundExceptionInterface; @@ -17,9 +19,9 @@ trait RefundOrderTrait */ protected function checkRefundOrder(): void { - $this->refundInfo = $this->refundOrderModel->getInfoByOrderSnoAndType($this->orderNo,self::PayType); + $this->refundInfo = $this->refundOrderModel->getInfoByOrderSnoAndType($this->orderSno,$this->payType); $this->orderInfo = $this->orderModel->getInfoById($this->refundInfo->order_id); - $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) || empty($this->refundInfo)) throw new ErrException('订单不存在'); @@ -48,6 +50,12 @@ trait RefundOrderTrait } } + /** + * @var OrderGood + */ + #[Inject] + protected OrderGood $orderGoodModel; + /** * @return void * @throws ContainerExceptionInterface @@ -62,13 +70,17 @@ trait RefundOrderTrait if ($isRefundMoney == $this->payInfo->pay_money) { $this->orderInfo->status = OrderCode::FINISH_REFUND; - $this->orderInfo->is_refund_all = 1;//todo 感觉可以删除这个值 } else { $this->orderInfo->status = OrderCode::UNCOMPLETED_REFUND; } if (!$this->orderInfo->save()) throw new Exception('更新退款订单失败'); + match ($this->refundInfo->type) { + RefundCode::FULL_GOOD_REFUND => $this->orderGoodModel->where('order_id',$this->refundInfo->order_id)->update(['refund_status' => OrderCode::FINISH_REFUND]), + RefundCode::PARTIAL_GOOD_REFUND => $this->orderGoodModel->whereIn('id',json_decode($this->refundInfo->good_ids,true))->update(['refund_status' => OrderCode::FINISH_REFUND]), + }; + }catch (Exception $e) { $this->log->error(__CLASS__.':Function:manageGoodOrder:'.$e->getMessage().':orderId:'.$this->orderInfo->id); throw new ErrException('Function:RefundFinish:'.$e->getMessage().':orderId:'.$this->orderInfo->id);