1.修改冬休逻辑

2.修改沉降数据接口,多加一个aname字段
This commit is contained in:
whm
2025-11-26 06:25:49 +08:00
parent 354beb769e
commit 3f4a22ce67
3 changed files with 51 additions and 14 deletions

View File

@@ -10,6 +10,7 @@ from ..models.level_data import LevelData
from ..models.section_data import SectionData from ..models.section_data import SectionData
import logging import logging
from datetime import datetime from datetime import datetime
from sqlalchemy import distinct
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
class SettlementDataService(BaseService[SettlementData]): class SettlementDataService(BaseService[SettlementData]):
@@ -48,7 +49,11 @@ class SettlementDataService(BaseService[SettlementData]):
skip: int = 0, skip: int = 0,
limit: Optional[int] = None) -> List[SettlementData]: limit: Optional[int] = None) -> List[SettlementData]:
"""根据多个条件搜索沉降数据,按上传时间倒序排序""" """根据多个条件搜索沉降数据,按上传时间倒序排序"""
query = db.query(SettlementData) # query = db.query(SettlementData)
# 返回数据多加一个 aname 字段
query = db.query(SettlementData, Checkpoint.aname).outerjoin(
Checkpoint, SettlementData.point_id == Checkpoint.point_id
)
if id is not None: if id is not None:
query = query.filter(SettlementData.id == id) query = query.filter(SettlementData.id == id)
@@ -69,8 +74,15 @@ class SettlementDataService(BaseService[SettlementData]):
query = query.offset(skip) query = query.offset(skip)
if limit is not None and limit > 0: if limit is not None and limit > 0:
query = query.limit(limit) query = query.limit(limit)
result_tuples = query.all()
settlement_data_list = []
for settlement, aname in result_tuples:
# 动态添加aname属性无匹配时为None
setattr(settlement, 'aname', aname)
settlement_data_list.append(settlement)
return query.all() return settlement_data_list
# return query.all()
def search_settlement_data_by_point_ids_formatted(self, db: Session, def search_settlement_data_by_point_ids_formatted(self, db: Session,
point_ids: List[str], point_ids: List[str],
@@ -176,7 +188,9 @@ class SettlementDataService(BaseService[SettlementData]):
limit: Optional[int] = None) -> Dict[str, Any]: limit: Optional[int] = None) -> Dict[str, Any]:
"""查询沉降数据并返回格式化结果,按上传时间倒序排序(支持分页)""" """查询沉降数据并返回格式化结果,按上传时间倒序排序(支持分页)"""
# 先获取总数(不计分页) # 先获取总数(不计分页)
count_query = db.query(SettlementData) count_query = db.query(distinct(SettlementData.id)).outerjoin(
Checkpoint, SettlementData.point_id == Checkpoint.point_id
)
if id is not None: if id is not None:
count_query = count_query.filter(SettlementData.id == id) count_query = count_query.filter(SettlementData.id == id)
if point_id is not None: if point_id is not None:
@@ -196,6 +210,7 @@ class SettlementDataService(BaseService[SettlementData]):
for settlement in settlement_data: for settlement in settlement_data:
settlement_dict = { settlement_dict = {
"id": settlement.id, "id": settlement.id,
"aname": settlement.aname,
"point_id": settlement.point_id, "point_id": settlement.point_id,
"CVALUE": settlement.CVALUE, "CVALUE": settlement.CVALUE,
"MAVALUE": settlement.MAVALUE, "MAVALUE": settlement.MAVALUE,

View File

@@ -2,8 +2,11 @@ from datetime import datetime
from typing import List, Dict from typing import List, Dict
import warnings import warnings
import copy import copy
# 注意:根据实际项目路径调整导入,若本地测试可注释掉
from ..core.logging_config import get_logger from ..core.logging_config import get_logger
logger = get_logger(__name__) logger = get_logger(__name__)
class ConstructionMonitorUtils: class ConstructionMonitorUtils:
def __init__(self): def __init__(self):
# 原始工况周期映射表(保持不变) # 原始工况周期映射表(保持不变)
@@ -33,7 +36,7 @@ class ConstructionMonitorUtils:
"轨道铺设完成后12个月以后": 180, "轨道铺设完成后12个月以后": 180,
"填筑或堆载,一般情况": 1, "填筑或堆载,一般情况": 1,
"填筑或堆载,沉降量突变情况": 1, "填筑或堆载,沉降量突变情况": 1,
"填筑或堆载,两次填筑间隔时间较长情况": 3, "填筑或堆载,两次填筑间隔时间较长情况": 3, # 该工况原周期为3天
"堆载预压或路基填筑完成第1至3个月": 7, "堆载预压或路基填筑完成第1至3个月": 7,
"堆载预压或路基填筑完成第4至6个月": 14, "堆载预压或路基填筑完成第4至6个月": 14,
"堆载预压或路基填筑完成6个月以后": 30, "堆载预压或路基填筑完成6个月以后": 30,
@@ -44,8 +47,7 @@ class ConstructionMonitorUtils:
"轨道板(道床)铺设后第2至3个月": 30, "轨道板(道床)铺设后第2至3个月": 30,
"轨道板(道床)铺设后3个月以后": 90, "轨道板(道床)铺设后3个月以后": 90,
"架桥机(运梁车) 首次通过后": 7, "架桥机(运梁车) 首次通过后": 7,
"架桥机(运梁车) 首次通过前":1, "架桥机(运梁车) 首次通过前": 1,
} }
# 构建中英文括号+逗号兼容映射表 # 构建中英文括号+逗号兼容映射表
self.compatible_periods = self._build_compatible_brackets_map() self.compatible_periods = self._build_compatible_brackets_map()
@@ -103,6 +105,8 @@ class ConstructionMonitorUtils:
continue continue
base_condition = None base_condition = None
# 新增:标记是否为冬休回溯到合法工况的场景
is_winter_break = False # 初始化冬休标识
if latest_condition != "冬休": if latest_condition != "冬休":
if latest_condition not in self.compatible_periods: if latest_condition not in self.compatible_periods:
result["error_data"].append(latest_item) result["error_data"].append(latest_item)
@@ -124,6 +128,7 @@ class ConstructionMonitorUtils:
base_condition = None base_condition = None
break break
base_condition = history_condition base_condition = history_condition
is_winter_break = True # 触发:冬休且回溯到合法历史工况
break break
item_copy = copy.deepcopy(latest_item) item_copy = copy.deepcopy(latest_item)
@@ -147,16 +152,29 @@ class ConstructionMonitorUtils:
result["winter"].append(item_copy) result["winter"].append(item_copy)
continue continue
period = self.compatible_periods[base_condition] # 核心修改:冬休回溯场景下调整测量间隔(基准周期)
original_period = self.compatible_periods[base_condition]
if is_winter_break:
# 规则1原周期为3天则改为7天规则2其他周期直接翻倍
if original_period == 3:
adjusted_period = 7
else:
adjusted_period = original_period * 2
else:
# 非冬休场景,使用原始周期
adjusted_period = original_period
# 基于调整后的周期计算剩余天数
days_passed = (calc_date - create_date).days days_passed = (calc_date - create_date).days
due_days = period - days_passed due_days = adjusted_period - days_passed
if due_days < 0: if due_days < 0:
item_copy["remaining"] = 0 - int(abs(due_days)) item_copy["remaining"] = 0 - int(abs(due_days))
result["error_data"].append(item_copy) result["error_data"].append(item_copy)
warn_msg = ( warn_msg = (
f"【超期警报】测点{point_idx} 最新工况'{latest_condition}'{create_date}" f"【超期警报】测点{point_idx} 最新工况'{latest_condition}'{create_date}"
f"已超期{abs(due_days)}天!基准工况:{base_condition}周期{period}" f"已超期{abs(due_days)}天!基准工况:{base_condition}"
f"原始周期{original_period}{',冬休调整后周期'+str(adjusted_period)+'' if is_winter_break else ''}"
) )
logger.warning(warn_msg) logger.warning(warn_msg)
warnings.warn(warn_msg, UserWarning) warnings.warn(warn_msg, UserWarning)

