| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261 |
- <?php
- namespace App\Module\Pet\Models;
- use UCore\ModelCore;
- use Illuminate\Database\Eloquent\Relations\BelongsTo;
- /**
- * 宠物激活技能模型
- *
- * 用于记录宠物当前激活的技能状态,支持定时任务自动处理
- *
- * @property int $id 主键ID
- * @property int $pet_id 宠物ID
- * @property int $skill_id 技能ID
- * @property string $skill_name 技能名称
- * @property \Carbon\Carbon $start_time 开始时间
- * @property \Carbon\Carbon $end_time 结束时间
- * @property string $status 状态:active-生效中,expired-已过期,cancelled-已取消
- * @property array $config 技能配置信息
- * @property \Carbon\Carbon $created_at 创建时间
- * @property \Carbon\Carbon $updated_at 更新时间
- */
- class PetActiveSkill extends ModelCore
- {
- /**
- * 数据表名
- *
- * @var string
- */
- protected $table = 'pet_active_skills';
- /**
- * 可批量赋值的属性
- *
- * @var array<int, string>
- */
- protected $fillable = [
- 'pet_id',
- 'skill_id',
- 'skill_name',
- 'start_time',
- 'end_time',
- 'status',
- 'config',
- ];
- /**
- * 属性类型转换
- *
- * @var array<string, string>
- */
- protected $casts = [
- 'start_time' => 'datetime',
- 'end_time' => 'datetime',
- 'config' => 'array',
- 'created_at' => 'datetime',
- 'updated_at' => 'datetime',
- ];
- /**
- * 技能状态常量
- */
- const STATUS_ACTIVE = 'active'; // 生效中
- const STATUS_EXPIRED = 'expired'; // 已过期
- const STATUS_CANCELLED = 'cancelled'; // 已取消
- /**
- * 关联宠物
- *
- * @return BelongsTo
- */
- public function pet(): BelongsTo
- {
- return $this->belongsTo(PetUser::class, 'pet_id');
- }
- /**
- * 关联技能配置
- *
- * @return BelongsTo
- */
- public function skill(): BelongsTo
- {
- return $this->belongsTo(PetSkill::class, 'skill_id');
- }
- /**
- * 检查技能是否仍然有效
- *
- * @return bool
- */
- public function isActive(): bool
- {
- return $this->status === self::STATUS_ACTIVE && $this->end_time > now();
- }
- /**
- * 检查技能是否已过期
- *
- * @return bool
- */
- public function isExpired(): bool
- {
- return $this->end_time <= now();
- }
- /**
- * 获取剩余时间(秒)
- *
- * @return int
- */
- public function getRemainingSeconds(): int
- {
- if ($this->isExpired()) {
- return 0;
- }
- return now()->diffInSeconds($this->end_time);
- }
- /**
- * 标记技能为已过期
- *
- * @return bool
- */
- public function markAsExpired(): bool
- {
- $this->status = self::STATUS_EXPIRED;
- return $this->save();
- }
- /**
- * 取消技能
- *
- * @return bool
- */
- public function cancel(): bool
- {
- $this->status = self::STATUS_CANCELLED;
- return $this->save();
- }
- /**
- * 更新最后检查时间
- *
- * @return bool
- */
- public function updateLastCheckTime(): bool
- {
- $config = $this->config;
- $config['last_check_time'] = now()->toDateTimeString();
- $this->config = $config;
- return $this->save();
- }
- /**
- * 获取最后检查时间
- *
- * @return \Carbon\Carbon|null
- */
- public function getLastCheckTime(): ?\Carbon\Carbon
- {
- $lastCheckTime = $this->config['last_check_time'] ?? null;
- return $lastCheckTime ? \Carbon\Carbon::parse($lastCheckTime) : null;
- }
- /**
- * 检查是否需要执行检查
- *
- * @return bool
- */
- public function shouldCheck(): bool
- {
- if (!$this->isActive()) {
- return false;
- }
- $lastCheckTime = $this->getLastCheckTime();
- if (!$lastCheckTime) {
- return true;
- }
- $checkInterval = $this->config['check_interval'] ?? 60; // 默认60秒
- return now()->diffInSeconds($lastCheckTime) >= $checkInterval;
- }
- /**
- * 获取技能配置中的特定值
- *
- * @param string $key 配置键名
- * @param mixed $default 默认值
- * @return mixed
- */
- public function getConfigValue(string $key, $default = null)
- {
- return $this->config[$key] ?? $default;
- }
- /**
- * 设置技能配置中的特定值
- *
- * @param string $key 配置键名
- * @param mixed $value 配置值
- * @return bool
- */
- public function setConfigValue(string $key, $value): bool
- {
- $config = $this->config;
- $config[$key] = $value;
- $this->config = $config;
- return $this->save();
- }
- /**
- * 查询生效中的技能
- *
- * @param \Illuminate\Database\Eloquent\Builder $query
- * @return \Illuminate\Database\Eloquent\Builder
- */
- public function scopeActive($query)
- {
- return $query->where('status', self::STATUS_ACTIVE)
- ->where('end_time', '>', now());
- }
- /**
- * 查询已过期的技能
- *
- * @param \Illuminate\Database\Eloquent\Builder $query
- * @return \Illuminate\Database\Eloquent\Builder
- */
- public function scopeExpired($query)
- {
- return $query->where('end_time', '<=', now());
- }
- /**
- * 查询特定技能类型
- *
- * @param \Illuminate\Database\Eloquent\Builder $query
- * @param string $skillName 技能名称
- * @return \Illuminate\Database\Eloquent\Builder
- */
- public function scopeBySkillName($query, string $skillName)
- {
- return $query->where('skill_name', $skillName);
- }
- /**
- * 查询特定宠物的技能
- *
- * @param \Illuminate\Database\Eloquent\Builder $query
- * @param int $petId 宠物ID
- * @return \Illuminate\Database\Eloquent\Builder
- */
- public function scopeByPet($query, int $petId)
- {
- return $query->where('pet_id', $petId);
- }
- }
|