ItemUser.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 user(): BelongsTo
  66. {
  67. return $this->belongsTo(\App\Module\User\Models\User::class, 'user_id');
  68. }
  69. /**
  70. * 获取关联的物品
  71. *
  72. * @return BelongsTo
  73. */
  74. public function item(): BelongsTo
  75. {
  76. return $this->belongsTo(Item::class, 'item_id');
  77. }
  78. /**
  79. * 获取关联的物品实例(如果有)
  80. *
  81. * @return BelongsTo
  82. */
  83. public function instance(): BelongsTo
  84. {
  85. return $this->belongsTo(ItemInstance::class, 'instance_id');
  86. }
  87. /**
  88. * 获取关联的冻结日志
  89. *
  90. * @return BelongsTo
  91. */
  92. public function freezeLog(): BelongsTo
  93. {
  94. return $this->belongsTo(ItemFreezeLog::class, 'frozen_log_id');
  95. }
  96. /**
  97. * 检查是否为单独属性物品
  98. *
  99. * @return bool
  100. */
  101. public function isUniqueItem(): bool
  102. {
  103. return !empty($this->instance_id);
  104. }
  105. /**
  106. * 检查是否为冻结状态
  107. *
  108. * @return bool
  109. */
  110. public function isFrozen(): bool
  111. {
  112. return $this->is_frozen;
  113. }
  114. /**
  115. * 检查是否可用(未冻结)
  116. *
  117. * @return bool
  118. */
  119. public function isAvailable(): bool
  120. {
  121. return !$this->is_frozen;
  122. }
  123. /**
  124. * 获取用户指定物品的可用数量(排除冻结的堆叠)
  125. *
  126. * @param int $userId 用户ID
  127. * @param int $itemId 物品ID
  128. * @param int|null $instanceId 实例ID
  129. * @return int 可用数量
  130. */
  131. public static function getAvailableQuantity(int $userId, int $itemId, ?int $instanceId = null): int
  132. {
  133. $query = static::where('user_id', $userId)
  134. ->where('item_id', $itemId)
  135. ->where('is_frozen', false);
  136. if ($instanceId) {
  137. $query->where('instance_id', $instanceId);
  138. } else {
  139. $query->whereNull('instance_id');
  140. }
  141. return $query->sum('quantity');
  142. }
  143. /**
  144. * 获取用户指定物品的冻结数量
  145. *
  146. * @param int $userId 用户ID
  147. * @param int $itemId 物品ID
  148. * @param int|null $instanceId 实例ID
  149. * @return int 冻结数量
  150. */
  151. public static function getFrozenQuantity(int $userId, int $itemId, ?int $instanceId = null): int
  152. {
  153. $query = static::where('user_id', $userId)
  154. ->where('item_id', $itemId)
  155. ->where('is_frozen', true);
  156. if ($instanceId) {
  157. $query->where('instance_id', $instanceId);
  158. } else {
  159. $query->whereNull('instance_id');
  160. }
  161. return $query->sum('quantity');
  162. }
  163. /**
  164. * 获取用户指定物品的总数量(包括冻结的)
  165. *
  166. * @param int $userId 用户ID
  167. * @param int $itemId 物品ID
  168. * @param int|null $instanceId 实例ID
  169. * @return int 总数量
  170. */
  171. public static function getTotalQuantity(int $userId, int $itemId, ?int $instanceId = null): int
  172. {
  173. $query = static::where('user_id', $userId)
  174. ->where('item_id', $itemId);
  175. if ($instanceId) {
  176. $query->where('instance_id', $instanceId);
  177. } else {
  178. $query->whereNull('instance_id');
  179. }
  180. return $query->sum('quantity');
  181. }
  182. }