star-cloud/app/Jobs/Product/SyncProductsToMachinesJob.php
sky121113 9ff9abef89 [FEAT] 實作「同步到所有機台」功能與商品資料同步機制
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 的系統指令規格中。
2026-05-14 09:33:43 +08:00

53 lines
1.4 KiB
PHP

<?php
namespace App\Jobs\Product;
use App\Models\System\Company;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
class SyncProductsToMachinesJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* Create a new job instance.
*
* @param int $companyId
* @param string|null $remark
* @param int|null $userId
*/
public function __construct(
protected int $companyId,
protected ?string $remark = null,
protected ?int $userId = null
) {}
/**
* Execute the job.
*
* @return void
*/
public function handle(): void
{
$company = Company::with('machines')->find($this->companyId);
if (!$company || $company->machines->isEmpty()) {
return;
}
$delayMs = 100; // 100ms per machine
foreach ($company->machines as $index => $machine) {
// Calculate delay to avoid thundering herd (100ms, 200ms, 300ms...)
$currentDelay = ($index * $delayMs) / 1000;
SendProductSyncCommandJob::dispatch($machine->id, $this->remark, $this->userId)
->delay(now()->addSeconds($currentDelay));
}
}
}