82 lines
1.7 KiB
PHP
82 lines
1.7 KiB
PHP
<?php
|
|
|
|
/**
|
|
* 文件功能:聊天室礼包(红包)主表 Model
|
|
*
|
|
* 由 superlevel 管理员在聊天室内发出,每次固定 888 金币分 10 个名额。
|
|
* 先到先得,领完或超时后自动关闭。
|
|
*
|
|
* @author ChatRoom Laravel
|
|
*
|
|
* @version 1.0.0
|
|
*/
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
|
|
class RedPacketEnvelope extends Model
|
|
{
|
|
protected $fillable = [
|
|
'sender_id',
|
|
'sender_username',
|
|
'room_id',
|
|
'type',
|
|
'total_amount',
|
|
'total_count',
|
|
'claimed_count',
|
|
'claimed_amount',
|
|
'status',
|
|
'expires_at',
|
|
];
|
|
|
|
/**
|
|
* 类型转换。
|
|
*/
|
|
public function casts(): array
|
|
{
|
|
return [
|
|
'expires_at' => 'datetime',
|
|
];
|
|
}
|
|
|
|
/**
|
|
* 关联领取记录。
|
|
*
|
|
* @return HasMany<RedPacketClaim>
|
|
*/
|
|
public function claims(): HasMany
|
|
{
|
|
return $this->hasMany(RedPacketClaim::class, 'envelope_id');
|
|
}
|
|
|
|
/**
|
|
* 判断红包当前是否可以被领取。
|
|
*
|
|
* 条件:状态为 active + 未过期 + 未领满。
|
|
*/
|
|
public function isClaimable(): bool
|
|
{
|
|
return $this->status === 'active'
|
|
&& $this->expires_at->isFuture()
|
|
&& $this->claimed_count < $this->total_count;
|
|
}
|
|
|
|
/**
|
|
* 剩余可领份数。
|
|
*/
|
|
public function remainingCount(): int
|
|
{
|
|
return max(0, $this->total_count - $this->claimed_count);
|
|
}
|
|
|
|
/**
|
|
* 剩余金额。
|
|
*/
|
|
public function remainingAmount(): int
|
|
{
|
|
return max(0, $this->total_amount - $this->claimed_amount);
|
|
}
|
|
}
|