getLandId(); $userItemId = $data->getItemId(); $userId = $this->user_id; // 验证土地是否存在且属于当前用户 // 获取用户所有土地 $landInfo = LandService::getUserLand($userId,$landId); if (!$landInfo) { throw new LogicException("土地不存在或不属于当前用户"); } // 验证用户是否拥有该物品 $hasItem = ItemService::getUserItems($userId, ['item_id' => $userItemId]); if ($hasItem->isEmpty()) { throw new LogicException("您没有该除虫物品"); } // 除虫概率 百分比 ,100 = 100% $rate =ItemService::getItemNumericAttribute($userItemId,'fram_pesticide_rate'); if($rate<=0){ throw new LogicException("不是除虫物品"); } // 调用除虫服务 $result = CropService::clearDisaster($userId, $landId, 2); // 2表示虫害灾害类型 if (!$result) { throw new LogicException("除虫失败,请检查土地状态或作物生长阶段"); } // 消耗物品 ItemService::consumeItem($userId, $userItemId, null, 1, [ 'source_type' => 'land_pesticide', 'source_id' => $landId, 'details' => ['land_id' => $landId] ]); // 设置响应状态 $this->response->setCode(0); $this->response->setMsg('除虫成功'); Log::info('用户除虫成功', [ 'user_id' => $userId, 'land_id' => $landId, 'item_id' => $userItemId ]); } catch (LogicException $e) { // 设置错误响应 $this->response->setCode(400); $this->response->setMsg($e->getMessage()); Log::warning('用户除虫失败', [ 'user_id' => $this->user_id, 'error' => $e->getMessage(), 'trace' => $e->getTraceAsString() ]); } catch (\Exception $e) { // 设置错误响应 $this->response->setCode(500); $this->response->setMsg('系统错误,请稍后再试'); Log::error('除虫操作异常', [ 'user_id' => $this->user_id, 'error' => $e->getMessage(), 'trace' => $e->getTraceAsString() ]); } return $response; } }