2025-05-27 11:56:04 +08:00
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Jobs;
|
|
|
|
|
|
|
|
|
|
use App\Models\Branch;
|
|
|
|
|
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 ExportSqliteBranchJob 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/tempBranch.sqlite');
|
|
|
|
|
|
|
|
|
|
if (!file_exists(dirname($sqlitePath))) {
|
|
|
|
|
mkdir(dirname($sqlitePath), 0755, true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!file_exists($sqlitePath)) {
|
|
|
|
|
file_put_contents($sqlitePath, '');
|
|
|
|
|
}
|
2025-05-28 09:15:52 +08:00
|
|
|
|
$connectionName = 'tempsqlite_' . md5($sqlitePath . microtime());
|
|
|
|
|
config(["database.connections.{$connectionName}" => [
|
2025-05-27 11:56:04 +08:00
|
|
|
|
'driver' => 'sqlite',
|
|
|
|
|
'database' => $sqlitePath,
|
|
|
|
|
'prefix' => '',
|
|
|
|
|
]]);
|
|
|
|
|
|
2025-05-28 09:15:52 +08:00
|
|
|
|
$exporter = new SqliteExportService($connectionName);
|
2025-05-27 11:56:04 +08:00
|
|
|
|
|
2025-05-28 09:15:52 +08:00
|
|
|
|
$exporter->exportMultiple([
|
2025-05-27 11:56:04 +08:00
|
|
|
|
'branches' => [
|
2025-06-16 22:23:08 +08:00
|
|
|
|
'query' => fn () => Branch::where('id', $this->branchId),
|
2025-05-27 11:56:04 +08:00
|
|
|
|
'tableSchema' => function ($table) {
|
|
|
|
|
$table->id();
|
|
|
|
|
$table->string('name')->comment('店名');
|
|
|
|
|
$table->string('external_ip')->comment('對外IP'); // 原本是 ipAddress,這裡改 string
|
|
|
|
|
$table->tinyInteger('enable')->default(1)->comment('狀態');
|
|
|
|
|
$table->timestamps();
|
|
|
|
|
},
|
|
|
|
|
'transformer' => fn ($branch) => [
|
|
|
|
|
'id' => $branch->id,
|
|
|
|
|
'name' => $branch->name,
|
|
|
|
|
'external_ip' => $branch->external_ip,
|
|
|
|
|
'enable' => $branch->enable ?? 1,
|
|
|
|
|
'created_at' => $branch->created_at,
|
|
|
|
|
'updated_at' => $branch->updated_at,
|
|
|
|
|
],
|
|
|
|
|
]
|
|
|
|
|
]);
|
|
|
|
|
SendSqliteFileJob::dispatch($sqlitePath, $this->branchId);
|
|
|
|
|
}
|
|
|
|
|
}
|