public async Task ProceedNextOrderAsync() { if (_ordersQueue.Count == 0) { return; } OrderDto order = _ordersQueue.Dequeue(); if (order == null) { return; } await _orderService.ChangeOrderStateAsync(order, OrderStateDto.Assignment); //2. Change order state to Assignment PointDto orderLocationPoint = _pointService.GetOrderLocation(order.Id); //3. Get order destination point coordinates IEnumerable <CourierDto> couriers = _courierService.GetNearestCouriers(orderLocationPoint); //4. Find nearest couriers by next criterias (distance to point > number of assigned orders > courier state) var possibleCandidate = couriers.FirstOrDefault(courier => courier.State == CourierStateDto.Idle); if (possibleCandidate != null) { await _orderService.ChangeOrderStateAsync(order, OrderStateDto.WaitingOnWarehouse); await _courierService.AssignOrder(possibleCandidate.Id, order.Id); } else { RouteCandidate bestRoute = await FindBestCandidateAsync(couriers, orderLocationPoint); if (bestRoute == null) { await _orderService.ChangeOrderStateAsync(order, OrderStateDto.NotAssigned); _ordersQueue.Enqueue(order); } else { await _orderService.ChangeOrderStateAsync(order, OrderStateDto.WaitingOnWarehouse); await _courierService.AssignOrder(bestRoute.Courier.Id, order.Id); await _routeService.ChangeCourierCurrentRouteAsync(bestRoute.Courier.Id, bestRoute.GetRoute()); } } }
public void GetNearestCouriers_CorrectData_ListOfCouriersIsNotEmpty() { CourierDto[] couriers = new CourierDto[] { CourierFactory(new PointDto(49.83498756, 24.03488874)), CourierFactory(new PointDto(49.83609475, 24.02265787)), CourierFactory(new PointDto(49.84495134, 24.03587579)), CourierFactory(new PointDto(49.84401041, 23.98257493)), CourierFactory(new PointDto(49.82712580, 23.98296117)), CourierFactory(new PointDto(49.82014880, 23.98823976)), CourierFactory(new PointDto(49.82390000, 24.02079100)), CourierFactory(new PointDto(49.83226000, 24.01255100)) }; foreach (var courier in couriers) { courierService.AddCourierAsync(courier).Wait(); } var result = courierService.GetNearestCouriers(new PointDto(49.830213, 24.030651), 3); Assert.IsTrue(result.Count == 3 && result.First().Location.Latitude == 49.83498756); }