xiatian 4 недель назад
Родитель
Сommit
795da802a5

+ 81 - 6
src/main/java/cn/com/qmth/archive/service/impl/MarkingServiceImpl.java

@@ -1,12 +1,17 @@
 package cn.com.qmth.archive.service.impl;
 
 import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
 import java.io.IOException;
+import java.io.InputStream;
 import java.time.Instant;
 import java.time.LocalDateTime;
 import java.time.ZoneId;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Date;
+import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
@@ -19,8 +24,10 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
 import com.qmth.boot.core.exception.StatusException;
+import com.qmth.boot.tools.excel.ExcelReader;
+import com.qmth.boot.tools.excel.enums.ExcelType;
+import com.qmth.boot.tools.excel.model.DataMap;
 
-import cn.com.qmth.archive.bean.TableCols;
 import cn.com.qmth.archive.config.SysProperty;
 import cn.com.qmth.archive.dao.MarkingDao;
 import cn.com.qmth.archive.service.MarkingService;
@@ -32,6 +39,8 @@ public class MarkingServiceImpl implements MarkingService {
 
     private final static Logger log = LoggerFactory.getLogger(MarkingService.class);
 
+    private static final String[] EXCEL_HEADER = new String[] { "表名", "字段名" };
+
     @Autowired
     private SysProperty sysProperty;
 
@@ -60,18 +69,84 @@ public class MarkingServiceImpl implements MarkingService {
                 throw new StatusException("配置文件中workids错误");
             }
         }
+
         log.warn("*********************开始处理,workid:" + StringUtils.join(workids, ","));
         try {
-            getWork(dir, workids);
+            Map<String, List<String>> clos = getCols();
+            getWork(clos, dir, workids);
             log.warn("*********************处理结束,成功");
         } catch (Exception e) {
             throw new StatusException("*********************处理结束,失败", e);
         }
     }
 
-    private void getWork(File dir, List<Long> workids) throws IOException {
+    private String trimAndNullIfBlank(String s) {
+        if (StringUtils.isBlank(s)) {
+            return null;
+        }
+        return s.trim();
+    }
+
+    @SuppressWarnings("deprecation")
+    private Map<String, List<String>> getCols() throws FileNotFoundException {
+        File file = new File("./columns/columns.xlsx");
+        if (!file.exists()) {
+            throw new StatusException("columns下没有表字段文件");
+        }
+        InputStream inputStream = null;
+        try {
+            inputStream = new FileInputStream(file);
+            List<DataMap> lineList = null;
+            ExcelReader reader = ExcelReader.create(ExcelType.XLSX, inputStream, 0);
+            try {
+                lineList = reader.getDataMapList();
+            } catch (Exception e) {
+                throw new StatusException("Excel 解析失败");
+            }
+            if (!Arrays.equals(EXCEL_HEADER, reader.getColumnNames())) {
+                throw new StatusException("Excel表头错误");
+            }
+            if (CollectionUtils.isEmpty(lineList)) {
+                throw new StatusException("Excel无内容");
+            }
+            Map<String, List<String>> cols = new HashMap<>();
+            for (int i = 1; i < lineList.size(); i++) {
+                DataMap line = lineList.get(i);
+                String tname = trimAndNullIfBlank(line.get(EXCEL_HEADER[0]));
+                if (StringUtils.isBlank(tname)) {
+                    throw new StatusException("第" + (i + 1) + "行表名为空");
+                }
+                String cname = trimAndNullIfBlank(line.get(EXCEL_HEADER[1]));
+                if (StringUtils.isBlank(cname)) {
+                    throw new StatusException("第" + (i + 1) + "行字段名为空");
+                }
+                List<String> tem = cols.get(tname);
+                if (tem == null) {
+                    tem = new ArrayList<>();
+                    cols.put(tname, tem);
+                }
+                tem.add(cname);
+            }
+            return cols;
+        } finally {
+            if (inputStream != null) {
+                try {
+                    inputStream.close();
+                } catch (IOException e) {
+                }
+            }
+        }
+
+    }
+
+    private void getWork(Map<String, List<String>> clos, File dir, List<Long> workids) throws IOException {
         log.warn("*********************处理work");
-        List<Map<String, Object>> work = markingDao.getWork(TableCols.work, workids);
+        String tableName = "ec_m_mark_work";
+        List<String> col = clos.get(tableName);
+        if (CollectionUtils.isEmpty(col)) {
+            throw new StatusException("ec_m_mark_work表没有字段信息");
+        }
+        List<Map<String, Object>> work = markingDao.getWork(col, workids);
         if (CollectionUtils.isEmpty(work)) {
             throw new StatusException("work表没有数据");
         }
@@ -79,12 +154,12 @@ public class MarkingServiceImpl implements MarkingService {
         sql.createNewFile();
         List<String> ret = new ArrayList<>();
         for (Map<String, Object> map : work) {
-            ret.add(getLine("ec_m_mark_work", TableCols.work, map));
+            ret.add(getLine(tableName, col, map));
         }
         FileUtils.writeLines(sql, "utf-8", ret, false);
     }
 
-    private String getLine(String tbName, String[] cols, Map<String, Object> line) {
+    private String getLine(String tbName, List<String> cols, Map<String, Object> line) {
         StringBuilder sb = new StringBuilder("INSERT INTO `" + tbName + "` (`");
         sb.append(StringUtils.join(cols, "`,`") + "`) ");
         sb.append("values (");