2025-05-28 09:24:03 +08:00
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Observers;
|
|
|
|
|
|
|
|
|
|
use Illuminate\Support\Facades\Auth;
|
2025-08-20 15:26:05 +08:00
|
|
|
|
use Illuminate\Support\Arr;
|
2025-05-28 09:24:03 +08:00
|
|
|
|
|
|
|
|
|
use App\Models\Room;
|
|
|
|
|
use App\Models\RoomStatusLog;
|
|
|
|
|
|
|
|
|
|
class RoomObserver
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* Handle the Room "created" event.
|
|
|
|
|
*/
|
|
|
|
|
public function created(Room $room): void
|
|
|
|
|
{
|
2025-08-20 15:26:05 +08:00
|
|
|
|
// 建立初始 log
|
|
|
|
|
$this->createStatusLog($room);
|
2025-05-28 09:24:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Handle the Room "updated" event.
|
|
|
|
|
*/
|
|
|
|
|
public function updated(Room $room): void
|
|
|
|
|
{
|
|
|
|
|
// 檢查是否有變更狀態
|
2025-08-22 09:54:26 +08:00
|
|
|
|
if ($room->wasChanged(array_diff(array_keys($room->getChanges()), ['updated_at']))) {
|
2025-08-20 15:26:05 +08:00
|
|
|
|
$this->createStatusLog($room);
|
|
|
|
|
}
|
|
|
|
|
if ($room->isDirty('status')) {
|
|
|
|
|
$now = now();
|
|
|
|
|
|
|
|
|
|
// 找到最後一筆未結束 session
|
|
|
|
|
$lastSession = $room->sessions()->whereNull('ended_at')->latest('started_at')->first();
|
|
|
|
|
|
|
|
|
|
if ($lastSession) {
|
|
|
|
|
// 結束上一筆 session
|
|
|
|
|
$lastSession->update([
|
|
|
|
|
'status' => $room->status->value,
|
|
|
|
|
'ended_at' => $now,
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 如果狀態是 active 或 maintain,開新 session
|
|
|
|
|
if (in_array($room->status->value, ['active', 'maintain'])) {
|
|
|
|
|
$mode = $room->status->value === 'active' ? 'normal' : 'test';
|
|
|
|
|
$room->sessions()->create([
|
|
|
|
|
'mode' => $mode,
|
|
|
|
|
'status' => $room->status->value,
|
|
|
|
|
'started_at' => $now,
|
|
|
|
|
]);
|
|
|
|
|
}
|
2025-05-28 09:24:03 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Handle the Room "deleted" event.
|
|
|
|
|
*/
|
|
|
|
|
public function deleted(Room $room): void
|
|
|
|
|
{
|
2025-08-20 15:26:05 +08:00
|
|
|
|
$message = sprintf(
|
|
|
|
|
"%s:%s%s (%s:%s) 已刪除",
|
|
|
|
|
$room->branch->name,
|
|
|
|
|
$room->type->value,
|
|
|
|
|
$room->name,
|
|
|
|
|
$room->internal_ip,
|
|
|
|
|
$room->port
|
|
|
|
|
);
|
|
|
|
|
$this->createStatusLog($room,$message);
|
2025-05-28 09:24:03 +08:00
|
|
|
|
}
|
2025-08-20 15:26:05 +08:00
|
|
|
|
|
2025-05-28 09:24:03 +08:00
|
|
|
|
/**
|
2025-08-20 15:26:05 +08:00
|
|
|
|
* 建立 RoomStatusLog
|
2025-05-28 09:24:03 +08:00
|
|
|
|
*/
|
2025-08-20 15:26:05 +08:00
|
|
|
|
private function createStatusLog(Room $room,$log_message =null): void
|
2025-05-28 09:24:03 +08:00
|
|
|
|
{
|
2025-08-20 15:26:05 +08:00
|
|
|
|
$message=($log_message !=null)?$log_message:$room->log_message ?? '';
|
|
|
|
|
RoomStatusLog::create([
|
|
|
|
|
'branch_id' => $room->branch->id,
|
|
|
|
|
'room_id' => $room->id,
|
|
|
|
|
'user_id' => Auth::id() ?? 0,
|
|
|
|
|
'is_online' => $room->is_online,
|
|
|
|
|
'status' => $room->status,
|
|
|
|
|
'started_at' => $room->started_at,
|
|
|
|
|
'ended_at' => $room->ended_at,
|
|
|
|
|
'message' => $message,
|
|
|
|
|
'source' => $this->getSource($room),
|
|
|
|
|
]);
|
2025-05-28 09:24:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
2025-08-20 15:26:05 +08:00
|
|
|
|
private function getSource(Room $room): string
|
2025-05-28 09:24:03 +08:00
|
|
|
|
{
|
2025-08-20 15:26:05 +08:00
|
|
|
|
return app()->runningInConsole() ? 'system' : ($room->log_source ?? 'manual');
|
2025-05-28 09:24:03 +08:00
|
|
|
|
}
|
|
|
|
|
}
|