|
@@ -2,7 +2,6 @@ package com.izouma.dingdong.service;
|
|
|
|
|
|
|
|
import cn.hutool.core.bean.BeanUtil;
|
|
import cn.hutool.core.bean.BeanUtil;
|
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
|
-import com.alibaba.fastjson.JSONObject;
|
|
|
|
|
import com.izouma.dingdong.converter.StringArrayConverter;
|
|
import com.izouma.dingdong.converter.StringArrayConverter;
|
|
|
import com.izouma.dingdong.domain.*;
|
|
import com.izouma.dingdong.domain.*;
|
|
|
import com.izouma.dingdong.domain.merchant.Merchant;
|
|
import com.izouma.dingdong.domain.merchant.Merchant;
|
|
@@ -13,10 +12,7 @@ import com.izouma.dingdong.domain.user.Address;
|
|
|
import com.izouma.dingdong.domain.user.UserCoupon;
|
|
import com.izouma.dingdong.domain.user.UserCoupon;
|
|
|
import com.izouma.dingdong.dto.OrderDTO;
|
|
import com.izouma.dingdong.dto.OrderDTO;
|
|
|
import com.izouma.dingdong.dto.UserOrderDTO;
|
|
import com.izouma.dingdong.dto.UserOrderDTO;
|
|
|
-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.exception.BusinessException;
|
|
|
import com.izouma.dingdong.repo.*;
|
|
import com.izouma.dingdong.repo.*;
|
|
|
import com.izouma.dingdong.repo.user.ShoppingCartRepo;
|
|
import com.izouma.dingdong.repo.user.ShoppingCartRepo;
|
|
@@ -56,6 +52,8 @@ public class OrderInfoService {
|
|
|
private MerchantRepo merchantRepo;
|
|
private MerchantRepo merchantRepo;
|
|
|
private UserCouponRepo userCouponRepo;
|
|
private UserCouponRepo userCouponRepo;
|
|
|
private MoneyRecordRepo moneyRecordRepo;
|
|
private MoneyRecordRepo moneyRecordRepo;
|
|
|
|
|
+ private OrderRefundApplyService orderRefundApplyService;
|
|
|
|
|
+ private SysConfigRepo sysConfigRepo;
|
|
|
|
|
|
|
|
/*
|
|
/*
|
|
|
用户下单
|
|
用户下单
|
|
@@ -66,9 +64,6 @@ public class OrderInfoService {
|
|
|
|
|
|
|
|
//商家
|
|
//商家
|
|
|
MerchantSettings merchantSettings = merchantSettingsRepo.findByMerchantId(cart.getMerchantId()).orElseThrow(new BusinessException("无商家"));
|
|
MerchantSettings merchantSettings = merchantSettingsRepo.findByMerchantId(cart.getMerchantId()).orElseThrow(new BusinessException("无商家"));
|
|
|
- //商家
|
|
|
|
|
-// Merchant merchant = merchantRepo.findById(userOrderDTO.getMerchantId()).orElseThrow(new BusinessException("无商家"));
|
|
|
|
|
-// MerchantSettings merchantSettings = merchantSettingsRepo.findByMerchantId(userOrderDTO.getMerchantId()).orElseThrow(new BusinessException("无商家"));
|
|
|
|
|
|
|
|
|
|
LocalTime startTime = merchantSettings.getStartTime();
|
|
LocalTime startTime = merchantSettings.getStartTime();
|
|
|
LocalTime endTime = merchantSettings.getEndTime();
|
|
LocalTime endTime = merchantSettings.getEndTime();
|
|
@@ -84,10 +79,10 @@ public class OrderInfoService {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
//如果未设置起送金额为0
|
|
//如果未设置起送金额为0
|
|
|
- if (ObjectUtil.isEmpty(merchantSettings.getStartingAmount())){
|
|
|
|
|
|
|
+ if (ObjectUtil.isEmpty(merchantSettings.getStartingAmount())) {
|
|
|
merchantSettings.setStartingAmount(BigDecimal.ZERO);
|
|
merchantSettings.setStartingAmount(BigDecimal.ZERO);
|
|
|
}
|
|
}
|
|
|
- if (merchantSettings.getStartingAmount().compareTo(cart.getGoodsTotal()) > 0){
|
|
|
|
|
|
|
+ if (merchantSettings.getStartingAmount().compareTo(cart.getGoodsTotal()) > 0) {
|
|
|
throw new BusinessException("未达到起送金额");
|
|
throw new BusinessException("未达到起送金额");
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -100,7 +95,7 @@ public class OrderInfoService {
|
|
|
orderInfo.setEnabled(true);
|
|
orderInfo.setEnabled(true);
|
|
|
|
|
|
|
|
//如果未设置预计到达为30分钟
|
|
//如果未设置预计到达为30分钟
|
|
|
- if (ObjectUtil.isEmpty(merchantSettings.getPreparationTime())){
|
|
|
|
|
|
|
+ if (ObjectUtil.isEmpty(merchantSettings.getPreparationTime())) {
|
|
|
merchantSettings.setPreparationTime(30L);
|
|
merchantSettings.setPreparationTime(30L);
|
|
|
}
|
|
}
|
|
|
//预计送达时间
|
|
//预计送达时间
|
|
@@ -214,11 +209,14 @@ public class OrderInfoService {
|
|
|
orderInfo.setRiderId(151L);
|
|
orderInfo.setRiderId(151L);
|
|
|
orderInfo.setRiderStatus(RiderStatus.RECEIVED);
|
|
orderInfo.setRiderStatus(RiderStatus.RECEIVED);
|
|
|
|
|
|
|
|
|
|
+
|
|
|
} else {
|
|
} else {
|
|
|
orderInfo.setMerchantStatus(MerchantStatus.REJECTED);
|
|
orderInfo.setMerchantStatus(MerchantStatus.REJECTED);
|
|
|
orderInfo.setCancel(true);
|
|
orderInfo.setCancel(true);
|
|
|
- orderInfo.setReason("商家拒单");
|
|
|
|
|
|
|
+ orderInfo.setReason(RefundReason.MERCHANT_REJECTION);
|
|
|
//退款流程
|
|
//退款流程
|
|
|
|
|
+ OrderRefundApply apply = orderRefundApplyService.apply(orderId, RefundReason.MERCHANT_REJECTION, null);
|
|
|
|
|
+ orderRefundApplyService.audit(apply.getId(), true);
|
|
|
}
|
|
}
|
|
|
return orderInfoRepo.save(orderInfo);
|
|
return orderInfoRepo.save(orderInfo);
|
|
|
}
|
|
}
|
|
@@ -243,6 +241,9 @@ public class OrderInfoService {
|
|
|
*/
|
|
*/
|
|
|
public void ridReceiveOrder(Long riderId, Long orderId, Boolean pass) {
|
|
public void ridReceiveOrder(Long riderId, Long orderId, Boolean pass) {
|
|
|
OrderInfo orderInfo = orderInfoRepo.findById(orderId).orElseThrow(new BusinessException("无订单"));
|
|
OrderInfo orderInfo = orderInfoRepo.findById(orderId).orElseThrow(new BusinessException("无订单"));
|
|
|
|
|
+ if (OrderStatus.CANCELLED.equals(orderInfo.getStatus())) {
|
|
|
|
|
+ throw new BusinessException("订单已取消");
|
|
|
|
|
+ }
|
|
|
if (pass) {
|
|
if (pass) {
|
|
|
orderInfo.setRiderId(riderId);
|
|
orderInfo.setRiderId(riderId);
|
|
|
orderInfo.setRiderStatus(RiderStatus.RECEIVED);
|
|
orderInfo.setRiderStatus(RiderStatus.RECEIVED);
|
|
@@ -264,6 +265,10 @@ public class OrderInfoService {
|
|
|
//按商户Id找出用户
|
|
//按商户Id找出用户
|
|
|
User userMer = userRepo.findById(merchantRepo.findUserIdById(orderInfo.getMerchantId())).orElseThrow(new BusinessException("无用户"));
|
|
User userMer = userRepo.findById(merchantRepo.findUserIdById(orderInfo.getMerchantId())).orElseThrow(new BusinessException("无用户"));
|
|
|
|
|
|
|
|
|
|
+ if (OrderStatus.CANCELLED.equals(orderInfo.getStatus())) {
|
|
|
|
|
+ throw new BusinessException("订单已取消");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
if (MerchantStatus.RECEIVED.equals(orderInfo.getMerchantStatus())) {
|
|
if (MerchantStatus.RECEIVED.equals(orderInfo.getMerchantStatus())) {
|
|
|
RiderStatus riderStatus = orderInfo.getRiderStatus();
|
|
RiderStatus riderStatus = orderInfo.getRiderStatus();
|
|
|
switch (riderStatus) {
|
|
switch (riderStatus) {
|
|
@@ -280,12 +285,13 @@ public class OrderInfoService {
|
|
|
//添加销量数据
|
|
//添加销量数据
|
|
|
salesService.addSale(orderInfo);
|
|
salesService.addSale(orderInfo);
|
|
|
|
|
|
|
|
- //扣除金额
|
|
|
|
|
|
|
|
|
|
- //商家应得
|
|
|
|
|
-// Merchant merchant = merchantRepo.findById(orderInfo.getMerchantId()).orElseThrow(new BusinessException("商家不存在"));
|
|
|
|
|
-// merchant.setMoney(merchant.getMoney().add(orderInfo.getRealAmount().subtract(orderInfo.getDeliveryAmount())));
|
|
|
|
|
- userMer.setMoney(userMer.getMoney().add(orderInfo.getRealAmount().subtract(orderInfo.getDeliveryAmount())));
|
|
|
|
|
|
|
+ //扣除金额
|
|
|
|
|
+ SysConfig sysConfig = sysConfigRepo.findByName("commission").orElseThrow(new BusinessException("无设置"));
|
|
|
|
|
+ //平台抽成
|
|
|
|
|
+ BigDecimal platform = orderInfo.getRealAmount().multiply(new BigDecimal(sysConfig.getValue()));
|
|
|
|
|
+ //商家应得 = 减去骑手应得,减去平台抽成
|
|
|
|
|
+ userMer.setMoney(userMer.getMoney().add(orderInfo.getRealAmount().subtract(orderInfo.getDeliveryAmount()).subtract(platform)));
|
|
|
userRepo.save(userMer);
|
|
userRepo.save(userMer);
|
|
|
|
|
|
|
|
//购买的user
|
|
//购买的user
|
|
@@ -324,7 +330,7 @@ public class OrderInfoService {
|
|
|
*/
|
|
*/
|
|
|
public List<OrderInfo> my(Long userId) {
|
|
public List<OrderInfo> my(Long userId) {
|
|
|
User user = userRepo.findById(userId).orElseThrow(new BusinessException("无用户"));
|
|
User user = userRepo.findById(userId).orElseThrow(new BusinessException("无用户"));
|
|
|
- if (user.getIdentity()==null){
|
|
|
|
|
|
|
+ if (user.getIdentity() == null) {
|
|
|
throw new BusinessException("身份错误");
|
|
throw new BusinessException("身份错误");
|
|
|
}
|
|
}
|
|
|
switch (user.getIdentity()) {
|
|
switch (user.getIdentity()) {
|
|
@@ -360,9 +366,46 @@ public class OrderInfoService {
|
|
|
/*
|
|
/*
|
|
|
用户取消订单
|
|
用户取消订单
|
|
|
*/
|
|
*/
|
|
|
- public void cancel(Long id, String reason) {
|
|
|
|
|
|
|
+ public void cancel(Long id, RefundReason reason) {
|
|
|
OrderInfo orderInfo = orderInfoRepo.findById(id).orElseThrow(new BusinessException("无订单"));
|
|
OrderInfo orderInfo = orderInfoRepo.findById(id).orElseThrow(new BusinessException("无订单"));
|
|
|
|
|
+
|
|
|
|
|
+ if (orderInfo.getStatus().equals(OrderStatus.UNPAID)) {
|
|
|
|
|
+ //未支付,无退款
|
|
|
|
|
+ orderInfo.setCancel(true);
|
|
|
|
|
+ orderInfo.setReason(reason);
|
|
|
|
|
+ orderInfo.setStatus(OrderStatus.CANCELLED);
|
|
|
|
|
+ orderInfoRepo.save(orderInfo);
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (orderInfo.getStatus().equals(OrderStatus.RATED) || orderInfo.getStatus().equals(OrderStatus.COMPLETED)) {
|
|
|
|
|
+ throw new BusinessException("订单已完成");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ //用户已支付
|
|
|
|
|
+ if (OrderStatus.PAID.equals(orderInfo.getStatus())) {
|
|
|
|
|
+
|
|
|
|
|
+ OrderRefundApply apply = orderRefundApplyService.apply(id, reason, null);
|
|
|
|
|
+ //商家未接单,直接退
|
|
|
|
|
+ if (MerchantStatus.NOT_RECEIVED.equals(orderInfo.getMerchantStatus())) {
|
|
|
|
|
+ orderRefundApplyService.audit(apply.getId(), true);
|
|
|
|
|
+
|
|
|
|
|
+ } else {//商家已接单
|
|
|
|
|
+ //骑手未接单 直接退
|
|
|
|
|
+ if (RiderStatus.NOT_RECEIVED.equals(orderInfo.getRiderStatus())) {
|
|
|
|
|
+ orderRefundApplyService.audit(apply.getId(), true);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ //已接单,已完成,不能退 只能投诉
|
|
|
|
|
+ if (RiderStatus.CARRY_OUT.equals(orderInfo.getRiderStatus())) {
|
|
|
|
|
+ throw new BusinessException("已完成无法取消");
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
orderInfo.setCancel(true);
|
|
orderInfo.setCancel(true);
|
|
|
orderInfo.setReason(reason);
|
|
orderInfo.setReason(reason);
|
|
|
|
|
+ orderInfo.setStatus(OrderStatus.CANCELLED);
|
|
|
|
|
+ orderInfoRepo.save(orderInfo);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|