From e1c7d34edc7bd57c1ff55b8870184712c1ba411f Mon Sep 17 00:00:00 2001 From: wangliwen Date: Fri, 17 Jun 2022 14:18:25 +0800 Subject: [PATCH] =?UTF-8?q?=E8=83=BD=E5=8A=9B=E5=AF=BC=E5=85=A5=E9=87=87?= =?UTF-8?q?=E7=94=A8=E5=85=A8=E5=BC=82=E6=AD=A5=EF=BC=9B=E6=89=80=E5=B1=9E?= =?UTF-8?q?=E9=83=A8=E9=97=A8=E4=B8=8D=E5=AD=98=E5=9C=A8=E6=97=B6=E9=BB=98?= =?UTF-8?q?=E8=AE=A4deptId=20=E4=B8=BA1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/ResourceController.java | 19 +++++--- .../excel/ResourceExcelImportListener.java | 45 ++++++++++++++----- 2 files changed, 48 insertions(+), 16 deletions(-) diff --git a/renren-admin/src/main/java/io/renren/modules/resource/controller/ResourceController.java b/renren-admin/src/main/java/io/renren/modules/resource/controller/ResourceController.java index 9e42c45c..44404738 100644 --- a/renren-admin/src/main/java/io/renren/modules/resource/controller/ResourceController.java +++ b/renren-admin/src/main/java/io/renren/modules/resource/controller/ResourceController.java @@ -38,6 +38,7 @@ import java.io.IOException; import java.text.SimpleDateFormat; import java.time.LocalDateTime; import java.util.*; +import java.util.concurrent.CompletableFuture; /** * 资源表 @@ -221,15 +222,23 @@ public class ResourceController { // 对上传的文件重命名,避免文件重名 String oldName = uploadFile.getOriginalFilename(); String newName = UUID.randomUUID().toString() - + oldName.substring(oldName.lastIndexOf("."), oldName.length()); + + oldName.substring(oldName.lastIndexOf(".")); try { // 文件保存 File file = new File(folder, newName); uploadFile.transferTo(file); - EasyExcel.read(file, new ResourceExcelImportListener(0, dept, resourceService)).sheet(0).headRowNumber(6).doRead(); - EasyExcel.read(file, new ResourceExcelImportListener(1, dept, resourceService)).sheet(1).headRowNumber(5).doRead(); - EasyExcel.read(file, new ResourceExcelImportListener(2, dept, resourceService)).sheet(2).headRowNumber(5).doRead(); - EasyExcel.read(file, new ResourceExcelImportListener(3, dept, resourceService)).sheet(3).headRowNumber(6).doRead(); + CompletableFuture.runAsync(() -> { + EasyExcel.read(file, new ResourceExcelImportListener(0, dept, resourceService)).sheet(0).headRowNumber(1).doReadSync(); + }); + CompletableFuture.runAsync(() -> { + EasyExcel.read(file, new ResourceExcelImportListener(1, dept, resourceService)).sheet(1).headRowNumber(1).doReadSync(); + }); + CompletableFuture.runAsync(() -> { + EasyExcel.read(file, new ResourceExcelImportListener(2, dept, resourceService)).sheet(2).headRowNumber(1).doReadSync(); + }); + CompletableFuture.runAsync(() -> { + EasyExcel.read(file, new ResourceExcelImportListener(3, dept, resourceService)).sheet(3).headRowNumber(1).doReadSync(); + }); } catch (IOException e) { return new Result().error(e.getMessage()); } diff --git a/renren-admin/src/main/java/io/renren/modules/resource/excel/ResourceExcelImportListener.java b/renren-admin/src/main/java/io/renren/modules/resource/excel/ResourceExcelImportListener.java index 15271aaa..d7f20dcf 100644 --- a/renren-admin/src/main/java/io/renren/modules/resource/excel/ResourceExcelImportListener.java +++ b/renren-admin/src/main/java/io/renren/modules/resource/excel/ResourceExcelImportListener.java @@ -10,7 +10,9 @@ import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.text.SimpleDateFormat; import java.util.*; +import java.util.stream.Collectors; /** @@ -30,6 +32,8 @@ public class ResourceExcelImportListener extends AnalysisEventListener headMap = null; + private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + public ResourceExcelImportListener(int sheet, List> deptList, ResourceService resourceService) { this.sheet = sheet; this.deptList = deptList; @@ -79,7 +83,11 @@ public class ResourceExcelImportListener extends AnalysisEventListener index.values().contains(dept.getOrDefault("name", "").toString())).findAny(); if (!deptOptional.isPresent()) { logger.info("找不到部门:{}", index.toString()); - return null; + deptOptional = Optional.of(new LinkedHashMap() { + { + put("id", 1L); + } + }); } ResourceDTO resourceDTO = new ResourceDTO(); switch (sheet) { @@ -99,9 +107,14 @@ public class ResourceExcelImportListener extends AnalysisEventListener index != null && StringUtils.isNotEmpty(index.getName())).forEach(index -> { - resourceService.insertWithAttrs(index); - logger.info("完成导入:{}", index.getName()); + }).filter(index -> index != null).forEach(index -> { + try { + logger.info("准备插入:" + JSON.toJSONString(index)); + resourceService.insertWithAttrs(index); + logger.info("完成导入:{}", index.getName()); + } catch (Exception exception) { + logger.error("导入异常", exception); + } }); } @@ -130,7 +143,7 @@ public class ResourceExcelImportListener extends AnalysisEventListener StringUtils.isNotEmpty(info.getAttrValue())).collect(Collectors.toList())); // infoList内空属性不导入 resourceDTO.setDelFlag(0); - resourceDTO.setDeptId(deptId); // 所属部门 +// resourceDTO.setDeptId(deptId); // 所属部门 return resourceDTO; }