Prechádzať zdrojové kódy

fix bug 退款申请

skyline 2 rokov pred
rodič
commit
6a1f7df6f6

+ 8 - 6
service/src/main/java/com/kym/service/wechat/impl/WxPayServiceImpl.java

@@ -57,6 +57,8 @@ import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.IOException;
+import java.math.BigDecimal;
+import java.math.RoundingMode;
 import java.nio.charset.StandardCharsets;
 import java.time.LocalDateTime;
 import java.util.ArrayList;
@@ -399,7 +401,7 @@ public class WxPayServiceImpl implements WxPayService {
 
         // 余减去优惠金额作为退款金额
         AtomicInteger refundAmount = new AtomicInteger(account.getBalance() - account.getDiscountAmount());
-        var originalRefundAmount = refundAmount.get();
+        var originalRefundAmount = BigDecimal.valueOf(refundAmount.get());
         // 充值记录
         var payLogs = payLogService.lambdaQuery().eq(PayLog::getUserId, userId).eq(PayLog::getTradeState, PayLog.STATUS_充值成功).orderByDesc(PayLog::getSuccessTime).list();
         // 充值金额综合不足以支付余额退款
@@ -420,6 +422,7 @@ public class WxPayServiceImpl implements WxPayService {
         } else if (!CommUtil.isEmptyOrNull(payLogs) && payLogs.get(0).getTotal() < refundAmount.get()) {
             // 最后一次的充值金额不能覆盖退款金额,拆分成多笔退款
             int amount = 0;
+            // 用来退款的充值记录
             var refundPayLogs = new ArrayList<PayLog>();
             for (PayLog payLog : payLogs) {
                 amount += payLog.getTotal();
@@ -436,7 +439,7 @@ public class WxPayServiceImpl implements WxPayService {
 
             refundPayLogs.forEach(LambadaTools.forEachWithIndex((item, index) -> {
                 // 如果不是最后一笔,金额全退,最后一笔退剩余的金额
-                if (index < size) {
+                if (index < size - 1) {
                     refundLogList.add(new RefundLog().setUserId(item.getUserId()).setOutTradeNo(item.getOutTradeNo())
                             .setTotal(item.getTotal()).setRefund(item.getTotal()).setOutRefundNo(OrderUtils.getOrderNo()));
                     refundAmount.addAndGet(-item.getTotal());
@@ -448,14 +451,13 @@ public class WxPayServiceImpl implements WxPayService {
             }));
 
             // 不可退金额按退款金额比例放入每笔退款中
-
             refundLogList.forEach(LambadaTools.forEachWithIndex((refundLog, index) -> {
                 int discountAmount;
-                if (index < size) {
-                    discountAmount = account.getDiscountAmount() * (refundLog.getRefund() / originalRefundAmount);
+                if (index < size - 1) {
+                    discountAmount = BigDecimal.valueOf(account.getDiscountAmount()).multiply((BigDecimal.valueOf(refundLog.getRefund()).divide(originalRefundAmount,2, RoundingMode.HALF_UP))).intValue();
                 } else {
                     // 前面存在精度误差,最后一个元素用总数相减最精确
-                    discountAmount = account.getDiscountAmount() - refundLogList.stream().mapToInt(RefundLog::getDiscountAmount).sum();
+                    discountAmount = account.getDiscountAmount() - newRefundLogList.stream().mapToInt(RefundLog::getDiscountAmount).sum();
                 }
                 refundLog.setDiscountAmount(discountAmount);
                 refundLog.setReason(CommUtil.isEmptyOrNull(reason) ? reason : JSONObject.parseObject(reason).getString("reason"));