| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- <?php
- namespace App\Module\Farm\Validators;
- use App\Module\Farm\Models\FarmLandType;
- use App\Module\Farm\Models\FarmUser;
- use Illuminate\Support\Facades\Log;
- use UCore\Validator;
- /**
- * 土地升级房屋等级验证器
- *
- * 验证用户房屋等级是否满足土地升级要求
- */
- class LandUpgradeHouseLevelValidator extends Validator
- {
- /**
- * 验证房屋等级
- *
- * @param mixed $value 土地ID
- * @param array $data 包含用户ID的数组
- * @return bool 验证是否通过
- */
- public function validate(mixed $value, array $data): bool
- {
- // 从 args 获取参数
- $userIdKey = $this->args[0] ?? 'user_id';
- $upgradeConfigKey = $this->args[1] ?? 'upgrade_config';
-
- $userId = $data[$userIdKey] ?? null;
- $upgradeConfig = $this->validation->$upgradeConfigKey ?? null;
- if (!$userId) {
- $this->addError('用户ID不能为空');
- return false;
- }
- if (!$upgradeConfig) {
- $this->addError('升级配置不存在,请先验证升级路径');
- return false;
- }
- try {
- // 获取目标土地类型
- $targetLandType = FarmLandType::find($upgradeConfig->to_type_id);
- if (!$targetLandType) {
- $this->addError('目标土地类型不存在');
- return false;
- }
- // 获取用户房屋信息
- $farmUser = FarmUser::where('user_id', $userId)->first();
- if (!$farmUser) {
- $this->addError('用户农场信息不存在');
- return false;
- }
- // 检查房屋等级是否满足要求
- if ($farmUser->house_level < $targetLandType->unlock_house_level) {
- $this->addError("房屋等级不足,需要{$targetLandType->unlock_house_level}级,当前{$farmUser->house_level}级");
- return false;
- }
- return true;
- } catch (\Exception $e) {
- Log::error('验证房屋等级时发生错误', [
- 'user_id' => $userId,
- 'error' => $e->getMessage(),
- 'trace' => $e->getTraceAsString()
- ]);
-
- $this->addError('验证房屋等级时发生错误: ' . $e->getMessage());
- return false;
- }
- }
- }
|