229 lines
10 KiB
Python
229 lines
10 KiB
Python
from sqlalchemy.orm import Session
|
||
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):
|
||
super().__init__(LevelData)
|
||
|
||
def get_by_nyid(self, db: Session, nyid: str) -> List[LevelData]:
|
||
"""根据期数ID获取水准数据"""
|
||
return self.get_by_field(db, "NYID", nyid)
|
||
|
||
def get_by_nyids(self, db: Session, nyids: List[str]) -> List[LevelData]:
|
||
"""根据多个期数ID获取水准数据"""
|
||
return db.query(LevelData).filter(LevelData.NYID.in_(nyids)).all()
|
||
|
||
def get_by_linecode(self, db: Session, linecode: str) -> List[LevelData]:
|
||
"""根据水准线路编码获取水准数据"""
|
||
return self.get_by_field(db, "linecode", linecode)
|
||
|
||
def search_level_data(self, db: Session,
|
||
id: Optional[str] = None,
|
||
linecode: Optional[str] = None,
|
||
nyid: Optional[str] = None,
|
||
benchmarkids: Optional[str] = None) -> List[LevelData]:
|
||
"""根据多个条件搜索水准数据"""
|
||
conditions = {}
|
||
if linecode is not None:
|
||
conditions["linecode"] = linecode
|
||
if nyid is not None:
|
||
conditions["NYID"] = nyid
|
||
if benchmarkids is not None:
|
||
conditions["benchmarkids"] = benchmarkids
|
||
if id is not None:
|
||
conditions["id"] = id
|
||
|
||
return self.search_by_conditions(db, conditions)
|
||
|
||
|
||
def get_by_nyid_and_linecode(self, db: Session, nyid: str, linecode: str = None) -> Optional[LevelData]:
|
||
"""根据期数ID和线路编码获取水准数据"""
|
||
return db.query(LevelData).filter(
|
||
LevelData.NYID == nyid if nyid else True,
|
||
LevelData.linecode == linecode if linecode else True
|
||
).first()
|
||
|
||
def _check_settlement_exists(self, db: Session, nyid: str) -> bool:
|
||
"""检查期数id沉降数据是否存在"""
|
||
settlement = db.query(SettlementData).filter(SettlementData.NYID == nyid).first()
|
||
return settlement is not None
|
||
|
||
def batch_import_level_data(self, db: Session, data: List) -> Dict[str, Any]:
|
||
"""
|
||
批量导入水准数据,根据期数ID和线路编码判断是否重复,重复数据改为更新操作
|
||
支持事务回滚,失败时重试一次
|
||
"""
|
||
import logging
|
||
logger = logging.getLogger(__name__)
|
||
|
||
total_count = len(data)
|
||
success_count = 0
|
||
failed_count = 0
|
||
failed_items = []
|
||
|
||
for attempt in range(2): # 最多重试1次
|
||
try:
|
||
db.begin()
|
||
success_count = 0
|
||
failed_count = 0
|
||
failed_items = []
|
||
|
||
for item_data in data:
|
||
try:
|
||
|
||
# 判断期数id沉降数据是否存在
|
||
settlement = self._check_settlement_exists(db, item_data.get('NYID'))
|
||
if not settlement:
|
||
logger.error(f"Settlement {item_data.get('NYID')} not found")
|
||
raise Exception(f"Settlement {item_data.get('NYID')} not found")
|
||
|
||
level_data = self.get_by_nyid_and_linecode(
|
||
db,
|
||
# item_data.get('linecode'),
|
||
nyid=item_data.get('NYID')
|
||
)
|
||
if level_data:
|
||
# 更新操作
|
||
level_data.benchmarkids = item_data.get('benchmarkids')
|
||
level_data.wsphigh = item_data.get('wsphigh')
|
||
level_data.mtype = item_data.get('mtype')
|
||
level_data.createDate = item_data.get('createDate')
|
||
logger.info(f"Updated level data: {item_data.get('linecode')}-{item_data.get('NYID')}")
|
||
else:
|
||
# 新增操作
|
||
level_data = LevelData(
|
||
linecode=item_data.get('linecode'),
|
||
benchmarkids=item_data.get('benchmarkids'),
|
||
wsphigh=item_data.get('wsphigh'),
|
||
mtype=item_data.get('mtype'),
|
||
NYID=item_data.get('NYID'),
|
||
createDate=item_data.get('createDate')
|
||
)
|
||
db.add(level_data)
|
||
logger.info(f"Created level data: {item_data.get('linecode')}-{item_data.get('NYID')}")
|
||
|
||
success_count += 1
|
||
except Exception as e:
|
||
failed_count += 1
|
||
failed_items.append({
|
||
'data': item_data,
|
||
'error': str(e)
|
||
})
|
||
logger.error(f"Failed to process level data {item_data.get('linecode')}-{item_data.get('NYID')}: {str(e)}")
|
||
raise e
|
||
|
||
db.commit()
|
||
logger.info(f"Batch import level data completed. Success: {success_count}, Failed: {failed_count}")
|
||
break
|
||
|
||
except Exception as e:
|
||
db.rollback()
|
||
logger.warning(f"Batch import attempt {attempt + 1} failed: {str(e)}")
|
||
if attempt == 1: # 最后一次重试失败
|
||
logger.error("Batch import level data failed after retries")
|
||
return {
|
||
'success': False,
|
||
'message': f'批量导入失败: {str(e)}',
|
||
'total_count': total_count,
|
||
'success_count': 0,
|
||
'failed_count': total_count,
|
||
'failed_items': failed_items
|
||
}
|
||
|
||
return {
|
||
'success': True,
|
||
'message': '批量导入完成' if failed_count == 0 else f'部分导入失败',
|
||
'total_count': total_count,
|
||
'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 |