ItemUser.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. namespace App\Module\GameItems\Models;
  3. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  4. use UCore\ModelCore;
  5. /**
  6. * 用户物品关联
  7. *
  8. * field start
  9. * @property int $id 记录ID,主键
  10. * @property int $user_id 用户ID
  11. * @property int $item_id 统一属性物品ID,外键关联kku_item_items表
  12. * @property int $instance_id 单独属性物品ID,外键关联kku_item_instances表(可为空)
  13. * @property int $quantity 数量(对于单独属性物品,该值始终为1)
  14. * @property string $expire_at 用户物品过期时间(可为空)
  15. * @property \Carbon\Carbon $created_at 获取时间
  16. * @property \Carbon\Carbon $updated_at 更新时间
  17. * @property bool $is_frozen 是否冻结(0:未冻结, 1:已冻结)
  18. * @property int $frozen_log_id 冻结日志ID,关联kku_item_freeze_logs表
  19. * field end
  20. */
  21. class ItemUser extends ModelCore
  22. {
  23. /**
  24. * 与模型关联的表名
  25. *
  26. * @var string
  27. */
  28. protected $table = 'item_users';
  29. // attrlist start
  30. protected $fillable = [
  31. 'id',
  32. 'user_id',
  33. 'item_id',
  34. 'instance_id',
  35. 'quantity',
  36. 'expire_at',
  37. 'is_frozen',
  38. 'frozen_log_id',
  39. ];
  40. // attrlist end
  41. /**
  42. * 应该被转换为日期的属性
  43. *
  44. * @var array
  45. */
  46. protected $dates = [
  47. 'expire_at',
  48. 'created_at',
  49. 'updated_at',
  50. ];
  51. /**
  52. * 应该被转换为原生类型的属性
  53. *
  54. * @var array
  55. */
  56. protected $casts = [
  57. 'is_frozen' => 'boolean',
  58. 'frozen_log_id' => 'integer',
  59. ];
  60. /**
  61. * 获取关联的物品
  62. *
  63. * @return BelongsTo
  64. */
  65. public function item(): BelongsTo
  66. {
  67. return $this->belongsTo(Item::class, 'item_id');
  68. }
  69. /**
  70. * 获取关联的物品实例(如果有)
  71. *
  72. * @return BelongsTo
  73. */
  74. public function instance(): BelongsTo
  75. {
  76. return $this->belongsTo(ItemInstance::class, 'instance_id');
  77. }
  78. /**
  79. * 获取关联的冻结日志
  80. *
  81. * @return BelongsTo
  82. */
  83. public function freezeLog(): BelongsTo
  84. {
  85. return $this->belongsTo(ItemFreezeLog::class, 'frozen_log_id');
  86. }
  87. /**
  88. * 检查是否为单独属性物品
  89. *
  90. * @return bool
  91. */
  92. public function isUniqueItem(): bool
  93. {
  94. return !empty($this->instance_id);
  95. }
  96. /**
  97. * 检查是否为冻结状态
  98. *
  99. * @return bool
  100. */
  101. public function isFrozen(): bool
  102. {
  103. return $this->is_frozen;
  104. }
  105. /**
  106. * 检查是否可用(未冻结)
  107. *
  108. * @return bool
  109. */
  110. public function isAvailable(): bool
  111. {
  112. return !$this->is_frozen;
  113. }
  114. /**
  115. * 获取用户指定物品的可用数量(排除冻结的堆叠)
  116. *
  117. * @param int $userId 用户ID
  118. * @param int $itemId 物品ID
  119. * @param int|null $instanceId 实例ID
  120. * @return int 可用数量
  121. */
  122. public static function getAvailableQuantity(int $userId, int $itemId, ?int $instanceId = null): int
  123. {
  124. $query = static::where('user_id', $userId)
  125. ->where('item_id', $itemId)
  126. ->where('is_frozen', false);
  127. if ($instanceId) {
  128. $query->where('instance_id', $instanceId);
  129. } else {
  130. $query->whereNull('instance_id');
  131. }
  132. return $query->sum('quantity');
  133. }
  134. /**
  135. * 获取用户指定物品的冻结数量
  136. *
  137. * @param int $userId 用户ID
  138. * @param int $itemId 物品ID
  139. * @param int|null $instanceId 实例ID
  140. * @return int 冻结数量
  141. */
  142. public static function getFrozenQuantity(int $userId, int $itemId, ?int $instanceId = null): int
  143. {
  144. $query = static::where('user_id', $userId)
  145. ->where('item_id', $itemId)
  146. ->where('is_frozen', true);
  147. if ($instanceId) {
  148. $query->where('instance_id', $instanceId);
  149. } else {
  150. $query->whereNull('instance_id');
  151. }
  152. return $query->sum('quantity');
  153. }
  154. /**
  155. * 获取用户指定物品的总数量(包括冻结的)
  156. *
  157. * @param int $userId 用户ID
  158. * @param int $itemId 物品ID
  159. * @param int|null $instanceId 实例ID
  160. * @return int 总数量
  161. */
  162. public static function getTotalQuantity(int $userId, int $itemId, ?int $instanceId = null): int
  163. {
  164. $query = static::where('user_id', $userId)
  165. ->where('item_id', $itemId);
  166. if ($instanceId) {
  167. $query->where('instance_id', $instanceId);
  168. } else {
  169. $query->whereNull('instance_id');
  170. }
  171. return $query->sum('quantity');
  172. }
  173. }