| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- <?php
- namespace App\Module\GameItems\Logics;
- use App\Module\GameItems\Models\ItemUser;
- use Exception;
- /**
- * 物品数量逻辑类
- *
- * 提供物品数量相关的逻辑处理,支持普通物品和实例物品
- */
- class ItemQuantity
- {
- /**
- * 获取用户物品数量
- *
- * @param int $userId 用户ID
- * @param int $itemId 物品ID
- * @param bool $includeExpired 是否包含已过期物品
- * @return int 物品数量
- * @throws Exception 如果查询过程中发生错误
- */
- public static function getUserItemQuantity(int $userId, int $itemId, bool $includeExpired = false): int
- {
- try {
- $query = ItemUser::where('user_id', $userId)
- ->where('item_id', $itemId);
- // 排除过期物品
- if (!$includeExpired) {
- $now = now();
- $query->where(function ($q) use ($now) {
- $q->whereNull('expire_at')
- ->orWhere('expire_at', '>', $now);
- })->whereHas('item', function ($q) use ($now) {
- $q->where(function ($subQ) use ($now) {
- $subQ->whereNull('global_expire_at')
- ->orWhere('global_expire_at', '>', $now);
- });
- });
- }
- return $query->sum('quantity');
- } catch (Exception $e) {
- throw new Exception("获取用户物品数量失败: " . $e->getMessage(), 0, $e);
- }
- }
- /**
- * 获取用户实例物品数量
- *
- * @param int $userId 用户ID
- * @param int $itemId 物品ID
- * @param int|null $instanceId 实例ID,如果为null则获取所有实例
- * @param bool $includeExpired 是否包含已过期物品
- * @return int 物品数量
- * @throws Exception 如果查询过程中发生错误
- */
- public static function getUserInstanceItemQuantity(int $userId, int $itemId, ?int $instanceId = null, bool $includeExpired = false): int
- {
- try {
- $query = ItemUser::where('user_id', $userId)
- ->where('item_id', $itemId)
- ->whereNotNull('instance_id');
- // 如果指定了实例ID,则只查询该实例
- if ($instanceId !== null) {
- $query->where('instance_id', $instanceId);
- }
- // 排除过期物品
- if (!$includeExpired) {
- $now = now();
- $query->where(function ($q) use ($now) {
- $q->whereNull('expire_at')
- ->orWhere('expire_at', '>', $now);
- })->whereHas('item', function ($q) use ($now) {
- $q->where(function ($subQ) use ($now) {
- $subQ->whereNull('global_expire_at')
- ->orWhere('global_expire_at', '>', $now);
- });
- });
- }
- return $query->sum('quantity');
- } catch (Exception $e) {
- throw new Exception("获取用户实例物品数量失败: " . $e->getMessage(), 0, $e);
- }
- }
- /**
- * 获取用户物品总数量(包括普通物品和实例物品)
- *
- * @param int $userId 用户ID
- * @param int $itemId 物品ID
- * @param bool $includeExpired 是否包含已过期物品
- * @return int 物品总数量
- * @throws Exception 如果查询过程中发生错误
- */
- public static function getUserTotalItemQuantity(int $userId, int $itemId, bool $includeExpired = false): int
- {
- $normalQuantity = self::getUserItemQuantity($userId, $itemId, $includeExpired);
- $instanceQuantity = self::getUserInstanceItemQuantity($userId, $itemId, null, $includeExpired);
- return $normalQuantity + $instanceQuantity;
- }
- /**
- * 检查用户是否拥有足够数量的普通物品
- *
- * @param int $userId 用户ID
- * @param int $itemId 物品ID
- * @param int $quantity 需要的数量
- * @param bool $includeExpired 是否包含已过期物品
- * @return bool 是否拥有足够数量
- * @throws Exception 如果查询过程中发生错误
- */
- public static function hasEnoughItems(int $userId, int $itemId, int $quantity, bool $includeExpired = false): bool
- {
- $userQuantity = self::getUserItemQuantity($userId, $itemId, $includeExpired);
- return $userQuantity >= $quantity;
- }
- /**
- * 检查用户是否拥有指定的实例物品
- *
- * @param int $userId 用户ID
- * @param int $itemId 物品ID
- * @param int $instanceId 实例ID
- * @param bool $includeExpired 是否包含已过期物品
- * @return bool 是否拥有该实例物品
- * @throws Exception 如果查询过程中发生错误
- */
- public static function hasInstanceItem(int $userId, int $itemId, int $instanceId, bool $includeExpired = false): bool
- {
- $quantity = self::getUserInstanceItemQuantity($userId, $itemId, $instanceId, $includeExpired);
- return $quantity > 0;
- }
- /**
- * 检查用户是否拥有足够数量的物品(包括普通物品和实例物品)
- *
- * @param int $userId 用户ID
- * @param int $itemId 物品ID
- * @param int $quantity 需要的数量
- * @param bool $includeExpired 是否包含已过期物品
- * @return bool 是否拥有足够数量
- * @throws Exception 如果查询过程中发生错误
- */
- public static function hasEnoughTotalItems(int $userId, int $itemId, int $quantity, bool $includeExpired = false): bool
- {
- $totalQuantity = self::getUserTotalItemQuantity($userId, $itemId, $includeExpired);
- return $totalQuantity >= $quantity;
- }
- }
|