From 049118ad68d1c88f635157bccfe37b51bdfff338 Mon Sep 17 00:00:00 2001 From: "allen.yan" Date: Tue, 27 May 2025 11:56:04 +0800 Subject: [PATCH] =?UTF-8?q?=E5=90=8C=E6=AD=A5=20=E5=88=86=E5=BA=97?= =?UTF-8?q?=E8=B3=87=E8=A8=8A=2020250527?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Jobs/ExportSqliteBranchJob.php | 104 ++++++++++++++++++ app/Jobs/SendSqliteFileJob.php | 6 +- app/Livewire/Admin/BranchTable.php | 16 +++ .../0001_01_01_000002_create_jobs_table.php | 10 +- database/seeders/CreateAdminUserSeeder.php | 4 + database/seeders/FavoriteSongsSeeder.php | 20 +++- 開發手冊.ini | 14 ++- 7 files changed, 164 insertions(+), 10 deletions(-) create mode 100644 app/Jobs/ExportSqliteBranchJob.php diff --git a/app/Jobs/ExportSqliteBranchJob.php b/app/Jobs/ExportSqliteBranchJob.php new file mode 100644 index 0000000..00d2bd5 --- /dev/null +++ b/app/Jobs/ExportSqliteBranchJob.php @@ -0,0 +1,104 @@ +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, ''); + } + + config(['database.connections.tempsqlite' => [ + 'driver' => 'sqlite', + 'database' => $sqlitePath, + 'prefix' => '', + ]]); + + $exportService = new SqliteExportService(); + + $exportService->exportMultiple([ + 'branches' => [ + 'modelClass' => Branch::class, + 'query' => fn () => Branch::where('id', $this->branchId)->get(), + '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, + ], + ], + 'rooms' => [ + 'modelClass' => Room::class, + 'query' => fn () => Room::where('branch_id', $this->branchId)->get(), + 'tableSchema' => function ($table) { + $table->id(); + $table->unsignedBigInteger('branch_id')->comment('關聯分店'); + $table->unsignedTinyInteger('floor')->default(1)->comment('樓層'); + $table->string('type')->default('unset')->comment('包廂類別'); // enum 改成 string + $table->string('name')->comment('包廂名稱'); + $table->string('internal_ip')->nullable()->comment('內部 IP'); + $table->unsignedSmallInteger('port')->nullable()->comment('通訊 Port'); + $table->tinyInteger('is_online')->default(0)->comment('連線狀態'); + $table->string('status')->default('error')->comment('狀態'); // enum 改成 string + $table->dateTime('started_at')->nullable()->comment('開始時間'); + $table->dateTime('ended_at')->nullable()->comment('結束時間'); + $table->timestamps(); + }, + 'transformer' => fn ($room) => [ + 'id' => $room->id, + 'branch_id' => $room->branch_id, + 'floor' => $room->floor, + 'type' => $room->type, + 'name' => $room->name, + 'internal_ip' => $room->internal_ip, + 'port' => $room->port, + 'is_online' => $room->is_online, + 'status' => $room->status, + 'started_at' => $room->started_at, + 'ended_at' => $room->ended_at, + 'created_at' => $room->created_at, + 'updated_at' => $room->updated_at, + ], + ] + ]); + + SendSqliteFileJob::dispatch($sqlitePath, $this->branchId); + } +} diff --git a/app/Jobs/SendSqliteFileJob.php b/app/Jobs/SendSqliteFileJob.php index fb085de..8bf6db1 100644 --- a/app/Jobs/SendSqliteFileJob.php +++ b/app/Jobs/SendSqliteFileJob.php @@ -26,7 +26,7 @@ class SendSqliteFileJob implements ShouldQueue public function handle(): void { - $path = storage_path($this->filename); + $path = $this->filename; if (!file_exists($path)) { Log::error("❌ SQLite 檔案不存在: {$path}"); @@ -36,8 +36,8 @@ class SendSqliteFileJob implements ShouldQueue $token = $user->api_plain_token; $branches = $this->branchId - ? Branch::where('id', $this->branchId)->where('enabled', true)->get() - : Branch::where('enabled', true)->cursor(); + ? Branch::where('id', $this->branchId)->get() + : Branch::where('enable', true)->cursor(); foreach ($branches as $branch) { $client = new ApiClient($token, $branch->external_ip); diff --git a/app/Livewire/Admin/BranchTable.php b/app/Livewire/Admin/BranchTable.php index a5d2a0a..042358a 100644 --- a/app/Livewire/Admin/BranchTable.php +++ b/app/Livewire/Admin/BranchTable.php @@ -3,6 +3,7 @@ namespace App\Livewire\Admin; use App\Models\Branch; +use App\Jobs\ExportSqliteBranchJob; use Illuminate\Support\Carbon; use Illuminate\Support\Collection; use Illuminate\Support\Facades\Auth; @@ -125,6 +126,16 @@ final class BranchTable extends PowerGridComponent $column[]=Column::action(__('branches.actions')); return $column; } + #[On('synchronous.{tableName}')] + public function synchronous($branch_id): void + { + ExportSqliteBranchJob::dispatch($branch_id); + $this->notification()->send([ + 'icon' => 'success', + 'title' => '分店:'.$branch_id, + 'description' => '已經加入排程', + ]); + } #[On('bulkDelete.{tableName}')] public function bulkDelete(): void { @@ -188,6 +199,11 @@ final class BranchTable extends PowerGridComponent ->icon('solid-cog') ->class('inline-flex items-center gap-1 px-3 py-1 rounded bg-amber-200 text-black') ->dispatchTo('admin.room-grid', 'openModal', ['branch_id' => $row->id]); + $actions[] = Button::add('room-synchronous') + ->slot('包廂同步') + ->icon('solid-cog') + ->class('inline-flex items-center gap-1 px-3 py-1 rounded bg-amber-200 text-black') + ->dispatch('synchronous.' . $this->tableName, ['branch_id' => $row->id]); if ($this->canEdit) { $actions[] =Button::add('edit') ->slot(__('branches.edit')) diff --git a/database/migrations/0001_01_01_000002_create_jobs_table.php b/database/migrations/0001_01_01_000002_create_jobs_table.php index bdd0253..425e705 100644 --- a/database/migrations/0001_01_01_000002_create_jobs_table.php +++ b/database/migrations/0001_01_01_000002_create_jobs_table.php @@ -16,8 +16,8 @@ return new class extends Migration $table->string('queue')->index(); $table->longText('payload'); $table->unsignedTinyInteger('attempts'); - $table->timestamp('reserved_at')->nullable(); - $table->timestamp('available_at'); + $table->unsignedInteger('reserved_at')->nullable(); + $table->unsignedInteger('available_at'); $table->unsignedInteger('created_at'); }); @@ -29,9 +29,9 @@ return new class extends Migration $table->integer('failed_jobs'); $table->longText('failed_job_ids'); $table->mediumText('options')->nullable(); - $table->timestamp('cancelled_at')->nullable(); - $table->timestamp('created_at'); - $table->timestamp('finished_at')->nullable(); + $table->integer('cancelled_at')->nullable(); + $table->integer('created_at'); + $table->integer('finished_at')->nullable(); }); Schema::create('failed_jobs', function (Blueprint $table) { diff --git a/database/seeders/CreateAdminUserSeeder.php b/database/seeders/CreateAdminUserSeeder.php index 471bb0d..f406bf3 100644 --- a/database/seeders/CreateAdminUserSeeder.php +++ b/database/seeders/CreateAdminUserSeeder.php @@ -21,6 +21,10 @@ class CreateAdminUserSeeder extends Seeder 'password' => bcrypt('aa1234') ]); $user->assignRole('Admin'); + $abilities = $user->getAllPermissions()->pluck('name')->toArray(); + $token = $user->createToken('all', $abilities)->plainTextToken; + $user->api_plain_token = $token; + $user->save(); $user = User::create([ 'name' => 'Allen Yan(machine)', 'email' => 'MachineKTV@gmail.com', diff --git a/database/seeders/FavoriteSongsSeeder.php b/database/seeders/FavoriteSongsSeeder.php index e370bce..ce0baea 100644 --- a/database/seeders/FavoriteSongsSeeder.php +++ b/database/seeders/FavoriteSongsSeeder.php @@ -14,10 +14,28 @@ class FavoriteSongsSeeder extends Seeder public function run(): void { DB::table('FavoriteSongs')->insert([ - 'songNumber' => 999996, + 'songNumber' => "999996", 'userPhone' => '0912345678', 'created_at' => Carbon::now(), 'updated_at' => Carbon::now(), ]); + DB::table('FavoriteSongs')->insert([ + 'songNumber' => '0909123456的歌單', + 'userPhone' => '0909123456', + 'created_at' => Carbon::now(), + 'updated_at' => Carbon::now(), + ]); + DB::table('FavoriteSongs')->insert([ + 'songNumber' => '70011', + 'userPhone' => '0909123456', + 'created_at' => Carbon::now(), + 'updated_at' => Carbon::now(), + ]); + DB::table('FavoriteSongs')->insert([ + 'songNumber' => '0987654321的歌單', + 'userPhone' => '0987654321', + 'created_at' => Carbon::now(), + 'updated_at' => Carbon::now(), + ]); } } \ No newline at end of file diff --git a/開發手冊.ini b/開發手冊.ini index 6737491..4be6a6d 100644 --- a/開發手冊.ini +++ b/開發手冊.ini @@ -134,4 +134,16 @@ php artisan export:sqlite user --sync # 同步匯出歌曲 php artisan export:sqlite song # 同步匯出歌曲 php artisan export:sqlite user # 非同步匯出使用者 php artisan export:sqlite all # 非同步匯出所有 -php artisan export:sqlite all --sync # 同步匯出所有 \ No newline at end of file +php artisan export:sqlite all --sync # 同步匯出所有 + + + +名稱 +測試 +IP +125.229.176.129 + +1F;svr01,svr02 +1F;pc101,pc102,pc103,pc104,pc105,pc106,pc108 +2F;pc201,pc202,pc203,pc205 +9F;pc901,pc902,pc903 \ No newline at end of file