ItemService.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. namespace App\Module\GameItems\Services;
  3. use App\Module\GameItems\Logics\Item as ItemLogic;
  4. use App\Module\GameItems\Models\ItemItem;
  5. use App\Module\GameItems\Models\ItemUser;
  6. use Exception;
  7. use Illuminate\Database\Eloquent\Collection;
  8. class ItemService
  9. {
  10. /**
  11. * 物品逻辑类
  12. *
  13. * @var ItemLogic
  14. */
  15. private $itemLogic;
  16. /**
  17. * 构造函数
  18. */
  19. public function __construct()
  20. {
  21. $this->itemLogic = new ItemLogic();
  22. }
  23. /**
  24. * 获取用户物品列表
  25. *
  26. * @param int $userId 用户ID
  27. * @param array $filters 过滤条件
  28. * @param bool $includeExpired 是否包含已过期物品
  29. * @return Collection
  30. */
  31. public function getUserItems(int $userId, array $filters = [], bool $includeExpired = false): Collection
  32. {
  33. $query = ItemUser::where('user_id', $userId)
  34. ->with(['item', 'instance']);
  35. // 应用过滤条件
  36. if (isset($filters['item_id'])) {
  37. $query->where('item_id', $filters['item_id']);
  38. }
  39. if (isset($filters['category_id'])) {
  40. $query->whereHas('item', function ($q) use ($filters) {
  41. $q->where('category_id', $filters['category_id']);
  42. });
  43. }
  44. if (isset($filters['type'])) {
  45. $query->whereHas('item', function ($q) use ($filters) {
  46. $q->where('type', $filters['type']);
  47. });
  48. }
  49. // 排除过期物品
  50. if (!$includeExpired) {
  51. $now = now();
  52. $query->where(function ($q) use ($now) {
  53. $q->whereNull('expire_at')
  54. ->orWhere('expire_at', '>', $now);
  55. })->whereHas('item', function ($q) use ($now) {
  56. $q->where(function ($subQ) use ($now) {
  57. $subQ->whereNull('global_expire_at')
  58. ->orWhere('global_expire_at', '>', $now);
  59. });
  60. });
  61. }
  62. return $query->get();
  63. }
  64. /**
  65. * 添加物品到用户背包
  66. *
  67. * @param int $userId 用户ID
  68. * @param int $itemId 物品ID
  69. * @param int $quantity 数量
  70. * @param array $options 选项
  71. * @return array 添加结果
  72. * @throws Exception
  73. */
  74. public function addItem(int $userId, int $itemId, int $quantity, array $options = []): array
  75. {
  76. // 获取物品信息
  77. $item = ItemItem::findOrFail($itemId);
  78. // 检查物品是否已过期(全局过期)
  79. if ($this->itemLogic->isExpired($item)) {
  80. throw new Exception("物品 {$itemId} 已全局过期");
  81. }
  82. // 处理单独属性物品
  83. if ($item->is_unique) {
  84. return $this->itemLogic->addUniqueItem($userId, $itemId, $options);
  85. }
  86. // 处理统一属性物品
  87. return $this->itemLogic->addNormalItem($userId, $itemId, $quantity, $options);
  88. }
  89. /**
  90. * 消耗用户物品
  91. *
  92. * @param int $userId 用户ID
  93. * @param int $itemId 物品ID
  94. * @param int|null $instanceId 物品实例ID(单独属性物品)
  95. * @param int $quantity 数量
  96. * @param array $options 选项
  97. * @return array 消耗结果
  98. * @throws Exception
  99. */
  100. public function consumeItem(int $userId, int $itemId, ?int $instanceId, int $quantity, array $options = []): array
  101. {
  102. // 获取物品信息
  103. $item = ItemItem::findOrFail($itemId);
  104. if ($instanceId) {
  105. // 消耗单独属性物品
  106. return $this->itemLogic->consumeUniqueItem($userId, $itemId, $instanceId, $options);
  107. } else {
  108. // 消耗统一属性物品
  109. return $this->itemLogic->consumeNormalItem($userId, $itemId, $quantity, $options);
  110. }
  111. }
  112. }