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:
@@ -31,6 +31,8 @@ public interface AdapayUnsplitRecordMapper {
|
||||
|
||||
int batchInsert(@Param("list") List<AdapayUnsplitRecord> list);
|
||||
|
||||
int batchInsertOrUpdateSelective(@Param("list") List<AdapayUnsplitRecord> list);
|
||||
|
||||
List<AdapayUnsplitRecord> queryUnsplitOrders(@Param("startTime") String startTime, @Param("endTime") String endTime);
|
||||
|
||||
List<AdapayUnsplitRecordVO> queryList();
|
||||
|
||||
@@ -28,6 +28,8 @@ public interface AdapayUnsplitRecordService{
|
||||
|
||||
int batchInsert(List<AdapayUnsplitRecord> list);
|
||||
|
||||
int batchInsertOrUpdateSelective(List<AdapayUnsplitRecord> list);
|
||||
|
||||
List<AdapayUnsplitRecord> queryUnsplitOrders(String startTime, String endTime);
|
||||
|
||||
List<AdapayUnsplitRecordVO> queryList();
|
||||
|
||||
@@ -69,6 +69,11 @@ public class AdapayUnsplitRecordServiceImpl implements AdapayUnsplitRecordServic
|
||||
return adapayUnsplitRecordMapper.batchInsert(list);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int batchInsertOrUpdateSelective(List<AdapayUnsplitRecord> list) {
|
||||
return adapayUnsplitRecordMapper.batchInsertOrUpdateSelective(list);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<AdapayUnsplitRecord> queryUnsplitOrders(String startTime, String endTime) {
|
||||
return adapayUnsplitRecordMapper.queryUnsplitOrders(startTime, endTime);
|
||||
|
||||
@@ -447,6 +447,32 @@
|
||||
#{item.id,jdbcType=INTEGER}
|
||||
</foreach>
|
||||
</update>
|
||||
<insert id="batchInsertOrUpdateSelective" keyColumn="id" keyProperty="id" parameterType="map" useGeneratedKeys="true">
|
||||
insert into adapay_unsplit_record
|
||||
(merchant_code, pay_time, payment_id, order_no, pay_amount, confirmed_split_amount,
|
||||
refund_amount, payment_revoke_amount, remaining_split_amount, order_code, pile_type,
|
||||
due_refund_amount, settle_amount, refund_flag, split_flag, update_time)
|
||||
values
|
||||
<foreach collection="list" item="item" separator=",">
|
||||
(#{item.merchantCode,jdbcType=VARCHAR}, #{item.payTime,jdbcType=TIMESTAMP}, #{item.paymentId,jdbcType=VARCHAR},
|
||||
#{item.orderNo,jdbcType=VARCHAR}, #{item.payAmount,jdbcType=DECIMAL}, #{item.confirmedSplitAmount,jdbcType=DECIMAL},
|
||||
#{item.refundAmount,jdbcType=DECIMAL}, #{item.paymentRevokeAmount,jdbcType=DECIMAL},
|
||||
#{item.remainingSplitAmount,jdbcType=DECIMAL}, #{item.orderCode,jdbcType=VARCHAR},
|
||||
#{item.pileType,jdbcType=VARCHAR}, #{item.dueRefundAmount,jdbcType=DECIMAL}, #{item.settleAmount,jdbcType=DECIMAL},
|
||||
#{item.refundFlag,jdbcType=VARCHAR}, #{item.splitFlag,jdbcType=VARCHAR}, #{item.updateTime,jdbcType=TIMESTAMP})
|
||||
</foreach>
|
||||
on duplicate key update
|
||||
merchant_code = values(merchant_code),
|
||||
pay_time = values(pay_time),
|
||||
order_no = values(order_no),
|
||||
pay_amount = values(pay_amount),
|
||||
confirmed_split_amount = values(confirmed_split_amount),
|
||||
refund_amount = values(refund_amount),
|
||||
payment_revoke_amount = values(payment_revoke_amount),
|
||||
remaining_split_amount = values(remaining_split_amount),
|
||||
order_code = values(order_code),
|
||||
update_time = values(update_time)
|
||||
</insert>
|
||||
<insert id="batchInsert" keyColumn="id" keyProperty="id" parameterType="map" useGeneratedKeys="true">
|
||||
<!--@mbg.generated-->
|
||||
insert into adapay_unsplit_record
|
||||
|
||||
@@ -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