diff --git a/diboot-file-starter/src/main/java/com/diboot/file/controller/BaseExcelFileController.java b/diboot-file-starter/src/main/java/com/diboot/file/controller/BaseExcelFileController.java index 9f485e6..c905d78 100644 --- a/diboot-file-starter/src/main/java/com/diboot/file/controller/BaseExcelFileController.java +++ b/diboot-file-starter/src/main/java/com/diboot/file/controller/BaseExcelFileController.java @@ -1,6 +1,5 @@ package com.diboot.file.controller; -import com.diboot.file.excel.listener.DynamicHeadExcelListener; import com.diboot.file.excel.listener.FixedHeadExcelListener; import com.diboot.file.util.ExcelHelper; import com.diboot.file.util.FileHelper; @@ -61,9 +60,8 @@ public abstract class BaseExcelFileController extends BaseFileController { String fileUid = S.substringBefore(previewFileName, "."); String fullPath = FileHelper.getFullPath(previewFileName); String ext = FileHelper.getFileExtByName(originFileName); - String description = getString(request, "description"); // 保存文件上传记录 - createUploadFile(entityClass, fileUid, originFileName, fullPath, ext, description); + createUploadFile(entityClass, fileUid, originFileName, fullPath, ext, request); return new JsonResult(Status.OK); } @@ -94,13 +92,10 @@ public abstract class BaseExcelFileController extends BaseFileController { String newFileName = S.newUuid() + "." + ext; String fullPath = FileHelper.saveFile(file, newFileName); // 预览 - DynamicHeadExcelListener listener = new DynamicHeadExcelListener() { - @Override - protected void saveData(Map headMap, List> dataList) { - } - }; + FixedHeadExcelListener listener = getExcelDataListener(); + listener.setRequestParams(super.getParamsMap(request)); try { - ExcelHelper.readDynamicHeadExcel(fullPath, listener); + ExcelHelper.previewReadExcel(fullPath, listener); } catch (Exception e) { log.warn("解析并校验excel文件失败", e); @@ -125,11 +120,13 @@ public abstract class BaseExcelFileController extends BaseFileController { * 保存文件之后的处理逻辑,如解析excel */ @Override - protected int extractDataCount(String fileUuid, String fullPath) throws Exception{ + protected int extractDataCount(String fileUuid, String fullPath, HttpServletRequest request) throws Exception{ FixedHeadExcelListener listener = getExcelDataListener(); listener.setUploadFileUuid(fileUuid); + listener.setPreview(false); + listener.setRequestParams(super.getParamsMap(request)); try{ - ExcelHelper.readAndSave(fullPath, listener); + ExcelHelper.readAndSaveExcel(fullPath, listener); } catch(Exception e){ log.warn("上传数据错误: "+ e.getMessage(), e); diff --git a/diboot-file-starter/src/main/java/com/diboot/file/controller/BaseFileController.java b/diboot-file-starter/src/main/java/com/diboot/file/controller/BaseFileController.java index 1758422..968f7e2 100644 --- a/diboot-file-starter/src/main/java/com/diboot/file/controller/BaseFileController.java +++ b/diboot-file-starter/src/main/java/com/diboot/file/controller/BaseFileController.java @@ -68,9 +68,8 @@ public abstract class BaseFileController extends BaseController { String fileUid = S.newUuid(); String newFileName = fileUid + "." + ext; String fullPath = FileHelper.saveFile(file, newFileName); - String description = getString(request, "description"); // 保存文件上传记录 - createUploadFile(entityClass, fileUid, originFileName, fullPath, ext, description); + createUploadFile(entityClass, fileUid, originFileName, fullPath, ext, request); Map dataMap = new HashMap<>(); dataMap.put("uuid", fileUid); return new JsonResult(Status.OK).data(dataMap); @@ -83,17 +82,18 @@ public abstract class BaseFileController extends BaseController { * @param originFileName * @param storagePath * @param fileType - * @param description + * @param request * @param * @throws Exception */ - protected void createUploadFile(Class entityClass, String fileUid, String originFileName, String storagePath, String fileType, String description) throws Exception{ + protected void createUploadFile(Class entityClass, String fileUid, String originFileName, String storagePath, String fileType, HttpServletRequest request) throws Exception{ // 保存文件之后的处理逻辑 - int dataCount = extractDataCount(fileUid, storagePath); + int dataCount = extractDataCount(fileUid, storagePath, request); UploadFile uploadFile = new UploadFile(); uploadFile.setUuid(fileUid).setFileName(originFileName).setFileType(fileType); uploadFile.setRelObjType(entityClass.getSimpleName()).setStoragePath(storagePath); // 初始设置为0,批量保存数据后更新 + String description = getString(request, "description"); uploadFile.setDataCount(dataCount).setDescription(description); // 保存文件上传记录 uploadFileService.createEntity(uploadFile); @@ -102,7 +102,7 @@ public abstract class BaseFileController extends BaseController { /** * 保存文件之后的处理逻辑,如解析excel */ - protected int extractDataCount(String fileUuid, String fullPath) throws Exception{ + protected int extractDataCount(String fileUuid, String fullPath, HttpServletRequest request) throws Exception{ return 0; } diff --git a/diboot-file-starter/src/main/java/com/diboot/file/excel/listener/FixedHeadExcelListener.java b/diboot-file-starter/src/main/java/com/diboot/file/excel/listener/FixedHeadExcelListener.java index 65332de..b322af9 100644 --- a/diboot-file-starter/src/main/java/com/diboot/file/excel/listener/FixedHeadExcelListener.java +++ b/diboot-file-starter/src/main/java/com/diboot/file/excel/listener/FixedHeadExcelListener.java @@ -12,6 +12,7 @@ import com.diboot.core.vo.Status; import com.diboot.file.excel.cache.DictTempCache; import lombok.extern.slf4j.Slf4j; +import javax.servlet.http.HttpServletRequest; import java.util.*; /*** @@ -29,10 +30,21 @@ public abstract class FixedHeadExcelListener extends A private List validateErrorMsgs = new ArrayList<>(); // 导入文件的uuid protected String uploadFileUuid; + // 是否为预览模式 + private boolean preview = false; + // 注入request + private Map requestParams; public FixedHeadExcelListener(){ } + public void setPreview(boolean isPrevieew){ + this.preview = isPrevieew; + } + public void setRequestParams(Map requestParams){ + this.requestParams = requestParams; + } + public void setUploadFileUuid(String uploadFileUuid){ this.uploadFileUuid = uploadFileUuid; } @@ -70,9 +82,9 @@ public abstract class FixedHeadExcelListener extends A throw new BusinessException(Status.FAIL_VALIDATION, S.join(this.validateErrorMsgs, "; ")); } // 保存 - if(V.notEmpty(dataList)){ + if(preview == false && V.notEmpty(dataList)){ // 保存数据 - saveData(dataList); + saveData(dataList, requestParams); } } @@ -137,7 +149,7 @@ public abstract class FixedHeadExcelListener extends A /** * 保存数据 */ - protected abstract void saveData(List dataList); + protected abstract void saveData(List dataList, Map requestParams); /** * 校验错误信息 diff --git a/diboot-file-starter/src/main/java/com/diboot/file/util/ExcelHelper.java b/diboot-file-starter/src/main/java/com/diboot/file/util/ExcelHelper.java index cd13c93..62650c7 100644 --- a/diboot-file-starter/src/main/java/com/diboot/file/util/ExcelHelper.java +++ b/diboot-file-starter/src/main/java/com/diboot/file/util/ExcelHelper.java @@ -24,12 +24,23 @@ import java.util.List; public class ExcelHelper { /** - * 简单读取excel文件数据并保存到数据库 + * 预览读取excel文件数据 * @param filePath * @param listener * @return */ - public static boolean readAndSave(String filePath, FixedHeadExcelListener listener) throws Exception{ + public static boolean previewReadExcel(String filePath, FixedHeadExcelListener listener) throws Exception{ + listener.setPreview(true); + return readAndSaveExcel(filePath, listener); + } + + /** + * 读取excel文件数据并保存到数据库 + * @param filePath + * @param listener + * @return + */ + public static boolean readAndSaveExcel(String filePath, FixedHeadExcelListener listener) throws Exception{ File excel = getExcelFile(filePath); Class headClazz = BeanUtils.getGenericityClass(listener, 0); EasyExcel.read(excel).registerReadListener(listener).head(headClazz).sheet().doRead(); diff --git a/diboot-file-starter/src/test/java/com/diboot/file/example/custom/listener/DepartmentImportListener.java b/diboot-file-starter/src/test/java/com/diboot/file/example/custom/listener/DepartmentImportListener.java index adf7ff0..ef1f213 100644 --- a/diboot-file-starter/src/test/java/com/diboot/file/example/custom/listener/DepartmentImportListener.java +++ b/diboot-file-starter/src/test/java/com/diboot/file/example/custom/listener/DepartmentImportListener.java @@ -10,6 +10,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.util.List; +import java.util.Map; /** * @@ -39,7 +40,7 @@ public class DepartmentImportListener extends FixedHeadExcelListener dataList) { + protected void saveData(List dataList, Map paramsMap) { // 转换数据 List departmentList = BeanUtils.convertList(getDataList(), Department.class); // 保存数据 diff --git a/diboot-file-starter/src/test/java/com/diboot/file/example/test/FixedHeadExcelReadTest.java b/diboot-file-starter/src/test/java/com/diboot/file/example/test/FixedHeadExcelReadTest.java index e4b0e89..e4ae932 100644 --- a/diboot-file-starter/src/test/java/com/diboot/file/example/test/FixedHeadExcelReadTest.java +++ b/diboot-file-starter/src/test/java/com/diboot/file/example/test/FixedHeadExcelReadTest.java @@ -33,7 +33,7 @@ public class FixedHeadExcelReadTest extends ExcelWriteTest { prepareNormalDataExcel(); // 读且保存 DepartmentImportListener listener = new DepartmentImportListener(); - boolean success = ExcelHelper.readAndSave(getTempFilePath(), listener); + boolean success = ExcelHelper.previewReadExcel(getTempFilePath(), listener); Assert.assertTrue(success); System.out.println(JSON.stringify(listener.getDataList())); } @@ -54,7 +54,7 @@ public class FixedHeadExcelReadTest extends ExcelWriteTest { // 预览读 //ExcelHelper.previewRead(getTempFilePath(), new DepartmentImportListener()); // 读且保存 - ExcelHelper.readAndSave(getTempFilePath(), new DepartmentImportListener()); + ExcelHelper.previewReadExcel(getTempFilePath(), new DepartmentImportListener()); } catch (Exception e){ e.printStackTrace();