This commit is contained in:
Андрей Дувакин 2024-10-08 10:05:00 +05:00
parent f7301f9635
commit 644653c08d
3 changed files with 73 additions and 75 deletions

View File

@ -566,10 +566,6 @@ export const updateDeliveryOrderRoute = async (
estimatedRouteTime, estimatedRouteTime,
route route
) => { ) => {
console.log({
estimated_route_time_in_minutes: estimatedRouteTime,
route: route,
});
try { try {
const response = await axios.put( const response = await axios.put(
`${API_URL}/delivery-orders/${deliveryOrderId}/route`, `${API_URL}/delivery-orders/${deliveryOrderId}/route`,

View File

@ -53,84 +53,84 @@ const DeliveryOrdersList = ({
}; };
const calculateRoutes = async () => { const calculateRoutes = async () => {
const ordersToCalculate = deliveryOrders.filter( const ordersToCalculate = deliveryOrders.filter(
(order) => !order.estimated_route_time_in_minutes (order) => !order.estimated_route_time_in_minutes
); );
setCalculatingRoutes(ordersToCalculate.map((order) => order.id)); setCalculatingRoutes(ordersToCalculate.map((order) => order.id));
await Promise.all( await Promise.all(
ordersToCalculate.map(async (order) => { ordersToCalculate.map(async (order) => {
try { try {
const deliveryOrderDetails = await getDeliveryOrderDetails(order.id); const deliveryOrderDetails = await getDeliveryOrderDetails(order.id);
const accessories = await getDeliveryAccessories( const accessories = await getDeliveryAccessories(
deliveryOrderDetails.id deliveryOrderDetails.id
); );
const coords = await Promise.all( const coords = await Promise.all(
accessories.map(async (accessory) => { accessories.map(async (accessory) => {
if (accessory.latitude && accessory.longitude) { if (accessory.latitude && accessory.longitude) {
return { return {
city: accessory.city_name, city: accessory.city_name,
latitude: accessory.latitude, latitude: accessory.latitude,
longitude: accessory.longitude, longitude: accessory.longitude,
accessory_name: accessory_name:
accessory.accessory_name + accessory.accessory_name +
Math.round( Math.round(
(accessory.accessory_volume * accessory.count) / 100 (accessory.accessory_volume * accessory.count) / 100
) + ) +
"шт.", "шт.",
}; };
} else { } else {
const coords = await getCoordinates(accessory.city_name); const coords = await getCoordinates(accessory.city_name);
return { return {
city: accessory.city_name, city: accessory.city_name,
accessory_name: accessory_name:
accessory.accessory_name + accessory.accessory_name +
": " + ": " +
Math.round( Math.round(
(accessory.accessory_volume * accessory.count) / 100 (accessory.accessory_volume * accessory.count) / 100
) + ) +
"шт.", "шт.",
...coords, ...coords,
}; };
}
})
);
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 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 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 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
);
} }
} catch (error) {
console.error("Ошибка при расчете маршрута для подзаказа:", error);
} }
} catch (error) { })
console.error("Ошибка при расчете маршрута для подзаказа:", error); );
}
})
);
fetchDeliveryOrders();
};
await fetchDeliveryOrders();
setCalculatingRoutes([]);
};
const ordersToCalculate = deliveryOrders.filter( const ordersToCalculate = deliveryOrders.filter(
(order) => !order.estimated_route_time_in_minutes (order) => !order.estimated_route_time_in_minutes

View File

@ -124,6 +124,7 @@ const Home = () => {
<th>Статус</th> <th>Статус</th>
<th>Количество роботов</th> <th>Количество роботов</th>
<th>Количество этапов</th> <th>Количество этапов</th>
<th>Итоговая цена</th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
@ -131,6 +132,7 @@ const Home = () => {
<td>{order.status_name || "Неизвестный статус"}</td> <td>{order.status_name || "Неизвестный статус"}</td>
<td>{order.count_robots}</td> <td>{order.count_robots}</td>
<td>{deliveryOrdersCount || 0}</td> <td>{deliveryOrdersCount || 0}</td>
<td>{order.price || 0} руб.</td>
</tr> </tr>
</tbody> </tbody>
</table> </table>