From 672fb3787bf62420d105d73fcea07a901cda0f33 Mon Sep 17 00:00:00 2001 From: jsowell <123@jsowell.com> Date: Thu, 28 May 2026 14:48:25 +0800 Subject: [PATCH] =?UTF-8?q?update=20=E6=B8=85=E9=99=A4=E6=97=A7=E6=96=B9?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AdapayUnsplitRecordHandleService.java | 4 - .../AdapayUnsplitRecordHandleServiceImpl.java | 254 +----------------- .../com/jsowell/quartz/task/JsowellTask.java | 18 +- 3 files changed, 16 insertions(+), 260 deletions(-) diff --git a/jsowell-quartz/src/main/java/com/jsowell/quartz/service/AdapayUnsplitRecordHandleService.java b/jsowell-quartz/src/main/java/com/jsowell/quartz/service/AdapayUnsplitRecordHandleService.java index e4f2de2da..9e93f52a3 100644 --- a/jsowell-quartz/src/main/java/com/jsowell/quartz/service/AdapayUnsplitRecordHandleService.java +++ b/jsowell-quartz/src/main/java/com/jsowell/quartz/service/AdapayUnsplitRecordHandleService.java @@ -12,10 +12,6 @@ public interface AdapayUnsplitRecordHandleService { int completeAdapayUnsplitRecordFields(String startTime, String endTime); - int refreshAdapayUnsplitRecordHandleFlag(String paymentId, String wechatAppId); - - int refreshAdapayUnsplitRecordHandleFlag(String startTime, String endTime, String wechatAppId, Integer pageSize); - void processUnSettledOrder(); int syncAndRefreshFlagsFromAdapay(String paymentId, String wechatAppId); 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 b4713f79a..aa0540c45 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 @@ -119,20 +119,20 @@ public class AdapayUnsplitRecordHandleServiceImpl implements AdapayUnsplitRecord // 有应退款金额时,必须先确认退款已足额成功;未退足会先发起差额退款并等待,未足额则本轮不分账。 if (dueRefundAmount.compareTo(BigDecimal.ZERO) > 0 && !ensureRefundBeforeSplit(item, wechatAppId)) { - refreshHandleFlagQuietly(paymentId, wechatAppId); + syncAndRefreshFlagsFromAdapay(paymentId, wechatAppId); skipped++; continue; } if (waitSplitAmount.compareTo(BigDecimal.ZERO) <= 0) { - refreshHandleFlagQuietly(paymentId, wechatAppId); + syncAndRefreshFlagsFromAdapay(paymentId, wechatAppId); skipped++; continue; } BigDecimal confirmAmt = getLatestConfirmAmount(waitSplitAmount, item.getPayAmount(), item.getRefundAmount(), paymentId, wechatAppId); if (confirmAmt.compareTo(BigDecimal.ZERO) <= 0) { - refreshHandleFlagQuietly(paymentId, wechatAppId); + syncAndRefreshFlagsFromAdapay(paymentId, wechatAppId); skipped++; continue; } @@ -156,28 +156,22 @@ public class AdapayUnsplitRecordHandleServiceImpl implements AdapayUnsplitRecord failed++; log.error("处理未分账数据到默认账户异常, paymentId:{}, orderCode:{}, confirmAmt:{}", paymentId, orderCode, confirmAmt, e); - markSplitResult(paymentId, HANDLE_FLAG_FAILED); - refreshHandleFlagQuietly(paymentId, wechatAppId); + syncAndRefreshFlagsFromAdapay(paymentId, wechatAppId); continue; } if (response != null && response.isSuccess()) { success++; - updateConfirmedSplitAmount(item, confirmAmt, paymentId); - refreshHandleFlagQuietly(paymentId, wechatAppId); - BigDecimal totalConfirmedAmt = parseAmount(item.getConfirmedSplitAmount()).add(confirmAmt) - .setScale(2, BigDecimal.ROUND_HALF_UP); - log.info("处理未分账数据成功, paymentId:{}, orderCode:{}, 本次分账金额:{}, 累计已分账金额:{}, response:{}", - paymentId, orderCode, confirmAmt, totalConfirmedAmt, JSON.toJSONString(response)); + log.info("处理未分账数据成功, paymentId:{}, orderCode:{}, 本次分账金额:{}, response:{}", + paymentId, orderCode, confirmAmt, JSON.toJSONString(response)); } else { failed++; String errorCode = response == null ? "response_null" : response.getError_code(); String errorMsg = response == null ? "response_is_null" : response.getError_msg(); log.error("处理未分账数据失败, paymentId:{}, orderCode:{}, confirmAmt:{}, errorCode:{}, errorMsg:{}", paymentId, orderCode, confirmAmt, errorCode, errorMsg); - markSplitResult(paymentId, HANDLE_FLAG_FAILED); - refreshHandleFlagQuietly(paymentId, wechatAppId); } + syncAndRefreshFlagsFromAdapay(paymentId, wechatAppId); } if (list.size() < size) { @@ -230,44 +224,6 @@ public class AdapayUnsplitRecordHandleServiceImpl implements AdapayUnsplitRecord return updatedCount; } - @Override - public int refreshAdapayUnsplitRecordHandleFlag(String paymentId, String wechatAppId) { - if (StringUtils.isBlank(paymentId)) { - return 0; - } - List list = adapayUnsplitRecordService.selectByPaymentIds(Lists.newArrayList(paymentId)); - if (CollectionUtils.isEmpty(list)) { - log.warn("刷新未分账处理标识失败,记录不存在, paymentId:{}", paymentId); - return 0; - } - return refreshUnsplitRecordHandleFlag(list, StringUtils.isBlank(wechatAppId) ? Constants.DEFAULT_APP_ID : wechatAppId); - } - - @Override - public int refreshAdapayUnsplitRecordHandleFlag(String startTime, String endTime, String wechatAppId, Integer pageSize) { - int size = pageSize == null || pageSize <= 0 ? 1000 : pageSize; - int pageNum = 1; - int updatedCount = 0; - String appId = StringUtils.isBlank(wechatAppId) ? Constants.DEFAULT_APP_ID : wechatAppId; - - while (true) { - PageUtils.startPage(pageNum, size); - List list = adapayUnsplitRecordService.queryUnsplitOrders(startTime, endTime); - if (CollectionUtils.isEmpty(list)) { - break; - } - - updatedCount += refreshUnsplitRecordHandleFlag(list, appId); - if (list.size() < size) { - break; - } - pageNum++; - } - - log.info("刷新未分账处理标识完成, startTime:{}, endTime:{}, 更新:{}条", startTime, endTime, updatedCount); - return updatedCount; - } - @Override public int syncAndRefreshFlagsFromAdapay(String paymentId, String wechatAppId) { if (StringUtils.isBlank(paymentId)) { @@ -757,160 +713,6 @@ public class AdapayUnsplitRecordHandleServiceImpl implements AdapayUnsplitRecord return updatedCount; } - private int refreshUnsplitRecordHandleFlag(List list, String wechatAppId) { - if (CollectionUtils.isEmpty(list)) { - return 0; - } - - Set orderCodeSet = new HashSet<>(); - for (AdapayUnsplitRecord record : list) { - String orderCode = record.getOrderCode(); - if (StringUtils.isBlank(orderCode)) { - orderCode = extractOrderCode(record.getOrderNo()); - } - if (StringUtils.isNotBlank(orderCode)) { - orderCodeSet.add(orderCode); - } - } - - Map orderMap = new HashMap<>(); - if (CollectionUtils.isNotEmpty(orderCodeSet)) { - List orderList = orderBasicInfoService.selectOrderTemp(orderCodeSet); - orderMap = orderList.stream() - .collect(Collectors.toMap(OrderBasicInfo::getOrderCode, v -> v, (k1, k2) -> k1)); - } - - List updateList = new ArrayList<>(); - Date now = DateUtils.getNowDate(); - for (AdapayUnsplitRecord record : list) { - if (record == null || StringUtils.isBlank(record.getPaymentId())) { - continue; - } - - boolean needUpdate = false; - String paymentId = record.getPaymentId(); - String orderCode = record.getOrderCode(); - if (StringUtils.isBlank(orderCode)) { - orderCode = extractOrderCode(record.getOrderNo()); - if (StringUtils.isNotBlank(orderCode)) { - record.setOrderCode(orderCode); - needUpdate = true; - } - } - - OrderBasicInfo orderBasicInfo = StringUtils.isBlank(orderCode) ? null : orderMap.get(orderCode); - if (orderBasicInfo != null) { - if (!isSameAmount(record.getDueRefundAmount(), orderBasicInfo.getRefundAmount())) { - record.setDueRefundAmount(defaultAmount(orderBasicInfo.getRefundAmount())); - needUpdate = true; - } - if (!isSameAmount(record.getSettleAmount(), orderBasicInfo.getSettleAmount())) { - record.setSettleAmount(orderBasicInfo.getSettleAmount()); - needUpdate = true; - } - String pileType = YouDianUtils.isEBikePileSn(orderBasicInfo.getPileSn()) ? "eBike" : "EV"; - if (!StringUtils.equals(record.getPileType(), pileType)) { - record.setPileType(pileType); - needUpdate = true; - } - } - - BigDecimal dueRefundAmount = defaultAmount(record.getDueRefundAmount()); - RefundAmountCheck refundAmountCheck = checkRefundAmount(orderBasicInfo, dueRefundAmount); - if (!isSameAmount(record.getRefundAmount(), refundAmountCheck.refundedAmount)) { - record.setRefundAmount(refundAmountCheck.refundedAmount); - needUpdate = true; - } - String refundFlag = calculateHandleFlag(dueRefundAmount, refundAmountCheck.refundedAmount, refundAmountCheck.acceptedRefundAmount); - if (!StringUtils.equals(record.getRefundFlag(), refundFlag)) { - record.setRefundFlag(refundFlag); - needUpdate = true; - } - - BigDecimal expectedSplitAmount = defaultAmount(record.getPayAmount()).subtract(dueRefundAmount).setScale(2, BigDecimal.ROUND_HALF_UP); - if (expectedSplitAmount.compareTo(BigDecimal.ZERO) < 0) { - expectedSplitAmount = BigDecimal.ZERO; - } - SplitAmountCheck splitAmountCheck = checkSplitAmount(paymentId, wechatAppId); - BigDecimal confirmedSplitAmount = defaultAmount(record.getConfirmedSplitAmount()).max(splitAmountCheck.confirmedSplitAmount); - if (!isSameAmount(record.getConfirmedSplitAmount(), confirmedSplitAmount)) { - record.setConfirmedSplitAmount(confirmedSplitAmount); - needUpdate = true; - } - String splitFlag = calculateHandleFlag(expectedSplitAmount, confirmedSplitAmount, confirmedSplitAmount.add(splitAmountCheck.reservedSplitAmount)); - if (!StringUtils.equals(record.getSplitFlag(), splitFlag)) { - record.setSplitFlag(splitFlag); - needUpdate = true; - } - - if (needUpdate) { - record.setUpdateTime(now); - updateList.add(record); - } - log.info("刷新未分账处理标识, paymentId:{}, orderCode:{}, dueRefundAmount:{}, refundedAmount:{}, expectedSplitAmount:{}, confirmedSplitAmount:{}, refundFlag:{}, splitFlag:{}", - paymentId, orderCode, dueRefundAmount, refundAmountCheck.refundedAmount, expectedSplitAmount, confirmedSplitAmount, refundFlag, splitFlag); - } - - if (CollectionUtils.isNotEmpty(updateList)) { - adapayUnsplitRecordService.updateBatchSelective(updateList); - return updateList.size(); - } - return 0; - } - - private void refreshHandleFlagQuietly(String paymentId, String wechatAppId) { - try { - refreshAdapayUnsplitRecordHandleFlag(paymentId, wechatAppId); - } catch (Exception e) { - log.warn("刷新未分账处理标识异常,本轮继续处理后续记录, paymentId:{}", paymentId, e); - } - } - - private RefundAmountCheck checkRefundAmount(OrderBasicInfo orderBasicInfo, BigDecimal dueRefundAmount) { - if (dueRefundAmount.compareTo(BigDecimal.ZERO) <= 0) { - return new RefundAmountCheck(BigDecimal.ZERO, BigDecimal.ZERO); - } - if (orderBasicInfo == null) { - return new RefundAmountCheck(BigDecimal.ZERO, BigDecimal.ZERO); - } - BigDecimal refundedAmount = getRefundedAmount(orderBasicInfo, false); - BigDecimal acceptedRefundAmount = getRefundedAmount(orderBasicInfo, true); - return new RefundAmountCheck(refundedAmount, acceptedRefundAmount); - } - - private SplitAmountCheck checkSplitAmount(String paymentId, String wechatAppId) { - try { - QueryPaymentConfirmDTO dto = new QueryPaymentConfirmDTO(); - dto.setWechatAppId(wechatAppId); - dto.setPaymentId(paymentId); - QueryPaymentConfirmDetailResponse response = adapayService.queryPaymentConfirmList(dto); - if (response == null || CollectionUtils.isEmpty(response.getPaymentConfirms())) { - return new SplitAmountCheck(BigDecimal.ZERO, BigDecimal.ZERO); - } - - BigDecimal maxConfirmedAmount = BigDecimal.ZERO; - BigDecimal maxReservedAmount = BigDecimal.ZERO; - for (PaymentConfirmInfo confirm : response.getPaymentConfirms()) { - if (confirm == null) { - continue; - } - BigDecimal confirmedAmount = parseAmount(confirm.getConfirmedAmt()); - BigDecimal reservedAmount = parseAmount(confirm.getReservedAmt()); - if (confirmedAmount.compareTo(maxConfirmedAmount) > 0) { - maxConfirmedAmount = confirmedAmount; - } - if (reservedAmount.compareTo(maxReservedAmount) > 0) { - maxReservedAmount = reservedAmount; - } - } - return new SplitAmountCheck(maxConfirmedAmount.setScale(2, BigDecimal.ROUND_HALF_UP), - maxReservedAmount.setScale(2, BigDecimal.ROUND_HALF_UP)); - } catch (Exception e) { - log.warn("刷新未分账处理标识时查询分账金额失败, paymentId:{}", paymentId, e); - return new SplitAmountCheck(BigDecimal.ZERO, BigDecimal.ZERO); - } - } - private String calculateHandleFlag(BigDecimal expectedAmount, BigDecimal successAmount, BigDecimal acceptedAmount) { BigDecimal expected = defaultAmount(expectedAmount); BigDecimal success = defaultAmount(successAmount); @@ -1016,29 +818,6 @@ public class AdapayUnsplitRecordHandleServiceImpl implements AdapayUnsplitRecord return false; } - private BigDecimal getRefundedAmount(OrderBasicInfo orderBasicInfo, boolean includeProcessing) { - List refundInfoList = orderBasicInfoService.getOrderRefundInfoList(orderBasicInfo); - if (CollectionUtils.isEmpty(refundInfoList)) { - return BigDecimal.ZERO; - } - - BigDecimal refundedAmount = BigDecimal.ZERO; - for (OrderDetailInfoVO.OrderRefundInfo refundInfo : refundInfoList) { - if (refundInfo == null) { - continue; - } - // includeProcessing=false:只累计成功退款;includeProcessing=true:成功 + 处理中,用于判断是否还需要补发差额退款。 - String status = refundInfo.getStatus(); - if (StringUtils.isNotBlank(status) - && !StringUtils.equals(status, AdapayStatusEnum.SUCCEEDED.getValue()) - && !(includeProcessing && StringUtils.equals(status, AdapayStatusEnum.PENDING.getValue()))) { - continue; - } - refundedAmount = refundedAmount.add(parseAmount(refundInfo.getReverseAmt())); - } - return refundedAmount.setScale(2, BigDecimal.ROUND_HALF_UP); - } - private BigDecimal getLatestConfirmAmount(BigDecimal waitSplitAmount, String payAmount, String refundAmount, String paymentId, String wechatAppId) { BigDecimal confirmAmt = waitSplitAmount; try { @@ -1306,23 +1085,4 @@ public class AdapayUnsplitRecordHandleServiceImpl implements AdapayUnsplitRecord } } - private static class RefundAmountCheck { - private final BigDecimal refundedAmount; - private final BigDecimal acceptedRefundAmount; - - private RefundAmountCheck(BigDecimal refundedAmount, BigDecimal acceptedRefundAmount) { - this.refundedAmount = refundedAmount == null ? BigDecimal.ZERO : refundedAmount; - this.acceptedRefundAmount = acceptedRefundAmount == null ? BigDecimal.ZERO : acceptedRefundAmount; - } - } - - private static class SplitAmountCheck { - private final BigDecimal confirmedSplitAmount; - private final BigDecimal reservedSplitAmount; - - private SplitAmountCheck(BigDecimal confirmedSplitAmount, BigDecimal reservedSplitAmount) { - this.confirmedSplitAmount = confirmedSplitAmount == null ? BigDecimal.ZERO : confirmedSplitAmount; - this.reservedSplitAmount = reservedSplitAmount == null ? BigDecimal.ZERO : reservedSplitAmount; - } - } } diff --git a/jsowell-quartz/src/main/java/com/jsowell/quartz/task/JsowellTask.java b/jsowell-quartz/src/main/java/com/jsowell/quartz/task/JsowellTask.java index 0512a010a..aa5262495 100644 --- a/jsowell-quartz/src/main/java/com/jsowell/quartz/task/JsowellTask.java +++ b/jsowell-quartz/src/main/java/com/jsowell/quartz/task/JsowellTask.java @@ -529,20 +529,20 @@ public class JsowellTask { } /** - * 按 paymentId 刷新 adapay_unsplit_record 的退款/分账处理标识 - * jsowellTask.refreshAdapayUnsplitRecordHandleFlag(paymentId, wechatAppId) + * 按 paymentId 从汇付同步并刷新 adapay_unsplit_record 的退款/分账标识 + * jsowellTask.syncAndRefreshFlagsFromAdapay(paymentId, wechatAppId) */ - public void refreshAdapayUnsplitRecordHandleFlag(String paymentId, String wechatAppId) { - adapayUnsplitRecordHandleService.refreshAdapayUnsplitRecordHandleFlag(paymentId, wechatAppId); + public void syncAndRefreshFlagsFromAdapay(String paymentId, String wechatAppId) { + adapayUnsplitRecordHandleService.syncAndRefreshFlagsFromAdapay(paymentId, wechatAppId); } /** - * 批量刷新 adapay_unsplit_record 的退款/分账处理标识 - * jsowellTask.refreshAdapayUnsplitRecordHandleFlag(startTime, endTime, wechatAppId, pageSize) - * 示例:jsowellTask.refreshAdapayUnsplitRecordHandleFlag('2024-01-01 00:00:00', '2025-12-31 23:59:59', 'app_id', 500) + * 批量从汇付同步并刷新 adapay_unsplit_record 的退款/分账标识 + * jsowellTask.syncAndRefreshFlagsFromAdapay(startTime, endTime, wechatAppId, pageSize) + * 示例:jsowellTask.syncAndRefreshFlagsFromAdapay('2024-01-01 00:00:00', '2025-12-31 23:59:59', 'app_id', 500) */ - public void refreshAdapayUnsplitRecordHandleFlag(String startTime, String endTime, String wechatAppId, Integer pageSize) { - adapayUnsplitRecordHandleService.refreshAdapayUnsplitRecordHandleFlag(startTime, endTime, wechatAppId, pageSize); + public void syncAndRefreshFlagsFromAdapay(String startTime, String endTime, String wechatAppId, Integer pageSize) { + adapayUnsplitRecordHandleService.syncAndRefreshFlagsFromAdapay(startTime, endTime, wechatAppId, pageSize); } /**