update 优化代码

This commit is contained in:
jsowell
2026-05-28 14:26:31 +08:00
parent cfa92ce477
commit 902c92661e
2 changed files with 21 additions and 11 deletions

View File

@@ -156,7 +156,7 @@ public class PaymentTestController {
*/
@Test
public void processUnsplitRecordToDefaultMemberTest() {
adapayUnsplitRecordHandleService.processUnsplitRecordToDefaultMember(wechatAppId1, 20);
adapayUnsplitRecordHandleService.processUnsplitRecordToDefaultMember(wechatAppId1, 2000);
}
/**

View File

@@ -925,7 +925,6 @@ public class AdapayUnsplitRecordHandleServiceImpl implements AdapayUnsplitRecord
private boolean ensureRefundBeforeSplit(AdapayUnsplitRecordVO item, String wechatAppId) {
String orderCode = item.getOrderCode();
String paymentId = item.getPaymentId();
// VO 中 refundAmount 来源于 adapay_unsplit_record.due_refund_amount表示分账前必须完成的应退款金额。
BigDecimal dueRefundAmount = parseAmount(item.getRefundAmount());
if (dueRefundAmount.compareTo(BigDecimal.ZERO) <= 0) {
return true;
@@ -938,19 +937,30 @@ public class AdapayUnsplitRecordHandleServiceImpl implements AdapayUnsplitRecord
return false;
}
// 先只统计成功退款金额;成功退款已足额时才允许继续后续分账。
BigDecimal refundedAmount = getRefundedAmount(orderBasicInfo, false);
// 以订单表最新退款金额为准,避免 VO 中的旧值导致误判
BigDecimal freshDueRefund = defaultAmount(orderBasicInfo.getRefundAmount());
if (!isSameAmount(dueRefundAmount, freshDueRefund)) {
dueRefundAmount = freshDueRefund;
AdapayUnsplitRecord syncRecord = buildUpdateRecordByPaymentId(paymentId);
syncRecord.setDueRefundAmount(freshDueRefund);
adapayUnsplitRecordService.insertOrUpdateSelective(syncRecord);
}
// 从汇付 API 查询实际退款状态,避免本地回调延迟导致数据滞后
RefundAmountResult refundResult = queryRefundAmountFromAdapay(paymentId, wechatAppId);
BigDecimal refundedAmount = refundResult.refundedAmount;
BigDecimal acceptedRefundAmount = refundResult.acceptedRefundAmount;
updateRefundAmount(paymentId, refundedAmount);
if (refundedAmount.compareTo(dueRefundAmount) >= 0) {
markRefundResult(paymentId, HANDLE_FLAG_SUCCESS);
return true;
}
// 再统计已受理退款金额(成功 + 处理中),避免处理中退款未回调时重复发起退款。
BigDecimal acceptedRefundAmount = getRefundedAmount(orderBasicInfo, true);
// 已受理退款已足额,等待退款完成
if (acceptedRefundAmount.compareTo(dueRefundAmount) >= 0) {
markRefundResult(paymentId, HANDLE_FLAG_PROCESSING);
return waitRefundFullySucceeded(orderBasicInfo, paymentId, dueRefundAmount);
return waitRefundFullySucceeded(orderBasicInfo, paymentId, dueRefundAmount, wechatAppId);
}
// 已受理退款仍不足时,只补发差额退款;补发后等待成功退款金额达到应退款金额。
@@ -966,7 +976,7 @@ public class AdapayUnsplitRecordHandleServiceImpl implements AdapayUnsplitRecord
markRefundResult(paymentId, HANDLE_FLAG_PROCESSING);
log.info("未分账数据先执行退款, paymentId:{}, orderCode:{}, dueRefundAmount:{}, refundedAmount:{}, refundAmount:{}",
paymentId, orderCode, dueRefundAmount, acceptedRefundAmount, refundAmount);
return waitRefundFullySucceeded(orderBasicInfo, paymentId, dueRefundAmount);
return waitRefundFullySucceeded(orderBasicInfo, paymentId, dueRefundAmount, wechatAppId);
} catch (Exception e) {
markRefundResult(paymentId, HANDLE_FLAG_FAILED);
log.error("未分账数据执行退款失败, paymentId:{}, orderCode:{}, dueRefundAmount:{}, refundedAmount:{}, refundAmount:{}",
@@ -975,7 +985,8 @@ public class AdapayUnsplitRecordHandleServiceImpl implements AdapayUnsplitRecord
return false;
}
private boolean waitRefundFullySucceeded(OrderBasicInfo orderBasicInfo, String paymentId, BigDecimal dueRefundAmount) {
private boolean waitRefundFullySucceeded(OrderBasicInfo orderBasicInfo, String paymentId,
BigDecimal dueRefundAmount, String wechatAppId) {
for (int i = 1; i <= REFUND_WAIT_MAX_ATTEMPTS; i++) {
try {
Thread.sleep(REFUND_WAIT_INTERVAL_MILLIS);
@@ -985,8 +996,7 @@ public class AdapayUnsplitRecordHandleServiceImpl implements AdapayUnsplitRecord
return false;
}
// 这里仍然只认成功退款金额,处理中退款不满足“退款足额后再分账”的条件。
BigDecimal refundedAmount = getRefundedAmount(orderBasicInfo, false);
BigDecimal refundedAmount = queryRefundAmountFromAdapay(paymentId, wechatAppId).refundedAmount;
updateRefundAmount(paymentId, refundedAmount);
if (refundedAmount.compareTo(dueRefundAmount) >= 0) {
markRefundResult(paymentId, HANDLE_FLAG_SUCCESS);