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:
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user