Item.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. namespace App\Module\GameItems\Models;
  3. use App\Module\GameItems\Enums\ITEM_TYPE;
  4. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  5. use Illuminate\Database\Eloquent\Relations\HasMany;
  6. use Illuminate\Database\Eloquent\Relations\HasOne;
  7. use UCore\ModelCore;
  8. /**
  9. * 物品基础信息
  10. *
  11. * field start
  12. * @property int $id 物品ID,主键
  13. * @property string $name 物品名称
  14. * @property string $description 物品描述
  15. * @property int $category_id 物品分类ID,外键关联kku_item_categories表
  16. * @property \App\Module\GameItems\Enums\ITEM_TYPE $type 物品类型(1:可使用, 2:可装备, 3:可合成, 4:可交任务, 5:可开启...)
  17. * @property bool $is_unique 是否是单独属性物品(0:否,默认, 1:是)
  18. * @property int $max_stack 最大堆叠数量(0表示无限堆叠)
  19. * @property int $sell_price 出售价格
  20. * @property bool $tradable 是否可交易(0:不可交易, 1:可交易,默认)
  21. * @property bool $dismantlable 是否可分解(0:不可分解, 1:可分解,默认)
  22. * @property int $default_expire_seconds 玩家获取物品后的默认有效秒数(0表示永久有效)
  23. * @property \App\Module\GameItems\Casts\DisplayAttributesCast $display_attributes 展示属性,以JSON格式存储键值对,用于界面展示和描述的属性
  24. * @property \App\Module\GameItems\Casts\NumericAttributesCast $numeric_attributes 数值属性,以JSON格式存储键值对,用于计算和游戏逻辑的属性
  25. * @property string $global_expire_at 物品全局过期时间(可为空)
  26. * @property int $chest_consume_group_id 宝箱开启消耗组ID(仅宝箱类型物品使用)
  27. * @property int $chest_reward_group_id 宝箱开启奖励组ID(仅宝箱类型物品使用)
  28. * @property \Carbon\Carbon $created_at 创建时间
  29. * @property \Carbon\Carbon $updated_at 更新时间
  30. * field end
  31. */
  32. class Item extends ModelCore
  33. {
  34. /**
  35. * 与模型关联的表名
  36. *
  37. * @var string
  38. */
  39. protected $table = 'item_items';
  40. // attrlist start
  41. protected $fillable = [
  42. 'id',
  43. 'name',
  44. 'description',
  45. 'category_id',
  46. 'type',
  47. 'is_unique',
  48. 'max_stack',
  49. 'sell_price',
  50. 'tradable',
  51. 'dismantlable',
  52. 'default_expire_seconds',
  53. 'display_attributes',
  54. 'numeric_attributes',
  55. 'global_expire_at',
  56. 'chest_consume_group_id',
  57. 'chest_reward_group_id',
  58. ];
  59. // attrlist end
  60. /**
  61. * 应该被转换为日期的属性
  62. *
  63. * @var array
  64. */
  65. protected $dates = [
  66. 'global_expire_at',
  67. 'created_at',
  68. 'updated_at',
  69. ];
  70. /**
  71. * 应该被转换为原生类型的属性
  72. *
  73. * @var array
  74. */
  75. protected $casts = [
  76. 'display_attributes' => \App\Module\GameItems\Casts\DisplayAttributesCast::class,
  77. 'numeric_attributes' => \App\Module\GameItems\Casts\NumericAttributesCast::class,
  78. 'type' => ITEM_TYPE::class,
  79. 'is_unique' => 'boolean',
  80. 'max_stack' => 'integer',
  81. 'sell_price' => 'integer',
  82. 'default_expire_seconds' => 'integer',
  83. 'tradable' => 'boolean',
  84. 'dismantlable' => 'boolean',
  85. ];
  86. /**
  87. * 获取物品所属分类
  88. *
  89. * @return BelongsTo
  90. */
  91. public function category(): BelongsTo
  92. {
  93. return $this->belongsTo(ItemCategory::class, 'category_id');
  94. }
  95. /**
  96. * 获取物品的所有实例
  97. *
  98. * @return HasMany
  99. */
  100. public function instances(): HasMany
  101. {
  102. return $this->hasMany(ItemInstance::class, 'item_id');
  103. }
  104. /**
  105. * 获取拥有该物品的用户
  106. *
  107. * @return HasMany
  108. */
  109. public function users(): HasMany
  110. {
  111. return $this->hasMany(ItemUser::class, 'item_id');
  112. }
  113. /**
  114. * 宝箱配置(新系统)
  115. *
  116. * @return HasMany
  117. */
  118. public function chestConfig(): HasOne
  119. {
  120. return $this->HasOne(\App\Module\GameItems\Models\ItemChestConfig::class, 'item_id', 'id');
  121. }
  122. }