数据接口完善
This commit is contained in:
@@ -1,13 +1,12 @@
|
||||
from sqlalchemy.orm import Session
|
||||
from typing import List, Optional, Dict, Any
|
||||
from ..models.checkpoint import Checkpoint
|
||||
from ..models.section_data import SectionData
|
||||
from .base import BaseService
|
||||
from .section_data import SectionDataService
|
||||
|
||||
class CheckpointService(BaseService[Checkpoint]):
|
||||
def __init__(self):
|
||||
super().__init__(Checkpoint)
|
||||
self.section_service = SectionDataService()
|
||||
|
||||
def get_by_section_id(self, db: Session, section_id: str) -> List[Checkpoint]:
|
||||
"""根据断面ID获取观测点"""
|
||||
@@ -33,6 +32,11 @@ class CheckpointService(BaseService[Checkpoint]):
|
||||
|
||||
return self.search_by_conditions(db, conditions)
|
||||
|
||||
def _check_section_exists(self, db: Session, section_id: str) -> bool:
|
||||
"""检查断面是否存在"""
|
||||
section = db.query(SectionData).filter(SectionData.section_id == section_id).first()
|
||||
return section is not None
|
||||
|
||||
def batch_import_checkpoints(self, db: Session, data: List) -> Dict[str, Any]:
|
||||
"""
|
||||
批量导入观测点数据,根据观测点ID判断是否重复,重复数据改为更新操作
|
||||
@@ -56,10 +60,8 @@ class CheckpointService(BaseService[Checkpoint]):
|
||||
|
||||
for item_data in data:
|
||||
try:
|
||||
|
||||
# 判断断面id是否存在
|
||||
section = self.section_service.get_by_section_id(db, item_data.get('section_id'))
|
||||
if not section:
|
||||
if not self._check_section_exists(db, item_data.get('section_id')):
|
||||
logger.error(f"Section {item_data.get('section_id')} not found")
|
||||
raise Exception(f"Section {item_data.get('section_id')} not found")
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ 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
|
||||
|
||||
class LevelDataService(BaseService[LevelData]):
|
||||
def __init__(self):
|
||||
@@ -30,13 +31,18 @@ class LevelDataService(BaseService[LevelData]):
|
||||
|
||||
return self.search_by_conditions(db, conditions)
|
||||
|
||||
def get_by_nyid_and_linecode(self, db: Session, nyid: str, linecode: str) -> Optional[LevelData]:
|
||||
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,
|
||||
LevelData.linecode == linecode
|
||||
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和线路编码判断是否重复,重复数据改为更新操作
|
||||
@@ -59,10 +65,17 @@ class LevelDataService(BaseService[LevelData]):
|
||||
|
||||
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('NYID'),
|
||||
item_data.get('linecode')
|
||||
# item_data.get('linecode'),
|
||||
nyid=item_data.get('NYID')
|
||||
)
|
||||
if level_data:
|
||||
# 更新操作
|
||||
|
||||
@@ -2,6 +2,7 @@ from sqlalchemy.orm import Session
|
||||
from typing import List, Optional, Dict, Any
|
||||
from ..models.original_data import OriginalData
|
||||
from .base import BaseService
|
||||
from ..models.settlement_data import SettlementData
|
||||
|
||||
class OriginalDataService(BaseService[OriginalData]):
|
||||
def __init__(self):
|
||||
@@ -33,6 +34,11 @@ class OriginalDataService(BaseService[OriginalData]):
|
||||
|
||||
return self.search_by_conditions(db, conditions)
|
||||
|
||||
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_original_data(self, db: Session, data: List) -> Dict[str, Any]:
|
||||
"""
|
||||
批量导入原始数据,直接新增,无需检查重复
|
||||
@@ -55,6 +61,14 @@ class OriginalDataService(BaseService[OriginalData]):
|
||||
|
||||
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")
|
||||
|
||||
|
||||
# 直接新增操作
|
||||
original_data = OriginalData(
|
||||
bfpcode=item_data.get('bfpcode'),
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
from sqlalchemy.orm import Session
|
||||
from typing import List, Optional, Dict, Any
|
||||
from ..models.settlement_data import SettlementData
|
||||
from ..models.checkpoint import Checkpoint
|
||||
from .base import BaseService
|
||||
|
||||
class SettlementDataService(BaseService[SettlementData]):
|
||||
@@ -15,11 +16,11 @@ class SettlementDataService(BaseService[SettlementData]):
|
||||
"""根据期数ID获取沉降数据"""
|
||||
return self.get_by_field(db, "NYID", nyid)
|
||||
|
||||
def get_by_point_and_nyid(self, db: Session, point_id: str, nyid: str) -> Optional[SettlementData]:
|
||||
def get_by_point_and_nyid(self, db: Session, point_id: str = None, nyid: str = None) -> Optional[SettlementData]:
|
||||
"""根据观测点ID和期数ID获取沉降数据"""
|
||||
return db.query(SettlementData).filter(
|
||||
SettlementData.point_id == point_id,
|
||||
SettlementData.NYID == nyid
|
||||
SettlementData.point_id == point_id if point_id else True,
|
||||
SettlementData.NYID == nyid if nyid else True
|
||||
).first()
|
||||
|
||||
def search_settlement_data(self, db: Session,
|
||||
@@ -40,9 +41,15 @@ class SettlementDataService(BaseService[SettlementData]):
|
||||
|
||||
return self.search_by_conditions(db, conditions)
|
||||
|
||||
def _check_checkpoint_exists(self, db: Session, point_id: str) -> bool:
|
||||
"""检查观测点数据是否存在"""
|
||||
checkpoint = db.query(Checkpoint).filter(Checkpoint.point_id == point_id).first()
|
||||
return checkpoint is not None
|
||||
|
||||
def batch_import_settlement_data(self, db: Session, data: List) -> Dict[str, Any]:
|
||||
"""
|
||||
批量导入沉降数据,根据观测点ID和期数ID判断是否重复,重复数据改为更新操作
|
||||
判断观测点数据是否存在,不存在则全部不导入
|
||||
支持事务回滚,失败时重试一次
|
||||
"""
|
||||
import logging
|
||||
@@ -62,10 +69,18 @@ class SettlementDataService(BaseService[SettlementData]):
|
||||
|
||||
for item_data in data:
|
||||
try:
|
||||
|
||||
# 判断观测点数据是否存在
|
||||
checkpoint = self._check_checkpoint_exists(db, item_data.get('point_id'))
|
||||
logger.info(f"Checkpoint {item_data.get('point_id')}: {checkpoint}")
|
||||
if not checkpoint:
|
||||
logger.error(f"Checkpoint {item_data.get('point_id')} not found")
|
||||
raise Exception(f"Checkpoint {item_data.get('point_id')} not found")
|
||||
|
||||
settlement = self.get_by_point_and_nyid(
|
||||
db,
|
||||
item_data.get('point_id'),
|
||||
item_data.get('NYID')
|
||||
# item_data.get('point_id'),
|
||||
nyid=item_data.get('NYID')
|
||||
)
|
||||
if settlement:
|
||||
# 更新操作
|
||||
|
||||
Reference in New Issue
Block a user