This commit is contained in:
jsowell
2026-05-27 17:13:13 +08:00
parent c0e86cd820
commit 1c66194063
5 changed files with 860 additions and 124 deletions

View File

@@ -15,6 +15,7 @@ import com.jsowell.common.constant.CacheConstants;
import com.jsowell.common.constant.Constants;
import com.jsowell.common.core.redis.RedisCache;
import com.jsowell.common.enums.SoftwareProtocolEnum;
import com.jsowell.common.enums.adapay.AdapayStatusEnum;
import com.jsowell.common.enums.thirdparty.ThirdPlatformTypeEnum;
import com.jsowell.common.enums.ykc.PileChannelEntity;
import com.jsowell.common.util.DateUtils;
@@ -26,6 +27,7 @@ import com.jsowell.pile.domain.OrderBasicInfo;
import com.jsowell.pile.domain.PileBasicInfo;
import com.jsowell.pile.domain.PileMerchantInfo;
import com.jsowell.pile.domain.PileStationInfo;
import com.jsowell.pile.dto.ApplyRefundDTO;
import com.jsowell.pile.domain.ykcCommond.PublishPileBillingTemplateCommand;
import com.jsowell.pile.domain.ykcCommond.ProofreadTimeCommand;
import com.jsowell.pile.domain.ykcCommond.StartChargingCommand;
@@ -33,6 +35,8 @@ import com.jsowell.pile.service.*;
import com.jsowell.pile.vo.AdapayUnsplitRecordVO;
import com.jsowell.pile.vo.base.StationInfoVO;
import com.jsowell.pile.vo.web.BillingTemplateVO;
import com.jsowell.pile.vo.web.OrderDetailInfoVO;
import com.jsowell.quartz.service.AdapayUnsplitRecordHandleService;
import com.jsowell.thirdparty.amap.service.AMapService;
import com.jsowell.thirdparty.common.NotificationDTO;
import com.jsowell.thirdparty.common.NotificationService;
@@ -100,6 +104,9 @@ public class JsowellTask {
@Autowired
private AdapayUnsplitRecordService adapayUnsplitRecordService;
@Autowired
private AdapayUnsplitRecordHandleService adapayUnsplitRecordHandleService;
private static final long YKC_DAILY_TIMECHECK_INTERVAL_MILLIS = 200L;
/**
@@ -514,7 +521,7 @@ public class JsowellTask {
* jsowellTask.processUnsplitRecordToDefaultMember()
*/
public void processUnsplitRecordToDefaultMember() {
processUnsplitRecordToDefaultMember(Constants.DEFAULT_APP_ID, 500);
adapayUnsplitRecordHandleService.processUnsplitRecordToDefaultMember();
}
/**
@@ -522,85 +529,7 @@ public class JsowellTask {
* jsowellTask.processUnsplitRecordToDefaultMember(wechatAppId, pageSize)
*/
public void processUnsplitRecordToDefaultMember(String wechatAppId, Integer pageSize) {
int size = pageSize == null || pageSize <= 0 ? 500 : pageSize;
int pageNum = 1;
int total = 0;
int success = 0;
int skipped = 0;
int failed = 0;
while (true) {
PageUtils.startPage(pageNum, size);
List<AdapayUnsplitRecordVO> list = adapayUnsplitRecordService.queryList();
if (CollectionUtils.isEmpty(list)) {
break;
}
log.info("处理未分账数据到默认账户, pageNum:{}, pageSize:{}, 当前页:{}条", pageNum, size, list.size());
for (AdapayUnsplitRecordVO item : list) {
total++;
String paymentId = item.getPaymentId();
String orderCode = item.getOrderCode();
BigDecimal waitSplitAmount = parseAmount(item.getWaitSplitAmount());
if (StringUtils.isBlank(paymentId) || waitSplitAmount.compareTo(BigDecimal.ZERO) <= 0) {
skipped++;
continue;
}
BigDecimal confirmAmt = getLatestConfirmAmount(waitSplitAmount, item.getPayAmount(), paymentId, wechatAppId);
if (confirmAmt.compareTo(BigDecimal.ZERO) <= 0) {
skipped++;
continue;
}
PaymentConfirmResponse response;
try {
DivMember divMember = new DivMember();
divMember.setMemberId(Constants.ZERO);
divMember.setAmount(confirmAmt.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString());
divMember.setFeeFlag(Constants.Y);
PaymentConfirmParam param = PaymentConfirmParam.builder()
.paymentId(paymentId)
.divMemberList(Lists.newArrayList(divMember))
.confirmAmt(confirmAmt)
.orderCode(orderCode)
.wechatAppId(wechatAppId)
.build();
response = adapayService.createPaymentConfirmRequest(param);
} catch (Exception e) {
failed++;
log.error("处理未分账数据到默认账户异常, paymentId:{}, orderCode:{}, confirmAmt:{}",
paymentId, orderCode, confirmAmt, e);
markSplitResult(paymentId, "FAILED");
continue;
}
if (response != null && response.isSuccess()) {
success++;
updateConfirmedSplitAmount(item, confirmAmt, paymentId);
markSplitResult(paymentId, "SUCCESS");
log.info("处理未分账数据成功, paymentId:{}, orderCode:{}, confirmAmt:{}, 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, "FAILED");
}
}
if (list.size() < size) {
break;
}
pageNum++;
}
log.info("处理未分账数据到默认账户结束, total:{}, success:{}, skipped:{}, failed:{}",
total, success, skipped, failed);
adapayUnsplitRecordHandleService.processUnsplitRecordToDefaultMember(wechatAppId, pageSize);
}
/**
@@ -614,7 +543,7 @@ public class JsowellTask {
* jsowellTask.importAdapayUnsplitRecordAndCompleteFields()
*/
public void importAdapayUnsplitRecordAndCompleteFields() {
importAdapayUnsplitRecordAndCompleteFields("doc/万车充小程序-未分账明细20260526.xlsx");
adapayUnsplitRecordHandleService.importAdapayUnsplitRecordAndCompleteFields();
}
/**
@@ -624,8 +553,7 @@ public class JsowellTask {
* 示例jsowellTask.completeAdapayUnsplitRecordFields('2024-01-01 00:00:00', '2025-12-31 23:59:59')
*/
public void completeAdapayUnsplitRecordFields(String startTime, String endTime) {
int updatedCount = completeUnsplitRecordMissingFields(startTime, endTime, 1000);
log.info("补齐未分账数据缺失字段完成, startTime:{}, endTime:{}, 更新:{}条", startTime, endTime, updatedCount);
adapayUnsplitRecordHandleService.completeAdapayUnsplitRecordFields(startTime, endTime);
}
/**
@@ -638,35 +566,7 @@ public class JsowellTask {
* jsowellTask.importAdapayUnsplitRecordAndCompleteFields(文件路径)
*/
public void importAdapayUnsplitRecordAndCompleteFields(String filePath) {
// 相对路径转绝对路径(基于 JVM 工作目录)
Path path = Paths.get(filePath);
if (!path.isAbsolute()) {
path = Paths.get(System.getProperty("user.dir"), filePath);
}
if (!Files.exists(path)) {
log.error("导入未分账数据失败,文件不存在:{}", path.toAbsolutePath());
return;
}
// 第一步读取Excel将每行数据 insertOrUpdate 到 adapay_unsplit_record 表
ImportSummary summary = importAdapayUnsplitRecord(path);
if (summary.successRows == 0) {
log.info("导入未分账数据结束,未写入任何数据,统计:{}", summary);
return;
}
// 第二步:以导入数据中最早/最晚支付时间为范围,补齐已入库记录的缺失字段
// 若 Excel 中无有效支付时间则使用兜底时间范围2024-01-01 至当前时间)
String startTime = summary.minPayTime == null
? "2024-01-01 00:00:00"
: DateUtils.formatDateTime(summary.minPayTime);
String endTime = summary.maxPayTime == null
? DateUtils.getDateTime()
: DateUtils.formatDateTime(summary.maxPayTime);
int updatedCount = completeUnsplitRecordMissingFields(startTime, endTime, 1000);
log.info("导入并补齐未分账数据完成, 导入统计:{}, 补齐更新:{}条", summary, updatedCount);
adapayUnsplitRecordHandleService.importAdapayUnsplitRecordAndCompleteFields(filePath);
}
private static final int IMPORT_BATCH_SIZE = 500;
@@ -885,14 +785,76 @@ public class JsowellTask {
return updatedCount;
}
private BigDecimal getLatestConfirmAmount(BigDecimal waitSplitAmount, String payAmount, String paymentId, String wechatAppId) {
private boolean ensureRefundBeforeSplit(AdapayUnsplitRecordVO item, String wechatAppId) {
String orderCode = item.getOrderCode();
String paymentId = item.getPaymentId();
BigDecimal dueRefundAmount = parseAmount(item.getRefundAmount());
if (dueRefundAmount.compareTo(BigDecimal.ZERO) <= 0) {
return true;
}
OrderBasicInfo orderBasicInfo = orderBasicInfoService.getOrderInfoByOrderCode(orderCode);
if (orderBasicInfo == null) {
log.warn("未分账数据退款前置校验失败,订单不存在, paymentId:{}, orderCode:{}", paymentId, orderCode);
markRefundResult(paymentId, "FAILED");
return false;
}
BigDecimal refundedAmount = getSucceededRefundAmount(orderBasicInfo);
updateRefundAmount(paymentId, refundedAmount);
if (refundedAmount.compareTo(dueRefundAmount) >= 0) {
markRefundResult(paymentId, "SUCCESS");
return true;
}
BigDecimal refundAmount = dueRefundAmount.subtract(refundedAmount).setScale(2, BigDecimal.ROUND_HALF_UP);
try {
ApplyRefundDTO dto = new ApplyRefundDTO();
dto.setOrderCode(orderCode);
dto.setRefundType(Constants.ONE);
dto.setRefundAmount(refundAmount);
dto.setWechatAppId(wechatAppId);
dto.setMemberId(orderBasicInfo.getMemberId());
orderBasicInfoService.refundOrderWithAdapay(dto);
markRefundResult(paymentId, "PROCESSING");
log.info("未分账数据先执行退款, paymentId:{}, orderCode:{}, dueRefundAmount:{}, refundedAmount:{}, refundAmount:{}",
paymentId, orderCode, dueRefundAmount, refundedAmount, refundAmount);
} catch (Exception e) {
markRefundResult(paymentId, "FAILED");
log.error("未分账数据执行退款失败, paymentId:{}, orderCode:{}, dueRefundAmount:{}, refundedAmount:{}, refundAmount:{}",
paymentId, orderCode, dueRefundAmount, refundedAmount, refundAmount, e);
}
return false;
}
private BigDecimal getSucceededRefundAmount(OrderBasicInfo orderBasicInfo) {
List<OrderDetailInfoVO.OrderRefundInfo> refundInfoList = orderBasicInfoService.getOrderRefundInfoList(orderBasicInfo);
if (CollectionUtils.isEmpty(refundInfoList)) {
return BigDecimal.ZERO;
}
BigDecimal refundedAmount = BigDecimal.ZERO;
for (OrderDetailInfoVO.OrderRefundInfo refundInfo : refundInfoList) {
if (refundInfo == null) {
continue;
}
String status = refundInfo.getStatus();
if (StringUtils.isNotBlank(status) && !StringUtils.equals(status, AdapayStatusEnum.SUCCEEDED.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 {
QueryPaymentConfirmDTO dto = new QueryPaymentConfirmDTO();
dto.setWechatAppId(wechatAppId);
dto.setPaymentId(paymentId);
QueryPaymentConfirmDetailResponse response = adapayService.queryPaymentConfirmList(dto);
BigDecimal latestRemaining = calculateLatestRemainingAmount(payAmount, response);
BigDecimal latestRemaining = calculateLatestRemainingAmount(payAmount, refundAmount, response);
if (latestRemaining.compareTo(BigDecimal.ZERO) > 0) {
confirmAmt = waitSplitAmount.min(latestRemaining);
}
@@ -903,10 +865,10 @@ public class JsowellTask {
return confirmAmt;
}
private BigDecimal calculateLatestRemainingAmount(String payAmount, QueryPaymentConfirmDetailResponse response) {
BigDecimal payAmt = parseAmount(payAmount);
if (payAmt.compareTo(BigDecimal.ZERO) <= 0 || response == null || CollectionUtils.isEmpty(response.getPaymentConfirms())) {
return payAmt;
private BigDecimal calculateLatestRemainingAmount(String payAmount, String refundAmount, QueryPaymentConfirmDetailResponse response) {
BigDecimal splitLimitAmount = parseAmount(payAmount).subtract(parseAmount(refundAmount));
if (splitLimitAmount.compareTo(BigDecimal.ZERO) <= 0 || response == null || CollectionUtils.isEmpty(response.getPaymentConfirms())) {
return splitLimitAmount.compareTo(BigDecimal.ZERO) > 0 ? splitLimitAmount : BigDecimal.ZERO;
}
BigDecimal maxConfirmedAmt = BigDecimal.ZERO;
@@ -922,7 +884,7 @@ public class JsowellTask {
maxReservedAmt = reservedAmt;
}
}
BigDecimal latestRemaining = payAmt.subtract(maxConfirmedAmt).subtract(maxReservedAmt);
BigDecimal latestRemaining = splitLimitAmount.subtract(maxConfirmedAmt).subtract(maxReservedAmt);
return latestRemaining.compareTo(BigDecimal.ZERO) > 0 ? latestRemaining : BigDecimal.ZERO;
}
@@ -944,6 +906,22 @@ public class JsowellTask {
adapayUnsplitRecordService.insertOrUpdateSelective(updateRecord);
}
private void updateRefundAmount(String paymentId, BigDecimal refundAmount) {
AdapayUnsplitRecord updateRecord = new AdapayUnsplitRecord();
updateRecord.setPaymentId(paymentId);
updateRecord.setRefundAmount(refundAmount.setScale(2, BigDecimal.ROUND_HALF_UP));
updateRecord.setUpdateTime(DateUtils.getNowDate());
adapayUnsplitRecordService.insertOrUpdateSelective(updateRecord);
}
private void markRefundResult(String paymentId, String refundFlag) {
AdapayUnsplitRecord updateRecord = new AdapayUnsplitRecord();
updateRecord.setPaymentId(paymentId);
updateRecord.setRefundFlag(refundFlag);
updateRecord.setUpdateTime(DateUtils.getNowDate());
adapayUnsplitRecordService.insertOrUpdateSelective(updateRecord);
}
private AdapayUnsplitRecord convertRowToRecord(Row row, Map<String, Integer> headerIndexMap, DataFormatter formatter) {
String paymentId = getCellString(row, headerIndexMap.get(normalizeHeader("交易流水号")), formatter);
if (StringUtils.isBlank(paymentId)) {