拆分物品库存与借出登记
This commit is contained in:
+40
-14
@@ -32,7 +32,7 @@ class ItemIn(BaseModel):
|
||||
return value.strip()
|
||||
|
||||
|
||||
class LoanIn(BaseModel):
|
||||
class LoanBaseIn(BaseModel):
|
||||
quantity: int = Field(ge=1, le=100000)
|
||||
reason: str = Field(min_length=1, max_length=500)
|
||||
contact: str = Field(min_length=1, max_length=200)
|
||||
@@ -44,6 +44,16 @@ class LoanIn(BaseModel):
|
||||
return value.strip()
|
||||
|
||||
|
||||
class LoanIn(LoanBaseIn):
|
||||
item_name: str = Field(min_length=1, max_length=100)
|
||||
|
||||
@field_validator('item_name')
|
||||
@classmethod
|
||||
def clean_item_name(cls, value):
|
||||
if not value.strip(): raise ValueError('借出物品不能为空')
|
||||
return value.strip()
|
||||
|
||||
|
||||
class CheckIn(BaseModel):
|
||||
quantities: dict[str, int]
|
||||
note: str = Field(default='', max_length=1000)
|
||||
@@ -308,24 +318,40 @@ def install(app, get_db, current_user, photo_root, visible_clause):
|
||||
await db.execute(insert(center_items).values(id=key, **payload.model_dump(), created_at=now_ms())); await db.commit()
|
||||
return {'id': key}
|
||||
|
||||
@app.post('/api/center/items/{item_id}/loans')
|
||||
async def borrow(item_id: str, payload: LoanIn, db=Depends(get_db), viewer=Depends(current_user)):
|
||||
await db.execute(text('BEGIN IMMEDIATE'))
|
||||
item=(await db.execute(select(center_items).where(center_items.c.id==item_id))).mappings().first()
|
||||
if not item: raise HTTPException(404,'物品不存在')
|
||||
used=(await db.execute(select(func.coalesce(func.sum(center_loans.c.quantity),0)).where(center_loans.c.item_id==item_id, center_loans.c.status=='approved', center_loans.c.returned_at.is_(None)))).scalar()
|
||||
if payload.quantity > item['quantity']-used: raise HTTPException(409,'可借数量不足,请刷新')
|
||||
@app.post('/api/center/loans')
|
||||
async def record_loan(payload: LoanIn, db=Depends(get_db), viewer=Depends(current_user)):
|
||||
"""仅记录借出,不扣减库存,也不要求物品已加入库存清单。"""
|
||||
key=uuid.uuid4().hex
|
||||
await db.execute(insert(center_loans).values(
|
||||
id=key,item_id=item_id,user_id=viewer['id'],**payload.model_dump(),
|
||||
status='approved',created_at=now_ms()
|
||||
id=key,item_id=None,user_id=viewer['id'],**payload.model_dump(),
|
||||
status='recorded',created_at=now_ms()
|
||||
))
|
||||
await db.commit(); return {'id':key,'status':'approved'}
|
||||
await db.commit(); return {'id':key,'status':'recorded'}
|
||||
|
||||
@app.post('/api/center/items/{item_id}/loans')
|
||||
async def record_linked_loan_compat(item_id: str, payload: LoanBaseIn, db=Depends(get_db), viewer=Depends(current_user)):
|
||||
"""兼容旧调用:仍关联库存名称,但只登记,不再占用库存。"""
|
||||
item=(await db.execute(select(center_items).where(center_items.c.id==item_id))).mappings().first()
|
||||
if not item: raise HTTPException(404,'物品不存在')
|
||||
key=uuid.uuid4().hex
|
||||
await db.execute(insert(center_loans).values(
|
||||
id=key,item_id=item_id,item_name=None,user_id=viewer['id'],**payload.model_dump(),
|
||||
status='recorded',created_at=now_ms()
|
||||
))
|
||||
await db.commit(); return {'id':key,'status':'recorded'}
|
||||
|
||||
@app.get('/api/center/loans')
|
||||
async def loans(status: str='all', db=Depends(get_db), viewer=Depends(current_user)):
|
||||
stmt=select(center_loans,center_items.c.name.label('item_name'),users.c.display_name).select_from(center_loans).join(center_items,center_items.c.id==center_loans.c.item_id).join(users,users.c.id==center_loans.c.user_id)
|
||||
if status=='borrowed':stmt=stmt.where(center_loans.c.status=='approved')
|
||||
stmt=select(
|
||||
center_loans.c.id, center_loans.c.item_id,
|
||||
func.coalesce(center_loans.c.item_name,center_items.c.name).label('item_name'),
|
||||
center_loans.c.user_id, center_loans.c.quantity, center_loans.c.reason,
|
||||
center_loans.c.contact, center_loans.c.created_at, center_loans.c.returned_at,
|
||||
center_loans.c.status, center_loans.c.reviewer_id, center_loans.c.reviewed_at,
|
||||
center_loans.c.review_note, users.c.display_name,
|
||||
).select_from(center_loans).outerjoin(center_items,center_items.c.id==center_loans.c.item_id).join(users,users.c.id==center_loans.c.user_id)
|
||||
if status=='recorded':stmt=stmt.where(center_loans.c.status=='recorded')
|
||||
elif status in ('approved','borrowed'):stmt=stmt.where(center_loans.c.status=='approved')
|
||||
elif status=='returned':stmt=stmt.where(center_loans.c.status==status)
|
||||
elif status!='all':raise HTTPException(422,'无效借用状态')
|
||||
rows=(await db.execute(stmt.order_by(center_loans.c.created_at.desc()).limit(200))).mappings().all()
|
||||
@@ -337,7 +363,7 @@ def install(app, get_db, current_user, photo_root, visible_clause):
|
||||
if not row:raise HTTPException(404,'借用记录不存在')
|
||||
if row['user_id']!=viewer['id']:raise HTTPException(403,'仅借用人可以登记归还')
|
||||
if row['status']=='returned':return {'ok':True}
|
||||
if row['status']!='approved':raise HTTPException(409,'仅已批准的借用可归还')
|
||||
if row['status']!='approved':raise HTTPException(409,'仅历史库存借出记录可归还')
|
||||
await db.execute(update(center_loans).where(center_loans.c.id==loan_id,center_loans.c.returned_at.is_(None)).values(returned_at=now_ms(),status='returned'))
|
||||
await db.commit(); return {'ok':True}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user