原始数据导入新接口
This commit is contained in:
@@ -9,6 +9,7 @@ from ..schemas.comprehensive_data import (
|
||||
BatchSettlementDataImportRequest,
|
||||
BatchLevelDataImportRequest,
|
||||
BatchOriginalDataImportRequest,
|
||||
BatchOriginalDataImportRequestNew,
|
||||
DataImportResponse,
|
||||
DataResponse,
|
||||
SectionDataQueryRequest,
|
||||
@@ -193,6 +194,70 @@ def batch_import_original_data(request: BatchOriginalDataImportRequest, db: Sess
|
||||
data={'total_count': 0, 'success_count': 0, 'failed_count': 0, 'failed_items': []}
|
||||
)
|
||||
|
||||
@router.post("/batch_import_original_data_new", response_model=DataImportResponse)
|
||||
def batch_import_original_data_new(request: BatchOriginalDataImportRequestNew, db: Session = Depends(get_db)):
|
||||
"""
|
||||
新版批量导入原始数据 - 支持分组格式
|
||||
传入参数格式:data:[[{},{},{}],[{},{}]]
|
||||
里层一个[{},{}]称为一组数据,数据{}内容与旧接口一致
|
||||
一组数据全部记录的NYID与account_id将会一样,不同组可能不同
|
||||
|
||||
导入逻辑:
|
||||
1. 按account_id分表存储,没表就建表
|
||||
2. 插入前根据NYID判断表中是否有重复数据
|
||||
3. 有重复就删除表中全部同NYID数据,插入新的,不重复就直接插入
|
||||
"""
|
||||
try:
|
||||
logger.info(f"Starting batch import original data (new), group count: {len(request.data)}")
|
||||
|
||||
# 验证分组数据
|
||||
if not request.data or len(request.data) == 0:
|
||||
return DataImportResponse(
|
||||
code=ResponseCode.BAD_REQUEST,
|
||||
message="导入数据不能为空",
|
||||
data={'total_count': 0, 'success_count': 0, 'failed_count': 0, 'failed_items': []}
|
||||
)
|
||||
|
||||
# 检查第一组数据是否包含account_id
|
||||
if len(request.data) == 0 or len(request.data[0]) == 0:
|
||||
return DataImportResponse(
|
||||
code=ResponseCode.BAD_REQUEST,
|
||||
message="分组数据不能为空",
|
||||
data={'total_count': 0, 'success_count': 0, 'failed_count': 0, 'failed_items': []}
|
||||
)
|
||||
|
||||
first_item = request.data[0][0]
|
||||
if 'account_id' not in first_item:
|
||||
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_new(db, data_list)
|
||||
logger.info(f"Batch import original data (new) completed: {result['message']}")
|
||||
|
||||
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 (new) failed: {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("/get_section", response_model=DataResponse)
|
||||
def get_section(request: SectionDataQueryRequest, db: Session = Depends(get_db)):
|
||||
|
||||
Reference in New Issue
Block a user