35 lines
1023 B
Python
35 lines
1023 B
Python
from fastapi import HTTPException
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.models.finished_goods import FinishedGoodsLocation
|
|
from app.schemas.location import LocationRequest
|
|
|
|
|
|
def register_location(db: Session, req: LocationRequest) -> FinishedGoodsLocation:
|
|
existing = (
|
|
db.query(FinishedGoodsLocation)
|
|
.filter(FinishedGoodsLocation.zongpai_no == req.zongpai_no)
|
|
.first()
|
|
)
|
|
if existing:
|
|
raise HTTPException(
|
|
status_code=409,
|
|
detail={
|
|
"error_code": "DUPLICATE_LOCATION",
|
|
"message": "该总排号已存在货位记录",
|
|
"detail": {
|
|
"existing_location": existing.location_code,
|
|
"created_at": existing.created_at.isoformat(),
|
|
},
|
|
},
|
|
)
|
|
|
|
record = FinishedGoodsLocation(
|
|
zongpai_no=req.zongpai_no,
|
|
location_code=req.location_code,
|
|
)
|
|
db.add(record)
|
|
db.commit()
|
|
db.refresh(record)
|
|
return record
|