getLandId(); $userItemId = $data->getItemId(); $userId = $this->user_id; // 开启事务 DB::beginTransaction(); // 使用统一的灾害去除逻辑,包含概率判断和事务检测 $result = CropService::removeDisasterWithItem( $userId, $landId, $userItemId, DISASTER_TYPE::WEED->value, 'land_weedicide' ); // 提交事务 DB::commit(); // 设置响应状态 $this->response->setCode(0); $this->response->setMsg($result['message']); } catch (LogicException $e) { // 回滚事务 DB::rollBack(); // 设置错误响应 $this->response->setCode(400); $this->response->setMsg($e->getMessage()); Log::warning('用户除草失败', [ 'user_id' => $this->user_id, 'land_id' => $landId ?? null, 'item_id' => $userItemId ?? null, 'error' => $e->getMessage(), 'trace' => $e->getTraceAsString() ]); } catch (\Exception $e) { // 回滚事务 DB::rollBack(); // 设置错误响应 $this->response->setCode(500); $this->response->setMsg('系统错误,请稍后再试'); Log::error('除草操作异常', [ 'user_id' => $this->user_id, 'land_id' => $landId ?? null, 'item_id' => $userItemId ?? null, 'error' => $e->getMessage(), 'trace' => $e->getTraceAsString() ]); } return $response; } }