| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- <?php
- namespace App\Module\Pet\Validators;
- use App\Module\Pet\Models\PetConfig;
- use App\Module\Pet\Models\PetUser;
- use UCore\Validator;
- /**
- * 宠物获取验证器
- *
- * 验证宠物获取操作是否有效,包括物品拥有、宠物种类配置、数量限制等
- */
- class PetGetValidator extends Validator
- {
- /**
- * 验证宠物获取操作
- *
- * @param mixed $value 物品ID
- * @param array $data 包含用户ID的数组
- * @return bool 验证是否通过
- */
- public function validate(mixed $value, array $data): bool
- {
- $itemId = (int)$value;
- // 从 args 获取字段键名
- $userIdKey = $this->args[0] ?? 'user_id';
- $userId = $data[$userIdKey] ?? null;
- if (!$userId) {
- $this->addError('用户ID不能为空');
- return false;
- }
- try {
- // 验证用户是否拥有该物品
- if (!$this->validateUserHasItem($userId, $itemId)) {
- return false;
- }
- // 验证物品是否具有宠物种类属性
- if (!$this->validateItemPetAttribute($itemId)) {
- return false;
- }
- // 验证用户宠物数量限制
- if (!$this->validatePetCountLimit($userId)) {
- return false;
- }
- return true;
- } catch (\Exception $e) {
- $this->addError('验证宠物获取时发生错误: ' . $e->getMessage());
- return false;
- }
- }
- /**
- * 验证用户是否拥有该物品
- *
- * @param int $userId 用户ID
- * @param int $itemId 物品ID
- * @return bool
- */
- private function validateUserHasItem(int $userId, int $itemId): bool
- {
- // 直接使用数据库查询,避免通过Service层可能触发的事件或DTO转换
- $totalQuantity = \App\Module\GameItems\Models\ItemUser::where('user_id', $userId)
- ->where('item_id', $itemId)
- ->where(function ($query) {
- $now = now();
- $query->whereNull('expire_at')
- ->orWhere('expire_at', '>', $now);
- })
- ->whereHas('item', function ($query) {
- $now = now();
- $query->where(function ($subQuery) use ($now) {
- $subQuery->whereNull('global_expire_at')
- ->orWhere('global_expire_at', '>', $now);
- });
- })
- ->sum('quantity');
- if ($totalQuantity < 1) {
- $this->addError("您没有该物品或物品数量不足");
- return false;
- }
- return true;
- }
- /**
- * 验证物品是否具有宠物种类属性
- *
- * @param int $itemId 物品ID
- * @return bool
- */
- private function validateItemPetAttribute(int $itemId): bool
- {
- // 直接查询物品信息,避免通过Service层可能触发的问题
- $item = \App\Module\GameItems\Models\Item::find($itemId);
- if (!$item) {
- $this->addError("物品不存在");
- return false;
- }
- // 直接从数值属性中获取宠物种类
- $numericAttributes = (array)$item->numeric_attributes;
- $petType = $numericAttributes['pet_type'] ?? '';
- if (empty($petType)) {
- $this->addError("该物品不能获取宠物");
- return false;
- }
- // 验证宠物种类是否存在配置
- $petConfig = PetConfig::where('id', $petType)->first();
- if (!$petConfig) {
- $this->addError("宠物种类配置不存在: {$petType}");
- return false;
- }
- return true;
- }
- /**
- * 验证用户宠物数量限制
- *
- * @param int $userId 用户ID
- * @return bool
- */
- private function validatePetCountLimit(int $userId): bool
- {
- $petCount = PetUser::where('user_id', $userId)->count();
- $maxPets = config('pet.max_pets_per_user', 3);
- if ($petCount >= $maxPets) {
- $this->addError("已达到最大宠物数量限制: {$maxPets}");
- return false;
- }
- return true;
- }
- }
|