增加account_id条件获取沉降数据
This commit is contained in:
@@ -232,20 +232,76 @@ def get_section(request: SectionDataQueryRequest, db: Session = Depends(get_db))
|
||||
# 根据观测点id查询沉降数据
|
||||
@router.post("/get_settlement", response_model=DataResponse)
|
||||
def get_settlement(request: SettlementDataQueryRequest, db: Session = Depends(get_db)):
|
||||
"""获取沉降数据,按上传时间倒序排序,支持分页参数(skip、limit)"""
|
||||
"""获取沉降数据,支持根据account_id查询(account_id -> 断面数据 -> 观测点数据 -> 沉降数据)"""
|
||||
try:
|
||||
logger.info(f"Querying settlement data with params: {request.dict()}")
|
||||
result = settlement_service.search_settlement_data_formatted(
|
||||
db,
|
||||
id=request.id,
|
||||
point_id=request.point_id,
|
||||
nyid=request.NYID,
|
||||
sjName=request.sjName,
|
||||
workinfoname=request.workinfoname,
|
||||
skip=request.skip,
|
||||
limit=request.limit
|
||||
)
|
||||
logger.info(f"Found {result['total']} settlement records, returning {len(result['data'])} records")
|
||||
|
||||
# 如果提供了account_id,则按新逻辑查询
|
||||
if request.account_id:
|
||||
logger.info(f"Using account_id to query: {request.account_id}")
|
||||
|
||||
# 1. 根据account_id查询断面数据
|
||||
section_data_list = section_service.get_by_account_id(db, request.account_id)
|
||||
logger.info(f"Found {len(section_data_list)} sections for account_id: {request.account_id}")
|
||||
|
||||
if not section_data_list:
|
||||
return DataResponse(
|
||||
code=ResponseCode.SUCCESS,
|
||||
message="未找到对应账号ID的断面数据",
|
||||
total=0,
|
||||
data=[]
|
||||
)
|
||||
|
||||
# 2. 使用批量查询一次性获取所有观测点数据(避免循环查询)
|
||||
section_ids = [section_data.section_id for section_data in section_data_list]
|
||||
logger.info(f"Querying {len(section_ids)} sections for account_id: {request.account_id}")
|
||||
|
||||
checkpoint_data_list = checkpoint_service.get_by_section_ids_batch(db, section_ids)
|
||||
logger.info(f"Found {len(checkpoint_data_list)} checkpoints total")
|
||||
|
||||
# 提取所有观测点ID(去重)
|
||||
point_ids = []
|
||||
for checkpoint in checkpoint_data_list:
|
||||
if checkpoint.point_id and checkpoint.point_id not in point_ids:
|
||||
point_ids.append(checkpoint.point_id)
|
||||
|
||||
logger.info(f"Total unique point_ids found: {len(point_ids)}")
|
||||
|
||||
if not point_ids:
|
||||
return DataResponse(
|
||||
code=ResponseCode.SUCCESS,
|
||||
message="未找到观测点数据",
|
||||
total=0,
|
||||
data=[]
|
||||
)
|
||||
|
||||
# 3. 使用优化的批量查询方法(一次性查询,避免多次数据库访问)
|
||||
result = settlement_service.search_settlement_data_by_point_ids_formatted(
|
||||
db,
|
||||
point_ids=point_ids,
|
||||
id=request.id,
|
||||
nyid=request.NYID,
|
||||
sjName=request.sjName,
|
||||
workinfoname=request.workinfoname,
|
||||
skip=request.skip,
|
||||
limit=request.limit
|
||||
)
|
||||
logger.info(f"Found {result['total']} settlement records using optimized batch query, returning {len(result['data'])} records")
|
||||
|
||||
else:
|
||||
# 原逻辑:不提供account_id,按原有方式查询
|
||||
logger.info("Using original query logic without account_id")
|
||||
result = settlement_service.search_settlement_data_formatted(
|
||||
db,
|
||||
id=request.id,
|
||||
point_id=request.point_id,
|
||||
nyid=request.NYID,
|
||||
sjName=request.sjName,
|
||||
workinfoname=request.workinfoname,
|
||||
skip=request.skip,
|
||||
limit=request.limit
|
||||
)
|
||||
logger.info(f"Found {result['total']} settlement records using original logic, returning {len(result['data'])} records")
|
||||
|
||||
return DataResponse(
|
||||
code=ResponseCode.SUCCESS,
|
||||
|
||||
Reference in New Issue
Block a user