同步 分店資訊 20250527

This commit is contained in:
allen.yan 2025-05-27 11:56:04 +08:00
parent 4f37c90a0b
commit 049118ad68
7 changed files with 164 additions and 10 deletions

View File

@ -0,0 +1,104 @@
<?php
namespace App\Jobs;
use App\Models\Branch;
use App\Models\Room;
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, '');
}
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);
}
}

View File

@ -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);

View File

@ -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'))

View File

@ -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) {

View File

@ -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',

View File

@ -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(),
]);
}
}

View File

@ -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 # 同步匯出所有
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