You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

221 lines
8.1 KiB

3 weeks ago
<?php
namespace App\Models;
1 week ago
use Carbon\Carbon;
3 weeks ago
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class ActivityDay extends Model
{
protected $fillable = [
'activity_id',
1 week ago
'session_name',
3 weeks ago
'activity_date',
'day_quota',
'booked_count',
'opens_at',
'closes_at',
1 week ago
'session_start_at',
'session_end_at',
'booking_deadline_at',
3 weeks ago
];
protected $casts = [
'activity_date' => 'date',
'opens_at' => 'datetime',
'closes_at' => 'datetime',
1 week ago
'session_start_at' => 'datetime',
'session_end_at' => 'datetime',
'booking_deadline_at' => 'datetime',
3 weeks ago
'day_quota' => 'integer',
'booked_count' => 'integer',
];
1 week ago
/** 管理端/接口序列化时附带,与 H5 中 `time_range_text` 一致 */
protected $appends = ['time_range_text'];
public function getTimeRangeTextAttribute(): string
{
return $this->formatSessionTimeRangeZh();
}
1 week ago
protected static function booted(): void
{
static::saving(function (ActivityDay $day) {
if ($day->session_start_at) {
$tz = (string) config('app.timezone');
$day->activity_date = $day->session_start_at->copy()->timezone($tz)->format('Y-m-d');
}
});
}
3 weeks ago
public function activity(): BelongsTo
{
return $this->belongsTo(Activity::class);
}
/**
1 week ago
* 场次时间段展示同日则「Y年m月d日 H:i-H:i」。
*/
public function formatSessionTimeRangeZh(): string
{
if (!$this->session_start_at || !$this->session_end_at) {
return $this->activity_date?->format('Y年m月d日') ?? '';
}
$tz = (string) config('app.timezone');
$s = $this->session_start_at->copy()->timezone($tz);
$e = $this->session_end_at->copy()->timezone($tz);
if ($s->toDateString() === $e->toDateString()) {
return $s->format('Y年m月d日 H:i') . '-' . $e->format('H:i');
}
return $s->format('Y年m月d日 H:i') . ' ~ ' . $e->format('Y年m月d日 H:i');
}
public function isSessionMode(): bool
{
return $this->session_start_at !== null
&& $this->session_end_at !== null
&& $this->booking_deadline_at !== null;
}
/**
* 当前时刻是否可预约:截止前、有余量、未重复预约等由业务层与 H5 数组字段共同约束。
* 这里仅表示「未过预约截止且有余量」legacy 为原 opens/closes 窗口逻辑)。
3 weeks ago
*/
public function isCurrentlyBookable(): bool
{
$available = max(0, (int) $this->day_quota - (int) $this->booked_count);
1 week ago
if ($available <= 0) {
return false;
}
if ($this->isSessionMode()) {
return now()->lte($this->booking_deadline_at);
}
$closesAt = $this->closes_at ?: $this->opens_at?->copy()->endOfDay();
3 weeks ago
$now = now();
1 week ago
if (!$this->opens_at || !$closesAt) {
return false;
}
3 weeks ago
$isOpenWindow = $this->opens_at->lte($now) && $closesAt->gte($now);
1 week ago
return $isOpenWindow;
}
/**
* H5 预约页/活动详情:单场次展示与可约态。
*
* @return array<string, mixed>
*/
public function toH5BookingDayArray(bool $alreadyReserved = false): array
{
$available = max(0, (int) $this->day_quota - (int) $this->booked_count);
$now = now();
1 week ago
$enrolledCountText = '已报名数:' . (int) $this->booked_count;
$bookedPeopleText = '已约' . (int) $this->booked_count . '人';
1 week ago
if ($this->isSessionMode()) {
$deadline = $this->booking_deadline_at;
$unavailableReason = null;
$isBookable = false;
if ($alreadyReserved) {
$unavailableReason = 'already_reserved';
1 week ago
$actionHeadline = '已预约';
1 week ago
$statusText = '已约';
$statusKind = 'reserved';
1 week ago
} elseif (! $deadline) {
$unavailableReason = 'closed';
$actionHeadline = '预约已结束';
$statusText = '预约已结束';
$statusKind = 'closed';
1 week ago
} elseif ($now->gt($deadline)) {
$unavailableReason = 'deadline_passed';
1 week ago
$actionHeadline = '预约已结束';
1 week ago
$statusText = '已约' . (int) $this->booked_count . '人';
$statusKind = 'booked_info';
} elseif ($available <= 0) {
$unavailableReason = 'sold_out';
1 week ago
$actionHeadline = '预约已满';
1 week ago
$statusText = '预约已满';
$statusKind = 'full';
} else {
$isBookable = true;
$unavailableReason = null;
1 week ago
$actionHeadline = '立即预约';
1 week ago
$statusText = '已约' . (int) $this->booked_count . '人';
$statusKind = 'open';
}
$timeDisplay = $this->formatSessionTimeRangeZh();
} else {
// legacy无场次字段时沿用原预约窗口
$timeDisplay = $this->activity_date?->format('Y年m月d日') ?? '';
if ($alreadyReserved) {
$unavailableReason = 'already_reserved';
1 week ago
$actionHeadline = '已预约';
1 week ago
$statusText = '已约';
$statusKind = 'reserved';
} elseif ($available <= 0) {
$unavailableReason = 'sold_out';
1 week ago
$actionHeadline = '预约已满';
1 week ago
$statusText = '预约已满';
$statusKind = 'full';
} else {
$closesAt = $this->closes_at ?: $this->opens_at?->copy()->endOfDay();
if ($this->opens_at && $this->opens_at->gt($now)) {
$unavailableReason = 'not_started';
1 week ago
$actionHeadline = '未开放';
1 week ago
$statusText = '未开放';
$statusKind = 'closed';
} elseif ($closesAt && $closesAt->lt($now)) {
$unavailableReason = 'closed';
1 week ago
$actionHeadline = '预约已结束';
1 week ago
$statusText = '不可预约';
$statusKind = 'closed';
} else {
$unavailableReason = null;
1 week ago
$actionHeadline = '立即预约';
1 week ago
$statusText = '已约' . (int) $this->booked_count . '人';
$statusKind = 'open';
}
}
1 week ago
$isBookable = $this->isCurrentlyBookable() && ! $alreadyReserved;
1 week ago
}
if ($alreadyReserved) {
$isBookable = false;
}
$closesAtLegacy = $this->closes_at ?: $this->opens_at?->copy()->endOfDay();
$isOpenWindow = $this->opens_at && $closesAtLegacy
? ($this->opens_at->lte($now) && $closesAtLegacy->gte($now))
: false;
return [
'id' => $this->id,
'session_name' => (string) ($this->session_name ?? ''),
'session_start_at' => $this->session_start_at?->format('Y-m-d H:i:s'),
'session_end_at' => $this->session_end_at?->format('Y-m-d H:i:s'),
'booking_deadline_at' => $this->booking_deadline_at?->format('Y-m-d H:i:s'),
'time_display' => $timeDisplay,
'activity_date' => $this->activity_date?->format('Y-m-d'),
'opens_at' => $this->opens_at?->format('Y-m-d H:i:s'),
'closes_at' => $closesAtLegacy?->format('Y-m-d H:i:s'),
'day_quota' => (int) $this->day_quota,
'booked_count' => (int) $this->booked_count,
'available_count' => $available,
'is_open' => $isOpenWindow,
'is_bookable' => $isBookable,
'unavailable_reason' => $unavailableReason,
'status_text' => $statusText,
'status_kind' => $statusKind,
'already_reserved' => $alreadyReserved,
1 week ago
'action_headline' => $actionHeadline,
'enrolled_count_text' => $enrolledCountText,
'booked_people_text' => $bookedPeopleText,
1 week ago
];
3 weeks ago
}
}