响应格式修改,原始数据查询修改

This commit is contained in:
lhx
2025-10-23 11:32:10 +08:00
parent 21c61cdec7
commit 34b698386a
12 changed files with 542 additions and 232 deletions

View File

@@ -2,6 +2,7 @@ from fastapi import APIRouter, Depends, HTTPException, status
from sqlalchemy.orm import Session
from typing import List, Optional
from ..core.database import get_db
from ..core.response_code import ResponseCode, ResponseMessage
from ..schemas.comprehensive_data import (
BatchSectionDataImportRequest,
BatchCheckpointDataImportRequest,
@@ -40,20 +41,27 @@ def batch_import_sections(request: BatchSectionDataImportRequest, db: Session =
"""批量导入断面数据"""
try:
logger.info(f"Starting batch import sections, count: {len(request.data)}")
# 直接使用字典列表,不需要转换
data_list = request.data
result = section_service.batch_import_sections(db, data_list)
logger.info(f"Batch import sections completed: {result['message']}")
return DataImportResponse(**result)
# 统一响应格式
return DataImportResponse(
code=ResponseCode.SUCCESS if result.get('success') else ResponseCode.IMPORT_FAILED,
message=result['message'],
data={
'total_count': result.get('total_count', 0),
'success_count': result.get('success_count', 0),
'failed_count': result.get('failed_count', 0),
'failed_items': result.get('failed_items', [])
}
)
except Exception as e:
logger.error(f"Batch import sections failed: {str(e)}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=f"批量导入断面数据失败: {str(e)}"
return DataImportResponse(
code=ResponseCode.IMPORT_FAILED,
message=f"{ResponseMessage.IMPORT_FAILED}: {str(e)}",
data={'total_count': 0, 'success_count': 0, 'failed_count': 0, 'failed_items': []}
)
@router.post("/batch_import_checkpoints", response_model=DataImportResponse)
@@ -61,20 +69,26 @@ def batch_import_checkpoints(request: BatchCheckpointDataImportRequest, db: Sess
"""批量导入观测点数据"""
try:
logger.info(f"Starting batch import checkpoints, count: {len(request.data)}")
# 直接使用字典列表,不需要转换
data_list = request.data
result = checkpoint_service.batch_import_checkpoints(db, data_list)
logger.info(f"Batch import checkpoints completed: {result['message']}")
return DataImportResponse(**result)
return DataImportResponse(
code=ResponseCode.SUCCESS if result.get('success') else ResponseCode.IMPORT_FAILED,
message=result['message'],
data={
'total_count': result.get('total_count', 0),
'success_count': result.get('success_count', 0),
'failed_count': result.get('failed_count', 0),
'failed_items': result.get('failed_items', [])
}
)
except Exception as e:
logger.error(f"Batch import checkpoints failed: {str(e)}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=f"批量导入观测点数据失败: {str(e)}"
return DataImportResponse(
code=ResponseCode.IMPORT_FAILED,
message=f"{ResponseMessage.IMPORT_FAILED}: {str(e)}",
data={'total_count': 0, 'success_count': 0, 'failed_count': 0, 'failed_items': []}
)
@router.post("/batch_import_settlement_data", response_model=DataImportResponse)
@@ -82,20 +96,26 @@ def batch_import_settlement_data(request: BatchSettlementDataImportRequest, db:
"""批量导入沉降数据"""
try:
logger.info(f"Starting batch import settlement data, count: {len(request.data)}")
# 直接使用字典列表,不需要转换
data_list = request.data
result = settlement_service.batch_import_settlement_data(db, data_list)
logger.info(f"Batch import settlement data completed: {result['message']}")
return DataImportResponse(**result)
return DataImportResponse(
code=ResponseCode.SUCCESS if result.get('success') else ResponseCode.IMPORT_FAILED,
message=result['message'],
data={
'total_count': result.get('total_count', 0),
'success_count': result.get('success_count', 0),
'failed_count': result.get('failed_count', 0),
'failed_items': result.get('failed_items', [])
}
)
except Exception as e:
logger.error(f"Batch import settlement data failed: {str(e)}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=f"批量导入沉降数据失败: {str(e)}"
return DataImportResponse(
code=ResponseCode.IMPORT_FAILED,
message=f"{ResponseMessage.IMPORT_FAILED}: {str(e)}",
data={'total_count': 0, 'success_count': 0, 'failed_count': 0, 'failed_items': []}
)
@router.post("/batch_import_level_data", response_model=DataImportResponse)
@@ -103,20 +123,26 @@ def batch_import_level_data(request: BatchLevelDataImportRequest, db: Session =
"""批量导入水准数据"""
try:
logger.info(f"Starting batch import level data, count: {len(request.data)}")
# 直接使用字典列表,不需要转换
data_list = request.data
result = level_service.batch_import_level_data(db, data_list)
logger.info(f"Batch import level data completed: {result['message']}")
return DataImportResponse(**result)
return DataImportResponse(
code=ResponseCode.SUCCESS if result.get('success') else ResponseCode.IMPORT_FAILED,
message=result['message'],
data={
'total_count': result.get('total_count', 0),
'success_count': result.get('success_count', 0),
'failed_count': result.get('failed_count', 0),
'failed_items': result.get('failed_items', [])
}
)
except Exception as e:
logger.error(f"Batch import level data failed: {str(e)}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=f"批量导入水准数据失败: {str(e)}"
return DataImportResponse(
code=ResponseCode.IMPORT_FAILED,
message=f"{ResponseMessage.IMPORT_FAILED}: {str(e)}",
data={'total_count': 0, 'success_count': 0, 'failed_count': 0, 'failed_items': []}
)
@router.post("/batch_import_original_data", response_model=DataImportResponse)
@@ -127,33 +153,40 @@ def batch_import_original_data(request: BatchOriginalDataImportRequest, db: Sess
# 验证数据中是否包含account_id
if not request.data or len(request.data) == 0:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="导入数据不能为空"
return DataImportResponse(
code=ResponseCode.BAD_REQUEST,
message="导入数据不能为空",
data={'total_count': 0, 'success_count': 0, 'failed_count': 0, 'failed_items': []}
)
# 检查第一条数据是否包含account_id
if 'account_id' not in request.data[0]:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="数据中必须包含account_id字段"
return DataImportResponse(
code=ResponseCode.BAD_REQUEST,
message="数据中必须包含account_id字段",
data={'total_count': 0, 'success_count': 0, 'failed_count': 0, 'failed_items': []}
)
# 直接使用字典列表,不需要转换
data_list = request.data
result = original_service.batch_import_original_data(db, data_list)
logger.info(f"Batch import original data completed: {result['message']}")
return DataImportResponse(**result)
except HTTPException:
raise
return DataImportResponse(
code=ResponseCode.SUCCESS if result.get('success') else ResponseCode.IMPORT_FAILED,
message=result['message'],
data={
'total_count': result.get('total_count', 0),
'success_count': result.get('success_count', 0),
'failed_count': result.get('failed_count', 0),
'failed_items': result.get('failed_items', [])
}
)
except Exception as e:
logger.error(f"Batch import original data failed: {str(e)}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=f"批量导入原始数据失败: {str(e)}"
return DataImportResponse(
code=ResponseCode.IMPORT_FAILED,
message=f"{ResponseMessage.IMPORT_FAILED}: {str(e)}",
data={'total_count': 0, 'success_count': 0, 'failed_count': 0, 'failed_items': []}
)
# 查询断面数据对应观察点数据
@@ -162,8 +195,6 @@ def get_section(request: SectionDataQueryRequest, db: Session = Depends(get_db))
"""获取断面数据 + 观测点"""
try:
logger.info(f"Querying section data with params: {request.dict()}")
# 调用服务层的业务方法
result_data = section_service.search_sections_with_checkpoints(
db,
id=request.id,
@@ -174,31 +205,29 @@ def get_section(request: SectionDataQueryRequest, db: Session = Depends(get_db))
status=request.status,
account_id=request.account_id
)
logger.info(f"Found {len(result_data)} sections with checkpoints")
return DataResponse(
success=True,
code=ResponseCode.SUCCESS,
message="查询成功",
count=len(result_data),
total=len(result_data),
data=result_data
)
except Exception as e:
logger.error(f"Query section data failed: {str(e)}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=f"查询断面数据失败: {str(e)}"
return DataResponse(
code=ResponseCode.QUERY_FAILED,
message=f"{ResponseMessage.QUERY_FAILED}: {str(e)}",
total=0,
data=[]
)
# 根据观测点id查询沉降数据
@router.post("/get_settlement", response_model=DataResponse)
def get_settlement(request: SettlementDataQueryRequest, db: Session = Depends(get_db)):
"""获取沉降数据按上传时间倒序排序支持limit参数限制返回数量"""
try:
logger.info(f"Querying settlement data with params: {request.dict()}")
# 调用服务层的业务方法
result_data = settlement_service.search_settlement_data_formatted(
db,
id=request.id,
@@ -208,20 +237,21 @@ def get_settlement(request: SettlementDataQueryRequest, db: Session = Depends(ge
workinfoname=request.workinfoname,
limit=request.limit
)
logger.info(f"Found {len(result_data)} settlement records")
return DataResponse(
success=True,
code=ResponseCode.SUCCESS,
message="查询成功",
count=len(result_data),
total=len(result_data),
data=result_data
)
except Exception as e:
logger.error(f"Query settlement data failed: {str(e)}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=f"查询沉降数据失败: {str(e)}"
return DataResponse(
code=ResponseCode.QUERY_FAILED,
message=f"{ResponseMessage.QUERY_FAILED}: {str(e)}",
total=0,
data=[]
)
# 查询沉降数据+观测点数据
@@ -230,8 +260,6 @@ def get_settlement_checkpoint(request: SettlementDataCheckpointQueryRequest, db:
"""获取沉降数据+观测点数据按上传时间倒序排序支持limit参数限制返回数量"""
try:
logger.info(f"Querying settlement data with params: {request.dict()}")
# 调用服务层的业务方法
result_data = settlement_service.search_settlement_checkpoint_data_formatted(
db,
id=request.id,
@@ -242,41 +270,32 @@ def get_settlement_checkpoint(request: SettlementDataCheckpointQueryRequest, db:
linecode=request.linecode,
limit=request.limit
)
logger.info(f"Found {len(result_data)} settlement records")
return DataResponse(
success=True,
code=ResponseCode.SUCCESS,
message="查询成功",
count=len(result_data),
total=len(result_data),
data=result_data
)
except Exception as e:
logger.error(f"Query settlement data failed: {str(e)}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=f"查询沉降数据失败: {str(e)}"
return DataResponse(
code=ResponseCode.QUERY_FAILED,
message=f"{ResponseMessage.QUERY_FAILED}: {str(e)}",
total=0,
data=[]
)
# 根据期数id获取原始数据
@router.post("/get_original", response_model=DataResponse)
def get_original(request: OriginalDataQueryRequest, db: Session = Depends(get_db)):
"""获取水准数据+原始数据 - 必须提供account_id"""
"""获取水准数据+原始数据 - account_id可选,不填则查询所有分表"""
try:
logger.info(f"Querying original data with params: {request.dict()}")
# 验证account_id
if not request.account_id:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="必须提供account_id参数"
)
# 调用综合服务的业务方法
result = comprehensive_service.get_level_and_original_data(
db,
account_id=request.account_id,
account_id=request.account_id, # 可选
id=request.id,
bfpcode=request.bfpcode,
bffb=request.bffb,
@@ -286,17 +305,16 @@ def get_original(request: OriginalDataQueryRequest, db: Session = Depends(get_db
)
return DataResponse(
success=result["success"],
code=ResponseCode.SUCCESS,
message=result["message"],
count=result["count"],
total=result["count"],
data=result["data"]
)
except HTTPException:
raise
except Exception as e:
logger.error(f"Query original data failed: {str(e)}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=f"查询原始数据失败: {str(e)}"
return DataResponse(
code=ResponseCode.QUERY_FAILED,
message=f"{ResponseMessage.QUERY_FAILED}: {str(e)}",
total=0,
data=[]
)