43 lines
1.8 KiB
Python
43 lines
1.8 KiB
Python
# app/routes/calculations.py
|
||
"""
|
||
Маршруты API для расчетов
|
||
Соответствует модулю 4 ТЗ по расчету материалов
|
||
"""
|
||
from fastapi import APIRouter, HTTPException
|
||
from app.models import MaterialCalculationRequest, MaterialCalculationResponse
|
||
import math
|
||
|
||
router = APIRouter()
|
||
|
||
@router.post("/calculate-material", response_model=MaterialCalculationResponse)
|
||
async def calculate_material(request: MaterialCalculationRequest):
|
||
"""
|
||
Расчет количества материала для производства продукции
|
||
Соответствует модулю 4 ТЗ
|
||
"""
|
||
try:
|
||
# Валидация входных параметров
|
||
if (request.param1 <= 0 or request.param2 <= 0 or
|
||
request.product_coeff <= 0 or request.defect_percent < 0):
|
||
return MaterialCalculationResponse(
|
||
material_quantity=-1,
|
||
status="error: invalid parameters"
|
||
)
|
||
|
||
# Расчет количества материала на одну единицу продукции
|
||
material_per_unit = request.param1 * request.param2 * request.product_coeff
|
||
|
||
# Расчет общего количества материала с учетом брака
|
||
total_material = material_per_unit * request.quantity
|
||
total_material_with_defect = total_material * (1 + request.defect_percent / 100)
|
||
|
||
# Округление до целого числа в большую сторону
|
||
material_quantity = math.ceil(total_material_with_defect)
|
||
|
||
return MaterialCalculationResponse(
|
||
material_quantity=material_quantity,
|
||
status="success"
|
||
)
|
||
|
||
except Exception as e:
|
||
raise HTTPException(status_code=500, detail=str(e))
|