mirror of
https://codeup.aliyun.com/67c68d4e484ca2f0a13ac3c1/ydc/jsowell-charger-web.git
synced 2026-04-19 18:45:03 +08:00
update 新增处理未分账订单逻辑
This commit is contained in:
180
doc/万车充小程序未分账补分账SOP.md
Normal file
180
doc/万车充小程序未分账补分账SOP.md
Normal file
@@ -0,0 +1,180 @@
|
||||
# 万车充小程序未分账补分账 SOP(adapayMemberId=0)
|
||||
|
||||
## 1. 目标与背景
|
||||
|
||||
- 目标:将未分账订单批量向汇付发起 `PaymentConfirm` 请求,分账对象固定为 `adapayMemberId=0`(平台默认账户)。
|
||||
- 数据来源:`doc/万车充小程序-未分账明细.xlsx`。
|
||||
- 编写日期:2026-03-11(Asia/Shanghai)。
|
||||
|
||||
说明:本 SOP 面向“拿到一份未分账明细文件后,批量补分账”的场景。文档中的流程可复用于后续同类任务。
|
||||
|
||||
## 2. 本次文件分析结论
|
||||
|
||||
对 `doc/万车充小程序-未分账明细.xlsx` 的 `Sheet1` 统计结果如下:
|
||||
|
||||
- 总记录数:`42,225`
|
||||
- `paymentId` 去重后重复数:`0`
|
||||
- `app_id`:全部为 `app_d0c80cb1-ffc8-48cb-a030-fe9bec823aaa`
|
||||
- 支付时间范围:`2024-09-07 19:29:00` ~ `2026-03-06 00:11:20`
|
||||
- 未分账金额(列“剩余未分账金额”)
|
||||
- 合计:`171,710.73`
|
||||
- 最小:`0.01`
|
||||
- 中位数:`0.50`
|
||||
- P90:`8.45`
|
||||
- 最大:`268.39`
|
||||
- 金额分布:
|
||||
- `0.01~1`: `34,367`
|
||||
- `1~10`: `4,018`
|
||||
- `10~50`: `3,061`
|
||||
- `50~100`: `507`
|
||||
- `>=100`: `272`
|
||||
- 交易订单号格式:`42,223/42,225` 包含下划线(通常为 `orderCode_时间戳`)
|
||||
|
||||
关键结论:
|
||||
|
||||
1. 量大(4.2 万+),必须批处理+可重试+可追踪。
|
||||
2. 文件最晚数据到 `2026-03-06`,执行时必须先查汇付最新状态,不能盲目按文件金额直发。
|
||||
3. 小额单非常多(0.01~1 占比高),要考虑接口速率与批次执行时长。
|
||||
|
||||
## 3. 现有代码落点(可复用)
|
||||
|
||||
- 发起汇付分账请求:
|
||||
- `jsowell-pile/src/main/java/com/jsowell/adapay/service/AdapayService.java`
|
||||
- 方法:`createPaymentConfirmRequest(PaymentConfirmParam param)`
|
||||
- 查询支付确认列表:
|
||||
- 同上 `queryPaymentConfirmList(QueryPaymentConfirmDTO dto)`
|
||||
- 分账对象模型:
|
||||
- `DivMember.memberId` 设为 `0`,`feeFlag` 设为 `Y`
|
||||
- 已有“未分账处理”参考(测试代码):
|
||||
- `jsowell-admin/src/test/java/PaymentTestController.java`
|
||||
- 方法:`processUnSettledOrder()`
|
||||
|
||||
注意:
|
||||
|
||||
- `jsowell-quartz` 里的 `processUnSettledOrder()` 当前版本(V2)主要是“查汇付确认信息”,不是生产可直接执行的补分账任务。
|
||||
|
||||
## 4. 推荐执行方案(生产可落地)
|
||||
|
||||
## 4.1 方案总览
|
||||
|
||||
分三阶段执行:
|
||||
|
||||
1. 任务清单准备
|
||||
2. 批量补分账执行
|
||||
3. 结果复核与失败重试
|
||||
|
||||
建议新增一个“一次性批处理工具(仅本次任务分支)”,不要直接依赖手工接口逐条处理。
|
||||
|
||||
## 4.2 任务清单准备
|
||||
|
||||
从 Excel 抽取以下字段形成任务清单(CSV/临时表均可):
|
||||
|
||||
- `payment_id` ← 交易流水号
|
||||
- `source_order_no` ← 交易订单号
|
||||
- `order_code` ← `source_order_no` 按 `_` 分割取前半段;若无 `_` 则回退原值
|
||||
- `pay_amt` ← 交易订单金额
|
||||
- `file_confirmed_amt` ← 已确认分账金额
|
||||
- `file_reserved_amt` ← 支付确认撤销金额
|
||||
- `file_remaining_amt` ← 剩余未分账金额
|
||||
- `pay_time` ← 支付时间
|
||||
|
||||
过滤条件:
|
||||
|
||||
- `file_remaining_amt > 0`
|
||||
- `payment_id` 非空
|
||||
|
||||
## 4.3 批量补分账执行(核心)
|
||||
|
||||
单条任务处理逻辑(必须幂等):
|
||||
|
||||
1. 用 `payment_id` 调 `queryPaymentConfirmList` 查询汇付最新确认信息。
|
||||
2. 计算“执行时最新可分账金额” `latest_remaining_amt`:
|
||||
- 基线金额使用文件中的 `file_remaining_amt`。
|
||||
- 若汇付返回了确认信息,使用 `pay_amt - confirmed_amt - reserved_amt` 再算一遍,取两者较小值(且不小于 0)。
|
||||
3. 若 `latest_remaining_amt <= 0`:跳过(记为 `SKIPPED_NO_REMAINING`)。
|
||||
4. 构造分账请求:
|
||||
- `paymentId = payment_id`
|
||||
- `confirmAmt = latest_remaining_amt`
|
||||
- `divMemberList = [{ memberId: "0", amount: latest_remaining_amt, feeFlag: "Y" }]`
|
||||
- `orderCode = order_code`
|
||||
- `wechatAppId = 对应 appId`
|
||||
5. 调用 `createPaymentConfirmRequest(param)`。
|
||||
6. 按响应记录结果:
|
||||
- 成功:记录 `payment_confirm_id`、`confirm_amt`、`fee_amt`
|
||||
- 失败:记录 `error_code`、`error_msg`
|
||||
|
||||
建议执行参数:
|
||||
|
||||
- 批次:每批 `500`(或 `200` 起步)
|
||||
- QPS:`3~5`(每笔 sleep `200~300ms`)
|
||||
- 失败重试:对可重试错误最多 `2` 次
|
||||
|
||||
建议错误处理:
|
||||
|
||||
- `confirm_amt_over_limit`:立即重查汇付最新金额,按新金额重试一次。
|
||||
- `payment_over_time_doing` / `refund_repeate_request`:放入重试队列,延迟重跑。
|
||||
- 其他错误:记录失败,人工复核。
|
||||
|
||||
## 4.4 结果复核与验收
|
||||
|
||||
必须输出三类结果文件(CSV):
|
||||
|
||||
- `success.csv`:成功明细(含 paymentConfirmId)
|
||||
- `skipped.csv`:跳过明细(无剩余可分账)
|
||||
- `failed.csv`:失败明细(含错误码)
|
||||
|
||||
验收标准:
|
||||
|
||||
1. `success + skipped + failed = 总任务数`
|
||||
2. 对 `success` 样本抽查 `queryPaymentConfirmList`,确认 `div_members` 包含 `memberId=0`
|
||||
3. `failed` 有明确可重试策略或人工处理结论
|
||||
|
||||
## 4.5 回滚预案(必要时)
|
||||
|
||||
如果误分账到错误对象或金额错误:
|
||||
|
||||
1. 从 `success.csv` 取 `payment_confirm_id`
|
||||
2. 调用 `createConfirmReverse(paymentConfirmId, wechatAppId)` 撤销支付确认
|
||||
3. 撤销后重新按正确参数执行补分账
|
||||
|
||||
代码参考:
|
||||
|
||||
- `AdapayService.createConfirmReverse(...)`
|
||||
|
||||
## 5. 下次同类任务复用清单
|
||||
|
||||
执行前 Checklist:
|
||||
|
||||
1. 明细文件是否包含 `payment_id`、`remaining_amt`、`pay_amt`
|
||||
2. 是否确认目标账户(本 SOP 为 `memberId=0`)
|
||||
3. 是否先做 100 条小批次试跑
|
||||
4. 是否有完整执行日志与 `success/skipped/failed` 三份结果
|
||||
5. 是否准备了 `payment_confirm_id` 级别回滚能力
|
||||
|
||||
执行后 Checklist:
|
||||
|
||||
1. 失败项是否重试完毕
|
||||
2. 关键样本是否验证到汇付侧最新状态
|
||||
3. 本次任务结果文件是否归档到 `doc/` 或运维归档目录
|
||||
|
||||
## 6. 当前已落地入口
|
||||
|
||||
已新增任务方法(`JsowellTask`):
|
||||
|
||||
- `importAdapayUnsplitRecordAndCompleteFields()`
|
||||
- `importAdapayUnsplitRecordAndCompleteFields(String filePath)`
|
||||
|
||||
实现位置:
|
||||
|
||||
- `jsowell-quartz/src/main/java/com/jsowell/quartz/task/JsowellTask.java`
|
||||
|
||||
方法职责:
|
||||
|
||||
1. 从 Excel 导入 `adapay_unsplit_record`(按 `insertOrUpdateSelective`,支持重复执行)
|
||||
2. 自动补齐 `order_code / due_refund_amount / settle_amount / pile_type` 等缺失字段
|
||||
3. 输出导入与补齐统计日志
|
||||
|
||||
执行示例:
|
||||
|
||||
- 使用默认路径:`jsowellTask.importAdapayUnsplitRecordAndCompleteFields()`
|
||||
- 指定路径:`jsowellTask.importAdapayUnsplitRecordAndCompleteFields('doc/万车充小程序-未分账明细.xlsx')`
|
||||
@@ -2,8 +2,12 @@ package com.jsowell.quartz.task;
|
||||
|
||||
import com.alibaba.fastjson2.JSON;
|
||||
import com.google.common.collect.Lists;
|
||||
import com.jsowell.adapay.common.DivMember;
|
||||
import com.jsowell.adapay.common.PaymentConfirmInfo;
|
||||
import com.jsowell.adapay.dto.PaymentConfirmParam;
|
||||
import com.jsowell.adapay.dto.QueryPaymentConfirmDTO;
|
||||
import com.jsowell.adapay.dto.WithdrawDTO;
|
||||
import com.jsowell.adapay.response.PaymentConfirmResponse;
|
||||
import com.jsowell.adapay.response.QueryPaymentConfirmDetailResponse;
|
||||
import com.jsowell.adapay.service.AdapayService;
|
||||
import com.jsowell.common.YouDianUtils;
|
||||
@@ -22,18 +26,24 @@ import com.jsowell.pile.domain.PileStationInfo;
|
||||
import com.jsowell.pile.domain.ykcCommond.PublishPileBillingTemplateCommand;
|
||||
import com.jsowell.pile.domain.ykcCommond.StartChargingCommand;
|
||||
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.thirdparty.amap.service.AMapService;
|
||||
import com.jsowell.thirdparty.common.NotificationDTO;
|
||||
import com.jsowell.thirdparty.common.NotificationService;
|
||||
import org.apache.commons.collections4.CollectionUtils;
|
||||
import org.apache.poi.ss.usermodel.*;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.math.BigDecimal;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.*;
|
||||
@@ -418,6 +428,532 @@ public class JsowellTask {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理adapay_unsplit_record待分账数据,统一分账到memberId=0
|
||||
* 依赖queryList(),请先完成settle_amount/due_refund_amount等字段补齐
|
||||
* jsowellTask.processUnsplitRecordToDefaultMember()
|
||||
*/
|
||||
public void processUnsplitRecordToDefaultMember() {
|
||||
processUnsplitRecordToDefaultMember(Constants.DEFAULT_APP_ID, 500);
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理adapay_unsplit_record待分账数据,统一分账到memberId=0
|
||||
* 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);
|
||||
}
|
||||
|
||||
/**
|
||||
* 从Excel导入adapay_unsplit_record,并补齐缺失字段
|
||||
* 默认文件路径:doc/万车充小程序-未分账明细.xlsx
|
||||
* jsowellTask.importAdapayUnsplitRecordAndCompleteFields()
|
||||
*/
|
||||
public void importAdapayUnsplitRecordAndCompleteFields() {
|
||||
importAdapayUnsplitRecordAndCompleteFields("doc/万车充小程序-未分账明细.xlsx");
|
||||
}
|
||||
|
||||
/**
|
||||
* 从Excel导入adapay_unsplit_record,并补齐缺失字段
|
||||
* jsowellTask.importAdapayUnsplitRecordAndCompleteFields(文件路径)
|
||||
*/
|
||||
public void importAdapayUnsplitRecordAndCompleteFields(String filePath) {
|
||||
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;
|
||||
}
|
||||
|
||||
ImportSummary summary = importAdapayUnsplitRecord(path);
|
||||
|
||||
if (summary.successRows == 0) {
|
||||
log.info("导入未分账数据结束,未写入任何数据,统计:{}", summary);
|
||||
return;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
private ImportSummary importAdapayUnsplitRecord(Path filePath) {
|
||||
ImportSummary summary = new ImportSummary();
|
||||
DataFormatter formatter = new DataFormatter();
|
||||
|
||||
try (InputStream inputStream = Files.newInputStream(filePath);
|
||||
Workbook workbook = WorkbookFactory.create(inputStream)) {
|
||||
Sheet sheet = workbook.getSheetAt(0);
|
||||
if (sheet == null) {
|
||||
log.error("导入未分账数据失败,Excel没有sheet, file:{}", filePath.toAbsolutePath());
|
||||
return summary;
|
||||
}
|
||||
|
||||
Row headerRow = sheet.getRow(sheet.getFirstRowNum());
|
||||
if (headerRow == null) {
|
||||
log.error("导入未分账数据失败,Excel没有表头, file:{}", filePath.toAbsolutePath());
|
||||
return summary;
|
||||
}
|
||||
|
||||
Map<String, Integer> headerIndexMap = buildHeaderIndexMap(headerRow, formatter);
|
||||
List<String> requiredHeaders = Lists.newArrayList(
|
||||
"商户号", "支付时间", "交易流水号", "交易订单号", "交易订单金额", "已确认分账金额", "已撤销金额", "支付确认撤销金额", "剩余未分账金额"
|
||||
);
|
||||
for (String requiredHeader : requiredHeaders) {
|
||||
if (!headerIndexMap.containsKey(normalizeHeader(requiredHeader))) {
|
||||
log.error("导入未分账数据失败,缺少字段:{}, file:{}", requiredHeader, filePath.toAbsolutePath());
|
||||
return summary;
|
||||
}
|
||||
}
|
||||
|
||||
int firstDataRow = sheet.getFirstRowNum() + 1;
|
||||
int lastDataRow = sheet.getLastRowNum();
|
||||
for (int rowNum = firstDataRow; rowNum <= lastDataRow; rowNum++) {
|
||||
Row row = sheet.getRow(rowNum);
|
||||
if (row == null || isRowEmpty(row)) {
|
||||
continue;
|
||||
}
|
||||
summary.totalRows++;
|
||||
|
||||
try {
|
||||
AdapayUnsplitRecord record = convertRowToRecord(row, headerIndexMap, formatter);
|
||||
if (record == null) {
|
||||
summary.skippedRows++;
|
||||
continue;
|
||||
}
|
||||
adapayUnsplitRecordService.insertOrUpdateSelective(record);
|
||||
summary.successRows++;
|
||||
summary.updatePayTimeRange(record.getPayTime());
|
||||
} catch (Exception e) {
|
||||
summary.failedRows++;
|
||||
log.error("导入未分账数据失败, rowNum:{}, file:{}", rowNum + 1, filePath.toAbsolutePath(), e);
|
||||
}
|
||||
|
||||
if (summary.totalRows % 1000 == 0) {
|
||||
log.info("导入未分账数据进行中, total:{}, success:{}, skipped:{}, failed:{}",
|
||||
summary.totalRows, summary.successRows, summary.skippedRows, summary.failedRows);
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("导入未分账数据失败, file:{}", filePath.toAbsolutePath(), e);
|
||||
}
|
||||
|
||||
return summary;
|
||||
}
|
||||
|
||||
private int completeUnsplitRecordMissingFields(String startTime, String endTime, int pageSize) {
|
||||
int pageNum = 1;
|
||||
int updatedCount = 0;
|
||||
|
||||
while (true) {
|
||||
PageUtils.startPage(pageNum, pageSize);
|
||||
List<AdapayUnsplitRecord> list = adapayUnsplitRecordService.queryUnsplitOrders(startTime, endTime);
|
||||
if (CollectionUtils.isEmpty(list)) {
|
||||
break;
|
||||
}
|
||||
|
||||
Set<String> orderCodeSet = new HashSet<>();
|
||||
for (AdapayUnsplitRecord record : list) {
|
||||
if (StringUtils.isBlank(record.getOrderCode())) {
|
||||
String extractedOrderCode = extractOrderCode(record.getOrderNo());
|
||||
if (StringUtils.isNotBlank(extractedOrderCode)) {
|
||||
record.setOrderCode(extractedOrderCode);
|
||||
}
|
||||
}
|
||||
if (StringUtils.isNotBlank(record.getOrderCode())) {
|
||||
orderCodeSet.add(record.getOrderCode());
|
||||
}
|
||||
}
|
||||
|
||||
Map<String, OrderBasicInfo> orderMap = new HashMap<>();
|
||||
if (CollectionUtils.isNotEmpty(orderCodeSet)) {
|
||||
List<OrderBasicInfo> orderList = orderBasicInfoService.selectOrderTemp(orderCodeSet);
|
||||
orderMap = orderList.stream()
|
||||
.collect(Collectors.toMap(OrderBasicInfo::getOrderCode, v -> v, (k1, k2) -> k1));
|
||||
}
|
||||
|
||||
List<AdapayUnsplitRecord> updateList = new ArrayList<>();
|
||||
Date now = DateUtils.getNowDate();
|
||||
for (AdapayUnsplitRecord record : list) {
|
||||
boolean needUpdate = false;
|
||||
|
||||
String orderCode = record.getOrderCode();
|
||||
if (StringUtils.isBlank(orderCode)) {
|
||||
orderCode = extractOrderCode(record.getOrderNo());
|
||||
if (StringUtils.isNotBlank(orderCode)) {
|
||||
record.setOrderCode(orderCode);
|
||||
needUpdate = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (StringUtils.isNotBlank(orderCode)) {
|
||||
OrderBasicInfo orderBasicInfo = orderMap.get(orderCode);
|
||||
if (orderBasicInfo != null) {
|
||||
BigDecimal refundAmount = orderBasicInfo.getRefundAmount();
|
||||
if (!isSameAmount(record.getDueRefundAmount(), refundAmount)) {
|
||||
record.setDueRefundAmount(refundAmount);
|
||||
needUpdate = true;
|
||||
}
|
||||
BigDecimal settleAmount = orderBasicInfo.getSettleAmount();
|
||||
if (!isSameAmount(record.getSettleAmount(), settleAmount)) {
|
||||
record.setSettleAmount(settleAmount);
|
||||
needUpdate = true;
|
||||
}
|
||||
String pileType = YouDianUtils.isEBikePileSn(orderBasicInfo.getPileSn()) ? "eBike" : "EV";
|
||||
if (!StringUtils.equals(record.getPileType(), pileType)) {
|
||||
record.setPileType(pileType);
|
||||
needUpdate = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (needUpdate) {
|
||||
record.setUpdateTime(now);
|
||||
updateList.add(record);
|
||||
}
|
||||
}
|
||||
|
||||
if (CollectionUtils.isNotEmpty(updateList)) {
|
||||
adapayUnsplitRecordService.updateBatchSelective(updateList);
|
||||
updatedCount += updateList.size();
|
||||
}
|
||||
|
||||
if (list.size() < pageSize) {
|
||||
break;
|
||||
}
|
||||
pageNum++;
|
||||
}
|
||||
|
||||
return updatedCount;
|
||||
}
|
||||
|
||||
private BigDecimal getLatestConfirmAmount(BigDecimal waitSplitAmount, String payAmount, 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);
|
||||
if (latestRemaining.compareTo(BigDecimal.ZERO) > 0) {
|
||||
confirmAmt = waitSplitAmount.min(latestRemaining);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.warn("查询汇付确认金额异常,使用数据库待分账金额继续处理, paymentId:{}, waitSplitAmount:{}",
|
||||
paymentId, waitSplitAmount, e);
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
BigDecimal maxConfirmedAmt = BigDecimal.ZERO;
|
||||
BigDecimal maxReservedAmt = BigDecimal.ZERO;
|
||||
List<PaymentConfirmInfo> confirms = response.getPaymentConfirms();
|
||||
for (PaymentConfirmInfo confirm : confirms) {
|
||||
BigDecimal confirmedAmt = parseAmount(confirm.getConfirmedAmt());
|
||||
BigDecimal reservedAmt = parseAmount(confirm.getReservedAmt());
|
||||
if (confirmedAmt.compareTo(maxConfirmedAmt) > 0) {
|
||||
maxConfirmedAmt = confirmedAmt;
|
||||
}
|
||||
if (reservedAmt.compareTo(maxReservedAmt) > 0) {
|
||||
maxReservedAmt = reservedAmt;
|
||||
}
|
||||
}
|
||||
BigDecimal latestRemaining = payAmt.subtract(maxConfirmedAmt).subtract(maxReservedAmt);
|
||||
return latestRemaining.compareTo(BigDecimal.ZERO) > 0 ? latestRemaining : BigDecimal.ZERO;
|
||||
}
|
||||
|
||||
private void updateConfirmedSplitAmount(AdapayUnsplitRecordVO item, BigDecimal confirmAmt, String paymentId) {
|
||||
BigDecimal oldConfirmedAmt = parseAmount(item.getConfirmedSplitAmount());
|
||||
BigDecimal newConfirmedAmt = oldConfirmedAmt.add(confirmAmt).setScale(2, BigDecimal.ROUND_HALF_UP);
|
||||
AdapayUnsplitRecord updateRecord = new AdapayUnsplitRecord();
|
||||
updateRecord.setPaymentId(paymentId);
|
||||
updateRecord.setConfirmedSplitAmount(newConfirmedAmt);
|
||||
updateRecord.setUpdateTime(DateUtils.getNowDate());
|
||||
adapayUnsplitRecordService.insertOrUpdateSelective(updateRecord);
|
||||
}
|
||||
|
||||
private void markSplitResult(String paymentId, String splitFlag) {
|
||||
AdapayUnsplitRecord updateRecord = new AdapayUnsplitRecord();
|
||||
updateRecord.setPaymentId(paymentId);
|
||||
updateRecord.setSplitFlag(splitFlag);
|
||||
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)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
String orderNo = getCellString(row, headerIndexMap.get(normalizeHeader("交易订单号")), formatter);
|
||||
Date payTime = parsePayTime(getCell(row, headerIndexMap.get(normalizeHeader("支付时间"))), formatter);
|
||||
|
||||
AdapayUnsplitRecord record = new AdapayUnsplitRecord();
|
||||
record.setMerchantCode(getCellString(row, headerIndexMap.get(normalizeHeader("商户号")), formatter));
|
||||
record.setPayTime(payTime);
|
||||
record.setPaymentId(paymentId);
|
||||
record.setOrderNo(orderNo);
|
||||
record.setOrderCode(extractOrderCode(orderNo));
|
||||
record.setPayAmount(getCellDecimal(row, headerIndexMap.get(normalizeHeader("交易订单金额")), formatter));
|
||||
record.setConfirmedSplitAmount(getCellDecimal(row, headerIndexMap.get(normalizeHeader("已确认分账金额")), formatter));
|
||||
record.setRefundAmount(getCellDecimal(row, headerIndexMap.get(normalizeHeader("已撤销金额")), formatter));
|
||||
record.setPaymentRevokeAmount(getCellDecimal(row, headerIndexMap.get(normalizeHeader("支付确认撤销金额")), formatter));
|
||||
record.setRemainingSplitAmount(getCellDecimal(row, headerIndexMap.get(normalizeHeader("剩余未分账金额")), formatter));
|
||||
record.setUpdateTime(DateUtils.getNowDate());
|
||||
return record;
|
||||
}
|
||||
|
||||
private Map<String, Integer> buildHeaderIndexMap(Row headerRow, DataFormatter formatter) {
|
||||
Map<String, Integer> headerIndexMap = new HashMap<>();
|
||||
short firstCellNum = headerRow.getFirstCellNum();
|
||||
short lastCellNum = headerRow.getLastCellNum();
|
||||
if (firstCellNum < 0 || lastCellNum < 0) {
|
||||
return headerIndexMap;
|
||||
}
|
||||
for (int cellIndex = firstCellNum; cellIndex < lastCellNum; cellIndex++) {
|
||||
Cell cell = headerRow.getCell(cellIndex, Row.MissingCellPolicy.RETURN_BLANK_AS_NULL);
|
||||
if (cell == null) {
|
||||
continue;
|
||||
}
|
||||
String header = normalizeHeader(formatter.formatCellValue(cell));
|
||||
if (StringUtils.isNotBlank(header)) {
|
||||
headerIndexMap.put(header, cellIndex);
|
||||
}
|
||||
}
|
||||
return headerIndexMap;
|
||||
}
|
||||
|
||||
private String getCellString(Row row, Integer columnIndex, DataFormatter formatter) {
|
||||
Cell cell = getCell(row, columnIndex);
|
||||
if (cell == null) {
|
||||
return null;
|
||||
}
|
||||
String value = formatter.formatCellValue(cell);
|
||||
return StringUtils.isBlank(value) ? null : value.trim();
|
||||
}
|
||||
|
||||
private Cell getCell(Row row, Integer columnIndex) {
|
||||
if (row == null || columnIndex == null) {
|
||||
return null;
|
||||
}
|
||||
return row.getCell(columnIndex, Row.MissingCellPolicy.RETURN_BLANK_AS_NULL);
|
||||
}
|
||||
|
||||
private BigDecimal getCellDecimal(Row row, Integer columnIndex, DataFormatter formatter) {
|
||||
String value = getCellString(row, columnIndex, formatter);
|
||||
return parseAmount(value);
|
||||
}
|
||||
|
||||
private Date parsePayTime(Cell cell, DataFormatter formatter) {
|
||||
if (cell == null) {
|
||||
return null;
|
||||
}
|
||||
if (cell.getCellType() == CellType.NUMERIC) {
|
||||
return DateUtil.getJavaDate(cell.getNumericCellValue());
|
||||
}
|
||||
if (cell.getCellType() == CellType.FORMULA && cell.getCachedFormulaResultType() == CellType.NUMERIC) {
|
||||
return DateUtil.getJavaDate(cell.getNumericCellValue());
|
||||
}
|
||||
|
||||
String value = formatter.formatCellValue(cell);
|
||||
if (StringUtils.isBlank(value)) {
|
||||
return null;
|
||||
}
|
||||
Date date = DateUtils.parseDate(value.trim());
|
||||
if (date != null) {
|
||||
return date;
|
||||
}
|
||||
try {
|
||||
return DateUtil.getJavaDate(Double.parseDouble(value.trim()));
|
||||
} catch (Exception e) {
|
||||
log.warn("解析支付时间失败,value:{}", value);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private String extractOrderCode(String orderNo) {
|
||||
if (StringUtils.isBlank(orderNo)) {
|
||||
return null;
|
||||
}
|
||||
int index = orderNo.indexOf("_");
|
||||
return index > 0 ? orderNo.substring(0, index) : orderNo;
|
||||
}
|
||||
|
||||
private String normalizeHeader(String header) {
|
||||
return header == null ? "" : header.replace(" ", "").trim();
|
||||
}
|
||||
|
||||
private boolean isSameAmount(BigDecimal left, BigDecimal right) {
|
||||
if (left == null && right == null) {
|
||||
return true;
|
||||
}
|
||||
if (left == null || right == null) {
|
||||
return false;
|
||||
}
|
||||
return left.compareTo(right) == 0;
|
||||
}
|
||||
|
||||
private BigDecimal parseAmount(String value) {
|
||||
if (StringUtils.isBlank(value)) {
|
||||
return BigDecimal.ZERO;
|
||||
}
|
||||
String normalizedValue = value.replace(",", "").trim();
|
||||
try {
|
||||
return new BigDecimal(normalizedValue);
|
||||
} catch (NumberFormatException e) {
|
||||
log.warn("解析数字失败,value:{}", value);
|
||||
return BigDecimal.ZERO;
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isRowEmpty(Row row) {
|
||||
if (row == null) {
|
||||
return true;
|
||||
}
|
||||
short firstCellNum = row.getFirstCellNum();
|
||||
short lastCellNum = row.getLastCellNum();
|
||||
if (firstCellNum < 0 || lastCellNum < 0) {
|
||||
return true;
|
||||
}
|
||||
for (int i = firstCellNum; i < lastCellNum; i++) {
|
||||
Cell cell = row.getCell(i, Row.MissingCellPolicy.RETURN_BLANK_AS_NULL);
|
||||
if (cell == null) {
|
||||
continue;
|
||||
}
|
||||
if (cell.getCellType() != CellType.BLANK) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private static class ImportSummary {
|
||||
private int totalRows;
|
||||
private int successRows;
|
||||
private int skippedRows;
|
||||
private int failedRows;
|
||||
private Date minPayTime;
|
||||
private Date maxPayTime;
|
||||
|
||||
private void updatePayTimeRange(Date payTime) {
|
||||
if (payTime == null) {
|
||||
return;
|
||||
}
|
||||
if (minPayTime == null || payTime.before(minPayTime)) {
|
||||
minPayTime = payTime;
|
||||
}
|
||||
if (maxPayTime == null || payTime.after(maxPayTime)) {
|
||||
maxPayTime = payTime;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "{"
|
||||
+ "\"totalRows\":" + totalRows
|
||||
+ ", \"successRows\":" + successRows
|
||||
+ ", \"skippedRows\":" + skippedRows
|
||||
+ ", \"failedRows\":" + failedRows
|
||||
+ ", \"minPayTime\":\"" + (minPayTime == null ? "" : DateUtils.formatDateTime(minPayTime)) + "\""
|
||||
+ ", \"maxPayTime\":\"" + (maxPayTime == null ? "" : DateUtils.formatDateTime(maxPayTime)) + "\""
|
||||
+ "}";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* V1方法,获取退款金额与结算金额
|
||||
* @param batchNum
|
||||
|
||||
Reference in New Issue
Block a user