From 0871e2f005c99b1245a75ed2ee8f190442e258c3 Mon Sep 17 00:00:00 2001 From: andrei Date: Mon, 7 Oct 2024 19:10:03 +0500 Subject: [PATCH] ._. --- .../entities/calculate_total_order_request.py | 6 ++++++ app/core/usecases/new_total_order_service.py | 15 +++++++-------- app/infrastructure/fastapi/total_order_routes.py | 11 ++++++++--- 3 files changed, 21 insertions(+), 11 deletions(-) create mode 100644 app/core/entities/calculate_total_order_request.py diff --git a/app/core/entities/calculate_total_order_request.py b/app/core/entities/calculate_total_order_request.py new file mode 100644 index 0000000..06d127e --- /dev/null +++ b/app/core/entities/calculate_total_order_request.py @@ -0,0 +1,6 @@ +from pydantic import BaseModel + + +class CalculateTotalOrderRequest(BaseModel): + deadline: str + robotsCount: int diff --git a/app/core/usecases/new_total_order_service.py b/app/core/usecases/new_total_order_service.py index 44d487b..381aa9b 100644 --- a/app/core/usecases/new_total_order_service.py +++ b/app/core/usecases/new_total_order_service.py @@ -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 diff --git a/app/infrastructure/fastapi/total_order_routes.py b/app/infrastructure/fastapi/total_order_routes.py index 5caed5f..a577f40 100644 --- a/app/infrastructure/fastapi/total_order_routes.py +++ b/app/infrastructure/fastapi/total_order_routes.py @@ -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"} \ No newline at end of file