From 53ced5ca5b25cb53ceb8b75fe92a2baeb7195b97 Mon Sep 17 00:00:00 2001 From: sky121113 Date: Mon, 11 May 2026 18:04:42 +0800 Subject: [PATCH] =?UTF-8?q?[FEAT]=20=E9=81=A0=E7=AB=AF=E5=87=BA=E8=B2=A8?= =?UTF-8?q?=E6=8C=87=E4=BB=A4=E6=94=AF=E6=8F=B4=201=20=E5=88=86=E9=90=98?= =?UTF-8?q?=E9=80=BE=E6=99=82=E8=A6=86=E8=93=8B=E6=A9=9F=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. 修改 MachineService::dispatchDispense:若同貨道有 pending 指令,超過 1 分鐘未回報則視為逾時,自動將舊指令標記為 timeout 並允許新指令覆蓋下發。 2. 1 分鐘內仍有 pending 指令則維持拒絕,防止短時間重複出貨。 --- app/Services/Machine/MachineService.php | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/app/Services/Machine/MachineService.php b/app/Services/Machine/MachineService.php index fae1270..73b0028 100644 --- a/app/Services/Machine/MachineService.php +++ b/app/Services/Machine/MachineService.php @@ -405,15 +405,26 @@ class MachineService return DB::transaction(function () use ($machine, $slotNo, $userId) { $slot = $machine->slots()->where('slot_no', $slotNo)->lockForUpdate()->firstOrFail(); - // 併行檢查 - $isPending = RemoteCommand::where('machine_id', $machine->id) + // 併行檢查:若有 pending 指令,超過 1 分鐘視為逾時可覆蓋 + $pendingCommand = RemoteCommand::where('machine_id', $machine->id) ->whereIn('command_type', ['reload_stock', 'dispense']) ->where('status', 'pending') ->whereJsonContains('payload->slot_no', (string)$slotNo) - ->exists(); + ->latest() + ->first(); - if ($isPending) { - throw new \Exception(__('This slot has a pending command. Please wait.')); + if ($pendingCommand) { + $isTimedOut = $pendingCommand->created_at->lt(now()->subMinute()); + + if (!$isTimedOut) { + throw new \Exception(__('This slot has a pending command. Please wait.')); + } + + // 超過 1 分鐘:視為逾時,自動標記舊指令 + $pendingCommand->update([ + 'status' => 'timeout', + 'note' => '指令逾時 (1 分鐘未回報),已被新指令覆蓋。', + ]); } if ($slot->stock <= 0) {