This commit is contained in:
jsowell
2026-05-27 17:41:09 +08:00
parent 1d0acb72c4
commit 8168b3686a

View File

@@ -56,6 +56,8 @@ import java.util.stream.Collectors;
public class AdapayUnsplitRecordHandleServiceImpl implements AdapayUnsplitRecordHandleService { public class AdapayUnsplitRecordHandleServiceImpl implements AdapayUnsplitRecordHandleService {
private static final int IMPORT_BATCH_SIZE = 500; 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); private final Logger log = LoggerFactory.getLogger(AdapayUnsplitRecordHandleServiceImpl.class);
@@ -494,14 +496,20 @@ public class AdapayUnsplitRecordHandleServiceImpl implements AdapayUnsplitRecord
return false; return false;
} }
BigDecimal refundedAmount = getSucceededRefundAmount(orderBasicInfo); BigDecimal refundedAmount = getRefundedAmount(orderBasicInfo, false);
updateRefundAmount(paymentId, refundedAmount); updateRefundAmount(paymentId, refundedAmount);
if (refundedAmount.compareTo(dueRefundAmount) >= 0) { if (refundedAmount.compareTo(dueRefundAmount) >= 0) {
markRefundResult(paymentId, "SUCCESS"); markRefundResult(paymentId, "SUCCESS");
return true; 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 { try {
ApplyRefundDTO dto = new ApplyRefundDTO(); ApplyRefundDTO dto = new ApplyRefundDTO();
dto.setOrderCode(orderCode); dto.setOrderCode(orderCode);
@@ -512,16 +520,45 @@ public class AdapayUnsplitRecordHandleServiceImpl implements AdapayUnsplitRecord
orderBasicInfoService.refundOrderWithAdapay(dto); orderBasicInfoService.refundOrderWithAdapay(dto);
markRefundResult(paymentId, "PROCESSING"); markRefundResult(paymentId, "PROCESSING");
log.info("未分账数据先执行退款, paymentId:{}, orderCode:{}, dueRefundAmount:{}, refundedAmount:{}, refundAmount:{}", 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) { } catch (Exception e) {
markRefundResult(paymentId, "FAILED"); markRefundResult(paymentId, "FAILED");
log.error("未分账数据执行退款失败, paymentId:{}, orderCode:{}, dueRefundAmount:{}, refundedAmount:{}, refundAmount:{}", log.error("未分账数据执行退款失败, paymentId:{}, orderCode:{}, dueRefundAmount:{}, refundedAmount:{}, refundAmount:{}",
paymentId, orderCode, dueRefundAmount, refundedAmount, refundAmount, e); paymentId, orderCode, dueRefundAmount, acceptedRefundAmount, refundAmount, e);
} }
return false; 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<OrderDetailInfoVO.OrderRefundInfo> refundInfoList = orderBasicInfoService.getOrderRefundInfoList(orderBasicInfo); List<OrderDetailInfoVO.OrderRefundInfo> refundInfoList = orderBasicInfoService.getOrderRefundInfoList(orderBasicInfo);
if (CollectionUtils.isEmpty(refundInfoList)) { if (CollectionUtils.isEmpty(refundInfoList)) {
return BigDecimal.ZERO; return BigDecimal.ZERO;
@@ -533,7 +570,9 @@ public class AdapayUnsplitRecordHandleServiceImpl implements AdapayUnsplitRecord
continue; continue;
} }
String status = refundInfo.getStatus(); 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; continue;
} }
refundedAmount = refundedAmount.add(parseAmount(refundInfo.getReverseAmt())); refundedAmount = refundedAmount.add(parseAmount(refundInfo.getReverseAmt()));