This commit is contained in:
Андрей Дувакин 2024-10-08 14:18:02 +05:00
parent c53fdbb063
commit 3f5b2528bf

View File

@ -54,7 +54,16 @@ class NewTotalOrderService:
for route in routes: for route in routes:
vehicle_count, selected_truck = self.calculate_vehicle_requirements( vehicle_count, selected_truck = self.calculate_vehicle_requirements(
[a for a in accessories if a.period == period], trucks) [a for a in accessories if a.period == period], trucks)
cost = self.calculate_route_cost(route, accessories, distances) route_length = self.calculate_route_length(route, distances)
selected_truck_capacity = list(
filter(
lambda truck: truck.id == selected_truck,
trucks
)
)[0].capacity
cost = route_length * 8 * selected_truck_capacity / 1000
delivery_order_deadline = new_total_order.deadline - timedelta(days=period) delivery_order_deadline = new_total_order.deadline - timedelta(days=period)
@ -64,7 +73,7 @@ class NewTotalOrderService:
order_datetime=datetime.now(), order_datetime=datetime.now(),
count_trucks=vehicle_count, count_trucks=vehicle_count,
deadline=delivery_order_deadline, deadline=delivery_order_deadline,
price=cost, price=cost * vehicle_count,
truck_id=selected_truck, truck_id=selected_truck,
total_order_id=new_total_order.id, total_order_id=new_total_order.id,
) )
@ -112,7 +121,7 @@ class NewTotalOrderService:
sum(accessory.weight for accessory in accessories if accessory.city_id == city.id) sum(accessory.weight for accessory in accessories if accessory.city_id == city.id)
for city in route for city in route
) )
cost = self.calculate_route_cost(route, accessories, distances) cost = self.calculate_route_length(route, distances)
routes_with_details.append((route, (distance, total_weight, cost))) routes_with_details.append((route, (distance, total_weight, cost)))
sorted_routes = sorted(routes_with_details, key=lambda x: (x[1][2], x[1][0])) sorted_routes = sorted(routes_with_details, key=lambda x: (x[1][2], x[1][0]))
@ -162,22 +171,17 @@ class NewTotalOrderService:
total_distance += distances[route[i].id - 1][route[i + 1].id - 1] total_distance += distances[route[i].id - 1][route[i + 1].id - 1]
return total_distance return total_distance
def calculate_route_cost(self, route, accessories, distances): @staticmethod
purpose_city_id = self.city_repository.get_by_name('Челябинск').id def calculate_route_length(route, distances):
total_length = 0.0
total_weight = 0.0
total_cost = 0.0
for i in range(len(route) - 1): for i in range(len(route) - 1):
if route[i].id != purpose_city_id: total_length += distances[route[i].id - 1][route[i + 1].id - 1]
city_accessories = [accessory for accessory in accessories if accessory.city_id == route[i].id]
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 return total_length
def calculate_vehicle_requirements(self, accessories, trucks): def calculate_vehicle_requirements(self, accessories, trucks):
total_weight = sum(accessory.weight for accessory in accessories) * self.robotsCount / 100 total_weight = sum(accessory.weight / 100 for accessory in accessories) * self.robotsCount
total_vehicles = float('inf') total_vehicles = float('inf')
selected_truck = 0 selected_truck = 0