90 lines
3.0 KiB
Python
90 lines
3.0 KiB
Python
# -*- coding: utf-8 -*-
|
||
from fastapi import APIRouter, Depends, HTTPException, status
|
||
from sqlalchemy.orm import Session
|
||
from typing import Dict, Any
|
||
from ..core.database import get_db
|
||
from ..core.response_code import ResponseCode
|
||
from ..schemas.daily import LinecodeToDailyRequest
|
||
from ..services.daily import DailyDataService
|
||
import logging
|
||
|
||
logger = logging.getLogger(__name__)
|
||
|
||
router = APIRouter(prefix="/daily", tags=["日常数据管理"])
|
||
|
||
# 实例化服务类
|
||
daily_service = DailyDataService()
|
||
|
||
|
||
@router.post("/create_from_linecode", response_model=Dict[str, Any])
|
||
def create_daily_data_from_linecode(
|
||
request: LinecodeToDailyRequest,
|
||
db: Session = Depends(get_db)
|
||
):
|
||
"""
|
||
综合业务接口:通过水准线路编码生成 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 对象,插入到数据库表
|
||
|
||
返回:成功插入的记录数
|
||
"""
|
||
try:
|
||
logger.info(f"开始处理 linecode={request.linecode} 的 daily 数据生成请求")
|
||
|
||
# 调用服务层方法
|
||
created_records = daily_service.create_daily_from_linecode(
|
||
db=db,
|
||
linecode=request.linecode,
|
||
account_id=request.account_id
|
||
)
|
||
|
||
if not created_records:
|
||
return {
|
||
"code": ResponseCode.SUCCESS,
|
||
"message": "没有生成任何 daily 记录",
|
||
"total": 0,
|
||
"data": []
|
||
}
|
||
|
||
logger.info(f"成功生成 {len(created_records)} 条 daily 记录")
|
||
|
||
return {
|
||
"code": ResponseCode.SUCCESS,
|
||
"message": f"成功生成 {len(created_records)} 条 daily 记录",
|
||
"total": len(created_records),
|
||
"data": [
|
||
{
|
||
"id": record.id,
|
||
"user_id": record.user_id,
|
||
"account_id": record.account_id,
|
||
"point_id": record.point_id,
|
||
"NYID": record.NYID,
|
||
"linecode": record.linecode,
|
||
"section_id": record.section_id,
|
||
"remaining": record.remaining,
|
||
"is_all": record.is_all
|
||
}
|
||
for record in created_records
|
||
]
|
||
}
|
||
|
||
except ValueError as e:
|
||
logger.warning(str(e))
|
||
raise HTTPException(
|
||
status_code=status.HTTP_404_NOT_FOUND,
|
||
detail=str(e)
|
||
)
|
||
except HTTPException:
|
||
raise
|
||
except Exception as e:
|
||
logger.error(f"生成 daily 数据失败:{str(e)}", exc_info=True)
|
||
raise HTTPException(
|
||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||
detail=f"生成 daily 数据失败:{str(e)}"
|
||
)
|