2026-03-25 23:59:29 +08:00
|
|
|
"""
|
|
|
|
|
物料管理路由模块
|
|
|
|
|
|
|
|
|
|
提供物料价格历史和供应商关联管理功能,包括:
|
|
|
|
|
- 物料价格历史记录
|
|
|
|
|
- 物料供应商关联管理
|
|
|
|
|
- 物料价格趋势分析
|
|
|
|
|
|
|
|
|
|
路由前缀: /api/materials
|
|
|
|
|
"""
|
2026-09-22 16:13:45 +08:00
|
|
|
from fastapi import APIRouter, Depends, Query
|
2026-03-25 23:59:29 +08:00
|
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
2026-09-22 16:13:45 +08:00
|
|
|
from typing import List
|
2026-03-25 23:59:29 +08:00
|
|
|
|
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
|
2026-09-17 16:15:49 +08:00
|
|
|
from shared.models.identity import User
|
2026-07-13 17:44:50 +08:00
|
|
|
from ..schemas import (
|
2026-03-25 23:59:29 +08:00
|
|
|
MaterialPriceHistoryCreate,
|
|
|
|
|
MaterialPriceHistoryResponse,
|
|
|
|
|
MaterialSupplierCreate,
|
|
|
|
|
MaterialSupplierResponse,
|
|
|
|
|
MaterialPriceTrendResponse
|
|
|
|
|
)
|
2026-09-22 16:13:45 +08:00
|
|
|
from ..services.material_service import material_service
|
2026-03-25 23:59:29 +08:00
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
):
|
2026-09-22 16:13:45 +08:00
|
|
|
return await material_service.add_price_history(db_session, product_id, price_data, current_user)
|
2026-03-25 23:59:29 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@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)
|
|
|
|
|
):
|
2026-09-22 16:13:45 +08:00
|
|
|
return await material_service.get_price_history(db_session, product_id, limit)
|
2026-03-25 23:59:29 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@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)
|
|
|
|
|
):
|
2026-09-22 16:13:45 +08:00
|
|
|
return await material_service.get_price_trend(db_session, product_id, months)
|
2026-03-25 23:59:29 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@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)
|
|
|
|
|
):
|
2026-09-22 16:13:45 +08:00
|
|
|
return await material_service.add_material_supplier(db_session, product_id, supplier_data, current_user)
|
2026-03-25 23:59:29 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@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)
|
|
|
|
|
):
|
2026-09-22 16:13:45 +08:00
|
|
|
return await material_service.get_material_suppliers(db_session, product_id)
|
2026-03-25 23:59:29 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@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)
|
|
|
|
|
):
|
2026-09-22 16:13:45 +08:00
|
|
|
return await material_service.remove_material_supplier(db_session, supplier_id, current_user)
|
2026-03-25 23:59:29 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@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)
|
|
|
|
|
):
|
2026-09-22 16:13:45 +08:00
|
|
|
return await material_service.get_supplier_materials(db_session, supplier_id)
|