KTV/app/Jobs/ExportSqliteBroadcastJob.php

68 lines
2.2 KiB
PHP
Raw Normal View History

2025-08-13 14:44:52 +08:00
<?php
namespace App\Jobs;
use App\Models\BroadcastTemplate;
use App\Services\SqliteExportService;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
class ExportSqliteBroadcastJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public $timeout = 600;
protected ?int $branchId;
public function __construct(?int $branchId = null)
{
$this->branchId = $branchId;
}
public function handle()
{
$sqlitePath = storage_path('app/database/tempBroadcastTemplate.sqlite');
if (!file_exists(dirname($sqlitePath))) {
mkdir(dirname($sqlitePath), 0755, true);
}
if (!file_exists($sqlitePath)) {
file_put_contents($sqlitePath, '');
}
$connectionName = 'tempsqlite_' . md5($sqlitePath . microtime());
config(["database.connections.{$connectionName}" => [
'driver' => 'sqlite',
'database' => $sqlitePath,
'prefix' => '',
]]);
$exporter = new SqliteExportService($connectionName);
$exporter->exportMultiple([
'broadcast_templates' => [
'query' => fn () => DB::table('broadcast_templates'),
'tableSchema' => function (Blueprint $table) {
$table->id();
$table->string('content')->comment('廣告公告');
$table->enum('color', ['white', 'red', 'green','blue'])->default('white')->comment('顯示顏色');
$table->boolean('is_active')->default(true); // 啟用狀態
$table->timestamps();
},
'transformer' => fn ($row) => [
'content' => $row->content,
'color' => $row->color,
'is_active' => $row->is_active,
'created_at' => $row->created_at,
'updated_at' => $row->updated_at,
],
],
]);
SendSqliteFileJob::dispatch($sqlitePath, $this->branchId);
}
}