From e53be8ff1204ab1f23e048ce30cf934baa390690 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=9D=99?= Date: Mon, 20 Dec 2021 16:31:22 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=8F=96=E6=B6=88=E8=AE=A2?= =?UTF-8?q?=E5=8D=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/Order/OrderController.php | 18 +------------ app/Services/OrderService.php | 27 ++++++++++++++++++- 2 files changed, 27 insertions(+), 18 deletions(-) diff --git a/app/Endpoint/Api/Http/Controllers/Order/OrderController.php b/app/Endpoint/Api/Http/Controllers/Order/OrderController.php index 2cde7480..545b8710 100644 --- a/app/Endpoint/Api/Http/Controllers/Order/OrderController.php +++ b/app/Endpoint/Api/Http/Controllers/Order/OrderController.php @@ -7,10 +7,8 @@ use App\Endpoint\Api\Http\Resources\OrderPackageResource; use App\Endpoint\Api\Http\Resources\OrderResource; use App\Endpoint\Api\Http\Resources\OrderResourceCollection; use App\Exceptions\BizException; -use App\Helpers\Order as OrderHelper; use App\Helpers\Paginator as PaginatorHelper; use App\Models\KuaidiLog; -use App\Models\Order; use App\Services\OrderService; use Illuminate\Database\Eloquent\ModelNotFoundException; use Illuminate\Http\Request; @@ -167,21 +165,7 @@ class OrderController extends Controller return DB::transaction(function () use ($id, $user) { $order = $user->orders()->lockForUpdate()->findOrFail($id); - if (! $order->isPending() && ! $order->isWaitShipping()) { - throw new BizException('订单状态不是待付款或待发货'); - } - - if ($order->isWaitShipping()) { - $order->refundTasks()->create([ - 'sn' => OrderHelper::serialNumber(), - 'amount' => $order->total_amount, - 'reason' => '取消订单', - ]); - } - - $order->update([ - 'status' => Order::STATUS_CANCELLED, - ]); + (new OrderService())->cancel($order); }); } catch (ModelNotFoundException | BizException $e) { throw $e; diff --git a/app/Services/OrderService.php b/app/Services/OrderService.php index 272bc433..744133b1 100644 --- a/app/Services/OrderService.php +++ b/app/Services/OrderService.php @@ -639,7 +639,7 @@ class OrderService } /** - * 订单确认收货 + * 确认订单 * * @param \App\Models\Order $order * @return void @@ -667,4 +667,29 @@ class OrderService 'completed_at' => $time, ]); } + + /** + * 取消订单 + * + * @param \App\Models\Order $order + * @return void + */ + public function cancel(Order $order) + { + if (! $order->isPending() && ! $order->isWaitShipping()) { + throw new BizException('订单状态不是待付款或待发货'); + } + + if ($order->isWaitShipping()) { + $order->refundTasks()->create([ + 'sn' => OrderHelper::serialNumber(), + 'amount' => $order->total_amount, + 'reason' => '取消订单', + ]); + } + + $order->update([ + 'status' => Order::STATUS_CANCELLED, + ]); + } }