skyline 2 жил өмнө
parent
commit
0f813d8bc9

+ 7 - 5
service/src/main/java/com/kym/service/wechat/impl/WxPayServiceImpl.java

@@ -408,14 +408,17 @@ public class WxPayServiceImpl implements WxPayService {
         var originalRefundAmount = BigDecimal.valueOf(refundAmount.get());
         // 充值记录
         var payLogs = payLogService.lambdaQuery().eq(PayLog::getUserId, userId).eq(PayLog::getTradeState, PayLog.STATUS_充值成功).orderByDesc(PayLog::getSuccessTime).list();
-        // 充值金额综合不足以支付余额退款
+        // 历史退款金额
+        var refundsAmount = refundLogService.lambdaQuery().eq(RefundLog::getUserId, userId).list().stream().mapToInt(RefundLog::getRefund).sum();
+        // 充值金额-历史退款金额不足以支付余额退款
         if (!CommUtil.isEmptyOrNull(payLogs)) {
             var total = payLogs.stream().mapToInt(PayLog::getTotal).sum();
-            if (total < account.getBalance()) {
-                LOGGER.error("用户:{},充值金额:{},不足以支付余额退款金额:{}", userId, total, account.getBalance());
+            if ((total - refundsAmount) < account.getBalance()) {
+                LOGGER.error("用户:{},历史充值金额:{},不足以支付余额退款金额:{}", userId, total - refundsAmount, account.getBalance());
                 throw new BusinessException("充值金额不足以支付余额退款");
             }
         }
+
         // 最后一次的充值金额可以覆盖退款金额
         if (!CommUtil.isEmptyOrNull(payLogs) && payLogs.get(0).getTotal() >= refundAmount.get()) {
             // 退款日志
@@ -450,7 +453,6 @@ public class WxPayServiceImpl implements WxPayService {
                             .setTotal(item.getTotal()).setRefund(item.getTotal()).setOutRefundNo(OrderUtils.getOrderNo()));
                     refundAmount.addAndGet(-item.getTotal());
                 } else {
-                    item.setTotal(refundAmount.get());
                     refundLogList.add(new RefundLog().setUserId(item.getUserId()).setOutTradeNo(item.getOutTradeNo())
                             .setTotal(item.getTotal()).setRefund(refundAmount.get()).setOutRefundNo(OrderUtils.getOrderNo()));
                 }
@@ -460,7 +462,7 @@ public class WxPayServiceImpl implements WxPayService {
             refundLogList.forEach(LambadaTools.forEachWithIndex((refundLog, index) -> {
                 int discountAmount;
                 if (index < size - 1) {
-                    discountAmount = BigDecimal.valueOf(account.getDiscountAmount()).multiply((BigDecimal.valueOf(refundLog.getRefund()).divide(originalRefundAmount,2, RoundingMode.HALF_UP))).intValue();
+                    discountAmount = BigDecimal.valueOf(account.getDiscountAmount()).multiply((BigDecimal.valueOf(refundLog.getRefund()).divide(originalRefundAmount, 2, RoundingMode.HALF_UP))).intValue();
                 } else {
                     // 前面存在精度误差,最后一个元素用总数相减最精确
                     discountAmount = account.getDiscountAmount() - newRefundLogList.stream().mapToInt(RefundLog::getDiscountAmount).sum();