|
|
@@ -3,15 +3,15 @@ package com.izouma.dingdong.service;
|
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
|
import com.izouma.dingdong.domain.OrderInfo;
|
|
|
import com.izouma.dingdong.domain.OrderRefundApply;
|
|
|
-import com.izouma.dingdong.domain.User;
|
|
|
+import com.izouma.dingdong.domain.SysConfig;
|
|
|
import com.izouma.dingdong.enums.*;
|
|
|
import com.izouma.dingdong.exception.BusinessException;
|
|
|
import com.izouma.dingdong.repo.OrderInfoRepo;
|
|
|
import com.izouma.dingdong.repo.OrderRefundApplyRepo;
|
|
|
+import com.izouma.dingdong.repo.SysConfigRepo;
|
|
|
import com.izouma.dingdong.repo.UserRepo;
|
|
|
import com.izouma.dingdong.repo.merchant.MerchantRepo;
|
|
|
import com.izouma.dingdong.repo.rider.RiderRepo;
|
|
|
-import com.izouma.dingdong.service.merchant.MerchantService;
|
|
|
import com.izouma.dingdong.service.merchant.MerchantSettingsService;
|
|
|
import com.izouma.dingdong.service.rider.RiderService;
|
|
|
import com.izouma.dingdong.utils.SnowflakeIdWorker;
|
|
|
@@ -32,24 +32,20 @@ public class OrderRefundApplyService {
|
|
|
|
|
|
@Autowired
|
|
|
private OrderRefundApplyRepo orderRefundApplyRepo;
|
|
|
-
|
|
|
@Autowired
|
|
|
private OrderInfoRepo orderInfoRepo;
|
|
|
-
|
|
|
@Autowired
|
|
|
private UserRepo userRepo;
|
|
|
-
|
|
|
@Autowired
|
|
|
private MerchantRepo merchantRepo;
|
|
|
-
|
|
|
@Autowired
|
|
|
private MerchantSettingsService merchantSettingsService;
|
|
|
-
|
|
|
@Autowired
|
|
|
private RiderService riderService;
|
|
|
-
|
|
|
@Autowired
|
|
|
private RiderRepo riderRepo;
|
|
|
+ @Autowired
|
|
|
+ private SysConfigRepo sysConfigRepo;
|
|
|
|
|
|
private OrderStatus status;
|
|
|
|
|
|
@@ -103,15 +99,17 @@ public class OrderRefundApplyService {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- OrderInfo orderInfo = apply.getOrderInfo();
|
|
|
+ //OrderInfo orderInfo = apply.getOrderInfo();
|
|
|
+ OrderInfo orderInfo = orderInfoRepo.findById(apply.getOrderId()).orElseThrow(new BusinessException("无订单"));
|
|
|
+
|
|
|
userRepo.findById(orderInfo.getUserId()).orElseThrow(new BusinessException("无用户"));
|
|
|
if (merchantAgree) {
|
|
|
|
|
|
String refundId = String.valueOf(new SnowflakeIdWorker(1, 1).nextId());
|
|
|
- apply.setStatus(RefundStatus.SUCCESS);
|
|
|
+ apply.setStatus(RefundStatus.PENDING_PAYMENT);
|
|
|
apply.setMerchantAgree(true);
|
|
|
|
|
|
- //商家同意损失都由商家承担
|
|
|
+ //商家同意损失都由商家承担 完成订单商家才得到钱,所以无需减
|
|
|
apply.setMerchantLiability(BigDecimal.ONE);
|
|
|
apply.setRiderLiability(BigDecimal.ZERO);
|
|
|
apply.setPlatformLiability(BigDecimal.ZERO);
|
|
|
@@ -121,7 +119,7 @@ public class OrderRefundApplyService {
|
|
|
}
|
|
|
|
|
|
//退款,线下退款订单完成商家才得款
|
|
|
-
|
|
|
+ orderInfo.setStatus(OrderStatus.REFUNDING);
|
|
|
|
|
|
//骑手得应得的
|
|
|
if (payRider) {
|
|
|
@@ -129,10 +127,11 @@ public class OrderRefundApplyService {
|
|
|
BigDecimal deliveryAmount = orderInfo.getDeliveryAmount();
|
|
|
//骑手收入
|
|
|
Long merId = merchantRepo.findUserIdById(orderInfo.getMerchantId());
|
|
|
- riderService.income(orderInfo.getRiderId(), merId, deliveryAmount, FinancialType.INCOME);
|
|
|
+ riderService.income(orderInfo.getRiderId(), merId, deliveryAmount, FinancialType.INCOME, apply.getOrderId().toString());
|
|
|
//商家支出
|
|
|
- Long riderUserId = userRepo.findById(riderRepo.findUserIdById(orderInfo.getRiderId())).orElseThrow(new BusinessException("无用户")).getId();
|
|
|
- merchantSettingsService.income(apply.getMerchantId(), riderUserId, deliveryAmount, FinancialType.LOSE_MONEY);
|
|
|
+ //User riderUser = userRepo.findById(riderRepo.findUserIdById(orderInfo.getRiderId())).orElseThrow(new BusinessException("无用户"));
|
|
|
+ Long riderUserId = orderInfo.getRiderId();
|
|
|
+ merchantSettingsService.income(apply.getMerchantId(), riderUserId, deliveryAmount, FinancialType.LOSE_MONEY, apply.getOrderId().toString());
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
@@ -174,7 +173,7 @@ public class OrderRefundApplyService {
|
|
|
|
|
|
|
|
|
//平台
|
|
|
- public void platformAudit(Long applyId, Boolean agree, BigDecimal merchantLiability, BigDecimal riderLiability) {
|
|
|
+ public void platformAudit(Long applyId, Boolean agree, BigDecimal merchantLiability, BigDecimal riderLiability, Boolean isCarryOut) {
|
|
|
|
|
|
OrderRefundApply apply = orderRefundApplyRepo.findById(applyId).orElseThrow(new BusinessException("无记录"));
|
|
|
if (ObjectUtil.isNull(merchantLiability)) {
|
|
|
@@ -183,35 +182,82 @@ public class OrderRefundApplyService {
|
|
|
if (ObjectUtil.isNull(riderLiability)) {
|
|
|
riderLiability = BigDecimal.ZERO;
|
|
|
}
|
|
|
+
|
|
|
+ OrderInfo orderInfo = orderInfoRepo.findById(apply.getOrderId()).orElseThrow(new BusinessException("无订单"));
|
|
|
+
|
|
|
if (agree) {
|
|
|
apply.setPlatformAgree(true);
|
|
|
apply.setPlatformLiability(BigDecimal.ONE.subtract(riderLiability).subtract(merchantLiability));
|
|
|
apply.setRiderLiability(riderLiability);
|
|
|
apply.setMerchantLiability(merchantLiability);
|
|
|
|
|
|
+ if (!isCarryOut) {
|
|
|
+ //先收入钱
|
|
|
+ this.orderCarryOut(apply.getOrderId());
|
|
|
+ }
|
|
|
+
|
|
|
if (!merchantLiability.equals(BigDecimal.ZERO)) {
|
|
|
//商家所赔金额
|
|
|
- BigDecimal merAmount = apply.getOrderInfo().getRealAmount().multiply(merchantLiability);
|
|
|
- merchantSettingsService.income(apply.getMerchantId(), apply.getOrderInfo().getUserId(), merAmount, FinancialType.REFUND);
|
|
|
+ BigDecimal merAmount = orderInfo.getRealAmount().multiply(merchantLiability);
|
|
|
+ merchantSettingsService.income(apply.getMerchantId(), orderInfo.getUserId(), merAmount, FinancialType.REFUND, apply.getOrderId().toString());
|
|
|
}
|
|
|
|
|
|
if (!riderLiability.equals(BigDecimal.ZERO)) {
|
|
|
//骑手赔付金额
|
|
|
- BigDecimal riderAmount = apply.getOrderInfo().getRealAmount().multiply(riderLiability);
|
|
|
- riderService.income(apply.getOrderInfo().getRiderId(), apply.getOrderInfo().getUserId(), riderAmount, FinancialType.REFUND);
|
|
|
+ BigDecimal riderAmount = orderInfo.getRealAmount().multiply(riderLiability);
|
|
|
+ riderService.income(orderInfo.getRiderId(), orderInfo.getUserId(), riderAmount, FinancialType.REFUND, apply.getOrderId().toString());
|
|
|
}
|
|
|
|
|
|
- apply.getOrderInfo().setStatus(OrderStatus.REFUNDED);
|
|
|
+ //改状态
|
|
|
+ orderInfo.setStatus(OrderStatus.REFUNDING);
|
|
|
+ apply.setStatus(RefundStatus.PENDING_PAYMENT);
|
|
|
} else {
|
|
|
apply.setPlatformAgree(false);
|
|
|
//原状态
|
|
|
- apply.getOrderInfo().setStatus(status);
|
|
|
- apply.getOrderInfo().setCancel(false);
|
|
|
+ orderInfo.setStatus(status);
|
|
|
+ orderInfo.setCancel(false);
|
|
|
+ //改状态
|
|
|
+ apply.setStatus(RefundStatus.DENY);
|
|
|
}
|
|
|
|
|
|
- orderInfoRepo.save(apply.getOrderInfo());
|
|
|
-
|
|
|
+ orderInfoRepo.save(orderInfo);
|
|
|
apply.setPlatformAuditTime(LocalDateTime.now());
|
|
|
orderRefundApplyRepo.save(apply);
|
|
|
}
|
|
|
+
|
|
|
+ /*
|
|
|
+ 订单完成结算钱
|
|
|
+ */
|
|
|
+ public void orderCarryOut(Long orderId) {
|
|
|
+ OrderInfo orderInfo = orderInfoRepo.findById(orderId).orElseThrow(new BusinessException("无订单"));
|
|
|
+
|
|
|
+ //扣除金额
|
|
|
+ SysConfig sysConfig = sysConfigRepo.findByName("commission").orElseThrow(new BusinessException("无设置"));
|
|
|
+
|
|
|
+ //实付减运费
|
|
|
+ BigDecimal subtractDelivery = orderInfo.getRealAmount().subtract(orderInfo.getDeliveryAmount());
|
|
|
+ //平台抽成 (实付金额减运费)
|
|
|
+ BigDecimal platform = subtractDelivery.multiply(new BigDecimal(sysConfig.getValue()));
|
|
|
+
|
|
|
+ //商家应得 = 减去骑手应得,减去平台抽成
|
|
|
+ BigDecimal deserve = subtractDelivery.subtract(platform);
|
|
|
+ merchantSettingsService.income(orderInfo.getMerchantId(), orderInfo.getUserId(), deserve, FinancialType.INCOME, orderInfo.getId().toString());
|
|
|
+
|
|
|
+ //骑手应得
|
|
|
+ riderService.income(orderInfo.getRiderId(), orderInfo.getUserId(), orderInfo.getDeliveryAmount(), FinancialType.INCOME, orderInfo.getId().toString());
|
|
|
+ }
|
|
|
+
|
|
|
+ /*
|
|
|
+ 打款
|
|
|
+ */
|
|
|
+ public void payment(Long applyId) {
|
|
|
+ OrderRefundApply apply = orderRefundApplyRepo.findById(applyId).orElseThrow(new BusinessException("无退款申请"));
|
|
|
+ apply.setStatus(RefundStatus.SUCCESS);
|
|
|
+ apply.setRefundTime(LocalDateTime.now());
|
|
|
+
|
|
|
+ OrderInfo orderInfo = orderInfoRepo.findById(apply.getOrderId()).orElseThrow(new BusinessException("无订单"));
|
|
|
+ orderInfo.setStatus(OrderStatus.REFUNDED);
|
|
|
+ orderInfoRepo.save(orderInfo);
|
|
|
+ orderRefundApplyRepo.save(apply);
|
|
|
+ }
|
|
|
}
|