更新flag标识

This commit is contained in:
jsowell
2026-05-28 13:59:09 +08:00
parent 1c44b2242c
commit 2d8e5f1ba7
4 changed files with 438 additions and 3 deletions

View File

@@ -17,4 +17,8 @@ public interface AdapayUnsplitRecordHandleService {
int refreshAdapayUnsplitRecordHandleFlag(String startTime, String endTime, String wechatAppId, Integer pageSize);
void processUnSettledOrder();
int syncAndRefreshFlagsFromAdapay(String paymentId, String wechatAppId);
int syncAndRefreshFlagsFromAdapay(String startTime, String endTime, String wechatAppId, Integer pageSize);
}

View File

@@ -7,6 +7,7 @@ import com.jsowell.adapay.common.PaymentConfirmInfo;
import com.jsowell.adapay.dto.PaymentConfirmParam;
import com.jsowell.adapay.dto.QueryPaymentConfirmDTO;
import com.jsowell.adapay.response.PaymentConfirmResponse;
import com.jsowell.adapay.response.PaymentReverseResponse;
import com.jsowell.adapay.response.QueryPaymentConfirmDetailResponse;
import com.jsowell.adapay.service.AdapayService;
import com.jsowell.common.YouDianUtils;
@@ -265,6 +266,231 @@ public class AdapayUnsplitRecordHandleServiceImpl implements AdapayUnsplitRecord
return updatedCount;
}
@Override
public int syncAndRefreshFlagsFromAdapay(String paymentId, String wechatAppId) {
if (StringUtils.isBlank(paymentId)) {
return 0;
}
List<AdapayUnsplitRecord> list = adapayUnsplitRecordService.selectByPaymentIds(Lists.newArrayList(paymentId));
if (CollectionUtils.isEmpty(list)) {
log.warn("同步刷新未分账记录失败,记录不存在, paymentId:{}", paymentId);
return 0;
}
String appId = StringUtils.isBlank(wechatAppId) ? Constants.DEFAULT_APP_ID : wechatAppId;
return doSyncAndRefresh(list.get(0), appId) ? 1 : 0;
}
@Override
public int syncAndRefreshFlagsFromAdapay(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<AdapayUnsplitRecord> list = adapayUnsplitRecordService.queryUnsplitOrders(startTime, endTime);
if (CollectionUtils.isEmpty(list)) {
break;
}
int batchUpdated = 0;
for (AdapayUnsplitRecord record : list) {
if (doSyncAndRefresh(record, appId)) {
batchUpdated++;
}
}
updatedCount += batchUpdated;
if (list.size() < size) {
break;
}
pageNum++;
}
log.info("同步刷新未分账记录完成, startTime:{}, endTime:{}, 更新:{}条", startTime, endTime, updatedCount);
return updatedCount;
}
private boolean doSyncAndRefresh(AdapayUnsplitRecord record, String wechatAppId) {
if (record == null || StringUtils.isBlank(record.getPaymentId())) {
return false;
}
String paymentId = record.getPaymentId();
String orderCode = record.getOrderCode();
if (StringUtils.isBlank(orderCode)) {
orderCode = extractOrderCode(record.getOrderNo());
if (StringUtils.isNotBlank(orderCode)) {
record.setOrderCode(orderCode);
}
}
boolean needUpdate = false;
RefundAmountResult refundResult = queryRefundAmountFromAdapay(paymentId, wechatAppId);
SplitAmountResult splitResult = querySplitAmountFromAdapay(paymentId, wechatAppId);
if (StringUtils.isNotBlank(orderCode)) {
OrderBasicInfo orderBasicInfo = orderBasicInfoService.getOrderInfoByOrderCode(orderCode);
if (orderBasicInfo != null) {
BigDecimal orderDueRefund = defaultAmount(orderBasicInfo.getRefundAmount());
if (!isSameAmount(record.getDueRefundAmount(), orderDueRefund)) {
record.setDueRefundAmount(orderDueRefund);
needUpdate = true;
}
BigDecimal orderSettle = defaultAmount(orderBasicInfo.getSettleAmount());
if (!isSameAmount(record.getSettleAmount(), orderSettle)) {
record.setSettleAmount(orderSettle);
needUpdate = true;
}
String pileType = YouDianUtils.isEBikePileSn(orderBasicInfo.getPileSn()) ? "eBike" : "EV";
if (!StringUtils.equals(record.getPileType(), pileType)) {
record.setPileType(pileType);
needUpdate = true;
}
}
}
if (!isSameAmount(record.getRefundAmount(), refundResult.refundedAmount)) {
record.setRefundAmount(refundResult.refundedAmount);
needUpdate = true;
}
if (!isSameAmount(record.getConfirmedSplitAmount(), splitResult.confirmedSplitAmount)) {
record.setConfirmedSplitAmount(splitResult.confirmedSplitAmount);
needUpdate = true;
}
BigDecimal remaining = calculateRemainingSplitAmount(
defaultAmount(record.getPayAmount()),
defaultAmount(record.getDueRefundAmount()),
splitResult.confirmedSplitAmount,
splitResult.reservedSplitAmount);
if (!isSameAmount(record.getRemainingSplitAmount(), remaining)) {
record.setRemainingSplitAmount(remaining);
needUpdate = true;
}
BigDecimal dueRefundAmount = defaultAmount(record.getDueRefundAmount());
String refundFlag = calculateHandleFlag(dueRefundAmount, refundResult.refundedAmount, refundResult.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;
}
BigDecimal acceptedSplitAmount = splitResult.confirmedSplitAmount.add(splitResult.reservedSplitAmount);
String splitFlag = calculateHandleFlag(expectedSplitAmount, splitResult.confirmedSplitAmount, acceptedSplitAmount);
if (!StringUtils.equals(record.getSplitFlag(), splitFlag)) {
record.setSplitFlag(splitFlag);
needUpdate = true;
}
if (needUpdate) {
record.setUpdateTime(DateUtils.getNowDate());
adapayUnsplitRecordService.insertOrUpdateSelective(record);
log.info("同步刷新未分账记录, paymentId:{}, orderCode:{}, dueRefundAmount:{}, refundedAmount:{}, "
+ "expectedSplitAmount:{}, confirmedSplitAmount:{}, remainingSplitAmount:{}, refundFlag:{}, splitFlag:{}",
paymentId, orderCode, dueRefundAmount, refundResult.refundedAmount,
expectedSplitAmount, splitResult.confirmedSplitAmount, remaining, refundFlag, splitFlag);
return true;
}
return false;
}
private RefundAmountResult queryRefundAmountFromAdapay(String paymentId, String wechatAppId) {
try {
List<PaymentReverseResponse> reverses =
adapayService.queryPaymentReverse(paymentId, wechatAppId);
if (CollectionUtils.isEmpty(reverses)) {
return new RefundAmountResult(BigDecimal.ZERO, BigDecimal.ZERO);
}
BigDecimal refundedAmount = BigDecimal.ZERO;
BigDecimal acceptedRefundAmount = BigDecimal.ZERO;
for (PaymentReverseResponse reverse : reverses) {
if (reverse == null) {
continue;
}
BigDecimal amt = parseAmount(reverse.getReverse_amt());
acceptedRefundAmount = acceptedRefundAmount.add(amt);
if (reverse.isSuccess()) {
refundedAmount = refundedAmount.add(amt);
}
}
return new RefundAmountResult(
refundedAmount.setScale(2, BigDecimal.ROUND_HALF_UP),
acceptedRefundAmount.setScale(2, BigDecimal.ROUND_HALF_UP));
} catch (Exception e) {
log.warn("查询汇付支付撤销金额异常, paymentId:{}", paymentId, e);
return new RefundAmountResult(BigDecimal.ZERO, BigDecimal.ZERO);
}
}
private SplitAmountResult querySplitAmountFromAdapay(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 SplitAmountResult(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 SplitAmountResult(
maxConfirmedAmount.setScale(2, BigDecimal.ROUND_HALF_UP),
maxReservedAmount.setScale(2, BigDecimal.ROUND_HALF_UP));
} catch (Exception e) {
log.warn("查询汇付支付确认金额异常, paymentId:{}", paymentId, e);
return new SplitAmountResult(BigDecimal.ZERO, BigDecimal.ZERO);
}
}
private BigDecimal calculateRemainingSplitAmount(BigDecimal payAmount, BigDecimal dueRefundAmount,
BigDecimal confirmedSplitAmount, BigDecimal reservedSplitAmount) {
BigDecimal remaining = payAmount.subtract(dueRefundAmount)
.subtract(confirmedSplitAmount)
.subtract(reservedSplitAmount);
return remaining.compareTo(BigDecimal.ZERO) > 0
? remaining.setScale(2, BigDecimal.ROUND_HALF_UP) : BigDecimal.ZERO;
}
private static class RefundAmountResult {
final BigDecimal refundedAmount;
final BigDecimal acceptedRefundAmount;
RefundAmountResult(BigDecimal refundedAmount, BigDecimal acceptedRefundAmount) {
this.refundedAmount = refundedAmount == null ? BigDecimal.ZERO : refundedAmount;
this.acceptedRefundAmount = acceptedRefundAmount == null ? BigDecimal.ZERO : acceptedRefundAmount;
}
}
private static class SplitAmountResult {
final BigDecimal confirmedSplitAmount;
final BigDecimal reservedSplitAmount;
SplitAmountResult(BigDecimal confirmedSplitAmount, BigDecimal reservedSplitAmount) {
this.confirmedSplitAmount = confirmedSplitAmount == null ? BigDecimal.ZERO : confirmedSplitAmount;
this.reservedSplitAmount = reservedSplitAmount == null ? BigDecimal.ZERO : reservedSplitAmount;
}
}
/**
* V1方法获取退款金额与结算金额
*/