PetActiveSkill.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. <?php
  2. namespace App\Module\Pet\Models;
  3. use UCore\ModelCore;
  4. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  5. /**
  6. * 宠物激活技能模型
  7. *
  8. * 用于记录宠物当前激活的技能状态,支持定时任务自动处理
  9. *
  10. * @property int $id 主键ID
  11. * @property int $pet_id 宠物ID
  12. * @property int $skill_id 技能ID
  13. * @property string $skill_name 技能名称
  14. * @property \Carbon\Carbon $start_time 开始时间
  15. * @property \Carbon\Carbon $end_time 结束时间
  16. * @property string $status 状态:active-生效中,expired-已过期,cancelled-已取消
  17. * @property array $config 技能配置信息
  18. * @property \Carbon\Carbon $created_at 创建时间
  19. * @property \Carbon\Carbon $updated_at 更新时间
  20. */
  21. class PetActiveSkill extends ModelCore
  22. {
  23. /**
  24. * 数据表名
  25. *
  26. * @var string
  27. */
  28. protected $table = 'pet_active_skills';
  29. /**
  30. * 可批量赋值的属性
  31. *
  32. * @var array<int, string>
  33. */
  34. protected $fillable = [
  35. 'pet_id',
  36. 'skill_id',
  37. 'skill_name',
  38. 'start_time',
  39. 'end_time',
  40. 'status',
  41. 'config',
  42. ];
  43. /**
  44. * 属性类型转换
  45. *
  46. * @var array<string, string>
  47. */
  48. protected $casts = [
  49. 'start_time' => 'datetime',
  50. 'end_time' => 'datetime',
  51. 'config' => 'array',
  52. 'created_at' => 'datetime',
  53. 'updated_at' => 'datetime',
  54. ];
  55. /**
  56. * 技能状态常量
  57. */
  58. const STATUS_ACTIVE = 'active'; // 生效中
  59. const STATUS_EXPIRED = 'expired'; // 已过期
  60. const STATUS_CANCELLED = 'cancelled'; // 已取消
  61. /**
  62. * 关联宠物
  63. *
  64. * @return BelongsTo
  65. */
  66. public function pet(): BelongsTo
  67. {
  68. return $this->belongsTo(PetUser::class, 'pet_id');
  69. }
  70. /**
  71. * 关联技能配置
  72. *
  73. * @return BelongsTo
  74. */
  75. public function skill(): BelongsTo
  76. {
  77. return $this->belongsTo(PetSkill::class, 'skill_id');
  78. }
  79. /**
  80. * 检查技能是否仍然有效
  81. *
  82. * @return bool
  83. */
  84. public function isActive(): bool
  85. {
  86. return $this->status === self::STATUS_ACTIVE && $this->end_time > now();
  87. }
  88. /**
  89. * 检查技能是否已过期
  90. *
  91. * @return bool
  92. */
  93. public function isExpired(): bool
  94. {
  95. return $this->end_time <= now();
  96. }
  97. /**
  98. * 获取剩余时间(秒)
  99. *
  100. * @return int
  101. */
  102. public function getRemainingSeconds(): int
  103. {
  104. if ($this->isExpired()) {
  105. return 0;
  106. }
  107. return now()->diffInSeconds($this->end_time);
  108. }
  109. /**
  110. * 标记技能为已过期
  111. *
  112. * @return bool
  113. */
  114. public function markAsExpired(): bool
  115. {
  116. $this->status = self::STATUS_EXPIRED;
  117. return $this->save();
  118. }
  119. /**
  120. * 取消技能
  121. *
  122. * @return bool
  123. */
  124. public function cancel(): bool
  125. {
  126. $this->status = self::STATUS_CANCELLED;
  127. return $this->save();
  128. }
  129. /**
  130. * 更新最后检查时间
  131. *
  132. * @return bool
  133. */
  134. public function updateLastCheckTime(): bool
  135. {
  136. $config = $this->config;
  137. $config['last_check_time'] = now()->toDateTimeString();
  138. $this->config = $config;
  139. return $this->save();
  140. }
  141. /**
  142. * 获取最后检查时间
  143. *
  144. * @return \Carbon\Carbon|null
  145. */
  146. public function getLastCheckTime(): ?\Carbon\Carbon
  147. {
  148. $lastCheckTime = $this->config['last_check_time'] ?? null;
  149. return $lastCheckTime ? \Carbon\Carbon::parse($lastCheckTime) : null;
  150. }
  151. /**
  152. * 检查是否需要执行检查
  153. *
  154. * @return bool
  155. */
  156. public function shouldCheck(): bool
  157. {
  158. if (!$this->isActive()) {
  159. return false;
  160. }
  161. $lastCheckTime = $this->getLastCheckTime();
  162. if (!$lastCheckTime) {
  163. return true;
  164. }
  165. $checkInterval = $this->config['check_interval'] ?? 60; // 默认60秒
  166. return now()->diffInSeconds($lastCheckTime) >= $checkInterval;
  167. }
  168. /**
  169. * 获取技能配置中的特定值
  170. *
  171. * @param string $key 配置键名
  172. * @param mixed $default 默认值
  173. * @return mixed
  174. */
  175. public function getConfigValue(string $key, $default = null)
  176. {
  177. return $this->config[$key] ?? $default;
  178. }
  179. /**
  180. * 设置技能配置中的特定值
  181. *
  182. * @param string $key 配置键名
  183. * @param mixed $value 配置值
  184. * @return bool
  185. */
  186. public function setConfigValue(string $key, $value): bool
  187. {
  188. $config = $this->config;
  189. $config[$key] = $value;
  190. $this->config = $config;
  191. return $this->save();
  192. }
  193. /**
  194. * 查询生效中的技能
  195. *
  196. * @param \Illuminate\Database\Eloquent\Builder $query
  197. * @return \Illuminate\Database\Eloquent\Builder
  198. */
  199. public function scopeActive($query)
  200. {
  201. return $query->where('status', self::STATUS_ACTIVE)
  202. ->where('end_time', '>', now());
  203. }
  204. /**
  205. * 查询已过期的技能
  206. *
  207. * @param \Illuminate\Database\Eloquent\Builder $query
  208. * @return \Illuminate\Database\Eloquent\Builder
  209. */
  210. public function scopeExpired($query)
  211. {
  212. return $query->where('end_time', '<=', now());
  213. }
  214. /**
  215. * 查询特定技能类型
  216. *
  217. * @param \Illuminate\Database\Eloquent\Builder $query
  218. * @param string $skillName 技能名称
  219. * @return \Illuminate\Database\Eloquent\Builder
  220. */
  221. public function scopeBySkillName($query, string $skillName)
  222. {
  223. return $query->where('skill_name', $skillName);
  224. }
  225. /**
  226. * 查询特定宠物的技能
  227. *
  228. * @param \Illuminate\Database\Eloquent\Builder $query
  229. * @param int $petId 宠物ID
  230. * @return \Illuminate\Database\Eloquent\Builder
  231. */
  232. public function scopeByPet($query, int $petId)
  233. {
  234. return $query->where('pet_id', $petId);
  235. }
  236. }