|
|
@@ -7,10 +7,12 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
import com.haha.common.enums.RefundStatus;
|
|
|
import com.haha.common.constant.PaymentConstants;
|
|
|
import com.haha.entity.Order;
|
|
|
+import com.haha.entity.OrderGoods;
|
|
|
import com.haha.entity.Refund;
|
|
|
import com.haha.entity.RefundItem;
|
|
|
import com.haha.mapper.RefundItemMapper;
|
|
|
import com.haha.mapper.RefundMapper;
|
|
|
+import com.haha.service.OrderGoodsService;
|
|
|
import com.haha.service.OrderService;
|
|
|
import com.haha.service.RefundService;
|
|
|
import com.haha.service.payment.PaymentService;
|
|
|
@@ -33,14 +35,17 @@ public class RefundServiceImpl extends ServiceImpl<RefundMapper, Refund> impleme
|
|
|
|
|
|
private final RefundItemMapper refundItemMapper;
|
|
|
private final OrderService orderService;
|
|
|
+ private final OrderGoodsService orderGoodsService;
|
|
|
|
|
|
@Autowired
|
|
|
@Lazy
|
|
|
private PaymentService paymentService;
|
|
|
|
|
|
- public RefundServiceImpl(RefundItemMapper refundItemMapper, OrderService orderService) {
|
|
|
+ public RefundServiceImpl(RefundItemMapper refundItemMapper, OrderService orderService,
|
|
|
+ OrderGoodsService orderGoodsService) {
|
|
|
this.refundItemMapper = refundItemMapper;
|
|
|
this.orderService = orderService;
|
|
|
+ this.orderGoodsService = orderGoodsService;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@@ -82,6 +87,32 @@ public class RefundServiceImpl extends ServiceImpl<RefundMapper, Refund> impleme
|
|
|
throw new RuntimeException("退款金额不能超过可退金额" + availableAmount + "元(实付" + paidAmount + "元,已退" + alreadyRefunded + "元)");
|
|
|
}
|
|
|
|
|
|
+ // 逐商品校验退款数量不超过剩余可退数量
|
|
|
+ if (products != null && !products.isEmpty()) {
|
|
|
+ Map<Long, Integer> refundedQuantityMap = getRefundedQuantityMap(orderId);
|
|
|
+ for (Map<String, Object> p : products) {
|
|
|
+ Long orderGoodsId = p.get("orderGoodsId") != null
|
|
|
+ ? Long.valueOf(p.get("orderGoodsId").toString()) : 0L;
|
|
|
+ int requestQty = p.get("quantity") != null
|
|
|
+ ? Integer.parseInt(p.get("quantity").toString()) : 1;
|
|
|
+ int alreadyRefundedQty = refundedQuantityMap.getOrDefault(orderGoodsId, 0);
|
|
|
+
|
|
|
+ if (alreadyRefundedQty > 0 || refundedQuantityMap.containsKey(orderGoodsId)) {
|
|
|
+ // 查询原始购买数量
|
|
|
+ OrderGoods orderGoods = orderGoodsService.getById(orderGoodsId);
|
|
|
+ int originalQty = orderGoods != null && orderGoods.getProductNum() != null
|
|
|
+ ? orderGoods.getProductNum() : Integer.MAX_VALUE;
|
|
|
+ int remainingQty = originalQty - alreadyRefundedQty;
|
|
|
+
|
|
|
+ if (requestQty > remainingQty) {
|
|
|
+ String productName = (String) p.getOrDefault("productName", "商品");
|
|
|
+ throw new RuntimeException(productName + " 可退数量不足:已退" + alreadyRefundedQty
|
|
|
+ + "件,最多可退" + remainingQty + "件");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
// 创建退款记录
|
|
|
Refund refund = new Refund();
|
|
|
refund.setOrderId(orderId);
|
|
|
@@ -151,6 +182,28 @@ public class RefundServiceImpl extends ServiceImpl<RefundMapper, Refund> impleme
|
|
|
return this.getById(refundId);
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public Map<Long, Integer> getRefundedQuantityMap(Long orderId) {
|
|
|
+ Map<Long, Integer> result = new java.util.HashMap<>();
|
|
|
+ // 查询该订单所有已退款成功的记录
|
|
|
+ List<Refund> refundedList = lambdaQuery()
|
|
|
+ .eq(Refund::getOrderId, orderId)
|
|
|
+ .eq(Refund::getStatus, RefundStatus.REFUNDED.getCode())
|
|
|
+ .list();
|
|
|
+ if (refundedList == null || refundedList.isEmpty()) {
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ for (Refund refund : refundedList) {
|
|
|
+ List<RefundItem> items = getRefundItems(refund.getId());
|
|
|
+ if (items != null) {
|
|
|
+ for (RefundItem item : items) {
|
|
|
+ result.merge(item.getOrderGoodsId(), item.getQuantity(), Integer::sum);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
public List<Refund> getByOrderId(Long orderId) {
|
|
|
return lambdaQuery()
|