1. 在商品管理介面新增「同步到所有機台」按鈕與 Alpine.js 確認彈窗。 2. 實作後端 syncToAllMachines 控制器邏輯,並加入同一公司 1 分鐘內禁止重複發送的限制。 3. 建立 SyncProductsToMachinesJob 與 SendProductSyncCommandJob 非同步隊列任務,支援舊指令覆蓋機制。 4. 補全繁體中文、英文與日文的多語系翻譯字串。 5. 更新遠端指令日誌視圖,支援展示商品同步指令及其備註。 6. 更新 MQTT 通訊規範與 API 技術規格文檔,納入 update_products 指令。 7. 整合 update_ads 與 update_products 指令至 config/api-docs.php 的系統指令規格中。
77 lines
2.2 KiB
PHP
77 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace App\Jobs\Product;
|
|
|
|
use App\Models\Machine\Machine;
|
|
use App\Models\Machine\RemoteCommand;
|
|
use App\Services\Machine\MqttService;
|
|
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\Log;
|
|
|
|
class SendProductSyncCommandJob implements ShouldQueue
|
|
{
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
/**
|
|
* Create a new job instance.
|
|
*
|
|
* @param int $machineId
|
|
* @param string|null $remark
|
|
* @param int|null $userId
|
|
*/
|
|
public function __construct(
|
|
protected int $machineId,
|
|
protected ?string $remark = null,
|
|
protected ?int $userId = null
|
|
) {}
|
|
|
|
/**
|
|
* Execute the job.
|
|
*
|
|
* @param MqttService $mqttService
|
|
* @return void
|
|
*/
|
|
public function handle(MqttService $mqttService): void
|
|
{
|
|
$machine = Machine::find($this->machineId);
|
|
|
|
if (!$machine) {
|
|
Log::error("SendProductSyncCommandJob: Machine not found", ['id' => $this->machineId]);
|
|
return;
|
|
}
|
|
|
|
// 覆蓋任何該機台尚在「待處理」狀態的商品同步指令
|
|
RemoteCommand::where('machine_id', $machine->id)
|
|
->where('command_type', 'update_products')
|
|
->where('status', 'pending')
|
|
->update([
|
|
'status' => 'superseded',
|
|
'note' => 'Superseded by new command'
|
|
]);
|
|
|
|
$command = RemoteCommand::create([
|
|
'machine_id' => $machine->id,
|
|
'user_id' => $this->userId,
|
|
'command_type' => 'update_products',
|
|
'payload' => [], // No extra payload needed for B012 trigger
|
|
'status' => 'pending',
|
|
'remark' => $this->remark ?: __('Batch Product Sync'),
|
|
]);
|
|
|
|
$success = $mqttService->pushCommand(
|
|
$machine->serial_no,
|
|
'update_products',
|
|
[],
|
|
(string) $command->id
|
|
);
|
|
|
|
if (!$success) {
|
|
$command->update(['status' => 'failed', 'note' => 'MQTT push failed']);
|
|
}
|
|
}
|
|
}
|