feat(xispeak): 新增Excel校验接口,只校验不保存到数据库
- JeecgController 新增 checkExcelByEasyExcel 基类方法,batchHandler 传 null 跳过入库 - ExcelImportListener 支持 batchHandler 为 null 时不攒 batch 内存 - BgXiSpeakController 新增 /checkExcelByEasyExcel 接口 Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
+34
@@ -237,6 +237,40 @@ public class JeecgController<T, S extends IService<T>> {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过EasyExcel校验数据,只返回校验结果,不保存到数据库
|
||||
*/
|
||||
protected Result<?> checkExcelByEasyExcel(HttpServletRequest request, Class<T> clazz) {
|
||||
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
|
||||
Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
|
||||
MultipartFile file = fileMap.values().stream().findFirst().orElse(null);
|
||||
if (file == null) {
|
||||
return Result.error("未找到上传文件!");
|
||||
}
|
||||
try (InputStream inputStream = file.getInputStream()) {
|
||||
long start = System.currentTimeMillis();
|
||||
List<ExcelAnnotationUtils.ValidationError> errors = new ArrayList<>();
|
||||
int total = ExcelAnnotationUtils.read(
|
||||
inputStream,
|
||||
clazz,
|
||||
null,
|
||||
BATCH_NUM,
|
||||
errors::addAll,
|
||||
dictQuery());
|
||||
log.info("【EasyExcel校验】校验完成, 数据行数: {}, 耗时: {}毫秒", total, System.currentTimeMillis() - start);
|
||||
if (!errors.isEmpty()) {
|
||||
for (ExcelAnnotationUtils.ValidationError err : errors) {
|
||||
log.warn("【EasyExcel校验】校验失败, 行号: {}, 错误: {}", err.row(), err.messages());
|
||||
}
|
||||
return Result.error("文件校验不通过: 存在" + errors.size() + "行数据校验不通过,请修改后重新导入", errors);
|
||||
}
|
||||
return Result.ok("文件校验通过!数据行数:" + total);
|
||||
} catch (IOException e) {
|
||||
log.error("【EasyExcel校验】文件读取失败", e);
|
||||
return Result.error("文件校验失败:" + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过excel导入数据
|
||||
*
|
||||
|
||||
+6
-4
@@ -47,7 +47,7 @@ public class ExcelImportListener<T> implements ReadListener<Map<Integer, String>
|
||||
this.displayToCode = displayToCode;
|
||||
this.batchHandler = batchHandler;
|
||||
this.errorHandler = errorHandler;
|
||||
this.batch = new ArrayList<>(batchSize);
|
||||
this.batch = batchHandler != null ? new ArrayList<>(batchSize) : null;
|
||||
this.errorBatch = new ArrayList<>(batchSize);
|
||||
}
|
||||
|
||||
@@ -133,13 +133,15 @@ public class ExcelImportListener<T> implements ReadListener<Map<Integer, String>
|
||||
return;
|
||||
}
|
||||
|
||||
// 4. 成功加入批次
|
||||
batch.add(obj);
|
||||
// 4. 成功计数
|
||||
successCount[0]++;
|
||||
if (batchHandler != null) {
|
||||
batch.add(obj);
|
||||
if (batch.size() >= batchSize) {
|
||||
batchHandler.accept(new ArrayList<>(batch));
|
||||
batch.clear();
|
||||
}
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
skipCount[0]++;
|
||||
@@ -158,7 +160,7 @@ public class ExcelImportListener<T> implements ReadListener<Map<Integer, String>
|
||||
|
||||
@Override
|
||||
public void doAfterAllAnalysed(AnalysisContext context) {
|
||||
if (!batch.isEmpty()) {
|
||||
if (batchHandler != null && batch != null && !batch.isEmpty()) {
|
||||
batchHandler.accept(new ArrayList<>(batch));
|
||||
batch.clear();
|
||||
}
|
||||
|
||||
+8
@@ -382,6 +382,14 @@ public class BgXiSpeakController extends JeecgController<BgXiSpeak, IBgXiSpeakSe
|
||||
return super.importExcelByEasyExcel(request, BgXiSpeak.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过EasyExcel校验数据,只返回校验结果,不保存到数据库
|
||||
*/
|
||||
@RequiresPermissions("bg.xispeak:bg_xi_speak:importExcel")
|
||||
@RequestMapping(value = "/checkExcelByEasyExcel", method = RequestMethod.POST)
|
||||
public Result<?> checkExcelByEasyExcel(HttpServletRequest request) {
|
||||
return super.checkExcelByEasyExcel(request, BgXiSpeak.class);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user