KTVCentral/app/Console/Commands/ClearMachineStatuses.php
allen.yan 7c8c3fe69b DB 滙入
User 介面 只能看資料不能做修改
Role 介面移除
Branch介面 只能看資料不能做修改
Room 介面 可操控 包廂開關台
Sqgger API 可操控API
Room 有操控異動記錄
machine_statuses 需做資料留存需留7 天
20250528
2025-05-28 09:24:03 +08:00

47 lines
1.2 KiB
PHP

<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Carbon\Carbon;
class ClearMachineStatuses extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'app:clear-machine-statuses';
/**
* The console command description.
*
* @var string
*/
protected $description = '備份並清空 machine_statuses 表,每週保留一次';
/**
* Execute the console command.
*/
public function handle()
{
$day = now()->format('l'); // e.g. "Monday"
$targetTable = "machine_statuses_" . $day;
DB::statement("CREATE TABLE IF NOT EXISTS _machine_statuses LIKE machine_statuses");
// 先刪除舊表(如存在)
DB::statement("DROP TABLE IF EXISTS {$targetTable}");
// 改名備份
DB::statement("RENAME TABLE machine_statuses TO {$targetTable}");
// 空表回命名
DB::statement("RENAME TABLE _machine_statuses TO machine_statuses");
$this->info("Machine statuses backed up to {$targetTable} and table cleared.");
}
}