This commit is contained in:
Андрей Дувакин 2024-10-08 09:52:31 +05:00
parent 6ef4ec1fda
commit f7301f9635

View File

@ -53,84 +53,67 @@ const DeliveryOrdersList = ({
};
const calculateRoutes = async () => {
const ordersToCalculate = deliveryOrders.filter(
(order) => !order.estimated_route_time_in_minutes
);
const ordersToCalculate = deliveryOrders.filter(
(order) => !order.estimated_route_time_in_minutes
);
setCalculatingRoutes(ordersToCalculate.map((order) => order.id));
setCalculatingRoutes(ordersToCalculate.map((order) => order.id));
await Promise.all(
ordersToCalculate.map(async (order) => {
try {
const deliveryOrderDetails = await getDeliveryOrderDetails(order.id);
const accessories = await getDeliveryAccessories(
deliveryOrderDetails.id
);
await Promise.all(
ordersToCalculate.map(async (order) => {
try {
const deliveryOrderDetails = await getDeliveryOrderDetails(order.id);
const accessories = await getDeliveryAccessories(
deliveryOrderDetails.id
);
const coords = await Promise.all(
accessories.map(async (accessory) => {
if (accessory.latitude && accessory.longitude) {
return {
city: accessory.city_name,
latitude: accessory.latitude,
longitude: accessory.longitude,
accessory_name:
accessory.accessory_name +
Math.round(
(accessory.accessory_volume * accessory.count) / 100
) +
"шт.",
};
} else {
const coords = await getCoordinates(accessory.city_name);
return {
city: accessory.city_name,
accessory_name:
accessory.accessory_name +
": " +
Math.round(
(accessory.accessory_volume * accessory.count) / 100
) +
"шт.",
...coords,
};
}
})
);
if (fullCoordinates.length > 1) {
const waypoints = fullCoordinates
.map(({ longitude, latitude }) => `${longitude},${latitude}`)
.join(";");
const routeUrl = `https://router.project-osrm.org/route/v1/driving/${waypoints}?overview=full`;
const response = await fetch(routeUrl);
const data = await response.json();
if (data.routes && data.routes.length > 0) {
const geometry = data.routes[0].geometry;
const decodedRoute = polyline.decode(geometry);
const duration = data.routes[0].duration;
await updateDeliveryOrderRoute(
order.id,
duration / 60,
decodedRoute
);
const coords = await Promise.all(
accessories.map(async (accessory) => {
if (accessory.latitude && accessory.longitude) {
return {
city: accessory.city_name,
latitude: accessory.latitude,
longitude: accessory.longitude,
accessory_name:
accessory.accessory_name +
Math.round(
(accessory.accessory_volume * accessory.count) / 100
) +
"шт.",
};
} else {
const coords = await getCoordinates(accessory.city_name);
return {
city: accessory.city_name,
accessory_name:
accessory.accessory_name +
": " +
Math.round(
(accessory.accessory_volume * accessory.count) / 100
) +
"шт.",
...coords,
};
}
}
})
);
const fullCoordinates = [...coords, DELIVERY_CITY];
const fullCoordinates = [...coords, DELIVERY_CITY];
if (fullCoordinates.length > 1) {
const waypoints = fullCoordinates
.map(({ longitude, latitude }) => `${longitude},${latitude}`)
.join(";");
const routeUrl = `https://router.project-osrm.org/route/v1/driving/${waypoints}?overview=full`;
const routeUrl = `https://router.project-osrm.org/route/v1/driving/${order.start_longitude},${order.start_latitude};${order.end_longitude},${order.end_latitude}?overview=full`;
const response = await fetch(routeUrl);
const data = await response.json();
if (data.routes && data.routes.length > 0) {
const duration = data.routes[0].duration;
const geometry = data.routes[0].geometry;
const decodedRoute = polyline.decode(geometry);
const duration = data.routes[0].duration;
await updateDeliveryOrderRoute(
order.id,
@ -138,14 +121,16 @@ const DeliveryOrdersList = ({
decodedRoute
);
}
} catch (error) {
console.error("Ошибка при расчете маршрута для подзаказа:", error);
}
})
);
} catch (error) {
console.error("Ошибка при расчете маршрута для подзаказа:", error);
}
})
);
fetchDeliveryOrders();
};
fetchDeliveryOrders();
};
const ordersToCalculate = deliveryOrders.filter(
(order) => !order.estimated_route_time_in_minutes