*/ use HasFactory; /** * 允许批量赋值的字段。 * * @var array */ 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 */ 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(); } }