PetGetValidator.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. namespace App\Module\Pet\Validators;
  3. use App\Module\Pet\Models\PetConfig;
  4. use App\Module\Pet\Models\PetUser;
  5. use UCore\Validator;
  6. /**
  7. * 宠物获取验证器
  8. *
  9. * 验证宠物获取操作是否有效,包括物品拥有、宠物种类配置、数量限制等
  10. */
  11. class PetGetValidator extends Validator
  12. {
  13. /**
  14. * 验证宠物获取操作
  15. *
  16. * @param mixed $value 物品ID
  17. * @param array $data 包含用户ID的数组
  18. * @return bool 验证是否通过
  19. */
  20. public function validate(mixed $value, array $data): bool
  21. {
  22. $itemId = (int)$value;
  23. // 从 args 获取字段键名
  24. $userIdKey = $this->args[0] ?? 'user_id';
  25. $userId = $data[$userIdKey] ?? null;
  26. if (!$userId) {
  27. $this->addError('用户ID不能为空');
  28. return false;
  29. }
  30. try {
  31. // 验证用户是否拥有该物品
  32. if (!$this->validateUserHasItem($userId, $itemId)) {
  33. return false;
  34. }
  35. // 验证物品是否具有宠物种类属性
  36. if (!$this->validateItemPetAttribute($itemId)) {
  37. return false;
  38. }
  39. // 验证用户宠物数量限制
  40. if (!$this->validatePetCountLimit($userId)) {
  41. return false;
  42. }
  43. return true;
  44. } catch (\Exception $e) {
  45. $this->addError('验证宠物获取时发生错误: ' . $e->getMessage());
  46. return false;
  47. }
  48. }
  49. /**
  50. * 验证用户是否拥有该物品
  51. *
  52. * @param int $userId 用户ID
  53. * @param int $itemId 物品ID
  54. * @return bool
  55. */
  56. private function validateUserHasItem(int $userId, int $itemId): bool
  57. {
  58. // 直接使用数据库查询,避免通过Service层可能触发的事件或DTO转换
  59. $totalQuantity = \App\Module\GameItems\Models\ItemUser::where('user_id', $userId)
  60. ->where('item_id', $itemId)
  61. ->where(function ($query) {
  62. $now = now();
  63. $query->whereNull('expire_at')
  64. ->orWhere('expire_at', '>', $now);
  65. })
  66. ->whereHas('item', function ($query) {
  67. $now = now();
  68. $query->where(function ($subQuery) use ($now) {
  69. $subQuery->whereNull('global_expire_at')
  70. ->orWhere('global_expire_at', '>', $now);
  71. });
  72. })
  73. ->sum('quantity');
  74. if ($totalQuantity < 1) {
  75. $this->addError("您没有该物品或物品数量不足");
  76. return false;
  77. }
  78. return true;
  79. }
  80. /**
  81. * 验证物品是否具有宠物种类属性
  82. *
  83. * @param int $itemId 物品ID
  84. * @return bool
  85. */
  86. private function validateItemPetAttribute(int $itemId): bool
  87. {
  88. // 直接查询物品信息,避免通过Service层可能触发的问题
  89. $item = \App\Module\GameItems\Models\Item::find($itemId);
  90. if (!$item) {
  91. $this->addError("物品不存在");
  92. return false;
  93. }
  94. // 直接从数值属性中获取宠物种类
  95. $numericAttributes = (array)$item->numeric_attributes;
  96. $petType = $numericAttributes['pet_type'] ?? '';
  97. if (empty($petType)) {
  98. $this->addError("该物品不能获取宠物");
  99. return false;
  100. }
  101. // 验证宠物种类是否存在配置
  102. $petConfig = PetConfig::where('id', $petType)->first();
  103. if (!$petConfig) {
  104. $this->addError("宠物种类配置不存在: {$petType}");
  105. return false;
  106. }
  107. return true;
  108. }
  109. /**
  110. * 验证用户宠物数量限制
  111. *
  112. * @param int $userId 用户ID
  113. * @return bool
  114. */
  115. private function validatePetCountLimit(int $userId): bool
  116. {
  117. $petCount = PetUser::where('user_id', $userId)->count();
  118. $maxPets = config('pet.max_pets_per_user', 3);
  119. if ($petCount >= $maxPets) {
  120. $this->addError("已达到最大宠物数量限制: {$maxPets}");
  121. return false;
  122. }
  123. return true;
  124. }
  125. }