ItemService.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. namespace App\Module\GameItems\Services;
  3. use App\Module\GameItems\Logics\Item as ItemLogic;
  4. use App\Module\GameItems\Models\Item;
  5. use App\Module\GameItems\Models\ItemUser;
  6. use Exception;
  7. use Illuminate\Database\Eloquent\Collection;
  8. /**
  9. * 物品服务类
  10. *
  11. * 提供物品相关的服务,包括获取用户物品、添加物品到用户背包、消耗用户物品等功能。
  12. * 该类是物品模块对外提供服务的主要入口,封装了物品操作的复杂逻辑,
  13. * 通过调用ItemLogic类实现具体的业务逻辑处理。
  14. *
  15. * 所有方法均为静态方法,可直接通过类名调用。
  16. */
  17. class ItemService
  18. {
  19. /**
  20. * 获取用户物品列表
  21. *
  22. * @param int $userId 用户ID
  23. * @param array $filters 过滤条件
  24. * @param bool $includeExpired 是否包含已过期物品
  25. * @return Collection|ItemUser[]
  26. */
  27. public static function getUserItems(int $userId, array $filters = [], bool $includeExpired = false): Collection
  28. {
  29. $query = ItemUser::where('user_id', $userId)
  30. ->with(['item', 'instance']);
  31. // 应用过滤条件
  32. if (isset($filters['item_id'])) {
  33. $query->where('item_id', $filters['item_id']);
  34. }
  35. if (isset($filters['category_id'])) {
  36. $query->whereHas('item', function ($q) use ($filters) {
  37. $q->where('category_id', $filters['category_id']);
  38. });
  39. }
  40. if (isset($filters['type'])) {
  41. $query->whereHas('item', function ($q) use ($filters) {
  42. $q->where('type', $filters['type']);
  43. });
  44. }
  45. // 排除过期物品
  46. if (!$includeExpired) {
  47. $now = now();
  48. $query->where(function ($q) use ($now) {
  49. $q->whereNull('expire_at')
  50. ->orWhere('expire_at', '>', $now);
  51. })->whereHas('item', function ($q) use ($now) {
  52. $q->where(function ($subQ) use ($now) {
  53. $subQ->whereNull('global_expire_at')
  54. ->orWhere('global_expire_at', '>', $now);
  55. });
  56. });
  57. }
  58. return $query->get();
  59. }
  60. /**
  61. * 添加物品到用户背包
  62. *
  63. * @param int $userId 用户ID
  64. * @param int $itemId 物品ID
  65. * @param int $quantity 数量
  66. * @param array $options 选项
  67. * @return array 添加结果
  68. * @throws Exception
  69. */
  70. public static function addItem(int $userId, int $itemId, int $quantity, array $options = []): array
  71. {
  72. // 获取物品信息
  73. $item = Item::findOrFail($itemId);
  74. // 检查物品是否已过期(全局过期)
  75. if (ItemLogic::isExpired($item)) {
  76. throw new Exception("物品 {$itemId} 已全局过期");
  77. }
  78. // 处理单独属性物品
  79. if ($item->is_unique) {
  80. return ItemLogic::addUniqueItem($userId, $itemId, $options);
  81. }
  82. // 处理统一属性物品
  83. return ItemLogic::addNormalItem($userId, $itemId, $quantity, $options);
  84. }
  85. /**
  86. * 消耗用户物品
  87. *
  88. * @param int $userId 用户ID
  89. * @param int $itemId 物品ID
  90. * @param int|null $instanceId 物品实例ID(单独属性物品)
  91. * @param int $quantity 数量
  92. * @param array $options 选项
  93. * @return array 消耗结果
  94. * @throws Exception
  95. */
  96. public static function consumeItem(int $userId, int $itemId, ?int $instanceId, int $quantity, array $options = []): array
  97. {
  98. // 获取物品信息
  99. $item = Item::findOrFail($itemId);
  100. if ($instanceId) {
  101. // 消耗单独属性物品
  102. return ItemLogic::consumeUniqueItem($userId, $itemId, $instanceId, $options);
  103. } else {
  104. // 消耗统一属性物品
  105. return ItemLogic::consumeNormalItem($userId, $itemId, $quantity, $options);
  106. }
  107. }
  108. /**
  109. * 获取物品信息
  110. *
  111. * @param int $itemId 物品ID
  112. * @return Item|null 物品信息
  113. */
  114. public static function getItemInfo(int $itemId): ?Item
  115. {
  116. return Item::find($itemId);
  117. }
  118. }