提交
This commit is contained in:
1 parent
299609706c
commit
e46c0480d4
87 files changed
+11975
-15543
No files matched your search
@@ -1,10 +1,9 @@
|
||||
"""地磅 API 路由聚合 — REST 风格,协议对齐原 simple-dibang-xiaoshou。"""
|
||||
from fastapi import APIRouter
|
||||
|
||||
from app.api.db import dashboard, inventory, plans, vehicles, weighing
|
||||
from app.api.db import dashboard, inventory, plans, weighing
|
||||
|
||||
db_router = APIRouter(tags=["地磅管理"])
|
||||
db_router.include_router(vehicles.router)
|
||||
db_router.include_router(plans.router)
|
||||
db_router.include_router(inventory.router)
|
||||
db_router.include_router(weighing.router)
|
||||
|
||||
@@ -11,9 +11,8 @@ from sqlalchemy.orm import Session
|
||||
from app.core.database import get_db
|
||||
from app.core.db_auth import DbUserContext, get_db_current_user, get_db_current_user_optional
|
||||
from app.core.tenant_context import get_tenant_no
|
||||
from app.core.utils import generate_uuid
|
||||
from app.services.db.inventory_id import next_inbound_outbound_id
|
||||
from app.models.db_models import DbInboundOutbound, DbPurchasePlan, DbVehicle, DbWeighingSlip
|
||||
from app.models.db_models import DbInboundOutbound, DbPurchasePlan, DbWeighingSlip
|
||||
from app.schemas.db.inventory import (
|
||||
CreateLinkForPlanPayload,
|
||||
InboundOutboundCreate,
|
||||
@@ -114,40 +113,6 @@ def _get_inventory(db: Session, tenant_no: str, inventory_id: str) -> DbInboundO
|
||||
return row
|
||||
|
||||
|
||||
def _ensure_vehicle_exists(
|
||||
db: Session,
|
||||
tenant_no: str,
|
||||
vehicle_no: str,
|
||||
*,
|
||||
create_by: Optional[str] = None,
|
||||
create_dept_id: Optional[str] = None,
|
||||
) -> None:
|
||||
if not (vehicle_no and vehicle_no.strip()):
|
||||
return
|
||||
normalized = vehicle_no.strip().lower()
|
||||
exists = (
|
||||
db.query(DbVehicle)
|
||||
.filter(
|
||||
DbVehicle.tenant_no == tenant_no,
|
||||
DbVehicle.del_flag == 0,
|
||||
func.lower(DbVehicle.vehicle_no) == normalized,
|
||||
)
|
||||
.first()
|
||||
)
|
||||
if exists:
|
||||
return
|
||||
db.add(
|
||||
DbVehicle(
|
||||
id=generate_uuid(),
|
||||
tenant_no=tenant_no,
|
||||
vehicle_no=vehicle_no.strip(),
|
||||
remark=None,
|
||||
create_by=create_by,
|
||||
create_dept_id=create_dept_id or "",
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
def _apply_inventory_product_update(
|
||||
db: Session, tenant_no: str, row: DbInboundOutbound, payload_dict: dict
|
||||
) -> None:
|
||||
@@ -423,10 +388,6 @@ def update_inventory(
|
||||
row = _get_inventory_by_id(db, id)
|
||||
payload_dict = payload.model_dump(exclude_unset=True)
|
||||
|
||||
vehicle_no_updated = payload_dict.get("vehicle_no")
|
||||
if vehicle_no_updated is not None and str(vehicle_no_updated).strip():
|
||||
_ensure_vehicle_exists(db, row.tenant_no, str(vehicle_no_updated).strip())
|
||||
|
||||
_apply_inventory_product_update(db, row.tenant_no, row, payload_dict)
|
||||
_apply_inventory_update(row, payload_dict)
|
||||
_maybe_promote_third_party_status(row)
|
||||
|
||||
@@ -1,146 +0,0 @@
|
||||
from typing import Optional
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query, status
|
||||
from sqlalchemy import func, or_
|
||||
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.tenant_context import get_tenant_no
|
||||
from app.core.utils import generate_uuid
|
||||
from app.models.db_models import DbVehicle
|
||||
from app.schemas.db.master_data import VehicleCreate, VehicleRead, VehicleUpdate
|
||||
from app.services.db.mappers import vehicle_to_read
|
||||
|
||||
router = APIRouter(prefix="/master-data/vehicles", tags=["vehicles"])
|
||||
|
||||
|
||||
def _base_query(db: Session, tenant_no: str):
|
||||
return db.query(DbVehicle).filter(
|
||||
DbVehicle.tenant_no == tenant_no,
|
||||
DbVehicle.del_flag == 0,
|
||||
)
|
||||
|
||||
|
||||
@router.get("/", response_model=list[VehicleRead])
|
||||
def list_vehicles(
|
||||
q: Optional[str] = Query(default=None, description="模糊搜索车牌号"),
|
||||
db: Session = Depends(get_db),
|
||||
user: DbUserContext = Depends(get_db_current_user),
|
||||
) -> list[VehicleRead]:
|
||||
tenant_no = get_tenant_no(db, user.to_tenant_user_dict())
|
||||
query = _base_query(db, tenant_no).order_by(DbVehicle.create_time.desc())
|
||||
if q:
|
||||
keyword = q.lower()
|
||||
query = query.filter(
|
||||
or_(
|
||||
func.lower(DbVehicle.vehicle_no).contains(keyword),
|
||||
func.lower(DbVehicle.remark).contains(keyword),
|
||||
)
|
||||
)
|
||||
rows = query.all()
|
||||
return [vehicle_to_read(row) for row in rows]
|
||||
|
||||
|
||||
@router.get("/{vehicle_id}", response_model=VehicleRead)
|
||||
def get_vehicle(
|
||||
vehicle_id: str,
|
||||
db: Session = Depends(get_db),
|
||||
user: DbUserContext = Depends(get_db_current_user),
|
||||
) -> VehicleRead:
|
||||
tenant_no = get_tenant_no(db, user.to_tenant_user_dict())
|
||||
row = _get_vehicle(db, tenant_no, vehicle_id)
|
||||
return vehicle_to_read(row)
|
||||
|
||||
|
||||
@router.post("/", response_model=VehicleRead, status_code=status.HTTP_201_CREATED)
|
||||
def create_vehicle(
|
||||
payload: VehicleCreate,
|
||||
db: Session = Depends(get_db),
|
||||
user: DbUserContext = Depends(get_db_current_user),
|
||||
) -> VehicleRead:
|
||||
tenant_no = get_tenant_no(db, user.to_tenant_user_dict())
|
||||
_ensure_unique_vehicle_no(db, tenant_no, payload.vehicle_no)
|
||||
row = DbVehicle(
|
||||
id=generate_uuid(),
|
||||
tenant_no=tenant_no,
|
||||
vehicle_no=payload.vehicle_no.strip(),
|
||||
remark=payload.remarks,
|
||||
create_by=user.id,
|
||||
create_dept_id=user.dept_id,
|
||||
)
|
||||
db.add(row)
|
||||
db.commit()
|
||||
db.refresh(row)
|
||||
return vehicle_to_read(row)
|
||||
|
||||
|
||||
@router.put("/{vehicle_id}", response_model=VehicleRead)
|
||||
def update_vehicle(
|
||||
vehicle_id: str,
|
||||
payload: VehicleUpdate,
|
||||
db: Session = Depends(get_db),
|
||||
user: DbUserContext = Depends(get_db_current_user),
|
||||
) -> VehicleRead:
|
||||
tenant_no = get_tenant_no(db, user.to_tenant_user_dict())
|
||||
row = _get_vehicle(db, tenant_no, vehicle_id)
|
||||
if payload.vehicle_no and payload.vehicle_no.strip().lower() != row.vehicle_no.lower():
|
||||
_ensure_unique_vehicle_no(db, tenant_no, payload.vehicle_no)
|
||||
row.vehicle_no = payload.vehicle_no.strip()
|
||||
if payload.remarks is not None:
|
||||
row.remark = payload.remarks
|
||||
row.update_by = user.id
|
||||
db.commit()
|
||||
db.refresh(row)
|
||||
return vehicle_to_read(row)
|
||||
|
||||
|
||||
@router.patch("/{vehicle_id}", response_model=VehicleRead)
|
||||
def patch_vehicle(
|
||||
vehicle_id: str,
|
||||
payload: VehicleUpdate,
|
||||
db: Session = Depends(get_db),
|
||||
user: DbUserContext = Depends(get_db_current_user),
|
||||
) -> VehicleRead:
|
||||
return update_vehicle(vehicle_id, payload, db, user)
|
||||
|
||||
|
||||
@router.delete("/{vehicle_id}", status_code=status.HTTP_204_NO_CONTENT)
|
||||
def delete_vehicle(
|
||||
vehicle_id: str,
|
||||
db: Session = Depends(get_db),
|
||||
user: DbUserContext = Depends(get_db_current_user),
|
||||
) -> None:
|
||||
tenant_no = get_tenant_no(db, user.to_tenant_user_dict())
|
||||
row = _get_vehicle(db, tenant_no, vehicle_id)
|
||||
row.del_flag = 1
|
||||
row.update_by = user.id
|
||||
db.commit()
|
||||
|
||||
|
||||
def _get_vehicle(db: Session, tenant_no: str, vehicle_id: str) -> DbVehicle:
|
||||
row = (
|
||||
_base_query(db, tenant_no)
|
||||
.filter(DbVehicle.id == vehicle_id)
|
||||
.one_or_none()
|
||||
)
|
||||
if row is None:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail=f"Vehicle {vehicle_id} not found",
|
||||
)
|
||||
return row
|
||||
|
||||
|
||||
def _ensure_unique_vehicle_no(db: Session, tenant_no: str, vehicle_no: str) -> None:
|
||||
normalized = vehicle_no.strip().lower()
|
||||
exists = (
|
||||
_base_query(db, tenant_no)
|
||||
.filter(func.lower(DbVehicle.vehicle_no) == normalized)
|
||||
.first()
|
||||
)
|
||||
if exists:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_409_CONFLICT,
|
||||
detail="Vehicle number already exists",
|
||||
)
|
||||
Reference in new issue
Block a user