diff --git a/app/Listener/RefundGoodOrderFinishStatementListener.php b/app/Listener/RefundGoodOrderFinishStatementListener.php index d5f6548..4969a1e 100644 --- a/app/Listener/RefundGoodOrderFinishStatementListener.php +++ b/app/Listener/RefundGoodOrderFinishStatementListener.php @@ -9,11 +9,15 @@ use App\Constants\Common\RefundCode; use App\Event\RefundGoodOrderFinishEvent; use App\Lib\Log; use App\Model\ChefStatement; +use App\Model\Cycle; use App\Model\FinancesStatement; use App\Model\Order; use App\Model\OrderGood; use App\Model\RefundOrder; +use App\Model\RefundStatement; use Exception; +use Hyperf\DbConnection\Db; +use Hyperf\Di\Annotation\Inject; use Hyperf\Event\Annotation\Listener; use Psr\Container\ContainerInterface; use Hyperf\Event\Contract\ListenerInterface; @@ -29,7 +33,9 @@ class RefundGoodOrderFinishStatementListener implements ListenerInterface protected OrderGood $orderGoodModel, protected ChefStatement $chefStatementModel, protected FinancesStatement $financesStatementModel, - protected Log $log + protected Log $log, + protected RefundStatement $refundStatementModel, + protected Cycle $cycleModel, ) {} public function listen(): array @@ -54,6 +60,11 @@ class RefundGoodOrderFinishStatementListener implements ListenerInterface */ protected array $orderGoodIds; + /** + * @var bool + */ + protected bool $isRollBackFlag = false; + public function process(object $event): void { try { @@ -66,10 +77,17 @@ class RefundGoodOrderFinishStatementListener implements ListenerInterface $this->orderGoodIds = $this->orderGoodModel->where('order_id',$event->orderId)->pluck('id')->toArray(); if (empty($this->orderGoodIds)) throw new Exception('订单商品信息不存在'); + Db::beginTransaction(); + + $this->isRollBackFlag = true; + $this->rollBackChefData(); $this->rollBackFinancesData(); + + Db::commit(); } catch (Exception $e) { + if ($this->isRollBackFlag) Db::rollBack(); $this->log->error(__CLASS__.':退款完成后生成订单结算数据失败:'.$e->getMessage()); } } @@ -113,12 +131,44 @@ class RefundGoodOrderFinishStatementListener implements ListenerInterface if (empty($refundOrderGoodIds)) throw new Exception('订单商品信息获取失败'); $skuIds = $this->orderGoodModel->whereIn('id',$refundOrderGoodIds)->pluck('quantity','sku_id')->toArray(); + if (empty($skuIds)) throw new Exception('订单商品信息02获取失败'); - //todo first + $updateData = $this->chefStatementModel->whereIn('sku_id',array_keys($skuIds))->get(); + if ($updateData->isEmpty()) return; + $updateData = array_column($updateData->toArray(),null,'sku_id'); + + foreach ($skuIds as $skuId => $quantity) { + if (empty($updateData[$skuId])) throw new Exception('个别数据还未生成,但是有数据生成了,数据异常:'.json_encode($this->refundOrderInfo)); + + $updateInfo = [ + 'sale' => $updateData[$skuId]['sale'] - $quantity, + 'refund' => $updateData[$skuId]['refund'] + $quantity, + ]; + + if (!(new ChefStatement)->where('id',$updateData['sku_id']['id'])->update($updateInfo)) throw new Exception('更新订单数据失败'.json_encode($this->refundOrderInfo)); + } } + /** + * @return void + * @throws Exception + */ private function rollBackFinancesData(): void { + $refundStatementInfo = $this->refundStatementModel->where('cycle_id',$this->orderInfo->cycle_id)->where('kitchen_id',$this->orderInfo->kitchen_id)->first(); + $cycleInfo = $this->cycleModel->find($this->orderInfo->cycle_id); + if (empty($cycleInfo)) throw new Exception('周期信息不存在,数据错误'); + if (empty($refundStatementInfo)) { + $refundStatementInfo = new RefundStatement(); + $refundStatementInfo->date = $cycleInfo->dates; + $refundStatementInfo->cycle_id = $this->orderInfo->cycle_id; + $refundStatementInfo->kitchen_id = $this->orderInfo->kitchen_id; + $refundStatementInfo->refund_price = $this->refundOrderInfo->refund_money; + } else { + $refundStatementInfo->refund_price = bcadd($refundStatementInfo->refund_price,$this->refundOrderInfo->refund_money,2); + } + + if (!$refundStatementInfo->save()) throw new Exception('更新退款数据失败'.json_encode($this->refundOrderInfo)); } } diff --git a/app/Model/RefundStatement.php b/app/Model/RefundStatement.php new file mode 100644 index 0000000..bae11b2 --- /dev/null +++ b/app/Model/RefundStatement.php @@ -0,0 +1,34 @@ + 'integer', 'cycle_id' => 'integer', 'kitchen_id' => 'integer']; +} diff --git a/config/autoload/listeners.php b/config/autoload/listeners.php index ce45341..ce6f132 100644 --- a/config/autoload/listeners.php +++ b/config/autoload/listeners.php @@ -9,8 +9,11 @@ declare(strict_types=1); * @contact group@hyperf.io * @license https://github.com/hyperf/hyperf/blob/master/LICENSE */ + return [ Hyperf\ExceptionHandler\Listener\ErrorExceptionHandler::class, Hyperf\Command\Listener\FailToHandleListener::class, App\Listener\FirstRegistrationListener::class, + App\Listener\RefundGoodOrderFinishStatementListener::class, + App\Listener\RefundGoodOrderFinishRollBackCouponListener::class, ];