加入 Branch,Room,RoomStausLog table 20250506

This commit is contained in:
allen.yan 2025-05-06 18:11:45 +08:00
parent e748b7ff3d
commit 0969a07a3e
13 changed files with 296 additions and 1 deletions

25
app/Enums/RoomStatus.php Normal file
View File

@ -0,0 +1,25 @@
<?php
namespace App\Enums;
use App\Enums\Traits\HasLabels;
enum RoomStatus: string {
use HasLabels;
case Active = 'active';
case Closed = 'closed';
case Error = 'error';
case Maintenance = 'maintenance';
// 返回對應的顯示文字
public function labels(): string
{
return match($this) {
self::Active => __('enums.room.status.Active'),
self::Closed => __('enums.room.status.Closed'),
self::Error => __('enums.room.status.Error'),
self::Maintenance => __('enums.room.status.Maintenance'),
};
}
}

BIN
app/Models/.DS_Store vendored Normal file

Binary file not shown.

26
app/Models/Branch.php Normal file
View File

@ -0,0 +1,26 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Branch extends Model
{
/** @use HasFactory<\Database\Factories\ArtistFactory> */
use HasFactory;
protected $fillable = [
'name',
'external_ip',
];
public function rooms() {
return $this->hasMany(Room::class);
}
public function songs(){
return $this->belongsToMany(Song::class)
->withPivot('counts')
->withTimestamps();
}
}

25
app/Models/Room.php Normal file
View File

@ -0,0 +1,25 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Room extends Model
{
/** @use HasFactory<\Database\Factories\ArtistFactory> */
use HasFactory;
protected $casts = [
'started_at' => 'datetime',
'ended_at' => 'datetime',
];
public function branch() {
return $this->belongsTo(Branch::class);
}
public function statusLogs() {
return $this->hasMany(RoomStatusLog::class);
}
}

View File

@ -0,0 +1,31 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class RoomStatusLog extends Model
{
/** @use HasFactory<\Database\Factories\ArtistFactory> */
use HasFactory;
public $timestamps = false;
protected $fillable =
[
'room_id',
'user_id',
'status',
'message',
'logged_at'
];
protected $casts = [
'logged_at' => 'datetime',
];
public function room() {
return $this->belongsTo(Room::class);
}
}

View File

@ -60,7 +60,11 @@ class Song extends Model
public function categories(){
return $this->belongsToMany(SongCategory::class);
}
public function branches(){
return $this->belongsToMany(Branch::class)
->withPivot('counts')
->withTimestamps();
}
protected static function booted()
{
// 無論是 creating 或 updating都執行這段共用的邏輯

View File

@ -0,0 +1,57 @@
<?php
namespace App\Observers;
use App\Models\Room;
class RoomObserver
{
/**
* Handle the Room "created" event.
*/
public function created(Room $room): void
{
//
}
/**
* Handle the Room "updated" event.
*/
public function updated(Room $room): void
{
// 檢查是否有變更狀態
if ($room->wasChanged('status')) {
RoomStatusLog::create([
'room_id' => $room->id,
'user_id' => Auth::id(), // 若是 console 或系統自動操作可能為 null
'status' => $room->status,
'message' => '狀態自動變更紀錄',
'logged_at' => now(),
]);
}
}
/**
* Handle the Room "deleted" event.
*/
public function deleted(Room $room): void
{
//
}
/**
* Handle the Room "restored" event.
*/
public function restored(Room $room): void
{
//
}
/**
* Handle the Room "force deleted" event.
*/
public function forceDeleted(Room $room): void
{
//
}
}

BIN
database/.DS_Store vendored Normal file

Binary file not shown.

BIN
database/migrations/.DS_Store vendored Normal file

Binary file not shown.

View File

@ -0,0 +1,29 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('branches', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->ipAddress('external_ip'); // 對外 IP
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('branches');
}
};

View File

@ -0,0 +1,34 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('rooms', function (Blueprint $table) {
$table->id();
$table->foreignId('branch_id')->constrained()->onDelete('cascade'); // 關聯分店
$table->string('name'); // 包廂名稱
$table->string('internal_ip'); // 內部 IP
$table->unsignedSmallInteger('port'); // 通訊 Port
$table->enum('status', ['active', 'closed', 'error', 'maintenance']); // 狀態:啟用中 / 已結束
$table->dateTime('started_at'); // 開始時間
$table->dateTime('ended_at')->nullable(); // 結束時間
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('rooms');
}
};

View File

@ -0,0 +1,31 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('room_status_logs', function (Blueprint $table) {
$table->id();
$table->foreignId('room_id')->constrained()->onDelete('cascade');
$table->foreignId('user_id')->nullable()->constrained()->onDelete('set null'); // 操作者,可為 null系統
$table->enum('status', ['active', 'closed', 'error', 'maintenance']);
$table->text('message')->nullable(); // 可填異常原因或操作說明
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('room_status_logs');
}
};

View File

@ -0,0 +1,33 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('branch_song', function (Blueprint $table) {
$table->unsignedBigInteger('song_id');
$table->unsignedBigInteger('branch_id');
$table->integer('counts')->default(0)->comment('點播次數');
$table->foreign('song_id')->references('id')->on('songs')->restrictOnDelete()->restrictOnUpdate();
$table->foreign('branch_id')->references('id')->on('branches')->restrictOnDelete()->restrictOnUpdate();
$table->primary(['song_id', 'branch_id']);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('branch_song');
}
};