This commit is contained in:
Андрей Дувакин 2024-10-07 19:10:03 +05:00
parent a7f8683b31
commit 0871e2f005
3 changed files with 21 additions and 11 deletions

View File

@ -0,0 +1,6 @@
from pydantic import BaseModel
class CalculateTotalOrderRequest(BaseModel):
deadline: str
robotsCount: int

View File

@ -19,6 +19,7 @@ from app.infrastructure.database.repository.truck_repository import TrucksReposi
class NewTotalOrderService:
def __init__(self, db: Session):
self.robotsCount = None
self.total_order_repository = TotalOrdersRepository(db)
self.delivery_order_repository = DeliveryOrdersRepository(db)
self.delivery_accessory_repository = DeliveryAccessoriesRepository(db)
@ -26,7 +27,8 @@ class NewTotalOrderService:
self.accessory_repository = AccessoriesRepository(db)
self.truck_repository = TrucksRepository(db)
def total_calculate(self, current_user):
def total_calculate(self, current_user, deadline, robotsCount):
self.robotsCount = robotsCount
accessories = self.accessory_repository.get_with_cities_all()
cities = self.city_repository.get_all_with_federal_district()
@ -39,7 +41,7 @@ class NewTotalOrderService:
with connect() as session:
new_total_order = TotalOrder(
order_datetime=datetime.now(),
count_robots=2000,
count_robots=robotsCount,
deadline=datetime.now(),
price=0,
user_id=current_user.id,
@ -48,7 +50,6 @@ class NewTotalOrderService:
session.add(new_total_order)
session.flush()
for period, routes in routes_by_period.items():
for route in routes:
vehicle_count, selected_truck = self.calculate_vehicle_requirements(
@ -92,7 +93,6 @@ class NewTotalOrderService:
session.commit()
def find_routes_by_period(self, grouped_by_period, distances, cities):
purpose_city_id = self.city_repository.get_by_name('Челябинск').id
@ -171,14 +171,13 @@ class NewTotalOrderService:
for i in range(len(route) - 1):
if route[i].id != purpose_city_id:
city_accessories = [accessory for accessory in accessories if accessory.city_id == route[i].id]
total_weight += sum(accessory.weight for accessory in city_accessories) * 20.0
total_weight += sum(accessory.weight for accessory in city_accessories) * self.robotsCount / 100
total_cost += distances[route[i].id - 1][route[i + 1].id - 1] * (total_weight / 1000.0) * 8
return total_cost
@staticmethod
def calculate_vehicle_requirements(accessories, trucks):
total_weight = sum(accessory.weight for accessory in accessories) * 20
def calculate_vehicle_requirements(self, accessories, trucks):
total_weight = sum(accessory.weight for accessory in accessories) * self.robotsCount / 100
total_vehicles = float('inf')
selected_truck = 0

View File

@ -3,6 +3,7 @@ from typing import List
from fastapi import APIRouter, HTTPException, Depends
from sqlalchemy.orm import Session
from app.core.entities.calculate_total_order_request import CalculateTotalOrderRequest
from app.core.usecases.auth_service import verify_token
from app.core.usecases.new_total_order_service import NewTotalOrderService
from app.infrastructure.database.dependencies import get_db
@ -58,7 +59,11 @@ def delete_total_order(total_order_id: int, db: Session = Depends(get_db),
@router.post("/total-orders/calculate")
def calculate_total_order(db: Session = Depends(get_db), current_user: User = Depends(verify_token)):
def calculate_total_order(
request: CalculateTotalOrderRequest,
db: Session = Depends(get_db),
current_user: User = Depends(verify_token)
):
service = NewTotalOrderService(db)
service.total_calculate(current_user)
return "OK"
service.total_calculate(current_user, request.deadline, request.robotsCount)
return {"status": "OK"}