2026-03-08 22:58:25 +08:00
|
|
|
"""
|
|
|
|
|
供应商管理路由模块
|
|
|
|
|
|
|
|
|
|
提供供应商信息的管理功能,包括:
|
|
|
|
|
- 供应商列表查询(支持分页、搜索)
|
|
|
|
|
- 创建新供应商(自动生成供应商编码)
|
2026-03-15 13:50:23 +08:00
|
|
|
- 更新供应商信息
|
|
|
|
|
- 删除供应商(软删除)
|
2026-03-08 22:58:25 +08:00
|
|
|
|
|
|
|
|
路由前缀: /api/suppliers
|
|
|
|
|
"""
|
2026-03-15 13:50:23 +08:00
|
|
|
from fastapi import APIRouter, Depends, Query, HTTPException
|
2026-03-08 22:58:25 +08:00
|
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
|
from sqlalchemy import select
|
|
|
|
|
from typing import Optional, List
|
|
|
|
|
from datetime import datetime
|
|
|
|
|
|
2026-05-29 18:10:08 +08:00
|
|
|
from shared.database.database import get_db_session
|
|
|
|
|
from shared.services.auth_service import get_current_active_user, get_current_admin_user
|
|
|
|
|
from shared.models.database import User, Supplier
|
2026-07-13 17:44:50 +08:00
|
|
|
from ..schemas import SupplierCreate, SupplierResponse
|
2026-03-08 22:58:25 +08:00
|
|
|
|
|
|
|
|
router = APIRouter(prefix="/suppliers", tags=["供应商管理"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.get("", response_model=List[SupplierResponse])
|
|
|
|
|
async def list_suppliers(
|
|
|
|
|
skip: int = Query(0, ge=0),
|
|
|
|
|
limit: int = Query(20, ge=1, le=100),
|
|
|
|
|
search: Optional[str] = None,
|
|
|
|
|
db_session: AsyncSession = Depends(get_db_session),
|
|
|
|
|
current_user: User = Depends(get_current_active_user)
|
|
|
|
|
):
|
|
|
|
|
query = select(Supplier).where(Supplier.is_active == True)
|
|
|
|
|
if search:
|
|
|
|
|
query = query.where(Supplier.name.ilike(f"%{search}%"))
|
|
|
|
|
query = query.offset(skip).limit(limit).order_by(Supplier.created_at.desc())
|
|
|
|
|
result = await db_session.execute(query)
|
|
|
|
|
return [SupplierResponse.from_orm(s) for s in result.scalars().all()]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.post("", response_model=SupplierResponse, status_code=201)
|
|
|
|
|
async def create_supplier(
|
|
|
|
|
supplier_data: SupplierCreate,
|
|
|
|
|
db_session: AsyncSession = Depends(get_db_session),
|
|
|
|
|
current_user: User = Depends(get_current_active_user)
|
|
|
|
|
):
|
|
|
|
|
data = supplier_data.dict()
|
|
|
|
|
if not data.get("code"):
|
|
|
|
|
data["code"] = f"S{datetime.now().strftime('%Y%m%d%H%M%S')}"
|
|
|
|
|
|
|
|
|
|
supplier = Supplier(**data)
|
|
|
|
|
db_session.add(supplier)
|
2026-07-30 10:30:50 +08:00
|
|
|
await db_session.flush()
|
2026-03-08 22:58:25 +08:00
|
|
|
await db_session.refresh(supplier)
|
|
|
|
|
return SupplierResponse.from_orm(supplier)
|
2026-03-15 13:50:23 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.put("/{supplier_id}", response_model=SupplierResponse)
|
|
|
|
|
async def update_supplier(
|
|
|
|
|
supplier_id: int,
|
|
|
|
|
supplier_data: SupplierCreate,
|
|
|
|
|
db_session: AsyncSession = Depends(get_db_session),
|
|
|
|
|
current_user: User = Depends(get_current_active_user)
|
|
|
|
|
):
|
|
|
|
|
result = await db_session.execute(select(Supplier).where(Supplier.id == supplier_id))
|
|
|
|
|
supplier = result.scalar_one_or_none()
|
|
|
|
|
if not supplier:
|
|
|
|
|
raise HTTPException(status_code=404, detail="供应商不存在")
|
|
|
|
|
|
|
|
|
|
for key, value in supplier_data.dict().items():
|
|
|
|
|
setattr(supplier, key, value)
|
|
|
|
|
|
2026-07-30 10:30:50 +08:00
|
|
|
await db_session.flush()
|
2026-03-15 13:50:23 +08:00
|
|
|
await db_session.refresh(supplier)
|
|
|
|
|
return SupplierResponse.from_orm(supplier)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.delete("/{supplier_id}")
|
|
|
|
|
async def delete_supplier(
|
|
|
|
|
supplier_id: int,
|
|
|
|
|
db_session: AsyncSession = Depends(get_db_session),
|
|
|
|
|
current_user: User = Depends(get_current_admin_user)
|
|
|
|
|
):
|
|
|
|
|
result = await db_session.execute(select(Supplier).where(Supplier.id == supplier_id))
|
|
|
|
|
supplier = result.scalar_one_or_none()
|
|
|
|
|
if not supplier:
|
|
|
|
|
raise HTTPException(status_code=404, detail="供应商不存在")
|
|
|
|
|
|
|
|
|
|
supplier.is_active = False
|
2026-07-30 10:30:50 +08:00
|
|
|
await db_session.flush()
|
2026-03-15 13:50:23 +08:00
|
|
|
return {"message": "供应商已删除"}
|