|
@@ -0,0 +1,119 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace App\Jobs;
|
|
|
+
|
|
|
+use App\Enums\OrderRecordStatus;
|
|
|
+use App\Enums\OrderStatus;
|
|
|
+use App\Models\CoachUser;
|
|
|
+use App\Models\Order;
|
|
|
+use App\Models\OrderRecord;
|
|
|
+use Illuminate\Bus\Queueable;
|
|
|
+use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
+use Illuminate\Foundation\Bus\Dispatchable;
|
|
|
+use Illuminate\Queue\InteractsWithQueue;
|
|
|
+use Illuminate\Queue\SerializesModels;
|
|
|
+use Illuminate\Support\Facades\DB;
|
|
|
+use Illuminate\Support\Facades\Log;
|
|
|
+
|
|
|
+class AutoFinishOrder implements ShouldQueue
|
|
|
+{
|
|
|
+ use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 任务最大尝试次数
|
|
|
+ *
|
|
|
+ * @var int
|
|
|
+ */
|
|
|
+ public $tries = 3;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 任务运行的超时时间
|
|
|
+ *
|
|
|
+ * @var int
|
|
|
+ */
|
|
|
+ public $timeout = 120;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Create a new job instance.
|
|
|
+ */
|
|
|
+ public function __construct(
|
|
|
+ public Order $order
|
|
|
+ ) {}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Execute the job.
|
|
|
+ */
|
|
|
+ public function handle(): void
|
|
|
+ {
|
|
|
+ try {
|
|
|
+ DB::transaction(function () {
|
|
|
+ // 获取订单信息并加锁
|
|
|
+ $order = Order::query()
|
|
|
+ ->with(['project'])
|
|
|
+ ->where('id', $this->order->id)
|
|
|
+ ->lockForUpdate()
|
|
|
+ ->first();
|
|
|
+
|
|
|
+ // 检查订单是否存在
|
|
|
+ if (! $order) {
|
|
|
+ Log::error('自动结束订单失败 - 订单不存在', [
|
|
|
+ 'order_id' => $this->order->id,
|
|
|
+ ]);
|
|
|
+
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 检查订单状态是否为服务中
|
|
|
+ if ($order->state !== OrderStatus::SERVING->value) {
|
|
|
+ Log::info('自动结束订单跳过 - 订单状态不是服务中', [
|
|
|
+ 'order_id' => $order->id,
|
|
|
+ 'current_state' => $order->state,
|
|
|
+ ]);
|
|
|
+
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 更新订单状态为已完成
|
|
|
+ $order->state = OrderStatus::FINISHED->value;
|
|
|
+ $order->finished_at = now();
|
|
|
+ $order->save();
|
|
|
+
|
|
|
+ // 记录订单状态变更日志
|
|
|
+ OrderRecord::create([
|
|
|
+ 'order_id' => $order->id,
|
|
|
+ 'state' => OrderRecordStatus::FINISHED->value,
|
|
|
+ 'object_id' => $order->coach_id,
|
|
|
+ 'object_type' => CoachUser::class,
|
|
|
+ 'remark' => '系统自动完成服务',
|
|
|
+ ]);
|
|
|
+
|
|
|
+ // TODO: 发送通知给用户和技师
|
|
|
+ // event(new OrderFinishedEvent($order));
|
|
|
+
|
|
|
+ Log::info('自动结束订单成功', [
|
|
|
+ 'order_id' => $order->id,
|
|
|
+ 'finished_at' => $order->finished_at,
|
|
|
+ ]);
|
|
|
+ });
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ Log::error('自动结束订单失败', [
|
|
|
+ 'order_id' => $this->order->id,
|
|
|
+ 'error' => $e->getMessage(),
|
|
|
+ 'trace' => $e->getTraceAsString(),
|
|
|
+ ]);
|
|
|
+ throw $e;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 处理失败的任务
|
|
|
+ */
|
|
|
+ public function failed(\Throwable $exception): void
|
|
|
+ {
|
|
|
+ Log::error('自动结束订单任务执行失败', [
|
|
|
+ 'order_id' => $this->order->id,
|
|
|
+ 'error' => $exception->getMessage(),
|
|
|
+ 'trace' => $exception->getTraceAsString(),
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+}
|