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