PetLevelConfig.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. namespace App\Module\Pet\Models;
  3. use UCore\ModelCore;
  4. use App\Module\Pet\Casts\DisplayAttributesCast;
  5. use App\Module\Pet\Casts\NumericAttributesCast;
  6. use App\Module\Pet\Models\PetSkill;
  7. use App\Module\Pet\Models\PetUser;
  8. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  9. /**
  10. * 宠物等级配置模型
  11. *
  12. * field start
  13. * @property int $id
  14. * @property int $pet_id 宠物 ID
  15. * @property int $level 等级
  16. * @property int $exp_required 升级所需经验值
  17. * @property array $skills 可用技能
  18. * @property \App\Module\Pet\Casts\DisplayAttributesCast $display_attributes 等级显示属性配置
  19. * @property \App\Module\Pet\Casts\NumericAttributesCast $numeric_attributes 等级数值属性配置
  20. * @property \Carbon\Carbon $created_at
  21. * @property \Carbon\Carbon $updated_at
  22. * field end
  23. */
  24. class PetLevelConfig extends ModelCore
  25. {
  26. /**
  27. * 与模型关联的表名
  28. *
  29. * @var string
  30. */
  31. protected $table = 'pet_level_configs';
  32. // attrlist start
  33. protected $fillable = [
  34. 'id',
  35. 'pet_id',
  36. 'level',
  37. 'exp_required',
  38. 'skills',
  39. 'display_attributes',
  40. 'numeric_attributes',
  41. ];
  42. // attrlist end
  43. /**
  44. * 应该被转换为原生类型的属性
  45. *
  46. * @var array
  47. */
  48. protected $casts = [
  49. 'level' => 'integer',
  50. 'exp_required' => 'integer',
  51. 'skills' => 'array',
  52. 'display_attributes' => DisplayAttributesCast::class,
  53. 'numeric_attributes' => NumericAttributesCast::class,
  54. ];
  55. /**
  56. * 获取关联的宠物
  57. *
  58. * @return BelongsTo
  59. */
  60. public function pet(): BelongsTo
  61. {
  62. return $this->belongsTo(PetUser::class, 'pet_id');
  63. }
  64. /**
  65. * 获取该等级可用的技能
  66. *
  67. * 通过 skills 字段中存储的技能ID数组关联到 PetSkill 模型
  68. *
  69. * @return \Illuminate\Database\Eloquent\Collection
  70. */
  71. public function getAvailableSkills()
  72. {
  73. if (empty($this->skills)) {
  74. return collect();
  75. }
  76. return PetSkill::whereIn('id', $this->skills)->get();
  77. }
  78. /**
  79. * 获取技能列表的访问器(带链接)
  80. *
  81. * @return string
  82. */
  83. public function getSkillsListAttribute()
  84. {
  85. $skills = $this->getAvailableSkills();
  86. if ($skills->isEmpty()) {
  87. return '<span class="text-muted">无可用技能</span>';
  88. }
  89. $skillLinks = [];
  90. foreach ($skills as $skill) {
  91. // 生成技能详情页面的链接
  92. $detailUrl = admin_url("pet-skills/{$skill->id}");
  93. $skillLinks[] = "<a href=\"{$detailUrl}\" target=\"_blank\" class=\"btn btn-xs btn-primary\" style=\"margin: 2px;\">" .
  94. "<i class=\"fa fa-eye\"></i> {$skill->skill_name}</a>";
  95. }
  96. return implode(' ', $skillLinks);
  97. }
  98. /**
  99. * 检查指定技能是否在该等级可用
  100. *
  101. * @param int $skillId 技能ID
  102. * @return bool
  103. */
  104. public function hasSkill(int $skillId): bool
  105. {
  106. return !empty($this->skills) && in_array($skillId, $this->skills);
  107. }
  108. }