From 9bce48a9f30f7d80c43f43f46d40df20fcb00e15 Mon Sep 17 00:00:00 2001
From: 13693261870 <252740454@qq.com>
Date: 星期五, 03 二月 2023 09:39:59 +0800
Subject: [PATCH] 1

---
 src/main/java/com/lf/server/service/data/UploadService.java |  255 +++++++++++++++++++++++++++++++-------------------
 1 files changed, 158 insertions(+), 97 deletions(-)

diff --git a/src/main/java/com/lf/server/service/data/UploadService.java b/src/main/java/com/lf/server/service/data/UploadService.java
index 455783f..49710c1 100644
--- a/src/main/java/com/lf/server/service/data/UploadService.java
+++ b/src/main/java/com/lf/server/service/data/UploadService.java
@@ -9,6 +9,7 @@
 import com.lf.server.mapper.data.UploadMapper;
 import com.lf.server.service.all.BaseQueryService;
 import com.lf.server.service.all.BaseUploadService;
+import org.apache.commons.io.FileUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
@@ -56,11 +57,12 @@
     /**
      * 鎻掑叆鏂囦欢
      */
-    public List<MetaEntity> insertFiles(UserEntity ue, List<MetaFileEntity> list, HttpServletRequest req) {
+    public void insertFiles(UserEntity ue, List<MetaFileEntity> list, HttpServletRequest req) {
         checkMetaFiles(ue, list);
         excelLoader(list, req);
-
-        return dataLoader(list);
+        loadData(list);
+        copyFiles(list);
+        insertMetas(list);
     }
 
     /**
@@ -75,23 +77,126 @@
             mf.setCreateTime(createTime);
             mf.setDepid(ue.getDepid());
             mf.setPath(tempPath + File.separator + mf.getPath());
+            mf.setMsg(null);
 
             File f = new File(mf.getPath());
             if (!f.exists()) {
                 mf.setMsg("鏂囦欢涓㈠け");
-                mf.setRows(-1);
+            }
+
+            MetaEntity old = metaService.selectByGuid(mf.getGuid(), null);
+            if (null != old) {
+                mf.setMsg("宸插叆搴�");
             }
         }
     }
 
     /**
-     * 鍔犺浇鏁版嵁
+     * Excel鍏ュ簱
      */
-    private List<MetaEntity> dataLoader(List<MetaFileEntity> list) {
-        loadData(list);
-        copyFiles(list);
+    private String excelLoader(List<MetaFileEntity> list, HttpServletRequest req) {
+        List<MetaFileEntity> xlsList = getExcelFiles(list);
+        if (xlsList.isEmpty()) {
+            return "";
+        }
 
-        return insertMetas(list);
+        String guid = null;
+        try {
+            MetaFileEntity meta = getExcelMeta(xlsList);
+            guid = fmeService.excelLoader(meta, req);
+        } catch (Exception ex) {
+            log.error(ex.getMessage(), ex);
+        }
+
+        return guid;
+    }
+
+    /**
+     * 鑾峰彇Excel鍏冩暟鎹枃浠�
+     */
+    private List<MetaFileEntity> getExcelFiles(List<MetaFileEntity> list) {
+        List<MetaFileEntity> xlsList = new ArrayList<>();
+        for (MetaFileEntity mf : list) {
+            if (null != mf.getMsg()) {
+                continue;
+            }
+            if (StaticData.XLS.equals(mf.getExtName()) || StaticData.XLSX.equals(mf.getExtName())) {
+                xlsList.add(mf);
+            }
+        }
+
+        return xlsList;
+    }
+
+    /**
+     * 鑾峰彇Excel鐨勫厓鏁版嵁
+     */
+    private MetaFileEntity getExcelMeta(List<MetaFileEntity> xlsList) {
+        List<String> pathList = new ArrayList<>();
+        List<String> dirList = new ArrayList<>();
+        List<String> pidList = new ArrayList<>();
+
+        String xlsBasePath = getXlsPath(xlsList.get(0).getPath());
+        for (int i = 0, c = xlsList.size(); i < c; i++) {
+            MetaFileEntity mf = xlsList.get(i);
+
+            String path = copyXlsFile(xlsBasePath, i, mf);
+            if (null != path) {
+                pathList.add(path);
+                dirList.add(mf.getDirid() + "");
+                pidList.add(mf.getEventid());
+            }
+        }
+
+        MetaFileEntity meta = new MetaFileEntity();
+        meta.setPath(StringHelper.join(pathList, ","));
+        meta.setEpsgCode(xlsList.get(0).getEpsgCode());
+        meta.setName(StringHelper.join(dirList, ";"));
+        meta.setDepid(xlsList.get(0).getDepid());
+        meta.setVerid(xlsList.get(0).getVerid());
+        meta.setCreateUser(xlsList.get(0).getCreateUser());
+        meta.setEventid(StringHelper.join(pidList, ";"));
+
+        return meta;
+    }
+
+    /**
+     * 鑾峰彇Xls鐩綍
+     */
+    private String getXlsPath(String filePath) {
+        String tempPath = pathHelper.getConfig().getTempPath() + File.separator;
+        String subPath = filePath.substring(tempPath.length());
+        subPath = tempPath + subPath.substring(0, subPath.indexOf(File.separator)).replace("_zip", "") + "_xls";
+
+        File f = new File(subPath);
+        if (!f.exists() || !f.isDirectory()) {
+            f.mkdirs();
+        }
+
+        return subPath;
+    }
+
+    /**
+     * 澶嶅埗Xls鏂囦欢
+     */
+    private String copyXlsFile(String xlsBasePath, int i, MetaFileEntity mf) {
+        try {
+            String xlsPath = xlsBasePath + File.separator + i;
+
+            File file = new File(xlsPath);
+            if (!file.exists() || !file.isDirectory()) {
+                file.mkdirs();
+            }
+            file = new File(mf.getPath());
+
+            File newFile = new File(xlsPath + File.separator + FileHelper.getFileName(file.getPath()));
+            FileUtils.copyFile(file, newFile);
+
+            return newFile.getPath();
+        } catch (Exception ex) {
+            log.error(ex.getMessage(), ex);
+            return null;
+        }
     }
 
     /**
@@ -99,7 +204,7 @@
      */
     private void loadData(List<MetaFileEntity> list) {
         for (MetaFileEntity mf : list) {
-            if (StringHelper.isEmpty(mf.getEntity())) {
+            if (null != mf.getMsg() || StringHelper.isEmpty(mf.getEntity())) {
                 continue;
             }
             if (StaticData.SHP.equals(mf.getExtName()) || StaticData.GDB.equals(mf.getExtName())) {
@@ -124,7 +229,19 @@
             return;
         }
 
-        List<?> list = null;
+        File file = new File(mf.getPath());
+        if (!file.exists()) {
+            mf.setMsg("鏂囦欢涓㈠け");
+            return;
+        }
+
+        MetaEntity old = metaService.selectByGuid(mf.getGuid(), tabName);
+        if (null != old) {
+            mf.setMsg("宸插叆搴�");
+            return;
+        }
+
+        List<?> list;
         if (StaticData.SHP.equals(mf.getExtName())) {
             list = ShpHelper.readData(clazz, mf.getPath());
         } else {
@@ -137,6 +254,7 @@
 
         int rows = basicMapper.insertBatch(list);
         if (rows > 0) {
+            mf.setEntity(mf.getTab());
             mf.setTab(tabName);
             mf.setRows(rows);
         }
@@ -172,6 +290,10 @@
     private void copyFiles(List<MetaFileEntity> list) {
         List<String> gdbList = new ArrayList<>();
         for (MetaFileEntity mf : list) {
+            if (null != mf.getMsg()) {
+                continue;
+            }
+
             switch (mf.getExtName()) {
                 case StaticData.MPT:
                     copyMultiFile(mf, StaticData.MPT_EXT);
@@ -193,8 +315,8 @@
                         String path = findPathByGuid(list, mf);
                         if (null != path) {
                             mf.setPath(path);
+                            continue;
                         }
-                        continue;
                     }
 
                     gdbList.add(mf.getPath());
@@ -217,26 +339,25 @@
             return -1;
         }
 
-        MetaEntity old = metaService.selectByGuid(mf.getGuid());
+        MetaEntity old = metaService.selectByGuid(mf.getGuid(), null);
         if (null != old) {
-            mf.setMsg("鏂囦欢宸插瓨鍦�");
+            mf.setMsg("宸插叆搴�");
             file.delete();
             return 0;
         }
 
         String uploadPath = pathHelper.getUploadFullPath();
         String targetPath = uploadPath + File.separator + mf.getGuid() + mf.getExtName();
+        String subPath = FileHelper.getRelativePath(targetPath);
 
         File newFile = new File(targetPath);
         if (newFile.exists()) {
-            mf.setMsg("鏂囦欢宸插瓨鍦�");
+            mf.setPath(subPath);
             file.delete();
             return 0;
         }
 
         file.renameTo(newFile);
-
-        String subPath = FileHelper.getRelativePath(targetPath);
         mf.setPath(subPath);
 
         return 1;
@@ -288,19 +409,20 @@
             return;
         }
 
-        MetaEntity old = metaService.selectByGuid(mf.getGuid());
+        MetaEntity old = metaService.selectByGuid(mf.getGuid(), null);
         if (null != old) {
-            mf.setMsg("鏂囦欢宸插瓨鍦�");
+            mf.setMsg("宸插叆搴�");
             FileHelper.deleteFiles(file);
             return;
         }
 
         String uploadPath = pathHelper.getUploadFullPath();
         String targetPath = uploadPath + File.separator + mf.getGuid() + mf.getExtName();
+        String subPath = FileHelper.getRelativePath(targetPath);
 
         File newFile = new File(targetPath);
         if (newFile.exists() && newFile.isDirectory()) {
-            mf.setMsg("鏂囦欢宸插瓨鍦�");
+            mf.setPath(subPath);
             FileHelper.deleteFiles(file);
             return;
         }
@@ -312,11 +434,9 @@
         }
 
         for (File f : files) {
-            String subPath = targetPath + File.separator + FileHelper.getFileName(f.getPath());
-            f.renameTo(new File(subPath));
+            String subFile = targetPath + File.separator + FileHelper.getFileName(f.getPath());
+            f.renameTo(new File(subFile));
         }
-
-        String subPath = FileHelper.getRelativePath(targetPath);
         mf.setPath(subPath);
     }
 
@@ -325,7 +445,7 @@
      */
     private String findPathByGuid(List<MetaFileEntity> list, MetaFileEntity mf) {
         for (MetaFileEntity meta : list) {
-            if (meta.getGuid().equals(mf.getGuid())) {
+            if (meta.getGuid().equals(mf.getGuid()) && !meta.getPath().equals(mf.getPath())) {
                 return meta.getPath();
             }
         }
@@ -336,20 +456,17 @@
     /**
      * 鎻掑叆鍏冩暟鎹�
      */
-    private List<MetaEntity> insertMetas(List<MetaFileEntity> list) {
-        List<MetaEntity> metas = new ArrayList<>();
+    private void insertMetas(List<MetaFileEntity> list) {
         for (MetaFileEntity mf : list) {
-            if (StringHelper.isEmpty(mf.getMsg())) {
-                metas.add(createMeta(mf));
+            if (null != mf.getMsg()) {
+                continue;
             }
-        }
-        if (metas.isEmpty()) {
-            return null;
-        }
 
-        int rows = metaService.inserts(metas);
+            MetaEntity me = createMeta(mf);
+            metaService.insert(me);
 
-        return rows > 0 ? metas : null;
+            mf.setMsg(me.getId() > 0 ? "鎴愬姛" : "澶辫触");
+        }
     }
 
     /**
@@ -366,72 +483,16 @@
         me.setGuid(mf.getGuid());
         me.setPath(mf.getPath());
         me.setSizes(mf.getSizes());
-        me.setTab(mf.getTab());
+        if (mf.getRows() > 0) {
+            me.setTab(mf.getTab());
+        }
+        if (!StringHelper.isEmpty(mf.getEntity())) {
+            me.setLayer(mf.getEntity());
+        }
         me.setRows(mf.getRows());
         me.setCreateUser(mf.getCreateUser());
         me.setCreateTime(mf.getCreateTime());
 
         return me;
-    }
-
-    /**
-     * Excel鍏ュ簱
-     */
-    private String excelLoader(List<MetaFileEntity> list, HttpServletRequest req) {
-        List<MetaFileEntity> xlsList = getExcelFiles(list);
-        if (xlsList.isEmpty()) {
-            return "";
-        }
-
-        String guid = null;
-        try {
-            MetaFileEntity meta = getExcelMeta(xlsList);
-            guid = fmeService.excelLoader(meta, req);
-        } catch (Exception ex) {
-            log.error(ex.getMessage(), ex);
-        }
-
-        return guid;
-    }
-
-    /**
-     * 鑾峰彇Excel鍏冩暟鎹枃浠�
-     */
-    private List<MetaFileEntity> getExcelFiles(List<MetaFileEntity> list) {
-        List<MetaFileEntity> xlsList = new ArrayList<>();
-        for (MetaFileEntity mf : list) {
-            boolean isXls = StaticData.XLS.equals(mf.getExtName()) || StaticData.XLSX.equals(mf.getExtName());
-            if (mf.getRows() > -1 && isXls) {
-                xlsList.add(mf);
-            }
-        }
-
-        return xlsList;
-    }
-
-    /**
-     * 鑾峰彇Excel鐨勫厓鏁版嵁
-     */
-    private MetaFileEntity getExcelMeta(List<MetaFileEntity> xlsList) {
-        List<String> pathList = new ArrayList<>();
-        List<String> dirList = new ArrayList<>();
-        List<String> pidList = new ArrayList<>();
-
-        for (MetaFileEntity mf : xlsList) {
-            pathList.add(mf.getPath());
-            dirList.add(mf.getDirid() + "");
-            pidList.add(mf.getEventid());
-        }
-
-        MetaFileEntity meta = new MetaFileEntity();
-        meta.setPath(StringHelper.join(pathList, ","));
-        meta.setEpsgCode(xlsList.get(0).getEpsgCode());
-        meta.setName(StringHelper.join(dirList, ","));
-        meta.setDepid(xlsList.get(0).getDepid());
-        meta.setVerid(xlsList.get(0).getVerid());
-        meta.setCreateUser(xlsList.get(0).getCreateUser());
-        meta.setEventid(StringHelper.join(pidList, ","));
-
-        return meta;
     }
 }

--
Gitblit v1.9.3