109 lines
3.6 KiB
Python
109 lines
3.6 KiB
Python
"""小程序/门户登录态 · 运输计划单(现场记录/回传)"""
|
|
from __future__ import annotations
|
|
|
|
from typing import Optional
|
|
|
|
from fastapi import APIRouter, Depends, File, Form, HTTPException, Query, UploadFile
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.core.database import get_db
|
|
from app.core.db_auth import DbUserContext, get_db_current_user
|
|
from app.core.response import error_response, success_response
|
|
from app.core.tenant_context import get_tenant_no
|
|
from app.schemas.pl.transport import PublicTransportSiteStopFeedback
|
|
from app.services import pl_site_plan_service as site_svc
|
|
|
|
router = APIRouter(prefix="/site-plan", tags=["运输计划-现场计划单"])
|
|
|
|
|
|
@router.get("/history")
|
|
def get_site_plan_history(
|
|
date: str = Query(..., description="筛选日 YYYY-MM-DD"),
|
|
q: Optional[str] = Query(None, description="车牌号/产品名称模糊"),
|
|
db: Session = Depends(get_db),
|
|
user: DbUserContext = Depends(get_db_current_user),
|
|
):
|
|
tenant_no = get_tenant_no(db, user.to_tenant_user_dict())
|
|
page, err = site_svc.build_site_history_page(
|
|
db, tenant_no=tenant_no, date_str=date, keyword=q
|
|
)
|
|
if err:
|
|
return error_response(400, err)
|
|
return success_response(page.model_dump(by_alias=True))
|
|
|
|
|
|
@router.get("/calendar-counts")
|
|
def get_site_plan_calendar_counts(
|
|
month: str = Query(..., description="月份 YYYY-MM"),
|
|
db: Session = Depends(get_db),
|
|
user: DbUserContext = Depends(get_db_current_user),
|
|
):
|
|
tenant_no = get_tenant_no(db, user.to_tenant_user_dict())
|
|
counts, err = site_svc.build_site_calendar_counts(
|
|
db, tenant_no=tenant_no, month_str=month
|
|
)
|
|
if err:
|
|
return error_response(400, err)
|
|
return success_response({"month": month, "counts": counts or {}})
|
|
|
|
|
|
@router.get("/{plan_id}")
|
|
def get_site_plan_detail(
|
|
plan_id: str,
|
|
db: Session = Depends(get_db),
|
|
user: DbUserContext = Depends(get_db_current_user),
|
|
):
|
|
tenant_no = get_tenant_no(db, user.to_tenant_user_dict())
|
|
page, err = site_svc.build_site_page_for_plan(
|
|
db, tenant_no=tenant_no, plan_id=plan_id
|
|
)
|
|
if err or page is None:
|
|
raise HTTPException(status_code=404, detail=err or "计划不存在或无现场链接")
|
|
return success_response(page.model_dump(by_alias=True))
|
|
|
|
|
|
@router.post("/{plan_id}/feedback")
|
|
def save_site_plan_feedback(
|
|
plan_id: str,
|
|
payload: PublicTransportSiteStopFeedback,
|
|
db: Session = Depends(get_db),
|
|
user: DbUserContext = Depends(get_db_current_user),
|
|
):
|
|
tenant_no = get_tenant_no(db, user.to_tenant_user_dict())
|
|
ok, msg = site_svc.save_site_feedback_for_plan(
|
|
db,
|
|
tenant_no=tenant_no,
|
|
plan_id=plan_id,
|
|
stop_type=payload.stop_type,
|
|
stop_id=payload.stop_id,
|
|
quantity=payload.quantity,
|
|
attachment_urls=payload.attachment_urls,
|
|
remark=payload.remark,
|
|
)
|
|
if not ok:
|
|
return error_response(400, msg or "保存失败")
|
|
db.commit()
|
|
return success_response({"success": True, "message": "保存成功"})
|
|
|
|
|
|
@router.post("/{plan_id}/attachments")
|
|
def upload_site_plan_attachment(
|
|
plan_id: str,
|
|
file: UploadFile = File(...),
|
|
folder: Optional[str] = Form(""),
|
|
db: Session = Depends(get_db),
|
|
user: DbUserContext = Depends(get_db_current_user),
|
|
):
|
|
tenant_no = get_tenant_no(db, user.to_tenant_user_dict())
|
|
data, err = site_svc.upload_site_attachment_for_plan(
|
|
db,
|
|
tenant_no=tenant_no,
|
|
plan_id=plan_id,
|
|
file=file,
|
|
folder=folder or "",
|
|
)
|
|
if err:
|
|
code, msg = err
|
|
return error_response(code, msg)
|
|
return success_response(data)
|