From 8168b3686afe5e98cec99e248521ea54f026a989 Mon Sep 17 00:00:00 2001 From: jsowell <123@jsowell.com> Date: Wed, 27 May 2026 17:41:09 +0800 Subject: [PATCH] update --- .../AdapayUnsplitRecordHandleServiceImpl.java | 51 ++++++++++++++++--- 1 file changed, 45 insertions(+), 6 deletions(-) diff --git a/jsowell-quartz/src/main/java/com/jsowell/quartz/service/impl/AdapayUnsplitRecordHandleServiceImpl.java b/jsowell-quartz/src/main/java/com/jsowell/quartz/service/impl/AdapayUnsplitRecordHandleServiceImpl.java index 323f3a5de..c60743942 100644 --- a/jsowell-quartz/src/main/java/com/jsowell/quartz/service/impl/AdapayUnsplitRecordHandleServiceImpl.java +++ b/jsowell-quartz/src/main/java/com/jsowell/quartz/service/impl/AdapayUnsplitRecordHandleServiceImpl.java @@ -56,6 +56,8 @@ import java.util.stream.Collectors; public class AdapayUnsplitRecordHandleServiceImpl implements AdapayUnsplitRecordHandleService { private static final int IMPORT_BATCH_SIZE = 500; + private static final int REFUND_WAIT_MAX_ATTEMPTS = 12; + private static final long REFUND_WAIT_INTERVAL_MILLIS = 5000L; private final Logger log = LoggerFactory.getLogger(AdapayUnsplitRecordHandleServiceImpl.class); @@ -494,14 +496,20 @@ public class AdapayUnsplitRecordHandleServiceImpl implements AdapayUnsplitRecord return false; } - BigDecimal refundedAmount = getSucceededRefundAmount(orderBasicInfo); + BigDecimal refundedAmount = getRefundedAmount(orderBasicInfo, false); updateRefundAmount(paymentId, refundedAmount); if (refundedAmount.compareTo(dueRefundAmount) >= 0) { markRefundResult(paymentId, "SUCCESS"); return true; } - BigDecimal refundAmount = dueRefundAmount.subtract(refundedAmount).setScale(2, BigDecimal.ROUND_HALF_UP); + BigDecimal acceptedRefundAmount = getRefundedAmount(orderBasicInfo, true); + if (acceptedRefundAmount.compareTo(dueRefundAmount) >= 0) { + markRefundResult(paymentId, "PROCESSING"); + return waitRefundFullySucceeded(orderBasicInfo, paymentId, dueRefundAmount); + } + + BigDecimal refundAmount = dueRefundAmount.subtract(acceptedRefundAmount).setScale(2, BigDecimal.ROUND_HALF_UP); try { ApplyRefundDTO dto = new ApplyRefundDTO(); dto.setOrderCode(orderCode); @@ -512,16 +520,45 @@ public class AdapayUnsplitRecordHandleServiceImpl implements AdapayUnsplitRecord orderBasicInfoService.refundOrderWithAdapay(dto); markRefundResult(paymentId, "PROCESSING"); log.info("未分账数据先执行退款, paymentId:{}, orderCode:{}, dueRefundAmount:{}, refundedAmount:{}, refundAmount:{}", - paymentId, orderCode, dueRefundAmount, refundedAmount, refundAmount); + paymentId, orderCode, dueRefundAmount, acceptedRefundAmount, refundAmount); + return waitRefundFullySucceeded(orderBasicInfo, paymentId, dueRefundAmount); } catch (Exception e) { markRefundResult(paymentId, "FAILED"); log.error("未分账数据执行退款失败, paymentId:{}, orderCode:{}, dueRefundAmount:{}, refundedAmount:{}, refundAmount:{}", - paymentId, orderCode, dueRefundAmount, refundedAmount, refundAmount, e); + paymentId, orderCode, dueRefundAmount, acceptedRefundAmount, refundAmount, e); } return false; } - private BigDecimal getSucceededRefundAmount(OrderBasicInfo orderBasicInfo) { + private boolean waitRefundFullySucceeded(OrderBasicInfo orderBasicInfo, String paymentId, BigDecimal dueRefundAmount) { + for (int i = 1; i <= REFUND_WAIT_MAX_ATTEMPTS; i++) { + try { + Thread.sleep(REFUND_WAIT_INTERVAL_MILLIS); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + log.warn("等待退款足额被中断, paymentId:{}, orderCode:{}", paymentId, orderBasicInfo.getOrderCode()); + return false; + } + + BigDecimal refundedAmount = getRefundedAmount(orderBasicInfo, false); + updateRefundAmount(paymentId, refundedAmount); + if (refundedAmount.compareTo(dueRefundAmount) >= 0) { + markRefundResult(paymentId, "SUCCESS"); + log.info("未分账数据退款已足额,继续分账, paymentId:{}, orderCode:{}, dueRefundAmount:{}, refundedAmount:{}", + paymentId, orderBasicInfo.getOrderCode(), dueRefundAmount, refundedAmount); + return true; + } + log.info("等待未分账数据退款足额, paymentId:{}, orderCode:{}, attempt:{}/{}, dueRefundAmount:{}, refundedAmount:{}", + paymentId, orderBasicInfo.getOrderCode(), i, REFUND_WAIT_MAX_ATTEMPTS, dueRefundAmount, refundedAmount); + } + + markRefundResult(paymentId, "PROCESSING"); + log.warn("等待退款足额超时,本轮不分账, paymentId:{}, orderCode:{}, dueRefundAmount:{}", + paymentId, orderBasicInfo.getOrderCode(), dueRefundAmount); + return false; + } + + private BigDecimal getRefundedAmount(OrderBasicInfo orderBasicInfo, boolean includeProcessing) { List refundInfoList = orderBasicInfoService.getOrderRefundInfoList(orderBasicInfo); if (CollectionUtils.isEmpty(refundInfoList)) { return BigDecimal.ZERO; @@ -533,7 +570,9 @@ public class AdapayUnsplitRecordHandleServiceImpl implements AdapayUnsplitRecord continue; } String status = refundInfo.getStatus(); - if (StringUtils.isNotBlank(status) && !StringUtils.equals(status, AdapayStatusEnum.SUCCEEDED.getValue())) { + if (StringUtils.isNotBlank(status) + && !StringUtils.equals(status, AdapayStatusEnum.SUCCEEDED.getValue()) + && !(includeProcessing && StringUtils.equals(status, AdapayStatusEnum.PENDING.getValue()))) { continue; } refundedAmount = refundedAmount.add(parseAmount(refundInfo.getReverseAmt()));