增加水准编码查询账号接口
This commit is contained in:
@@ -103,3 +103,84 @@ class AccountService:
|
||||
db.commit()
|
||||
return True
|
||||
return False
|
||||
|
||||
@staticmethod
|
||||
def get_accounts_by_linecode(db: Session, linecode: str) -> List[Account]:
|
||||
"""
|
||||
通过水准线路编码查询账号信息(优化版,减少数据库查询次数)
|
||||
业务逻辑:
|
||||
1. 根据linecode在水准数据表查询最新的NYID
|
||||
2. 根据NYID在沉降数据表批量查询所有point_id(去重)
|
||||
3. 根据point_id列表在观测点表批量查询所有section_id(去重)
|
||||
4. 根据section_id列表在断面表批量查询所有account_id(去重)
|
||||
5. 根据account_id列表在账号表批量查询账号信息
|
||||
|
||||
使用IN查询避免循环,大幅提升性能
|
||||
"""
|
||||
from ..models.level_data import LevelData
|
||||
from ..models.settlement_data import SettlementData
|
||||
from ..models.checkpoint import Checkpoint
|
||||
from ..models.section_data import SectionData
|
||||
|
||||
try:
|
||||
logger.info(f"开始通过linecode={linecode}查询账号信息")
|
||||
|
||||
# 1. 根据linecode查询最新的水准数据(按NYID降序取第一条)
|
||||
level_data = db.query(LevelData).filter(
|
||||
LevelData.linecode == linecode
|
||||
).order_by(LevelData.NYID.desc()).first()
|
||||
|
||||
if not level_data:
|
||||
logger.warning(f"未找到linecode={linecode}对应的水准数据")
|
||||
return []
|
||||
|
||||
nyid = level_data.NYID
|
||||
logger.info(f"找到最新期数NYID={nyid}")
|
||||
|
||||
# 2. 根据NYID批量查询沉降数据,提取所有point_id(去重)
|
||||
settlement_list = db.query(SettlementData.point_id).filter(
|
||||
SettlementData.NYID == nyid
|
||||
).distinct().all()
|
||||
|
||||
if not settlement_list:
|
||||
logger.warning(f"未找到NYID={nyid}对应的沉降数据")
|
||||
return []
|
||||
|
||||
point_ids = [s.point_id for s in settlement_list if s.point_id]
|
||||
logger.info(f"找到{len(point_ids)}个观测点ID")
|
||||
|
||||
# 3. 根据point_id列表批量查询观测点数据,提取所有section_id(去重)
|
||||
checkpoint_list = db.query(Checkpoint.section_id).filter(
|
||||
Checkpoint.point_id.in_(point_ids)
|
||||
).distinct().all()
|
||||
|
||||
if not checkpoint_list:
|
||||
logger.warning(f"未找到对应的观测点数据")
|
||||
return []
|
||||
|
||||
section_ids = [c.section_id for c in checkpoint_list if c.section_id]
|
||||
logger.info(f"找到{len(section_ids)}个断面ID")
|
||||
|
||||
# 4. 根据section_id列表批量查询断面数据,提取所有account_id(去重)
|
||||
section_list = db.query(SectionData.account_id).filter(
|
||||
SectionData.section_id.in_(section_ids)
|
||||
).distinct().all()
|
||||
|
||||
if not section_list:
|
||||
logger.warning(f"未找到对应的断面数据")
|
||||
return []
|
||||
|
||||
account_ids = [s.account_id for s in section_list if s.account_id]
|
||||
logger.info(f"找到{len(account_ids)}个账号ID")
|
||||
|
||||
# 5. 根据account_id列表批量查询账号信息
|
||||
accounts = db.query(Account).filter(
|
||||
Account.id.in_(account_ids)
|
||||
).all()
|
||||
|
||||
logger.info(f"查询完成,共找到{len(accounts)}个账号")
|
||||
return accounts
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"通过linecode={linecode}查询账号失败: {str(e)}", exc_info=True)
|
||||
raise e
|
||||
|
||||
Reference in New Issue
Block a user