|
|
@@ -8,16 +8,14 @@ import com.izouma.dingdong.domain.merchant.Merchant;
|
|
|
import com.izouma.dingdong.domain.rider.Rider;
|
|
|
import com.izouma.dingdong.domain.user.Address;
|
|
|
import com.izouma.dingdong.dto.RiderDistanceDTO;
|
|
|
-import com.izouma.dingdong.enums.FinancialType;
|
|
|
-import com.izouma.dingdong.enums.MerchantStatus;
|
|
|
-import com.izouma.dingdong.enums.OrderStatus;
|
|
|
-import com.izouma.dingdong.enums.RiderStatus;
|
|
|
+import com.izouma.dingdong.enums.*;
|
|
|
import com.izouma.dingdong.exception.BusinessException;
|
|
|
import com.izouma.dingdong.repo.MoneyRecordRepo;
|
|
|
import com.izouma.dingdong.repo.OrderInfoRepo;
|
|
|
import com.izouma.dingdong.repo.UserRepo;
|
|
|
import com.izouma.dingdong.repo.rider.RiderRepo;
|
|
|
import com.izouma.dingdong.repo.user.AddressRepo;
|
|
|
+import com.izouma.dingdong.service.OrderInfoService;
|
|
|
import com.izouma.dingdong.service.OrderRefundApplyService;
|
|
|
import com.izouma.dingdong.service.merchant.SalesService;
|
|
|
import com.izouma.dingdong.utils.MapUtils;
|
|
|
@@ -39,6 +37,7 @@ public class RiderService {
|
|
|
private OrderRefundApplyService orderRefundApplyService;
|
|
|
private SalesService salesService;
|
|
|
private AddressRepo addressRepo;
|
|
|
+ private OrderInfoService orderInfoService;
|
|
|
|
|
|
/*
|
|
|
骑手签到
|
|
|
@@ -79,6 +78,7 @@ public class RiderService {
|
|
|
*/
|
|
|
public void riderReceiveOrder(Long riderId, Long orderId, Boolean pass) {
|
|
|
OrderInfo orderInfo = orderInfoRepo.findById(orderId).orElseThrow(new BusinessException("无订单"));
|
|
|
+
|
|
|
if (pass) {
|
|
|
if (!OrderStatus.PAID.equals(orderInfo.getStatus())) {
|
|
|
throw new BusinessException("订单不可接单");
|
|
|
@@ -89,13 +89,15 @@ public class RiderService {
|
|
|
if (!RiderStatus.NOT_RECEIVED.equals(orderInfo.getRiderStatus())) {
|
|
|
throw new BusinessException("已被其他骑手接单");
|
|
|
}
|
|
|
+
|
|
|
+ List<OrderInfo> infos = orderInfoRepo.findAllByRiderIdAndRiderStatusIsNot(riderId, RiderStatus.CARRY_OUT);
|
|
|
+ if (infos.size() >= 5) {
|
|
|
+ throw new BusinessException("未完成订单已达上线");
|
|
|
+ }
|
|
|
//设置订单状态
|
|
|
orderInfo.setRiderId(riderId);
|
|
|
orderInfo.setRiderStatus(RiderStatus.RECEIVED);
|
|
|
orderInfoRepo.save(orderInfo);
|
|
|
- } else {
|
|
|
- //已拒单不再显示给这个骑手
|
|
|
- orderInfo.setNotDisplayRiderIds(riderId + ",");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -103,8 +105,8 @@ public class RiderService {
|
|
|
系统派单
|
|
|
5分钟后自动接单
|
|
|
有空闲骑手离商家最近的距离排序,骑手不可拒单
|
|
|
- 无空闲,离商家最近的骑手,骑手可接单可拒单 --> 无法判断都拒单
|
|
|
- 骑手都拒单,或超过5分钟,按距离强制接单
|
|
|
+ 无空闲,离商家最近的3个骑手,订单量少的接单
|
|
|
+ 订单量相同,距离最近接单
|
|
|
商家3km内无骑手,取消订单
|
|
|
*/
|
|
|
public void dispatch(Long orderId, List<RiderDistanceDTO> dtos) {
|
|
|
@@ -115,26 +117,58 @@ public class RiderService {
|
|
|
Double distance = MapUtils.distance(merchant.getLongitude(), merchant.getLatitude(), d.getLongitude(), d.getLatitude());
|
|
|
map.put(d.getRiderId(), distance);
|
|
|
});
|
|
|
+
|
|
|
+ //无骑手,取消订单
|
|
|
+ if (CollUtil.isEmpty(map)) {
|
|
|
+ orderInfoService.cancel(orderId, RefundReason.UNABLE_TO_DELIVER, null, null);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
//排序
|
|
|
List<Map.Entry<Long, Double>> list = new ArrayList<>(map.entrySet());
|
|
|
list.sort(Comparator.comparing(Map.Entry<Long, Double>::getValue));
|
|
|
|
|
|
for (Map.Entry<Long, Double> m : list) {
|
|
|
Rider rider = riderRepo.findById(m.getKey()).orElseThrow(new BusinessException("无骑手"));
|
|
|
- //if (rider.getSignIn()) {
|
|
|
- List<OrderInfo> infos = orderInfoRepo.findAllByRiderIdAndRiderStatusIsNot(m.getKey(), RiderStatus.CARRY_OUT);
|
|
|
- //有空闲骑手,自动接单,不可拒单
|
|
|
- if (CollUtil.isEmpty(infos)) {
|
|
|
- orderInfo.setRiderId(m.getKey());
|
|
|
- orderInfo.setRiderStatus(RiderStatus.RECEIVED);
|
|
|
- break;
|
|
|
+ if (rider.getSignIn()) {
|
|
|
+ List<OrderInfo> infos = orderInfoRepo.findAllByRiderIdAndRiderStatusIsNot(m.getKey(), RiderStatus.CARRY_OUT);
|
|
|
+ //有空闲骑手,自动接单,不可拒单
|
|
|
+ if (CollUtil.isEmpty(infos)) {
|
|
|
+ //riderId = m.getKey();
|
|
|
+ orderInfo.setRiderId(m.getKey());
|
|
|
+ orderInfo.setRiderStatus(RiderStatus.RECEIVED);
|
|
|
+ orderInfoRepo.save(orderInfo);
|
|
|
+ return;
|
|
|
+ //break;
|
|
|
+ }
|
|
|
}
|
|
|
- // }
|
|
|
+ }
|
|
|
+
|
|
|
+ //无空闲骑手,未完成订单最少的接单
|
|
|
+ int infos1 = orderInfoRepo.findAllByRiderIdAndRiderStatusIsNot(list.get(0)
|
|
|
+ .getKey(), RiderStatus.CARRY_OUT).size();
|
|
|
+ int infos2 = orderInfoRepo.findAllByRiderIdAndRiderStatusIsNot(list.get(1)
|
|
|
+ .getKey(), RiderStatus.CARRY_OUT).size();
|
|
|
+ int infos3 = orderInfoRepo.findAllByRiderIdAndRiderStatusIsNot(list.get(2)
|
|
|
+ .getKey(), RiderStatus.CARRY_OUT).size();
|
|
|
+ Long riderId = null;
|
|
|
+ if (infos1 < infos2 && infos1 < infos3) {
|
|
|
+ riderId = list.get(0).getKey();
|
|
|
+ } else if (infos2 < infos1 && infos2 < infos3) {
|
|
|
+ riderId = list.get(1).getKey();
|
|
|
+ } else if (infos3 < infos1 && infos3 < infos2) {
|
|
|
+ riderId = list.get(2).getKey();
|
|
|
+ }
|
|
|
+ if (riderId != null) {
|
|
|
+ orderInfo.setRiderId(riderId);
|
|
|
+ orderInfo.setRiderStatus(RiderStatus.RECEIVED);
|
|
|
+ orderInfoRepo.save(orderInfo);
|
|
|
+ return;
|
|
|
}
|
|
|
|
|
|
//无空闲骑手 距离最近的接单
|
|
|
if (RiderStatus.NOT_RECEIVED.equals(orderInfo.getRiderStatus())) {
|
|
|
- Long riderId = list.get(0).getKey();
|
|
|
+ riderId = list.get(0).getKey();
|
|
|
orderInfo.setRiderId(riderId);
|
|
|
orderInfo.setRiderStatus(RiderStatus.RECEIVED);
|
|
|
}
|
|
|
@@ -154,8 +188,8 @@ public class RiderService {
|
|
|
throw new BusinessException("订单已取消");
|
|
|
}
|
|
|
|
|
|
- RiderStatus riderStatus = orderInfo.getRiderStatus();
|
|
|
- switch (riderStatus) {
|
|
|
+ //RiderStatus riderStatus = orderInfo.getRiderStatus();
|
|
|
+ switch (status) {
|
|
|
//已接单状态 待取餐
|
|
|
case ARRIVE:
|
|
|
//判断是否到达商家位置
|