This commit is contained in:
lhx
2025-11-14 18:05:22 +08:00
8 changed files with 720 additions and 177 deletions

View File

@@ -125,6 +125,12 @@ class CheckpointService(BaseService[Checkpoint]):
"""根据section_id获取所有相关的测点信息"""
return self.get_by_field(db, "section_id", section_id)
def get_by_section_ids_batch(self, db: Session, section_ids: List[str]) -> List[Checkpoint]:
"""批量根据section_id列表获取所有观测点数据使用IN查询优化性能"""
if not section_ids:
return []
return db.query(Checkpoint).filter(Checkpoint.section_id.in_(section_ids)).all()
def get_by_section_ids(self, db: Session, section_ids: List[str]) -> List[Checkpoint]:
"""根据多个section_id批量获取观测点数据"""
return db.query(Checkpoint).filter(Checkpoint.section_id.in_(section_ids)).all()

View File

@@ -94,7 +94,7 @@ class DailyDataService(BaseService[DailyData]):
max_num: int = 1
) -> List[List[dict]]:
"""
获取指定point_id的记录修复子查询中模型对象访问错误
获取指定point_id的记录修复子查询中模型对象访问错误同时过滤useflag=0的数据
"""
# 处理参数默认值
point_ids = point_ids or []
@@ -110,11 +110,22 @@ class DailyDataService(BaseService[DailyData]):
# 子查询:查询模型的所有字段 + 行号(不保留模型对象,只展平字段)
# 先获取模型的所有字段列表
model_columns = [getattr(SettlementData, col.name) for col in SettlementData.__table__.columns]
# -------------------------- 新增过滤条件useflag IS NOT NULL AND useflag != 0 --------------------------
# 基础条件过滤useflag为0或不存在的记录
base_conditions = [
SettlementData.useflag.isnot(None), # 确保useflag字段存在非NULL
SettlementData.useflag != 0 # 确保useflag值不为0
]
# 若指定了point_ids添加point_id过滤条件
if point_ids:
base_conditions.append(SettlementData.point_id.in_(point_ids))
subquery = (
select(*model_columns, row_num) # 展开所有字段 + 行号
.where(SettlementData.point_id.in_(point_ids) if point_ids else True)
.where(*base_conditions) # 应用组合条件包含useflag过滤
.subquery()
)
# ------------------------------------------------------------------------------------------------------
# 主查询:筛选行号<=max_num的记录
query = (

View File

@@ -20,10 +20,16 @@ class SectionDataService(BaseService[SectionData]):
"""根据断面ID获取断面数据"""
sections = self.get_by_field(db, "section_id", section_id)
return sections[0] if sections else None
def get_by_account_id(self, db: Session, account_id: str) -> Optional[SectionData]:
def get_by_account_id(self, db: Session, account_id: str) -> List[SectionData]:
"""根据账号ID获取断面数据"""
accounts = self.get_by_field(db, "account_id", account_id)
return accounts if accounts else None
return accounts if accounts else []
def get_by_account_id_batch(self, db: Session, account_ids: List[str]) -> List[SectionData]:
"""批量根据账号ID列表获取断面数据使用IN查询优化性能"""
if not account_ids:
return []
return db.query(SectionData).filter(SectionData.account_id.in_(account_ids)).all()
def get_by_number(self, db: Session, number: str) -> List[SectionData]:
"""根据桥梁墩(台)编号获取断面数据"""
return self.get_by_field(db, "number", number)

View File

@@ -70,6 +70,100 @@ class SettlementDataService(BaseService[SettlementData]):
return query.all()
def search_settlement_data_by_point_ids_formatted(self, db: Session,
point_ids: List[str],
id: Optional[int] = None,
nyid: Optional[str] = None,
sjName: Optional[str] = None,
workinfoname: Optional[str] = None,
skip: int = 0,
limit: Optional[int] = None) -> Dict[str, Any]:
"""
支持多个point_id的沉降数据批量查询优化版本
一次性查询,避免多次数据库访问
"""
if not point_ids:
return {
"data": [],
"total": 0,
"skip": skip,
"limit": limit
}
# 先获取总数(不计分页)
count_query = db.query(SettlementData)
if id is not None:
count_query = count_query.filter(SettlementData.id == id)
if nyid is not None:
count_query = count_query.filter(SettlementData.NYID == nyid)
if sjName is not None:
count_query = count_query.filter(SettlementData.sjName == sjName)
if workinfoname is not None:
count_query = count_query.filter(SettlementData.workinfoname == workinfoname)
# 支持多个point_id使用IN查询
count_query = count_query.filter(SettlementData.point_id.in_(point_ids))
total_count = count_query.count()
# 获取分页数据(使用相同的过滤条件)
query = db.query(SettlementData)
if id is not None:
query = query.filter(SettlementData.id == id)
if nyid is not None:
query = query.filter(SettlementData.NYID == nyid)
if sjName is not None:
query = query.filter(SettlementData.sjName == sjName)
if workinfoname is not None:
query = query.filter(SettlementData.workinfoname == workinfoname)
# 支持多个point_id
query = query.filter(SettlementData.point_id.in_(point_ids))
# 按上传时间倒序排序
query = query.order_by(SettlementData.createdate.desc())
# 添加分页支持
if skip > 0:
query = query.offset(skip)
if limit is not None and limit > 0:
query = query.limit(limit)
settlement_data = query.all()
# 格式化结果
result = []
for settlement in settlement_data:
settlement_dict = {
"id": settlement.id,
"point_id": settlement.point_id,
"CVALUE": settlement.CVALUE,
"MAVALUE": settlement.MAVALUE,
"MTIME_W": settlement.MTIME_W,
"NYID": settlement.NYID,
"PRELOADH": settlement.PRELOADH,
"PSTATE": settlement.PSTATE,
"REMARK": settlement.REMARK,
"WORKINFO": settlement.WORKINFO,
"createdate": settlement.createdate,
"day": settlement.day,
"day_jg": settlement.day_jg,
"isgzjdxz": settlement.isgzjdxz,
"mavalue_bc": settlement.mavalue_bc,
"mavalue_lj": settlement.mavalue_lj,
"sjName": settlement.sjName,
"useflag": settlement.useflag,
"workinfoname": settlement.workinfoname,
"upd_remark": settlement.upd_remark
}
result.append(settlement_dict)
return {
"data": result,
"total": total_count,
"skip": skip,
"limit": limit
}
def search_settlement_data_formatted(self, db: Session,
id: Optional[int] = None,
point_id: Optional[str] = None,