|
|
@@ -14,23 +14,11 @@ use Illuminate\Database\Eloquent\Collection;
|
|
|
* 提供物品相关的服务,包括获取用户物品、添加物品到用户背包、消耗用户物品等功能。
|
|
|
* 该类是物品模块对外提供服务的主要入口,封装了物品操作的复杂逻辑,
|
|
|
* 通过调用ItemLogic类实现具体的业务逻辑处理。
|
|
|
+ *
|
|
|
+ * 所有方法均为静态方法,可直接通过类名调用。
|
|
|
*/
|
|
|
class ItemService
|
|
|
{
|
|
|
- /**
|
|
|
- * 物品逻辑类
|
|
|
- *
|
|
|
- * @var ItemLogic
|
|
|
- */
|
|
|
- private $itemLogic;
|
|
|
-
|
|
|
- /**
|
|
|
- * 构造函数
|
|
|
- */
|
|
|
- public function __construct()
|
|
|
- {
|
|
|
- $this->itemLogic = new ItemLogic();
|
|
|
- }
|
|
|
/**
|
|
|
* 获取用户物品列表
|
|
|
*
|
|
|
@@ -39,7 +27,7 @@ class ItemService
|
|
|
* @param bool $includeExpired 是否包含已过期物品
|
|
|
* @return Collection|ItemUser[]
|
|
|
*/
|
|
|
- public function getUserItems(int $userId, array $filters = [], bool $includeExpired = false): Collection
|
|
|
+ public static function getUserItems(int $userId, array $filters = [], bool $includeExpired = false): Collection
|
|
|
{
|
|
|
$query = ItemUser::where('user_id', $userId)
|
|
|
->with(['item', 'instance']);
|
|
|
@@ -88,23 +76,23 @@ class ItemService
|
|
|
* @return array 添加结果
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
- public function addItem(int $userId, int $itemId, int $quantity, array $options = []): array
|
|
|
+ public static function addItem(int $userId, int $itemId, int $quantity, array $options = []): array
|
|
|
{
|
|
|
// 获取物品信息
|
|
|
$item = Item::findOrFail($itemId);
|
|
|
|
|
|
// 检查物品是否已过期(全局过期)
|
|
|
- if ($this->itemLogic->isExpired($item)) {
|
|
|
+ if (ItemLogic::isExpired($item)) {
|
|
|
throw new Exception("物品 {$itemId} 已全局过期");
|
|
|
}
|
|
|
|
|
|
// 处理单独属性物品
|
|
|
if ($item->is_unique) {
|
|
|
- return $this->itemLogic->addUniqueItem($userId, $itemId, $options);
|
|
|
+ return ItemLogic::addUniqueItem($userId, $itemId, $options);
|
|
|
}
|
|
|
|
|
|
// 处理统一属性物品
|
|
|
- return $this->itemLogic->addNormalItem($userId, $itemId, $quantity, $options);
|
|
|
+ return ItemLogic::addNormalItem($userId, $itemId, $quantity, $options);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -118,19 +106,29 @@ class ItemService
|
|
|
* @return array 消耗结果
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
- public function consumeItem(int $userId, int $itemId, ?int $instanceId, int $quantity, array $options = []): array
|
|
|
+ public static function consumeItem(int $userId, int $itemId, ?int $instanceId, int $quantity, array $options = []): array
|
|
|
{
|
|
|
// 获取物品信息
|
|
|
$item = Item::findOrFail($itemId);
|
|
|
|
|
|
if ($instanceId) {
|
|
|
// 消耗单独属性物品
|
|
|
- return $this->itemLogic->consumeUniqueItem($userId, $itemId, $instanceId, $options);
|
|
|
+ return ItemLogic::consumeUniqueItem($userId, $itemId, $instanceId, $options);
|
|
|
} else {
|
|
|
// 消耗统一属性物品
|
|
|
- return $this->itemLogic->consumeNormalItem($userId, $itemId, $quantity, $options);
|
|
|
+ return ItemLogic::consumeNormalItem($userId, $itemId, $quantity, $options);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 获取物品信息
|
|
|
+ *
|
|
|
+ * @param int $itemId 物品ID
|
|
|
+ * @return Item|null 物品信息
|
|
|
+ */
|
|
|
+ public static function getItemInfo(int $itemId): ?Item
|
|
|
+ {
|
|
|
+ return Item::find($itemId);
|
|
|
+ }
|
|
|
|
|
|
}
|