标段获取水准数据

This commit is contained in:
lhx
2025-11-12 18:04:02 +08:00
parent 155575f462
commit 9997e6286f
4 changed files with 154 additions and 1 deletions

View File

@@ -3,6 +3,12 @@ from typing import List, Optional, Dict, Any
from ..models.level_data import LevelData
from .base import BaseService
from ..models.settlement_data import SettlementData
from ..models.checkpoint import Checkpoint
from ..models.section_data import SectionData
from ..models.account import Account
import logging
logger = logging.getLogger(__name__)
class LevelDataService(BaseService[LevelData]):
def __init__(self):
@@ -140,4 +146,84 @@ class LevelDataService(BaseService[LevelData]):
'success_count': success_count,
'failed_count': failed_count,
'failed_items': failed_items
}
}
def get_level_data_by_project_name(self, db: Session, project_name: str) -> List[Dict[str, Any]]:
"""
通过project_name获取全部水准线路
业务逻辑:
1. 查询账号表获取账号数据 (通过project_name)
2. 查询断面表获取断面数据 (通过account_id)
3. 查询观测点表获取观测点数据 (通过section_id)
4. 查询沉降数据表获取沉降数据 (通过point_id)
5. 查询水准数据表获取水准数据 (通过NYID)
6. 将水准数据依照linecode去重同linecode只需保留一个
"""
try:
logger.info(f"开始查询project_name={project_name}对应的水准线路数据")
# 1. 查询账号表获取账号数据
accounts = db.query(Account).filter(Account.project_name.like(f"%{project_name}%")).all()
if not accounts:
logger.warning(f"未查询到project_name={project_name}对应的账号")
return []
account_ids = [str(account.id) for account in accounts]
logger.info(f"查询到{len(account_ids)}个账号: {account_ids}")
# 2. 查询断面表获取断面数据 (通过account_id)
sections = db.query(SectionData).filter(SectionData.account_id.in_(account_ids)).all()
if not sections:
logger.warning(f"未查询到对应的断面数据")
return []
section_ids = [section.section_id for section in sections]
logger.info(f"查询到{len(section_ids)}个断面: {section_ids}")
# 3. 查询观测点表获取观测点数据 (通过section_id)
checkpoints = db.query(Checkpoint).filter(Checkpoint.section_id.in_(section_ids)).all()
if not checkpoints:
logger.warning(f"未查询到对应的观测点数据")
return []
point_ids = [checkpoint.point_id for checkpoint in checkpoints]
logger.info(f"查询到{len(point_ids)}个观测点")
# 4. 查询沉降数据表获取沉降数据 (通过point_id)
settlements = db.query(SettlementData).filter(SettlementData.point_id.in_(point_ids)).all()
if not settlements:
logger.warning(f"未查询到对应的沉降数据")
return []
nyid_list = list(set([settlement.NYID for settlement in settlements if settlement.NYID]))
logger.info(f"查询到{len(nyid_list)}个期数ID")
# 5. 查询水准数据表获取水准数据 (通过NYID)
level_data_list = db.query(LevelData).filter(LevelData.NYID.in_(nyid_list)).all()
if not level_data_list:
logger.warning(f"未查询到对应的水准数据")
return []
# 6. 将水准数据依照linecode去重同linecode只需保留一个
linecode_seen = set()
unique_level_data = []
for level in level_data_list:
if level.linecode not in linecode_seen:
linecode_seen.add(level.linecode)
level_dict = {
"id": level.id,
"linecode": level.linecode,
"benchmarkids": level.benchmarkids,
"wsphigh": level.wsphigh,
"NYID": level.NYID,
"mtype": level.mtype,
"createDate": level.createDate.strftime("%Y-%m-%d %H:%M:%S") if level.createDate else None
}
unique_level_data.append(level_dict)
logger.info(f"查询完成,共{len(unique_level_data)}条去重后的水准数据")
return unique_level_data
except Exception as e:
logger.error(f"查询project_name={project_name}的水准数据失败: {str(e)}", exc_info=True)
raise e