标段获取水准数据
This commit is contained in:
34
app/api/level_data.py
Normal file
34
app/api/level_data.py
Normal file
@@ -0,0 +1,34 @@
|
||||
from fastapi import APIRouter, Depends, HTTPException, status
|
||||
from sqlalchemy.orm import Session
|
||||
from typing import List
|
||||
from ..core.database import get_db
|
||||
from ..core.response_code import ResponseCode, ResponseMessage
|
||||
from ..schemas.level_data import (
|
||||
LevelDataRequest,
|
||||
LevelDataListResponse,
|
||||
LevelDataResponse
|
||||
)
|
||||
from ..services.level_data import LevelDataService
|
||||
|
||||
router = APIRouter(prefix="/level_data", tags=["水准数据"])
|
||||
|
||||
@router.post("/get_level_data_by_project", response_model=LevelDataListResponse)
|
||||
def get_level_data_by_project(request: LevelDataRequest, db: Session = Depends(get_db)):
|
||||
"""
|
||||
通过标段名称获取全部水准线路
|
||||
"""
|
||||
try:
|
||||
level_service = LevelDataService()
|
||||
level_data_list = level_service.get_level_data_by_project_name(db, request.project_name)
|
||||
|
||||
return LevelDataListResponse(
|
||||
code=ResponseCode.SUCCESS,
|
||||
message="查询成功",
|
||||
total=len(level_data_list),
|
||||
data=level_data_list
|
||||
)
|
||||
except Exception as e:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail=f"查询失败: {str(e)}"
|
||||
)
|
||||
@@ -13,6 +13,7 @@ from .api.comprehensive_data import router as comprehensive_data_router
|
||||
from .api.export_excel import router as export_excel_router
|
||||
from .api.daily import router as daily_router
|
||||
from .api.section_data import router as section_data_router
|
||||
from .api.level_data import router as level_data_router
|
||||
from .utils.scheduler import task_scheduler
|
||||
|
||||
# 初始化日志系统
|
||||
@@ -74,6 +75,7 @@ app.include_router(comprehensive_data_router, prefix="/api")
|
||||
app.include_router(export_excel_router, prefix="/api")
|
||||
app.include_router(daily_router, prefix="/api")
|
||||
app.include_router(section_data_router, prefix="/api")
|
||||
app.include_router(level_data_router, prefix="/api")
|
||||
# app.include_router(test_router, prefix="/api")
|
||||
|
||||
# 根路径
|
||||
|
||||
31
app/schemas/level_data.py
Normal file
31
app/schemas/level_data.py
Normal file
@@ -0,0 +1,31 @@
|
||||
from pydantic import BaseModel, Field, ConfigDict
|
||||
from typing import Optional, List, Any
|
||||
from datetime import datetime
|
||||
|
||||
class LevelDataBase(BaseModel):
|
||||
"""水准数据基础模型"""
|
||||
linecode: Optional[str] = None
|
||||
benchmarkids: Optional[str] = None
|
||||
wsphigh: Optional[str] = None
|
||||
NYID: Optional[str] = None
|
||||
mtype: Optional[str] = None
|
||||
|
||||
class LevelDataResponse(LevelDataBase):
|
||||
"""水准数据响应模型"""
|
||||
id: int
|
||||
createDate: Optional[datetime] = None
|
||||
|
||||
model_config = ConfigDict(
|
||||
from_attributes=True
|
||||
)
|
||||
|
||||
class LevelDataRequest(BaseModel):
|
||||
"""水准数据请求模型"""
|
||||
project_name: str = Field(..., description="标段名称")
|
||||
|
||||
class LevelDataListResponse(BaseModel):
|
||||
"""水准数据列表响应格式"""
|
||||
code: int = 0
|
||||
message: str
|
||||
total: int
|
||||
data: List[LevelDataResponse] = []
|
||||
@@ -3,6 +3,12 @@ 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):
|
||||
@@ -140,4 +146,84 @@ class LevelDataService(BaseService[LevelData]):
|
||||
'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
|
||||
Reference in New Issue
Block a user