Prechádzať zdrojové kódy

优化资料批量导入

lvy 1 deň pred
rodič
commit
d206447bae

+ 61 - 9
blade-service/blade-manager/src/main/java/org/springblade/manager/controller/InformationImportRecordController.java

@@ -5,10 +5,18 @@ import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
+import com.spire.xls.CellRange;
 import io.swagger.annotations.*;
 import lombok.AllArgsConstructor;
 import org.apache.commons.io.IOUtils;
 import org.apache.commons.lang.StringUtils;
+import org.apache.poi.ss.usermodel.Comment;
+import org.apache.poi.ss.usermodel.Drawing;
+import org.apache.poi.ss.usermodel.Shape;
+import org.apache.poi.ss.util.CellAddress;
+import org.apache.poi.xssf.usermodel.XSSFComment;
+import org.apache.poi.xssf.usermodel.XSSFSheet;
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
 import org.jetbrains.annotations.NotNull;
 import org.jsoup.Jsoup;
 import org.jsoup.nodes.Document;
@@ -51,10 +59,7 @@ import org.springframework.scheduling.annotation.Scheduled;
 import org.springframework.web.bind.annotation.*;
 import org.springframework.web.multipart.MultipartFile;
 
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileNotFoundException;
-import java.io.InputStream;
+import java.io.*;
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.Paths;
@@ -195,6 +200,7 @@ public class InformationImportRecordController extends BladeController {
             String path = base + id + filename.substring(filename.lastIndexOf("."));
 
             Files.write(Paths.get(path), IOUtils.toByteArray(is));
+            String name = getFileNameBySheet(path);
 
             // 创建记录
             InformationImportRecord record = new InformationImportRecord();
@@ -202,10 +208,11 @@ public class InformationImportRecordController extends BladeController {
             record.setNodeId(nodeId);
             record.setContractId(contractId);
             record.setClassify(classify);
-            record.setFileName(filename.substring(0, filename.lastIndexOf(".")));
+            record.setFileName(name == null ? filename.substring(0, filename.lastIndexOf(".")) : name);
             record.setFilePath(path);
             record.setCreateTime(now);
             record.setCreateUser(AuthUtil.getUserId());
+            record.setRemark("::" + filename);
             records.add(record);
         }
         informationImportRecordService.saveBatch(records);
@@ -213,6 +220,29 @@ public class InformationImportRecordController extends BladeController {
         return R.data(true);
     }
 
+    private static String getFileNameBySheet(String excelPath) {
+        // 解析excel, 读取excel中的批注
+        try (InputStream is = Files.newInputStream(Paths.get(excelPath));XSSFWorkbook workbook = new XSSFWorkbook(is);) {
+            for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
+                XSSFSheet sheet = workbook.getSheetAt(i);
+                Map<CellAddress, XSSFComment> map = sheet.getCellComments();
+                for (Map.Entry<CellAddress, XSSFComment> entry : map.entrySet()) {
+                    XSSFComment value = entry.getValue();
+                    if (value != null && value.getString() != null) {
+                        String string = value.getString().getString();
+                        if (string != null && string.contains("》")) {
+                            return string.replaceAll("\r\n", "");
+                        }
+                    }
+                }
+            }
+            return null;
+        } catch (Exception e) {
+            logger.error("获取文件名失败", e);
+            return null;
+        }
+    }
+
     /**
      * 初始化: 匹配节点,创建子节点
      * @param id 记录ID
@@ -282,20 +312,42 @@ public class InformationImportRecordController extends BladeController {
                 }
             }
             if (record.getTargetId() != null && record.getTargetId() > 0) {
-                informationImportRecordService.updateProcess(record.getId(), 20, 1, null, record.getUpdateTime());
+                informationImportRecordService.update(Wrappers.<InformationImportRecord>lambdaUpdate().eq(InformationImportRecord::getId, record.getId())
+                        .set(InformationImportRecord::getTargetId, record.getTargetId()).set(InformationImportRecord::getProcess, 20));
                 return;
             }
         }
 
-        List<WbsTreePrivate> query = jdbcTemplate.query("select * from m_wbs_tree_private where is_deleted = 0 and node_type = 6 and project_id = " + target.getProjectId() + " and node_name like '" + lastName + "' limit 1",
+        List<WbsTreePrivate> query = jdbcTemplate.query("select * from m_wbs_tree_private where is_deleted = 0 and project_id = " + target.getProjectId() + " and node_name like '" + lastName + "'",
                 new BeanPropertyRowMapper<>(WbsTreePrivate.class));
         if (query.isEmpty()) {
             informationImportRecordService.updateProcess(record.getId(), null, 3, "节点未找到-后管::" + target.getFullName(), record.getUpdateTime());
             return;
         }
+        WbsTreePrivate wbsTreePrivate = null;
+        List<WbsTreePrivate> tables = null;
+        for (WbsTreePrivate treePrivate : query) {
+            tables = jdbcTemplate.query("select * from m_wbs_tree_private where is_deleted = 0 and type in (2,10) and p_id = " + treePrivate.getPKeyId(), new BeanPropertyRowMapper<>(WbsTreePrivate.class));
+            if (tables.isEmpty()) {
+                tables = jdbcTemplate.query("select * from m_wbs_tree_private where is_deleted = 0 and type in (2,10) and parent_id = " + treePrivate.getId() + " and project_id = " + target.getProjectId(), new BeanPropertyRowMapper<>(WbsTreePrivate.class));
+            }
+            if (!tables.isEmpty()) {
+                wbsTreePrivate = treePrivate;
+                break;
+            }
+            if (treePrivate.getNodeType() != null && treePrivate.getNodeType() == 6) {
+                wbsTreePrivate = treePrivate;
+            }
+        }
+        if (wbsTreePrivate == null) {
+            informationImportRecordService.updateProcess(record.getId(), null, 3, "节点未找到-后管::" + target.getFullName(), record.getUpdateTime());
+            return;
+        }
+        if (tables.isEmpty()) {
+            informationImportRecordService.updateProcess(record.getId(), null, 3, "表单未找到-后管::" + wbsTreePrivate.getNodeName(), record.getUpdateTime());
+            return;
+        }
         informationImportRecordService.updateProcess(record.getId(), 7, 1, null, record.getUpdateTime());
-        WbsTreePrivate wbsTreePrivate = query.get(0);
-        List<WbsTreePrivate> tables = jdbcTemplate.query("select * from m_wbs_tree_private where is_deleted = 0 and p_id = " + wbsTreePrivate.getPKeyId(), new BeanPropertyRowMapper<>(WbsTreePrivate.class));
 
         // 在target节点下创建子节点及表单
         Long parentPKeyId = SnowFlakeUtil.getId();