2026-03-08 22:58:25 +08:00
|
|
|
"""
|
|
|
|
|
库存管理路由模块
|
|
|
|
|
|
|
|
|
|
提供库存信息的查询功能,包括:
|
|
|
|
|
- 库存列表查询(支持分页、仓库筛选、产品筛选、低库存筛选)
|
|
|
|
|
- 显示产品库存数量、锁定数量、可用数量等信息
|
|
|
|
|
|
|
|
|
|
路由前缀: /api/inventory
|
|
|
|
|
"""
|
|
|
|
|
from fastapi import APIRouter, Depends, Query
|
|
|
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
|
from sqlalchemy import select
|
|
|
|
|
from typing import Optional, List
|
|
|
|
|
|
|
|
|
|
from database.database import get_db_session
|
|
|
|
|
from services.auth_service import get_current_active_user
|
|
|
|
|
from models.database import User, Product, Warehouse, Inventory
|
|
|
|
|
from .schemas import InventoryResponse
|
|
|
|
|
|
|
|
|
|
router = APIRouter(prefix="/inventory", tags=["库存管理"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.get("", response_model=List[InventoryResponse])
|
|
|
|
|
async def list_inventory(
|
|
|
|
|
warehouse_id: Optional[int] = None,
|
|
|
|
|
product_id: Optional[int] = None,
|
|
|
|
|
low_stock: bool = False,
|
|
|
|
|
skip: int = Query(0, ge=0),
|
|
|
|
|
limit: int = Query(20, ge=1, le=100),
|
|
|
|
|
db_session: AsyncSession = Depends(get_db_session),
|
|
|
|
|
current_user: User = Depends(get_current_active_user)
|
|
|
|
|
):
|
|
|
|
|
query = (
|
|
|
|
|
select(Inventory, Product, Warehouse)
|
|
|
|
|
.join(Product, Inventory.product_id == Product.id)
|
|
|
|
|
.join(Warehouse, Inventory.warehouse_id == Warehouse.id)
|
|
|
|
|
.where(Product.is_active == True)
|
2026-03-15 22:50:38 +08:00
|
|
|
.where(Product.item_type == "material")
|
2026-03-08 22:58:25 +08:00
|
|
|
.where(Warehouse.is_active == True)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if warehouse_id:
|
|
|
|
|
query = query.where(Inventory.warehouse_id == warehouse_id)
|
|
|
|
|
if product_id:
|
|
|
|
|
query = query.where(Inventory.product_id == product_id)
|
|
|
|
|
if low_stock:
|
|
|
|
|
query = query.where(Inventory.quantity <= Product.min_stock)
|
|
|
|
|
|
|
|
|
|
query = query.offset(skip).limit(limit)
|
|
|
|
|
result = await db_session.execute(query)
|
|
|
|
|
|
|
|
|
|
inventory_list = []
|
|
|
|
|
for inv, product, warehouse in result.all():
|
|
|
|
|
inventory_list.append(InventoryResponse(
|
|
|
|
|
id=inv.id,
|
|
|
|
|
product_id=inv.product_id,
|
|
|
|
|
product_name=product.name,
|
|
|
|
|
product_sku=product.sku,
|
|
|
|
|
warehouse_id=inv.warehouse_id,
|
|
|
|
|
warehouse_name=warehouse.name,
|
|
|
|
|
quantity=inv.quantity,
|
|
|
|
|
locked_quantity=inv.locked_quantity,
|
|
|
|
|
available_quantity=inv.available_quantity
|
|
|
|
|
))
|
|
|
|
|
|
|
|
|
|
return inventory_list
|