194 lines
4.9 KiB
PHP
194 lines
4.9 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Enums\DrawActivityStatus;
|
|
use Dcat\Admin\Traits\HasDateTimeFormatter;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
class DrawActivity extends Model
|
|
{
|
|
use HasDateTimeFormatter;
|
|
|
|
/**
|
|
* @var array
|
|
*/
|
|
protected $attributes = [
|
|
'status' => DrawActivityStatus::Created,
|
|
];
|
|
|
|
/**
|
|
* @var array
|
|
*/
|
|
protected $casts = [
|
|
'start_at' => 'datetime',
|
|
'end_at' => 'datetime',
|
|
'published_at' => 'datetime',
|
|
'status' => DrawActivityStatus::class,
|
|
];
|
|
|
|
/**
|
|
* @var array
|
|
*/
|
|
protected $fillable = [
|
|
'name',
|
|
'cover',
|
|
'desc',
|
|
'start_at',
|
|
'end_at',
|
|
'published_at',
|
|
'bg_image',
|
|
'bg_color',
|
|
'status',
|
|
];
|
|
|
|
/**
|
|
* 仅查询已发布的活动
|
|
*/
|
|
public function scopeOnlyPublished(Builder $builder): Builder
|
|
{
|
|
return $builder->where('status', '!=', DrawActivityStatus::Created)->where('published_at', '<=', now());
|
|
}
|
|
|
|
/**
|
|
* 仅查询已创建的活动
|
|
*/
|
|
public function scopeOnlyCreated(Builder $builder): Builder
|
|
{
|
|
return $builder->where('status', DrawActivityStatus::Created);
|
|
}
|
|
|
|
/**
|
|
* 仅查询发布中的活动
|
|
*/
|
|
public function scopeOnlyPublishing(Builder $builder): Builder
|
|
{
|
|
return $builder->where('status', DrawActivityStatus::Running)->where('published_at', '>', now());
|
|
}
|
|
|
|
/**
|
|
* 仅查询未开始的活动
|
|
*/
|
|
public function scopeOnlyUnstart(Builder $builder): Builder
|
|
{
|
|
$tz = now();
|
|
|
|
return $builder->where('status', DrawActivityStatus::Running)
|
|
->where('published_at', '<=', $tz)
|
|
->whereNotNull('start_at')
|
|
->where('start_at', '>', $tz);
|
|
}
|
|
|
|
/**
|
|
* 仅查询进行中的活动
|
|
*/
|
|
public function scopeOnlyRunning(Builder $builder): Builder
|
|
{
|
|
$tz = now();
|
|
|
|
return $builder->where('status', DrawActivityStatus::Running)
|
|
->where('published_at', '<=', $tz)
|
|
->where(function ($builder) use ($tz) {
|
|
$builder->where(function ($builder) use ($tz) {
|
|
$builder->whereNull('start_at')->orWhere(function ($builder) use ($tz) {
|
|
$builder->whereNotNull('start_at')->where('start_at', '<=', $tz);
|
|
});
|
|
})->where(function ($builder) use ($tz) {
|
|
$builder->whereNull('end_at')->orWhere(function ($builder) use ($tz) {
|
|
$builder->whereNotNull('end_at')->where('end_at', '>', $tz);
|
|
});
|
|
});
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 仅查询已结束的活动
|
|
*/
|
|
public function scopeOnlyClosed(Builder $builder): Builder
|
|
{
|
|
return $builder->where('status', DrawActivityStatus::Closed)
|
|
->orWhere(function ($builder) {
|
|
$tz = now();
|
|
|
|
return $builder->where('status', '!=', DrawActivityStatus::Created)
|
|
->where('published_at', '<=', $tz)
|
|
->whereNotNull('end_at')
|
|
->where('end_at', '<=', $tz);
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 属于此活动的奖品
|
|
*/
|
|
public function prizes()
|
|
{
|
|
return $this->hasMany(DrawActivityPrize::class, 'draw_activity_id');
|
|
}
|
|
|
|
/**
|
|
* 确认此活动是否已发布
|
|
*
|
|
* @return bool
|
|
*/
|
|
public function isPublished(): bool
|
|
{
|
|
return in_array($this->real_status, [
|
|
DrawActivityStatus::Unstart,
|
|
DrawActivityStatus::Running,
|
|
DrawActivityStatus::Closed,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 确认此活动是否未开始
|
|
*
|
|
* @return bool
|
|
*/
|
|
public function isUnstart(): bool
|
|
{
|
|
return $this->real_status === DrawActivityStatus::Unstart;
|
|
}
|
|
|
|
/**
|
|
* 确认此活动是否已结束
|
|
*
|
|
* @return bool
|
|
*/
|
|
public function isClosed(): bool
|
|
{
|
|
return $this->real_status === DrawActivityStatus::Closed;
|
|
}
|
|
|
|
public function getBgColorLabel()
|
|
{
|
|
if ($this->bg_color) {
|
|
return "<span class='label shadow-100' style='background: {$this->bg_color};'>{$this->bg_color}</span>";
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取此活动的真实状态
|
|
*
|
|
* @return DrawActivityStatus
|
|
*/
|
|
public function getRealStatusAttribute(): DrawActivityStatus
|
|
{
|
|
if ($this->status === DrawActivityStatus::Running) {
|
|
if (now()->lt($this->published_at)) {
|
|
return DrawActivityStatus::Publishing;
|
|
}
|
|
|
|
if ($this->start_at && now()->lt($this->start_at)) {
|
|
return DrawActivityStatus::Unstart;
|
|
}
|
|
|
|
if ($this->end_at && now()->gte($this->end_at)) {
|
|
return DrawActivityStatus::Closed;
|
|
}
|
|
}
|
|
|
|
return $this->status;
|
|
}
|
|
}
|