|
|
@@ -1,12 +1,16 @@
|
|
|
package com.izouma.dingdong.service.rider;
|
|
|
|
|
|
+import cn.hutool.core.bean.BeanUtil;
|
|
|
import cn.hutool.core.collection.CollUtil;
|
|
|
+import cn.hutool.core.util.ObjectUtil;
|
|
|
import com.izouma.dingdong.domain.MoneyRecord;
|
|
|
import com.izouma.dingdong.domain.OrderInfo;
|
|
|
+import com.izouma.dingdong.domain.OrderRefundApply;
|
|
|
import com.izouma.dingdong.domain.User;
|
|
|
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.domain.rider.RiderSign;
|
|
|
+import com.izouma.dingdong.dto.RiderDTO;
|
|
|
import com.izouma.dingdong.dto.RiderDistanceDTO;
|
|
|
import com.izouma.dingdong.enums.*;
|
|
|
import com.izouma.dingdong.exception.BusinessException;
|
|
|
@@ -14,12 +18,12 @@ 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.repo.rider.RiderSignRepo;
|
|
|
import com.izouma.dingdong.service.OrderRefundApplyService;
|
|
|
-import com.izouma.dingdong.service.merchant.SalesService;
|
|
|
import com.izouma.dingdong.utils.MapUtils;
|
|
|
import lombok.AllArgsConstructor;
|
|
|
+import org.apache.commons.lang3.RandomStringUtils;
|
|
|
+import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.math.BigDecimal;
|
|
|
@@ -35,19 +39,79 @@ public class RiderService {
|
|
|
private MoneyRecordRepo moneyRecordRepo;
|
|
|
private OrderInfoRepo orderInfoRepo;
|
|
|
private OrderRefundApplyService orderRefundApplyService;
|
|
|
- private SalesService salesService;
|
|
|
- private AddressRepo addressRepo;
|
|
|
-// private OrderInfoService orderInfoService;
|
|
|
+ private RiderSignRepo riderSignRepo;
|
|
|
+
|
|
|
|
|
|
/*
|
|
|
- 骑手签到
|
|
|
+ 骑手注册
|
|
|
*/
|
|
|
- public void sign(Long riderId) {
|
|
|
+ public Rider riderApply(RiderDTO riderDTO) {
|
|
|
+ userRepo.findById(riderDTO.getUserId()).orElseThrow(new BusinessException("无用户"));
|
|
|
+ Rider rider = new Rider();
|
|
|
+ BeanUtil.copyProperties(rider, riderDTO);
|
|
|
+ rider.setBlacklist(false);
|
|
|
+ rider.setEnabled(true);
|
|
|
+ //rider.setSignIn(false);
|
|
|
+ rider.setStatus(ApplyStatus.PENDING);
|
|
|
+ return riderRepo.save(rider);
|
|
|
+ }
|
|
|
|
|
|
+ /*
|
|
|
+ 骑手审核
|
|
|
+ */
|
|
|
+ public void riderAudit(Long riderId, Boolean pass) {
|
|
|
+ Rider rider = riderRepo.findById(riderId).orElseThrow(new BusinessException("无骑手"));
|
|
|
+ if (pass) {
|
|
|
+ rider.setStatus(ApplyStatus.PASS);
|
|
|
+ boolean i = true;
|
|
|
+ while (i) {
|
|
|
+ //随机生成骑手工号 一个首字母+四个数字
|
|
|
+ String jobNumber1 = RandomStringUtils.randomAlphabetic(1);
|
|
|
+ String jobNumber2 = RandomStringUtils.randomNumeric(4);
|
|
|
+ String jobNumber = jobNumber1 + jobNumber2;
|
|
|
+ Rider byJobNumber = riderRepo.findByJobNumber(jobNumber);
|
|
|
+ if (ObjectUtil.isNull(byJobNumber)) {
|
|
|
+ rider.setJobNumber(jobNumber);
|
|
|
+ i = false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ rider.setStatus(ApplyStatus.DENY);
|
|
|
+ }
|
|
|
+ riderRepo.save(rider);
|
|
|
}
|
|
|
|
|
|
+
|
|
|
/*
|
|
|
- 骑手收入,且保存到记录表
|
|
|
+ 骑手签到
|
|
|
+ */
|
|
|
+ public void sign(Long riderId, String password, Double longitude, Double latitude) {
|
|
|
+ Rider rider = riderRepo.findById(riderId).orElseThrow(new BusinessException("无骑手"));
|
|
|
+ Double distance = MapUtils.distance(longitude, latitude, rider.getLongitude(), rider.getLatitude());
|
|
|
+ if (distance > 3000) {
|
|
|
+ throw new BusinessException("不在工作区域");
|
|
|
+ }
|
|
|
+ User user = userRepo.findById(rider.getUserId()).orElseThrow(new BusinessException("无用户"));
|
|
|
+ boolean matches = new BCryptPasswordEncoder().matches(password, user.getPassword());
|
|
|
+ if (!matches) {
|
|
|
+ throw new BusinessException("密码不正确");
|
|
|
+ }
|
|
|
+ riderSignRepo.save(RiderSign.builder()
|
|
|
+ .latitude(latitude)
|
|
|
+ .longitude(longitude)
|
|
|
+ .riderId(riderId)
|
|
|
+ .signTime(LocalDateTime.now())
|
|
|
+ .build());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 骑手收入,且保存到记录表
|
|
|
+ *
|
|
|
+ * @param riderId 骑手ID
|
|
|
+ * @param fromUserId 来自用户
|
|
|
+ * @param amount 金额
|
|
|
+ * @param type 流水类型
|
|
|
+ * @param remark 备注
|
|
|
*/
|
|
|
public void income(Long riderId, Long fromUserId, BigDecimal amount, FinancialType type, String remark) {
|
|
|
//骑手应得 riderRepo.findUserIdById(riderId)
|
|
|
@@ -69,7 +133,6 @@ public class RiderService {
|
|
|
.remark(remark)
|
|
|
.enabled(true)
|
|
|
.build());
|
|
|
-
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -123,6 +186,8 @@ public class RiderService {
|
|
|
//无骑手,取消订单
|
|
|
if (CollUtil.isEmpty(map)) {
|
|
|
// orderInfoService.cancel(orderId, RefundReason.UNABLE_TO_DELIVER, null, null);
|
|
|
+ OrderRefundApply apply = orderRefundApplyService.apply(orderId, RefundReason.UNABLE_TO_DELIVER, null, null);
|
|
|
+ orderRefundApplyService.audit(apply.getId(), true, false);
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
@@ -132,18 +197,18 @@ public class RiderService {
|
|
|
|
|
|
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)) {
|
|
|
- //riderId = m.getKey();
|
|
|
- orderInfo.setRiderId(m.getKey());
|
|
|
- orderInfo.setRiderStatus(RiderStatus.RECEIVED);
|
|
|
- orderInfoRepo.save(orderInfo);
|
|
|
- return;
|
|
|
- //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;
|
|
|
}
|
|
|
+ //}
|
|
|
}
|
|
|
|
|
|
//无空闲骑手,未完成订单最少的接单
|
|
|
@@ -179,57 +244,4 @@ public class RiderService {
|
|
|
}
|
|
|
|
|
|
|
|
|
- /*
|
|
|
- 骑手改变状态
|
|
|
- */
|
|
|
-
|
|
|
- public void riderCarryOut(Long orderId, RiderStatus status, Double longitude, Double latitude) {
|
|
|
- OrderInfo orderInfo = orderInfoRepo.findById(orderId).orElseThrow(new BusinessException("无订单"));
|
|
|
-
|
|
|
- if (OrderStatus.CANCELLED.equals(orderInfo.getStatus())) {
|
|
|
- throw new BusinessException("订单已取消");
|
|
|
- }
|
|
|
-
|
|
|
- //RiderStatus riderStatus = orderInfo.getRiderStatus();
|
|
|
- switch (status) {
|
|
|
- //已接单状态 待取餐
|
|
|
- case ARRIVE:
|
|
|
- //判断是否到达商家位置
|
|
|
- Merchant merchant = orderInfo.getMerchant();
|
|
|
- Double latitude1 = merchant.getLatitude();
|
|
|
- Double longitude1 = merchant.getLongitude();
|
|
|
- if (MapUtils.distance(longitude, latitude, longitude1, latitude1) > 5.0) {
|
|
|
- throw new BusinessException("未到店");
|
|
|
- }
|
|
|
- //待取餐
|
|
|
- orderInfo.setRiderStatus(RiderStatus.ARRIVE);
|
|
|
- break;
|
|
|
- case TAKE_MEAL:
|
|
|
- //已取餐 待送达
|
|
|
- orderInfo.setRiderStatus(RiderStatus.TAKE_MEAL);
|
|
|
- //商家状态已完成
|
|
|
- orderInfo.setMerchantStatus(MerchantStatus.CARRY_OUT);
|
|
|
- break;
|
|
|
- case CARRY_OUT:
|
|
|
- //确认是否到达用户地址位置
|
|
|
- Address address = addressRepo.findById(orderInfo.getAddressId())
|
|
|
- .orElseThrow(new BusinessException("地址不存在"));
|
|
|
- Double latitude2 = address.getLatitude();
|
|
|
- Double longitude2 = address.getLongitude();
|
|
|
- if (MapUtils.distance(longitude, latitude, longitude2, latitude2) > 5) {
|
|
|
- throw new BusinessException("未到用户地点");
|
|
|
- }
|
|
|
- //已送达
|
|
|
- orderInfo.setRiderStatus(RiderStatus.CARRY_OUT);
|
|
|
- orderInfo.setUserReceivedTime(LocalDateTime.now());
|
|
|
- orderInfo.setStatus(OrderStatus.RATED);
|
|
|
-
|
|
|
- //支付价钱
|
|
|
- orderRefundApplyService.orderCarryOut(orderId);
|
|
|
- //添加销量数据
|
|
|
- salesService.addSale(orderInfo);
|
|
|
- break;
|
|
|
- }
|
|
|
- orderInfoRepo.save(orderInfo);
|
|
|
- }
|
|
|
}
|