tianfeng_task_modules/api/common_params_api.py
2025-03-17 18:31:20 +08:00

96 lines
3.2 KiB
Python

"""
常用参数API模块
提供获取各种常用参数数据的接口
"""
from typing import Dict, Any, List, Optional
from fastapi import APIRouter, HTTPException, Query
from pydantic import BaseModel, Field
from services.common_params_service import CommonParamsService
from config.component_config import CommonParamsConfig, CommonParamType
from api.models import ApiResponse
# 创建路由器
router = APIRouter(prefix="/common-params", tags=["常用参数"])
# 创建服务实例
common_params_service = CommonParamsService()
@router.get("/types", response_model=ApiResponse)
async def get_param_types():
"""获取所有常用参数类型"""
try:
param_types = CommonParamsConfig.get_param_types()
return {
"code": ApiResponseCode.SUCCESS,
"message": "获取常用参数类型成功",
"data": {
"param_types": param_types
}
}
except Exception as e:
raise HTTPException(
status_code=ApiResponseCode.SERVER_ERROR,
detail=f"获取常用参数类型失败: {str(e)}"
)
@router.get("/data/{param_type}", response_model=ApiResponse)
async def get_param_data(param_type: str):
"""获取指定类型的常用参数数据"""
try:
# 检查参数类型是否有效
param_type_config = CommonParamsConfig.get_param_type_by_type(param_type)
if not param_type_config:
return {
"code": ApiResponseCode.NOT_FOUND,
"message": f"未找到参数类型: {param_type}",
"data": {"param_data": []}
}
# 获取参数数据
param_data = common_params_service.get_param_data(param_type)
return {
"code": ApiResponseCode.SUCCESS,
"message": f"获取{param_type_config['name']}数据成功",
"data": {
"param_type": param_type_config,
"param_data": param_data
}
}
except Exception as e:
raise HTTPException(
status_code=ApiResponseCode.SERVER_ERROR,
detail=f"获取常用参数数据失败: {str(e)}"
)
@router.get("/all", response_model=ApiResponse)
async def get_all_param_data():
"""获取所有常用参数数据"""
try:
# 获取所有参数类型
param_types = CommonParamsConfig.get_param_types()
# 获取所有参数数据
all_param_data = {}
for param_type_config in param_types:
param_type = param_type_config["type"]
param_data = common_params_service.get_param_data(param_type)
all_param_data[param_type] = {
"param_type": param_type_config,
"param_data": param_data
}
return {
"code": ApiResponseCode.SUCCESS,
"message": "获取所有常用参数数据成功",
"data": {
"param_types": param_types,
"all_param_data": all_param_data
}
}
except Exception as e:
raise HTTPException(
status_code=ApiResponseCode.SERVER_ERROR,
detail=f"获取所有常用参数数据失败: {str(e)}"
)