202508201150
包廂狀態加入 RoomSession 表 點播歌曲加入到DB
This commit is contained in:
parent
9e61020ca4
commit
1bb201bacb
35
app/Enums/OrderedSongStatus.php
Normal file
35
app/Enums/OrderedSongStatus.php
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Enums;
|
||||||
|
|
||||||
|
use App\Enums\Traits\HasLabels;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @OA\Schema(
|
||||||
|
* schema="OrderedSongStatus",
|
||||||
|
* type="string",
|
||||||
|
* enum={"NotPlayed", "Playing", "Played", "NoFile", "Skipped", "InsertPlayback"},
|
||||||
|
* example="NotPlayed"
|
||||||
|
* )
|
||||||
|
*/
|
||||||
|
enum OrderedSongStatus: string
|
||||||
|
{
|
||||||
|
case NotPlayed = 'NotPlayed';
|
||||||
|
case Playing = 'Playing';
|
||||||
|
case Played = 'Played';
|
||||||
|
case NoFile = 'NoFile';
|
||||||
|
case Skipped = 'Skipped';
|
||||||
|
case InsertPlayback = 'InsertPlayback';
|
||||||
|
|
||||||
|
public function labels(): string
|
||||||
|
{
|
||||||
|
return match($this) {
|
||||||
|
self::NotPlayed => __('enums.NotPlayed'),
|
||||||
|
self::Playing => __('enums.Playing'),
|
||||||
|
self::Played => __('enums.Played'),
|
||||||
|
self::NoFile => __('enums.NoFile'),
|
||||||
|
self::Skipped => __('enums.Skipped'),
|
||||||
|
self::InsertPlayback => __('enums.InsertPlayback'),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
301
app/Http/Controllers/Api/RoomSongController.php
Normal file
301
app/Http/Controllers/Api/RoomSongController.php
Normal file
@ -0,0 +1,301 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Controllers\Api;
|
||||||
|
|
||||||
|
use App\Http\Controllers\Controller;
|
||||||
|
use App\Http\Requests\OrderSongRequest;
|
||||||
|
use App\Http\Requests\RoomSongRequest;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use App\Models\Room;
|
||||||
|
use App\Models\Song;
|
||||||
|
use App\Models\OrderedSong;
|
||||||
|
use App\Models\RoomSession;
|
||||||
|
use Illuminate\Support\Facades\Auth;
|
||||||
|
use Illuminate\Support\Carbon;
|
||||||
|
use App\Http\Responses\ApiResponse;
|
||||||
|
|
||||||
|
class RoomSongController extends Controller
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @OA\Post(
|
||||||
|
* path="/api/room/order-song",
|
||||||
|
* summary="點歌",
|
||||||
|
* description="在指定包廂點一首歌曲",
|
||||||
|
* tags={"Room Control Song"},
|
||||||
|
* security={{"Authorization":{}}},
|
||||||
|
* @OA\RequestBody(
|
||||||
|
* required=true,
|
||||||
|
* @OA\JsonContent(ref="#/components/schemas/OrderSongRequest")
|
||||||
|
* ),
|
||||||
|
* @OA\Response(
|
||||||
|
* response=200,
|
||||||
|
* description="成功",
|
||||||
|
* @OA\JsonContent(
|
||||||
|
* type="object",
|
||||||
|
* @OA\Property(property="success", type="boolean", example=true),
|
||||||
|
* @OA\Property(
|
||||||
|
* property="ordered_song",
|
||||||
|
* ref="#/components/schemas/OrderedSong"
|
||||||
|
* ),
|
||||||
|
* @OA\Property(property="next_song_name", type="string", example="XXXSSSS")
|
||||||
|
* )
|
||||||
|
* ),
|
||||||
|
* @OA\Response(
|
||||||
|
* response=422,
|
||||||
|
* description="驗證失敗",
|
||||||
|
* @OA\JsonContent(
|
||||||
|
* type="object",
|
||||||
|
* @OA\Property(property="message", type="string", example="The given data was invalid."),
|
||||||
|
* @OA\Property(property="errors", type="object")
|
||||||
|
* )
|
||||||
|
* )
|
||||||
|
* )
|
||||||
|
*/
|
||||||
|
public function orderSong(OrderSongRequest $request)
|
||||||
|
{
|
||||||
|
// 取得對應的 RoomSession(透過 api_token)
|
||||||
|
$roomSession = $this->getRoomSession($request->api_token) ;
|
||||||
|
|
||||||
|
// 取得歌曲名稱
|
||||||
|
$song = Song::findOrFail($request->song_id);
|
||||||
|
|
||||||
|
// 建立 OrderedSong
|
||||||
|
$orderedSong = OrderedSong::create([
|
||||||
|
'room_session_id' => $roomSession->id,
|
||||||
|
'from_by' => $request->from_by,
|
||||||
|
'status' => $request->status,
|
||||||
|
'song_id' => $request->song_id,
|
||||||
|
'song_name' => $song->name,
|
||||||
|
'artist_name' => $song->str_artists_plus(),
|
||||||
|
'ordered_at' => now(),
|
||||||
|
]);
|
||||||
|
|
||||||
|
// 檢查這首歌在此 session 是否第一次點
|
||||||
|
$countInSession = OrderedSong::where('room_session_id', $roomSession->id)
|
||||||
|
->where('song_id', $request->song_id)
|
||||||
|
->count();
|
||||||
|
|
||||||
|
if ($countInSession === 1) { // 第一次點才加
|
||||||
|
$song->increment('song_counts');
|
||||||
|
}
|
||||||
|
|
||||||
|
return ApiResponse::success([
|
||||||
|
'ordered_song' => $orderedSong,
|
||||||
|
'next_song_name' => $this->nextSongName($roomSession),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* @OA\Get(
|
||||||
|
* path="/api/room/ordered-songs",
|
||||||
|
* summary="取得包廂點歌列表",
|
||||||
|
* description="取得指定包廂的已播、正在播放、待播/插播歌曲列表",
|
||||||
|
* tags={"Room Control Song"},
|
||||||
|
* security={{"Authorization":{}}},
|
||||||
|
* @OA\Parameter(ref="#/components/parameters/ApiTokenQuery"),
|
||||||
|
* @OA\Response(
|
||||||
|
* response=200,
|
||||||
|
* description="成功取得點歌列表",
|
||||||
|
* @OA\JsonContent(
|
||||||
|
* type="object",
|
||||||
|
* @OA\Property(
|
||||||
|
* property="played",
|
||||||
|
* type="array",
|
||||||
|
* description="已播放 (Played, Skipped, NoFile)",
|
||||||
|
* @OA\Items(ref="#/components/schemas/OrderedSong")
|
||||||
|
* ),
|
||||||
|
* @OA\Property(
|
||||||
|
* property="playing",
|
||||||
|
* type="array",
|
||||||
|
* description="正在播放中 (Playing)",
|
||||||
|
* @OA\Items(ref="#/components/schemas/OrderedSong")
|
||||||
|
* ),
|
||||||
|
* @OA\Property(
|
||||||
|
* property="not_played",
|
||||||
|
* type="array",
|
||||||
|
* description="未播放 (NotPlayed, InsertPlayback)",
|
||||||
|
* @OA\Items(ref="#/components/schemas/OrderedSong")
|
||||||
|
* )
|
||||||
|
* )
|
||||||
|
* ),
|
||||||
|
* @OA\Response(
|
||||||
|
* response=422,
|
||||||
|
* description="驗證失敗",
|
||||||
|
* @OA\JsonContent(
|
||||||
|
* type="object",
|
||||||
|
* @OA\Property(property="message", type="string", example="The given data was invalid."),
|
||||||
|
* @OA\Property(property="errors", type="object")
|
||||||
|
* )
|
||||||
|
* )
|
||||||
|
* )
|
||||||
|
*/
|
||||||
|
public function listOrderedSongs(RoomSongRequest $request)
|
||||||
|
{
|
||||||
|
$roomSession = $this->getRoomSession($request->api_token) ;
|
||||||
|
// 已結束 (finished + canceled)
|
||||||
|
$played = OrderedSong::where('room_session_id', $roomSession->id)
|
||||||
|
->whereIn('status', ['Played', 'Skipped', 'NoFile'])
|
||||||
|
->orderByDesc('finished_at')
|
||||||
|
->get();
|
||||||
|
$playing = OrderedSong::where('room_session_id', $roomSession->id)
|
||||||
|
->whereIn('status', ['Playing'])
|
||||||
|
->get();
|
||||||
|
|
||||||
|
// 正在播 + 插播 + 待播
|
||||||
|
$not_played = OrderedSong::where('room_session_id', $roomSession->id)
|
||||||
|
->whereIn('status', ['Playing', 'InsertPlayback', 'NotPlayed'])
|
||||||
|
->orderByRaw("FIELD(status, 'Playing', 'InsertPlayback', 'NotPlayed')") // playing > InsertPlayback > NotPlayed
|
||||||
|
->orderByRaw("CASE
|
||||||
|
WHEN status = 'InsertPlayback' THEN ordered_at END DESC") // InsertPlayback 越後排越前
|
||||||
|
->orderByRaw("CASE
|
||||||
|
WHEN status = 'NotPlayed' THEN ordered_at END ASC") // NotPlayed 越後排越後
|
||||||
|
->get();
|
||||||
|
|
||||||
|
return ApiResponse::success([
|
||||||
|
'played' => $played,
|
||||||
|
'playing' => $playing,
|
||||||
|
'not_played' => $not_played,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* @OA\Post(
|
||||||
|
* path="/api/room/current-song",
|
||||||
|
* summary="取得目前播放中的歌曲",
|
||||||
|
* description="回傳當前房間正在播放的歌曲資訊 (包含部分 song 欄位)",
|
||||||
|
* tags={"Room Control Song"},
|
||||||
|
* security={{"Authorization":{}}},
|
||||||
|
* @OA\RequestBody(
|
||||||
|
* required=true,
|
||||||
|
* @OA\JsonContent(ref="#/components/schemas/RoomSongRequest")
|
||||||
|
* ),
|
||||||
|
* @OA\Response(
|
||||||
|
* response=200,
|
||||||
|
* description="成功回傳目前播放的歌曲",
|
||||||
|
* @OA\JsonContent(
|
||||||
|
* type="object",
|
||||||
|
* @OA\Property(
|
||||||
|
* property="success",
|
||||||
|
* type="boolean",
|
||||||
|
* example=true
|
||||||
|
* ),
|
||||||
|
* @OA\Property(
|
||||||
|
* property="data",
|
||||||
|
* type="object",
|
||||||
|
* @OA\Property(
|
||||||
|
* property="current",
|
||||||
|
* ref="#/components/schemas/OrderedSongWithPartialSong"
|
||||||
|
* )
|
||||||
|
* )
|
||||||
|
* )
|
||||||
|
* )
|
||||||
|
* )
|
||||||
|
*/
|
||||||
|
public function currentSong(RoomSongRequest $request)
|
||||||
|
{
|
||||||
|
$roomSession = $this->getRoomSession($request->api_token) ;
|
||||||
|
$current = OrderedSong::where('room_session_id', $roomSession->id)
|
||||||
|
->where('status', 'Playing')
|
||||||
|
->withPartialSong()
|
||||||
|
->first();
|
||||||
|
return ApiResponse::success([
|
||||||
|
'current' => $current ,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* @OA\Post(
|
||||||
|
* path="/api/room/next-song",
|
||||||
|
* summary="播放下一首歌曲",
|
||||||
|
* description="將目前播放的歌標記為結束,並將下一首設為播放中,回傳下首與下下首資訊。",
|
||||||
|
* tags={"Room Control Song"},
|
||||||
|
* security={{"Authorization":{}}},
|
||||||
|
* @OA\RequestBody(
|
||||||
|
* required=true,
|
||||||
|
* @OA\JsonContent(ref="#/components/schemas/RoomSongRequest")
|
||||||
|
* ),
|
||||||
|
* @OA\Response(
|
||||||
|
* response=200,
|
||||||
|
* description="成功回傳歌曲資訊",
|
||||||
|
* @OA\JsonContent(
|
||||||
|
* type="object",
|
||||||
|
* @OA\Property(
|
||||||
|
* property="current",
|
||||||
|
* type="object",
|
||||||
|
* nullable=true,
|
||||||
|
* ref="#/components/schemas/OrderedSongWithPartialSong"
|
||||||
|
* ),
|
||||||
|
* @OA\Property(
|
||||||
|
* property="next",
|
||||||
|
* type="object",
|
||||||
|
* nullable=true,
|
||||||
|
* ref="#/components/schemas/OrderedSongWithPartialSong"
|
||||||
|
* )
|
||||||
|
* )
|
||||||
|
* ),
|
||||||
|
* @OA\Response(
|
||||||
|
* response=401,
|
||||||
|
* description="未授權或 Token 錯誤"
|
||||||
|
* ),
|
||||||
|
* @OA\Response(
|
||||||
|
* response=404,
|
||||||
|
* description="找不到房間或歌曲"
|
||||||
|
* )
|
||||||
|
* )
|
||||||
|
*/
|
||||||
|
public function nextSong(RoomSongRequest $request)
|
||||||
|
{
|
||||||
|
$roomSession = $this->getRoomSession($request->api_token) ;
|
||||||
|
// 找目前正在播
|
||||||
|
$current = OrderedSong::where('room_session_id', $roomSession->id)
|
||||||
|
->where('status', 'Playing')
|
||||||
|
->first();
|
||||||
|
if ($current) {
|
||||||
|
// 把當前設為 finished,並記錄結束時間
|
||||||
|
$current->update([
|
||||||
|
'status' => 'Played',
|
||||||
|
'finished_at' => now(),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
// 撈出候播清單(下首 + 下下首)
|
||||||
|
$queue = OrderedSong::where('room_session_id', $roomSession->id)
|
||||||
|
->whereIn('status', ['InsertPlayback', 'NotPlayed'])
|
||||||
|
->withPartialSong()
|
||||||
|
->orderByRaw("FIELD(status, 'InsertPlayback', 'NotPlayed')") // InsertPlayback > NotPlayed
|
||||||
|
->orderByRaw("CASE WHEN status = 'InsertPlayback' THEN ordered_at END DESC") // InsertPlayback 後插播的先播
|
||||||
|
->orderByRaw("CASE WHEN status = 'NotPlayed' THEN ordered_at END ASC") // NotPlayed 先點的先播
|
||||||
|
->limit(2)
|
||||||
|
->get();
|
||||||
|
$current =$queue->get(0);
|
||||||
|
if ($current) {
|
||||||
|
$current->update([
|
||||||
|
'status' => 'Playing',
|
||||||
|
'started_at' => now(),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
return ApiResponse::success([
|
||||||
|
'current' => $current,// 下首
|
||||||
|
'next' => $queue->get(1) ?? null,// 下下首
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function nextSongName(RoomSession $roomSession)
|
||||||
|
{
|
||||||
|
// 找下首
|
||||||
|
$next = OrderedSong::where('room_session_id', $roomSession->id)
|
||||||
|
->whereIn('status', ['InsertPlayback', 'NotPlayed'])
|
||||||
|
->with('song')
|
||||||
|
->orderByRaw("FIELD(status, 'InsertPlayback', 'NotPlayed')")
|
||||||
|
->orderByRaw("CASE WHEN status = 'InsertPlayback' THEN ordered_at END DESC")
|
||||||
|
->orderByRaw("CASE WHEN status = 'NotPlayed' THEN ordered_at END ASC")
|
||||||
|
->first();
|
||||||
|
return $next?->song?->name;
|
||||||
|
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* 取得對應的 RoomSession
|
||||||
|
*/
|
||||||
|
private function getRoomSession($api_token): RoomSession
|
||||||
|
{
|
||||||
|
return RoomSession::where('api_token', $api_token)
|
||||||
|
->whereIn('status', ['active', 'maintain'])
|
||||||
|
->firstOrFail();
|
||||||
|
}
|
||||||
|
}
|
33
app/Http/Requests/OrderSongRequest.php
Normal file
33
app/Http/Requests/OrderSongRequest.php
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Requests;
|
||||||
|
|
||||||
|
use Illuminate\Validation\Rule;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @OA\Schema(
|
||||||
|
* schema="OrderSongRequest",
|
||||||
|
* required={"song_id","status","api_token"},
|
||||||
|
* @OA\Property(property="song_id", type="integer", example=1),
|
||||||
|
* @OA\Property(property="status", type="string", enum={"NotPlayed", "InsertPlayback", "Skipped"},example="NotPlayed"),
|
||||||
|
* @OA\Property(property="from_by", type="string", example="介面,ERP之類的說明"),
|
||||||
|
* @OA\Property(property="api_token", type="string", example="da9cdb88a60a377bba9fc53f09dd07838eb6f4f355d63f4927c711e7aaae3104"),
|
||||||
|
* )
|
||||||
|
*/
|
||||||
|
class OrderSongRequest extends ApiRequest
|
||||||
|
{
|
||||||
|
public function rules(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
|
||||||
|
'api_token' => [
|
||||||
|
'required',
|
||||||
|
Rule::exists('room_sessions', 'api_token')
|
||||||
|
->where(fn ($q) => $q->whereIn('status', ['active', 'maintain']))
|
||||||
|
],
|
||||||
|
'song_id' => 'required|exists:songs,id',
|
||||||
|
'status' => 'required|in:NotPlayed,InsertPlayback,Skipped',
|
||||||
|
'from_by' => 'nullable',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
43
app/Http/Requests/RoomSongRequest.php
Normal file
43
app/Http/Requests/RoomSongRequest.php
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Requests;
|
||||||
|
|
||||||
|
use Illuminate\Validation\Rule;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @OA\Parameter(
|
||||||
|
* parameter="ApiTokenQuery",
|
||||||
|
* name="api_token",
|
||||||
|
* in="query",
|
||||||
|
* required=true,
|
||||||
|
* description="Room session 的 API token",
|
||||||
|
* @OA\Schema(
|
||||||
|
* type="string",
|
||||||
|
* example="da9cdb88a60a377bba9fc53f09dd07838eb6f4f355d63f4927c711e7aaae3104"
|
||||||
|
* )
|
||||||
|
* )
|
||||||
|
* @OA\Schema(
|
||||||
|
* schema="RoomSongRequest",
|
||||||
|
* type="object",
|
||||||
|
* required={"api_token"},
|
||||||
|
* @OA\Property(
|
||||||
|
* property="api_token",
|
||||||
|
* type="string",
|
||||||
|
* description="Room session 的 API token",
|
||||||
|
* example="da9cdb88a60a377bba9fc53f09dd07838eb6f4f355d63f4927c711e7aaae3104"
|
||||||
|
* )
|
||||||
|
* )
|
||||||
|
*/
|
||||||
|
class RoomSongRequest extends ApiRequest
|
||||||
|
{
|
||||||
|
public function rules(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'api_token' => [
|
||||||
|
'required',
|
||||||
|
Rule::exists('room_sessions', 'api_token')
|
||||||
|
->where(fn ($q) => $q->whereIn('status', ['active', 'maintain']))
|
||||||
|
]
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
@ -2,7 +2,6 @@
|
|||||||
|
|
||||||
namespace App\Http\Requests;
|
namespace App\Http\Requests;
|
||||||
|
|
||||||
use Illuminate\Foundation\Http\FormRequest;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @OA\Schema(
|
* @OA\Schema(
|
||||||
@ -10,7 +9,7 @@ use Illuminate\Foundation\Http\FormRequest;
|
|||||||
* required={"branch_name", "room_name", "command"},
|
* required={"branch_name", "room_name", "command"},
|
||||||
* @OA\Property(property="branch_name", type="string", example="測試"),
|
* @OA\Property(property="branch_name", type="string", example="測試"),
|
||||||
* @OA\Property(property="room_name", type="string", example="pc102"),
|
* @OA\Property(property="room_name", type="string", example="pc102"),
|
||||||
* @OA\Property(property="command", type="string", enum={"active", "closed", "fire",maintain, "error"}, example="active"),
|
* @OA\Property(property="command", type="string", enum={"active", "closed", "fire","maintain", "error"}, example="active"),
|
||||||
* @OA\Property(property="started_at", type="string", nullable=true, example="2025-05-19 09:31:00"),
|
* @OA\Property(property="started_at", type="string", nullable=true, example="2025-05-19 09:31:00"),
|
||||||
* @OA\Property(property="ended_at", type="string", nullable=true, example="2025-05-19 09:31:00")
|
* @OA\Property(property="ended_at", type="string", nullable=true, example="2025-05-19 09:31:00")
|
||||||
* )
|
* )
|
||||||
|
@ -4,7 +4,7 @@ namespace App\Livewire\Modals;
|
|||||||
|
|
||||||
use App\Models\Room;
|
use App\Models\Room;
|
||||||
use App\Models\Branch;
|
use App\Models\Branch;
|
||||||
|
use Illuminate\Support\Carbon;
|
||||||
use Livewire\Component;
|
use Livewire\Component;
|
||||||
use App\Services\ApiClient;
|
use App\Services\ApiClient;
|
||||||
use Illuminate\Support\Facades\Auth;
|
use Illuminate\Support\Facades\Auth;
|
||||||
@ -35,20 +35,19 @@ class RoomDetailModal extends Component
|
|||||||
$this->showModal = false;
|
$this->showModal = false;
|
||||||
}
|
}
|
||||||
public function startNotify()
|
public function startNotify()
|
||||||
{
|
{ $data = $this->buildNotifyData('maintain', Carbon::now(), null);
|
||||||
$data = $this->buildNotifyData('maintain', null, null);
|
|
||||||
$this->send($data);
|
$this->send($data);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function stopNotify()
|
public function stopNotify()
|
||||||
{
|
{
|
||||||
$data = $this->buildNotifyData('closed', null, null);
|
$data = $this->buildNotifyData('closed', null, Carbon::now());
|
||||||
$this->send($data);
|
$this->send($data);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function fireNotify()
|
public function fireNotify()
|
||||||
{
|
{
|
||||||
$data = $this->buildNotifyData('fire', null, null);
|
$data = $this->buildNotifyData('fire', null, Carbon::now());
|
||||||
$this->send($data);
|
$this->send($data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
87
app/Models/OrderedSong.php
Normal file
87
app/Models/OrderedSong.php
Normal file
@ -0,0 +1,87 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Models;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @OA\Schema(
|
||||||
|
* schema="OrderedSong",
|
||||||
|
* type="object",
|
||||||
|
* @OA\Property(property="id", type="integer", example=123),
|
||||||
|
* @OA\Property(property="room_session_id", type="integer", example=1),
|
||||||
|
* @OA\Property(property="from_by", type="string", example="remote"),
|
||||||
|
* @OA\Property(property="song_id", type="integer", example=5),
|
||||||
|
* @OA\Property(property="song_name", type="string", example="歌名"),
|
||||||
|
* @OA\Property(property="artist_name", type="string", example="歌手名稱"),
|
||||||
|
* @OA\Property(property="status", ref="#/components/schemas/OrderedSongStatus"),
|
||||||
|
* @OA\Property(property="ordered_at", type="string", format="date-time", example="2025-08-18T14:00:00Z"),
|
||||||
|
* @OA\Property(property="started_at", type="string", format="date-time", nullable=true, example="2025-08-18T14:05:00Z"),
|
||||||
|
* @OA\Property(property="finished_at", type="string", format="date-time", nullable=true, example="2025-08-18T14:10:00Z"),
|
||||||
|
* )
|
||||||
|
* @OA\Schema(
|
||||||
|
* schema="OrderedSongWithPartialSong",
|
||||||
|
* allOf={
|
||||||
|
* @OA\Schema(ref="#/components/schemas/OrderedSong"),
|
||||||
|
* @OA\Schema(
|
||||||
|
* @OA\Property(
|
||||||
|
* property="song",
|
||||||
|
* type="object",
|
||||||
|
* nullable=true,
|
||||||
|
* @OA\Property(property="id", type="integer", example=5),
|
||||||
|
* @OA\Property(property="name", type="string", example="歌名"),
|
||||||
|
* @OA\Property(property="filename", type="string", example="song123.mp4"),
|
||||||
|
* @OA\Property(property="db_change", type="integer", example=-2),
|
||||||
|
* @OA\Property(property="vocal", type="boolean", example=true),
|
||||||
|
* @OA\Property(property="situation", type="string", example="party")
|
||||||
|
* )
|
||||||
|
* )
|
||||||
|
* }
|
||||||
|
* )
|
||||||
|
*/
|
||||||
|
class OrderedSong extends Model
|
||||||
|
{
|
||||||
|
use HasFactory;
|
||||||
|
|
||||||
|
public $timestamps = false;
|
||||||
|
|
||||||
|
protected $fillable = [
|
||||||
|
'room_session_id',
|
||||||
|
'from_by',
|
||||||
|
'song_id',
|
||||||
|
'song_name',
|
||||||
|
'artist_name',
|
||||||
|
'status',
|
||||||
|
'ordered_at',
|
||||||
|
'started_at',
|
||||||
|
'finished_at',
|
||||||
|
|
||||||
|
];
|
||||||
|
|
||||||
|
protected $casts = [
|
||||||
|
'ordered_at' => 'datetime',
|
||||||
|
'started_at' => 'datetime',
|
||||||
|
'finished_at' => 'datetime',
|
||||||
|
'status' => \App\Enums\OrderedSongStatus::class,
|
||||||
|
];
|
||||||
|
|
||||||
|
|
||||||
|
public function session()
|
||||||
|
{
|
||||||
|
return $this->belongsTo(RoomSession::class, 'room_session_id');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function song()
|
||||||
|
{
|
||||||
|
return $this->belongsTo(Song::class);
|
||||||
|
}
|
||||||
|
public function scopeWithPartialSong($query)
|
||||||
|
{
|
||||||
|
return $query->with([
|
||||||
|
'song' => function ($q) {
|
||||||
|
$q->select('id', 'name','filename','db_change','vocal','situation'); // 精簡版
|
||||||
|
}
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
@ -92,4 +92,8 @@ class Room extends Model
|
|||||||
public function statusLogs() {
|
public function statusLogs() {
|
||||||
return $this->hasMany(RoomStatusLog::class);
|
return $this->hasMany(RoomStatusLog::class);
|
||||||
}
|
}
|
||||||
|
public function sessions()
|
||||||
|
{
|
||||||
|
return $this->hasMany(RoomSession::class);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
42
app/Models/RoomSession.php
Normal file
42
app/Models/RoomSession.php
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Models;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
|
||||||
|
class RoomSession extends Model
|
||||||
|
{
|
||||||
|
use HasFactory;
|
||||||
|
|
||||||
|
protected $fillable = [
|
||||||
|
'room_id',
|
||||||
|
'started_at',
|
||||||
|
'ended_at',
|
||||||
|
'status',
|
||||||
|
'mode',
|
||||||
|
'close_reason',
|
||||||
|
'api_token',
|
||||||
|
];
|
||||||
|
|
||||||
|
protected $casts = [
|
||||||
|
'started_at' => 'datetime',
|
||||||
|
'ended_at' => 'datetime',
|
||||||
|
];
|
||||||
|
|
||||||
|
// 狀態常數
|
||||||
|
public const STATUS_ACTIVE = 'active';
|
||||||
|
public const STATUS_CLOSED = 'closed';
|
||||||
|
public const STATUS_FORCE_CLOSED = 'force_closed';
|
||||||
|
public const STATUS_FIRE_CLOSED = 'fire_closed';
|
||||||
|
|
||||||
|
// 模式常數
|
||||||
|
public const MODE_NORMAL = 'normal';
|
||||||
|
public const MODE_VIP = 'vip';
|
||||||
|
public const MODE_TEST = 'test';
|
||||||
|
|
||||||
|
public function room()
|
||||||
|
{
|
||||||
|
return $this->belongsTo(Room::class);
|
||||||
|
}
|
||||||
|
}
|
@ -53,6 +53,10 @@ class Song extends Model
|
|||||||
public function str_artists(){
|
public function str_artists(){
|
||||||
return $this->artists->pluck('name')->implode(', ');
|
return $this->artists->pluck('name')->implode(', ');
|
||||||
}
|
}
|
||||||
|
public function str_artists_plus(): string
|
||||||
|
{
|
||||||
|
return $this->artists->pluck('name')->implode(' + ');
|
||||||
|
}
|
||||||
public function artists(){
|
public function artists(){
|
||||||
return $this->belongsToMany(Artist::class);
|
return $this->belongsToMany(Artist::class);
|
||||||
}
|
}
|
||||||
|
@ -15,17 +15,8 @@ class RoomObserver
|
|||||||
*/
|
*/
|
||||||
public function created(Room $room): void
|
public function created(Room $room): void
|
||||||
{
|
{
|
||||||
RoomStatusLog::create([
|
// 建立初始 log
|
||||||
'branch_id' => $room->branch->id,
|
$this->createStatusLog($room);
|
||||||
'room_id' => $room->id,
|
|
||||||
'user_id' => Auth::id()?? 0,
|
|
||||||
'is_online' => $room->is_online,
|
|
||||||
'status' => $room->status ?? 'error',
|
|
||||||
'started_at' => $room->started_at,
|
|
||||||
'ended_at' => $room->ended_at,
|
|
||||||
'message' => $room->log_message ?? '',
|
|
||||||
'source' => $this->getSource($room),
|
|
||||||
]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -35,17 +26,32 @@ class RoomObserver
|
|||||||
{
|
{
|
||||||
// 檢查是否有變更狀態
|
// 檢查是否有變更狀態
|
||||||
if ($room->wasChanged()) {
|
if ($room->wasChanged()) {
|
||||||
RoomStatusLog::create([
|
$this->createStatusLog($room);
|
||||||
'branch_id' => $room->branch->id,
|
}
|
||||||
'room_id' => $room->id,
|
if ($room->isDirty('status')) {
|
||||||
'user_id' => Auth::id() ?? 0,
|
$now = now();
|
||||||
'is_online' => $room->is_online,
|
|
||||||
'status' => $room->status,
|
// 找到最後一筆未結束 session
|
||||||
'started_at' =>$room->started_at,
|
$lastSession = $room->sessions()->whereNull('ended_at')->latest('started_at')->first();
|
||||||
'ended_at' =>$room->ended_at,
|
|
||||||
'message' => $room->log_message ?? '',
|
if ($lastSession) {
|
||||||
'source' => $this->getSource($room),
|
// 結束上一筆 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,
|
||||||
|
'api_token' => bin2hex(random_bytes(32)),
|
||||||
|
]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -62,35 +68,28 @@ class RoomObserver
|
|||||||
$room->internal_ip,
|
$room->internal_ip,
|
||||||
$room->port
|
$room->port
|
||||||
);
|
);
|
||||||
|
$this->createStatusLog($room,$message);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 建立 RoomStatusLog
|
||||||
|
*/
|
||||||
|
private function createStatusLog(Room $room,$log_message =null): void
|
||||||
|
{
|
||||||
|
$message=($log_message !=null)?$log_message:$room->log_message ?? '';
|
||||||
RoomStatusLog::create([
|
RoomStatusLog::create([
|
||||||
'branch_id' => $room->branch->id,
|
'branch_id' => $room->branch->id,
|
||||||
'room_id' => $room->id,
|
'room_id' => $room->id,
|
||||||
'user_id' => Auth::id() ?? 0,
|
'user_id' => Auth::id() ?? 0,
|
||||||
'is_online' => $room->is_online,
|
'is_online' => $room->is_online,
|
||||||
'status' => $room->status,
|
'status' => $room->status,
|
||||||
'started_at' =>$room->started_at,
|
'started_at' => $room->started_at,
|
||||||
'ended_at' =>$room->ended_at,
|
'ended_at' => $room->ended_at,
|
||||||
'message' => $message,
|
'message' => $message,
|
||||||
'source' => $this->getSource($room),
|
'source' => $this->getSource($room),
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Handle the Room "restored" event.
|
|
||||||
*/
|
|
||||||
public function restored(Room $room): void
|
|
||||||
{
|
|
||||||
//
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Handle the Room "force deleted" event.
|
|
||||||
*/
|
|
||||||
public function forceDeleted(Room $room): void
|
|
||||||
{
|
|
||||||
//
|
|
||||||
}
|
|
||||||
|
|
||||||
private function getSource(Room $room): string
|
private function getSource(Room $room): string
|
||||||
{
|
{
|
||||||
return app()->runningInConsole() ? 'system' : ($room->log_source ?? 'manual');
|
return app()->runningInConsole() ? 'system' : ($room->log_source ?? 'manual');
|
||||||
|
@ -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('room_sessions', function (Blueprint $table) {
|
||||||
|
$table->id();
|
||||||
|
$table->foreignId('room_id')->constrained()->cascadeOnDelete();
|
||||||
|
$table->enum('status', ['active', 'closed','fire', 'error', 'maintain'])->default('error');
|
||||||
|
$table->timestamp('started_at')->nullable();
|
||||||
|
$table->timestamp('ended_at')->nullable();
|
||||||
|
$table->enum('mode', ['normal', 'test'])->default('normal');
|
||||||
|
$table->string('close_reason')->nullable();
|
||||||
|
$table->string('api_token', 64)->unique()->nullable();
|
||||||
|
$table->timestamps();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*/
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
Schema::dropIfExists('room_sessions');
|
||||||
|
}
|
||||||
|
};
|
@ -0,0 +1,44 @@
|
|||||||
|
<?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('ordered_songs', function (Blueprint $table) {
|
||||||
|
$table->id();
|
||||||
|
$table->foreignId('room_session_id')->constrained('room_sessions')->cascadeOnDelete();
|
||||||
|
$table->string('from_by')->nullable();
|
||||||
|
|
||||||
|
// 歌曲資訊
|
||||||
|
$table->foreignId('song_id')->constrained()->cascadeOnDelete();
|
||||||
|
$table->string('song_name')->nullable();
|
||||||
|
$table->string('artist_name')->nullable();
|
||||||
|
|
||||||
|
|
||||||
|
// 狀態:未播 / 播放中 / 已播 / 刪除
|
||||||
|
$table->enum('status', ['NotPlayed', 'Playing', 'Played', 'NoFile', 'Skipped' , 'InsertPlayback'])->default('NotPlayed');
|
||||||
|
|
||||||
|
// 播放流程
|
||||||
|
$table->timestamp('ordered_at')->useCurrent(); // 點歌時間
|
||||||
|
$table->timestamp('started_at')->nullable(); // 開始播放時間
|
||||||
|
$table->timestamp('finished_at')->nullable(); // 播放結束時間
|
||||||
|
|
||||||
|
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*/
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
Schema::dropIfExists('ordered_songs');
|
||||||
|
}
|
||||||
|
};
|
@ -5,6 +5,12 @@ return [
|
|||||||
'Other' => '其他',
|
'Other' => '其他',
|
||||||
'Male' =>'男',
|
'Male' =>'男',
|
||||||
'Female' =>'女',
|
'Female' =>'女',
|
||||||
|
'NotPlayed' =>'未播放',
|
||||||
|
'Playing' =>'播放中',
|
||||||
|
'Played' =>'播畢',
|
||||||
|
'NoFile' =>'無文件',
|
||||||
|
'Skipped' =>'刪除',
|
||||||
|
'InsertPlayback' =>'插播',
|
||||||
'arist.category.Group' => '團',
|
'arist.category.Group' => '團',
|
||||||
'arist.category.Foreign' => '外',
|
'arist.category.Foreign' => '外',
|
||||||
'song.situation.Romantic' => '浪漫',
|
'song.situation.Romantic' => '浪漫',
|
||||||
|
@ -5,6 +5,8 @@ use App\Http\Controllers\ArtistController;
|
|||||||
use App\Http\Controllers\AuthController;
|
use App\Http\Controllers\AuthController;
|
||||||
use App\Http\Controllers\BranchControlController;
|
use App\Http\Controllers\BranchControlController;
|
||||||
use App\Http\Controllers\RoomControlController;
|
use App\Http\Controllers\RoomControlController;
|
||||||
|
use App\Http\Controllers\Api\RoomSongController;
|
||||||
|
use App\Http\Controllers\Api\SongSearchController;
|
||||||
|
|
||||||
Route::get('/artists/search', [App\Http\Controllers\ArtistController::class, 'search'])->name('api.artists.search');
|
Route::get('/artists/search', [App\Http\Controllers\ArtistController::class, 'search'])->name('api.artists.search');
|
||||||
|
|
||||||
@ -12,9 +14,13 @@ Route::post('/login', [AuthController::class, 'login']);
|
|||||||
Route::post('/room/receiveRegister', [RoomControlController::class, 'receiveRegister']);
|
Route::post('/room/receiveRegister', [RoomControlController::class, 'receiveRegister']);
|
||||||
|
|
||||||
Route::middleware('auth:sanctum')->group(function () {
|
Route::middleware('auth:sanctum')->group(function () {
|
||||||
Route::get('/profile', [AuthController::class, 'profile']);
|
Route::get ('/profile', [AuthController::class, 'profile']);
|
||||||
Route::get('/branches',[BranchControlController::class, 'Branches']);
|
Route::get ('/branches',[BranchControlController::class, 'Branches']);
|
||||||
Route::post('/room/sendSwitch', [RoomControlController::class, 'sendSwitch']);
|
Route::post('/room/sendSwitch', [RoomControlController::class, 'sendSwitch']);
|
||||||
Route::post('/room/receiveSwitch', [RoomControlController::class, 'receiveSwitch']);
|
Route::post('/room/receiveSwitch', [RoomControlController::class, 'receiveSwitch']);
|
||||||
Route::post('/room/heartbeat', [RoomControlController::class, 'StatusReport']);
|
Route::post('/room/heartbeat', [RoomControlController::class, 'StatusReport']);
|
||||||
|
Route::post('/room/order-song', [RoomSongController::class, 'orderSong']);
|
||||||
|
Route::get ('/room/ordered-songs', [RoomSongController::class, 'listOrderedSongs']);
|
||||||
|
Route::post ('/room/current-song', [RoomSongController::class, 'currentSong']);
|
||||||
|
Route::post ('/room/next-song', [RoomSongController::class, 'nextSong']);
|
||||||
});
|
});
|
Loading…
x
Reference in New Issue
Block a user