84 lines
2.2 KiB
PHP
84 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use App\Enums\OrderedSongStatus;
|
|
|
|
class OrderedSong extends Model
|
|
{
|
|
use HasFactory;
|
|
|
|
public $timestamps = false;
|
|
|
|
protected $fillable = [
|
|
'room_session_id',
|
|
'from_by',
|
|
'order_number',
|
|
'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'); // 精簡版
|
|
}
|
|
]);
|
|
}
|
|
public function scopeForSession($query, $roomSessionId)
|
|
{
|
|
return $query->where('room_session_id', $roomSessionId);
|
|
}
|
|
|
|
public function scopePlaying($query)
|
|
{
|
|
return $query->where('status', OrderedSongStatus::Playing);
|
|
}
|
|
|
|
public function scopeNextSong($query)
|
|
{
|
|
return $query->whereIn('status', [OrderedSongStatus::InsertPlayback, OrderedSongStatus::NotPlayed])
|
|
->orderByRaw("FIELD(status, ?, ?)", [
|
|
OrderedSongStatus::InsertPlayback->value,
|
|
OrderedSongStatus::NotPlayed->value,
|
|
])
|
|
->orderByRaw("CASE WHEN status=? THEN ordered_at END DESC", [OrderedSongStatus::InsertPlayback->value])
|
|
->orderByRaw("CASE WHEN status=? THEN ordered_at END ASC", [OrderedSongStatus::NotPlayed->value]);
|
|
}
|
|
|
|
public function scopeFinished($query)
|
|
{
|
|
return $query->whereIn('status', [
|
|
OrderedSongStatus::Played,
|
|
OrderedSongStatus::Skipped,
|
|
OrderedSongStatus::NoFile,
|
|
])->orderByDesc('finished_at');
|
|
}
|
|
}
|