通过水准线路编码生成 daily 数据
This commit is contained in:
@@ -233,4 +233,115 @@ class DailyDataService(BaseService[DailyData]):
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"获取account_id={account_id}的日常数据失败:{str(e)}", exc_info=True)
|
||||
raise e
|
||||
raise e
|
||||
|
||||
def create_daily_from_linecode(
|
||||
self,
|
||||
db: Session,
|
||||
linecode: str,
|
||||
account_id: Optional[int] = None
|
||||
) -> List[DailyData]:
|
||||
"""
|
||||
通过水准线路编码生成 daily 数据
|
||||
|
||||
业务逻辑:
|
||||
1. 在水准数据表(level_data)中查找符合 linecode 的记录,且 NYID 最大
|
||||
2. 通过 NYID 查询沉降数据表(settlement_data)
|
||||
3. 通过沉降数据的 point_id 查询观测点表(checkpoint),得到 section_id
|
||||
4. 通过 section_id 查询断面表(section_data),得到 account_id
|
||||
5. 整合这些数据,形成 daily 对象,插入到数据库表
|
||||
|
||||
Args:
|
||||
db: 数据库会话
|
||||
linecode: 水准线路编码
|
||||
account_id: 可选的账户ID筛选条件
|
||||
|
||||
Returns:
|
||||
创建的 DailyData 记录列表
|
||||
"""
|
||||
try:
|
||||
logger.info(f"开始处理 linecode={linecode} 的 daily 数据生成请求")
|
||||
|
||||
from ..models.level_data import LevelData
|
||||
from ..models.settlement_data import SettlementData
|
||||
from ..models.checkpoint import Checkpoint
|
||||
from ..models.section_data import SectionData
|
||||
|
||||
# 1. 在水准数据表中查找符合 linecode 的记录,且 NYID 最大
|
||||
level_data_list = db.query(LevelData)\
|
||||
.filter(LevelData.linecode == linecode)\
|
||||
.all()
|
||||
|
||||
if not level_data_list:
|
||||
raise ValueError(f"未找到 linecode={linecode} 对应的水准数据")
|
||||
|
||||
# 找到 NYID 最大的记录(将 String 转换为数字进行比较)
|
||||
max_nyid = max(level_data_list, key=lambda x: int(x.NYID) if x.NYID.isdigit() else 0)
|
||||
target_nyid = max_nyid.NYID
|
||||
logger.info(f"找到最大 NYID: {target_nyid}")
|
||||
|
||||
# 2. 通过 NYID 查询沉降数据
|
||||
settlement_data_list = db.query(SettlementData)\
|
||||
.filter(SettlementData.NYID == target_nyid)\
|
||||
.all()
|
||||
|
||||
if not settlement_data_list:
|
||||
raise ValueError(f"未找到 NYID={target_nyid} 对应的沉降数据")
|
||||
|
||||
# 3. 遍历沉降数据,构建 daily 记录
|
||||
daily_records = []
|
||||
for settlement in settlement_data_list:
|
||||
# 3.1 通过沉降数据的 point_id 查询观测点表,得到 section_id
|
||||
checkpoint = db.query(Checkpoint)\
|
||||
.filter(Checkpoint.point_id == settlement.point_id)\
|
||||
.first()
|
||||
if not checkpoint:
|
||||
logger.warning(f"未找到 point_id={settlement.point_id} 对应的观测点,跳过该记录")
|
||||
continue
|
||||
|
||||
# 3.2 通过 section_id 查询断面表,得到 account_id
|
||||
section = db.query(SectionData)\
|
||||
.filter(SectionData.section_id == checkpoint.section_id)\
|
||||
.first()
|
||||
if not section:
|
||||
logger.warning(f"未找到 section_id={checkpoint.section_id} 对应的断面,跳过该记录")
|
||||
continue
|
||||
|
||||
# 3.3 从断面数据中获取 account_id,作为 user_id
|
||||
user_id = int(section.account_id) if section.account_id else None
|
||||
if not user_id:
|
||||
logger.warning(f"断面 section_id={checkpoint.section_id} 没有 account_id,跳过该记录")
|
||||
continue
|
||||
|
||||
# 如果提供了 account_id 筛选条件,则只处理匹配的记录
|
||||
if account_id is not None and user_id != account_id:
|
||||
continue
|
||||
|
||||
# 3.4 构建 daily 记录
|
||||
daily_record = DailyData(
|
||||
user_id=user_id,
|
||||
account_id=user_id,
|
||||
point_id=settlement.point_id,
|
||||
NYID=settlement.NYID,
|
||||
linecode=linecode,
|
||||
section_id=checkpoint.section_id,
|
||||
remaining=0,
|
||||
is_all=0
|
||||
)
|
||||
daily_records.append(daily_record)
|
||||
|
||||
if not daily_records:
|
||||
logger.warning(f"没有生成任何 daily 记录")
|
||||
return []
|
||||
|
||||
# 4. 批量插入 daily 记录
|
||||
created_records = self.batch_create_by_account_nyid(db, daily_records)
|
||||
|
||||
logger.info(f"成功生成 {len(created_records)} 条 daily 记录")
|
||||
return created_records
|
||||
|
||||
except ValueError:
|
||||
raise
|
||||
except Exception as e:
|
||||
logger.error(f"生成 daily 数据失败:{str(e)}", exc_info=True)
|
||||
raise
|
||||
Reference in New Issue
Block a user