update 批量插入数据库

This commit is contained in:
Guoqs
2026-03-12 15:23:32 +08:00
parent 719afc567d
commit 6cc444a4d5
5 changed files with 65 additions and 2 deletions

View File

@@ -528,15 +528,25 @@ public class JsowellTask {
* 默认文件路径doc/万车充小程序-未分账明细.xlsx
* jsowellTask.importAdapayUnsplitRecordAndCompleteFields()
*/
/**
* 从默认路径导入未分账明细并补齐缺失字段(无参入口)
* 默认读取 doc/万车充小程序-未分账明细.xlsx相对于工作目录
* jsowellTask.importAdapayUnsplitRecordAndCompleteFields()
*/
public void importAdapayUnsplitRecordAndCompleteFields() {
importAdapayUnsplitRecordAndCompleteFields("doc/万车充小程序-未分账明细.xlsx");
}
/**
* 从Excel导入adapay_unsplit_record并补齐缺失字段
* 流程:
* 1. 校验文件路径(相对路径自动拼接工作目录转为绝对路径)
* 2. 解析Excel逐行转换为 AdapayUnsplitRecord调用 insertOrUpdateSelective 写入数据库
* 3. 以导入数据的支付时间范围为条件,分页查询已入库记录,补齐 orderCode/退款金额/结算金额/桩类型等缺失字段
* jsowellTask.importAdapayUnsplitRecordAndCompleteFields(文件路径)
*/
public void importAdapayUnsplitRecordAndCompleteFields(String filePath) {
// 相对路径转绝对路径(基于 JVM 工作目录)
Path path = Paths.get(filePath);
if (!path.isAbsolute()) {
path = Paths.get(System.getProperty("user.dir"), filePath);
@@ -546,6 +556,7 @@ public class JsowellTask {
return;
}
// 第一步读取Excel将每行数据 insertOrUpdate 到 adapay_unsplit_record 表
ImportSummary summary = importAdapayUnsplitRecord(path);
if (summary.successRows == 0) {
@@ -553,6 +564,8 @@ public class JsowellTask {
return;
}
// 第二步:以导入数据中最早/最晚支付时间为范围,补齐已入库记录的缺失字段
// 若 Excel 中无有效支付时间则使用兜底时间范围2024-01-01 至当前时间)
String startTime = summary.minPayTime == null
? "2024-01-01 00:00:00"
: DateUtils.formatDateTime(summary.minPayTime);
@@ -564,6 +577,8 @@ public class JsowellTask {
log.info("导入并补齐未分账数据完成, 导入统计:{}, 补齐更新:{}条", summary, updatedCount);
}
private static final int IMPORT_BATCH_SIZE = 500;
private ImportSummary importAdapayUnsplitRecord(Path filePath) {
ImportSummary summary = new ImportSummary();
DataFormatter formatter = new DataFormatter();
@@ -595,6 +610,8 @@ public class JsowellTask {
int firstDataRow = sheet.getFirstRowNum() + 1;
int lastDataRow = sheet.getLastRowNum();
// 批量收集记录,每 IMPORT_BATCH_SIZE 条执行一次批量 upsert减少数据库交互次数
List<AdapayUnsplitRecord> batch = new ArrayList<>(IMPORT_BATCH_SIZE);
for (int rowNum = firstDataRow; rowNum <= lastDataRow; rowNum++) {
Row row = sheet.getRow(rowNum);
if (row == null || isRowEmpty(row)) {
@@ -608,9 +625,15 @@ public class JsowellTask {
summary.skippedRows++;
continue;
}
adapayUnsplitRecordService.insertOrUpdateSelective(record);
summary.successRows++;
batch.add(record);
summary.updatePayTimeRange(record.getPayTime());
// 达到批量大小时执行一次批量写入
if (batch.size() >= IMPORT_BATCH_SIZE) {
adapayUnsplitRecordService.batchInsertOrUpdateSelective(batch);
summary.successRows += batch.size();
batch.clear();
}
} catch (Exception e) {
summary.failedRows++;
log.error("导入未分账数据失败, rowNum:{}, file:{}", rowNum + 1, filePath.toAbsolutePath(), e);
@@ -621,6 +644,11 @@ public class JsowellTask {
summary.totalRows, summary.successRows, summary.skippedRows, summary.failedRows);
}
}
// 处理剩余不足一批的记录
if (!batch.isEmpty()) {
adapayUnsplitRecordService.batchInsertOrUpdateSelective(batch);
summary.successRows += batch.size();
}
} catch (Exception e) {
log.error("导入未分账数据失败, file:{}", filePath.toAbsolutePath(), e);
}