""" 物料管理路由模块 提供物料价格历史和供应商关联管理功能,包括: - 物料价格历史记录 - 物料供应商关联管理 - 物料价格趋势分析 路由前缀: /api/materials """ from fastapi import APIRouter, Depends, Query from sqlalchemy.ext.asyncio import AsyncSession from typing import List from shared.database.database import get_db_session from shared.services.auth_service import get_current_active_user from shared.models.identity import User from ..schemas import ( MaterialPriceHistoryCreate, MaterialPriceHistoryResponse, MaterialSupplierCreate, MaterialSupplierResponse, MaterialPriceTrendResponse ) from ..services.material_service import material_service router = APIRouter(prefix="/materials", tags=["物料管理"]) @router.post("/{product_id}/price-history", response_model=MaterialPriceHistoryResponse, status_code=201) async def add_material_price_history( product_id: int, price_data: MaterialPriceHistoryCreate, db_session: AsyncSession = Depends(get_db_session), current_user: User = Depends(get_current_active_user) ): return await material_service.add_price_history(db_session, product_id, price_data, current_user) @router.get("/{product_id}/price-history", response_model=List[MaterialPriceHistoryResponse]) async def get_material_price_history( product_id: int, limit: int = Query(20, ge=1, le=100), db_session: AsyncSession = Depends(get_db_session), current_user: User = Depends(get_current_active_user) ): return await material_service.get_price_history(db_session, product_id, limit) @router.get("/{product_id}/price-trend", response_model=MaterialPriceTrendResponse) async def get_material_price_trend( product_id: int, months: int = Query(6, ge=1, le=24), db_session: AsyncSession = Depends(get_db_session), current_user: User = Depends(get_current_active_user) ): return await material_service.get_price_trend(db_session, product_id, months) @router.post("/{product_id}/suppliers", response_model=MaterialSupplierResponse, status_code=201) async def add_material_supplier( product_id: int, supplier_data: MaterialSupplierCreate, db_session: AsyncSession = Depends(get_db_session), current_user: User = Depends(get_current_active_user) ): return await material_service.add_material_supplier(db_session, product_id, supplier_data, current_user) @router.get("/{product_id}/suppliers", response_model=List[MaterialSupplierResponse]) async def get_material_suppliers( product_id: int, db_session: AsyncSession = Depends(get_db_session), current_user: User = Depends(get_current_active_user) ): return await material_service.get_material_suppliers(db_session, product_id) @router.delete("/suppliers/{supplier_id}") async def remove_material_supplier( supplier_id: int, db_session: AsyncSession = Depends(get_db_session), current_user: User = Depends(get_current_active_user) ): return await material_service.remove_material_supplier(db_session, supplier_id, current_user) @router.get("/suppliers/{supplier_id}/materials", response_model=List[MaterialSupplierResponse]) async def get_supplier_materials( supplier_id: int, db_session: AsyncSession = Depends(get_db_session), current_user: User = Depends(get_current_active_user) ): return await material_service.get_supplier_materials(db_session, supplier_id)