Files
chatroom/app/Models/HolidayEventRun.php
T

112 lines
2.8 KiB
PHP

<?php
/**
* 文件功能:节日福利发放批次模型
*
* 记录节日福利模板的每一次实际发放批次,
* 承担领取有效期、批次统计与历史追踪职责。
*/
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
/**
* 类功能:封装节日福利单次发放批次的时间、状态和领取统计。
*/
class HolidayEventRun extends Model
{
/** @use HasFactory<\Database\Factories\HolidayEventRunFactory> */
use HasFactory;
/**
* 允许批量赋值的字段。
*
* @var array<int, string>
*/
protected $fillable = [
'holiday_event_id',
'event_name',
'event_description',
'total_amount',
'max_claimants',
'distribute_type',
'min_amount',
'max_amount',
'fixed_amount',
'target_type',
'target_value',
'repeat_type',
'scheduled_for',
'triggered_at',
'expires_at',
'status',
'audience_count',
'claimed_count',
'claimed_amount',
];
/**
* 属性类型转换。
*
* @return array<string, string>
*/
protected function casts(): array
{
return [
'scheduled_for' => 'datetime',
'triggered_at' => 'datetime',
'expires_at' => 'datetime',
'total_amount' => 'integer',
'max_claimants' => 'integer',
'min_amount' => 'integer',
'max_amount' => 'integer',
'fixed_amount' => 'integer',
'audience_count' => 'integer',
'claimed_count' => 'integer',
'claimed_amount' => 'integer',
];
}
/**
* 关联所属节日福利模板。
*/
public function holidayEvent(): BelongsTo
{
return $this->belongsTo(HolidayEvent::class, 'holiday_event_id');
}
/**
* 关联本批次的领取记录。
*/
public function claims(): HasMany
{
return $this->hasMany(HolidayClaim::class, 'run_id');
}
/**
* 判断当前批次是否仍处于可领取状态。
*/
public function isClaimable(): bool
{
return $this->status === 'active'
&& $this->expires_at !== null
&& $this->expires_at->isFuture();
}
/**
* 查询需要被自动收尾的批次。
*/
public static function pendingToExpire(): \Illuminate\Database\Eloquent\Collection
{
return static::query()
->where('status', 'active')
->whereNotNull('expires_at')
->where('expires_at', '<=', now())
->get();
}
}