View File

@@ -241,6 +241,7 @@ def scheduled_get_max_nyid_by_point_id(start: int = 0,end: int = 0):
# 2. 计算到期数据 # 2. 计算到期数据
monitor = ConstructionMonitorUtils() monitor = ConstructionMonitorUtils()
daily_data = monitor.get_due_data(result,start=start,end=end) daily_data = monitor.get_due_data(result,start=start,end=end)
# time.sleep(10000)
data = daily_data['data'] data = daily_data['data']
error_data = daily_data['error_data'] error_data = daily_data['error_data']
@@ -249,10 +250,13 @@ def scheduled_get_max_nyid_by_point_id(start: int = 0,end: int = 0):
# 3. 循环处理冬休数据,追溯历史非冬休记录 # 3. 循环处理冬休数据,追溯历史非冬休记录
max_num = 1 max_num = 1
while winters: print(f"首次获取冬休数据完成,共{len(winters)}条记录")
while 1:
max_num += 1 max_num += 1
print(max_num)
# 提取冬休数据的point_id列表 # 提取冬休数据的point_id列表
new_list = [w['point_id'] for w in winters] new_list = [int(w['point_id']) for w in winters]
print(new_list)
# 获取更多历史记录 # 获取更多历史记录
nyid_list = daily_service.get_nyid_by_point_id(db, new_list, max_num) nyid_list = daily_service.get_nyid_by_point_id(db, new_list, max_num)
w_list = monitor.get_due_data(nyid_list,start=start,end=end) w_list = monitor.get_due_data(nyid_list,start=start,end=end)
@@ -262,14 +266,14 @@ def scheduled_get_max_nyid_by_point_id(start: int = 0,end: int = 0):
# 过期数据一并处理 # 过期数据一并处理
# data.extend(w_list['error_data']) # data.extend(w_list['error_data'])
error_data.extend(w_list['error_data']) error_data.extend(w_list['error_data'])
print(w_list) if winters == []:
break
data.extend(error_data) data.extend(error_data)
# 4. 初始化服务实例 # 4. 初始化服务实例
level_service = LevelDataService() level_service = LevelDataService()
checkpoint_db = CheckpointService() checkpoint_db = CheckpointService()
section_db = SectionDataService() section_db = SectionDataService()
account_service = AccountService() account_service = AccountService()
print(len(data))
# 5. 关联其他表数据(核心逻辑保留) # 5. 关联其他表数据(核心逻辑保留)
for d in data: for d in data:
# 处理 LevelData假设返回列表取第一条 # 处理 LevelData假设返回列表取第一条
@@ -294,7 +298,7 @@ def scheduled_get_max_nyid_by_point_id(start: int = 0,end: int = 0):
d['account_data'] = account_response.__dict__ if account_response else None d['account_data'] = account_response.__dict__ if account_response else None
else: else:
d['account_data'] = None d['account_data'] = None
print(f"一共有{len(data)}条数据{data}")
# 6. 构造DailyData数据并批量创建 # 6. 构造DailyData数据并批量创建
# daily_create_data1 = set() # daily_create_data1 = set()
daily_create_data = [] daily_create_data = []