Add GET /api/public/db/vehicle-plates for unauthenticated fill/plan pages and remove manual plate entry plus auto-create. Co-authored-by: Cursor <cursoragent@cursor.com>
67 lines
2.2 KiB
Python
67 lines
2.2 KiB
Python
"""地磅公共只读:免登车牌 options(现场填写页)"""
|
|
from __future__ import annotations
|
|
|
|
from typing import Optional
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, Query
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.core.database import get_db
|
|
from app.models.db_models import DbInboundOutbound, DbPurchasePlan
|
|
from app.models.pl_models import PlVehicle
|
|
|
|
router = APIRouter(prefix="/public/db", tags=["地磅-公共车牌"])
|
|
|
|
|
|
def _resolve_tenant_no(
|
|
db: Session,
|
|
inventory_id: Optional[str],
|
|
plan_id: Optional[str],
|
|
) -> str:
|
|
if inventory_id:
|
|
row = (
|
|
db.query(DbInboundOutbound)
|
|
.filter(DbInboundOutbound.id == inventory_id, DbInboundOutbound.del_flag == 0)
|
|
.first()
|
|
)
|
|
if not row:
|
|
raise HTTPException(status_code=404, detail="出入库单不存在")
|
|
return row.tenant_no
|
|
if plan_id:
|
|
plan = (
|
|
db.query(DbPurchasePlan)
|
|
.filter(DbPurchasePlan.id == plan_id, DbPurchasePlan.del_flag == 0)
|
|
.first()
|
|
)
|
|
if not plan:
|
|
raise HTTPException(status_code=404, detail="计划不存在")
|
|
return plan.tenant_no
|
|
raise HTTPException(status_code=400, detail="需要提供 inventoryId 或 planId")
|
|
|
|
|
|
@router.get("/vehicle-plates")
|
|
def list_public_vehicle_plates(
|
|
q: Optional[str] = Query(None),
|
|
inventory_id: Optional[str] = Query(None, alias="inventoryId"),
|
|
plan_id: Optional[str] = Query(None, alias="planId"),
|
|
db: Session = Depends(get_db),
|
|
):
|
|
"""免登:按出入库/计划解析 tenant_no,返回 pl_vehicle 车牌(self + carrier)。"""
|
|
tenant_no = _resolve_tenant_no(db, inventory_id, plan_id)
|
|
query = db.query(PlVehicle).filter(
|
|
PlVehicle.tenant_no == tenant_no,
|
|
PlVehicle.del_flag == 0,
|
|
PlVehicle.vehicle_type.in_(["self", "carrier"]),
|
|
)
|
|
kw = (q or "").strip()
|
|
if kw:
|
|
query = query.filter(PlVehicle.license_plate.contains(kw))
|
|
rows = (
|
|
query.order_by(PlVehicle.create_time.desc()).limit(100).all()
|
|
)
|
|
return [
|
|
{"licensePlate": r.license_plate, "vehicleType": r.vehicle_type}
|
|
for r in rows
|
|
if r.license_plate
|
|
]
|