Add box_mode field to BoxSaveRequest and DuplicateZongpaiBindError exception. In one-to-one mode, prevent the same zongpai from being packed into multiple boxes, returning 409 with existing box info. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
126 lines
3.8 KiB
Python
126 lines
3.8 KiB
Python
import sys
|
|
|
|
from fastapi import FastAPI, Request
|
|
from fastapi.exceptions import RequestValidationError
|
|
from fastapi.responses import JSONResponse
|
|
|
|
from config.settings import load_settings
|
|
|
|
# 加载配置文件,失败则退出
|
|
try:
|
|
settings = load_settings()
|
|
except FileNotFoundError as e:
|
|
print(f"配置文件错误: {e}")
|
|
sys.exit(1)
|
|
except Exception as e:
|
|
print(f"加载配置失败: {e}")
|
|
sys.exit(1)
|
|
|
|
from app.api.v1.location import router as location_router
|
|
from app.api.v1.box import router as box_router
|
|
from app.services.location_service import DuplicateLocationError
|
|
from app.services.box_service import ZongpaiNotFoundError, DuplicateBoxItemError, InvalidZongpaiError, DuplicateZongpaiBindError
|
|
|
|
app = FastAPI(title="CargoTrace API", version="0.1.0")
|
|
|
|
app.include_router(location_router, prefix="/CargoTrace")
|
|
app.include_router(box_router, prefix="/CargoTrace")
|
|
|
|
|
|
@app.exception_handler(DuplicateLocationError)
|
|
async def duplicate_location_handler(request: Request, exc: DuplicateLocationError):
|
|
return JSONResponse(
|
|
status_code=409,
|
|
content={
|
|
"error_code": "DUPLICATE_LOCATION",
|
|
"message": "该总排号已存在货位记录",
|
|
"location_code": exc.location_code,
|
|
"registered_at": exc.registered_at,
|
|
},
|
|
)
|
|
|
|
|
|
@app.exception_handler(RequestValidationError)
|
|
async def validation_error_handler(request: Request, exc: RequestValidationError):
|
|
for err in exc.errors():
|
|
msg = err.get("msg", "")
|
|
if "INVALID_ZONGPAI" in msg or "INVALID_LOCATION" in msg or "INVALID_BOX_NO" in msg or "INVALID_QUANTITY" in msg:
|
|
error_code = msg.replace("Value error, ", "")
|
|
return JSONResponse(
|
|
status_code=400,
|
|
content={
|
|
"error_code": error_code,
|
|
"message": error_code,
|
|
},
|
|
)
|
|
return JSONResponse(
|
|
status_code=422,
|
|
content={"detail": exc.errors()},
|
|
)
|
|
|
|
|
|
@app.exception_handler(ValueError)
|
|
async def value_error_handler(request: Request, exc: ValueError):
|
|
error_code = str(exc)
|
|
return JSONResponse(
|
|
status_code=400,
|
|
content={
|
|
"error_code": error_code,
|
|
"message": error_code,
|
|
},
|
|
)
|
|
|
|
|
|
@app.exception_handler(InvalidZongpaiError)
|
|
async def invalid_zongpai_handler(request: Request, exc: InvalidZongpaiError):
|
|
return JSONResponse(
|
|
status_code=400,
|
|
content={
|
|
"error_code": "INVALID_ZONGPAI",
|
|
"message": "总排号格式不合法",
|
|
},
|
|
)
|
|
|
|
|
|
@app.exception_handler(ZongpaiNotFoundError)
|
|
async def zongpai_not_found_handler(request: Request, exc: ZongpaiNotFoundError):
|
|
return JSONResponse(
|
|
status_code=404,
|
|
content={
|
|
"error_code": "ZONGPAI_NOT_FOUND",
|
|
"message": "未找到该总排号对应的排产号信息",
|
|
},
|
|
)
|
|
|
|
|
|
@app.exception_handler(DuplicateBoxItemError)
|
|
async def duplicate_box_handler(request: Request, exc: DuplicateBoxItemError):
|
|
return JSONResponse(
|
|
status_code=409,
|
|
content={
|
|
"error_code": "DUPLICATE_BOX_NO",
|
|
"message": f"排产号 {exc.paichan_no} 下箱号 {exc.box_no} 已存在",
|
|
"paichan_no": exc.paichan_no,
|
|
"box_no": exc.box_no,
|
|
},
|
|
)
|
|
|
|
|
|
@app.exception_handler(DuplicateZongpaiBindError)
|
|
async def duplicate_zongpai_bind_handler(request: Request, exc: DuplicateZongpaiBindError):
|
|
return JSONResponse(
|
|
status_code=409,
|
|
content={
|
|
"error_code": "DUPLICATE_ZONGPAI_BIND",
|
|
"message": f"总排号 {exc.zongpai_no} 已绑定箱号 {exc.box_no},请勿重复装箱",
|
|
"paichan_no": exc.paichan_no,
|
|
"box_no": exc.box_no,
|
|
"zongpai_no": exc.zongpai_no,
|
|
},
|
|
)
|
|
|
|
|
|
@app.get("/")
|
|
async def root():
|
|
return {"message": "Welcome to CargoTrace API"}
|