From 4abab3d20dc8b96f90716bc30f67762e8c2406e6 Mon Sep 17 00:00:00 2001
From: 13693261870 <252740454@qq.com>
Date: 星期二, 10 一月 2023 18:18:59 +0800
Subject: [PATCH] 1

---
 src/main/java/com/lf/server/service/all/BaseUploadService.java |  162 ++++++++++++++++++++++++++++++++++++-----------------
 1 files changed, 110 insertions(+), 52 deletions(-)

diff --git a/src/main/java/com/lf/server/service/all/BaseUploadService.java b/src/main/java/com/lf/server/service/all/BaseUploadService.java
index 62fb8d6..22279eb 100644
--- a/src/main/java/com/lf/server/service/all/BaseUploadService.java
+++ b/src/main/java/com/lf/server/service/all/BaseUploadService.java
@@ -6,6 +6,7 @@
 import com.lf.server.entity.data.VerEntity;
 import com.lf.server.entity.sys.UserEntity;
 import com.lf.server.helper.*;
+import com.lf.server.service.data.DirService;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -27,7 +28,10 @@
 @Service
 public class BaseUploadService {
     @Autowired
-    public PathHelper pathHelper;
+    private DirService dirService;
+
+    @Autowired
+    private PathHelper pathHelper;
 
     public final Log log = LogFactory.getLog(getClass());
 
@@ -190,6 +194,7 @@
         }
 
         List<MetaFileEntity> list = new ArrayList<>();
+        List<DirEntity> dirs = dirService.selectDirRecursive(dir.getName());
         for (MetaFileEntity meta : metas) {
             meta.setEventid(StringHelper.getGuid());
             meta.setDirid(dir.getId());
@@ -199,14 +204,13 @@
             meta.setEpsgCode(epsgCode);
 
             if (StaticData.ZIP.equals(meta.getExtName())) {
-                List<MetaFileEntity> subs = getMapperFiles(path, meta);
+                List<MetaFileEntity> subs = getMapperFiles(path, dir, dirs, meta);
                 if (null != subs && subs.size() > 0) {
                     list.addAll(subs);
                     continue;
                 }
             }
 
-            meta.setEventid(StringHelper.getGuid());
             list.add(meta);
         }
 
@@ -216,8 +220,9 @@
     /**
      * 鑾峰彇鏄犲皠鏂囦欢
      */
-    private List<MetaFileEntity> getMapperFiles(String path, MetaFileEntity meta) {
-        String zipFile = pathHelper.getConfig().getTempPath() + File.separator + meta.getPath();
+    private List<MetaFileEntity> getMapperFiles(String path, DirEntity dir, List<DirEntity> dirs, MetaFileEntity meta) {
+        String tempPath = pathHelper.getConfig().getTempPath();
+        String zipFile = tempPath + File.separator + meta.getPath();
         File file = new File(zipFile);
         if (!file.exists() || file.isDirectory()) {
             return null;
@@ -231,19 +236,16 @@
         String subPath = zipFolder + File.separator + meta.getName().toLowerCase().replace(".zip", "");
         ZipHelper.unzip(zipFile, subPath);
 
-        File subFile = new File(subPath);
-        File[] files = subFile.listFiles();
-        if (null == files || files.length == 0) {
-            return null;
-        }
+        List<File> files = new ArrayList<>();
+        FileHelper.getFilesByPath(files, subPath);
 
-        return getMapperFiles(files, meta, subPath);
+        return getMapperFiles(files, dir, dirs, meta, tempPath.length() + 1);
     }
 
     /**
      * 鑾峰彇鏄犲皠鏂囦欢
      */
-    private List<MetaFileEntity> getMapperFiles(File[] files, MetaFileEntity meta,String subPath) {
+    private List<MetaFileEntity> getMapperFiles(List<File> files, DirEntity dir, List<DirEntity> dirs, MetaFileEntity meta, int start) {
         List<MetaFileEntity> list = new ArrayList<>();
         for (File f : files) {
             boolean isGdb = isGdbFile(f);
@@ -257,21 +259,30 @@
                 continue;
             }
 
+            int dirid = getDirByPath(f.getPath(), dir, dirs);
             boolean isShp = StaticData.SHP.equals(extName);
             if (isGdb) {
-                String md5 = getGdbMd5(f.getPath());
+                List<MetaFileEntity> rs = getGdbMappers(f, meta, dirid, start);
+                if (null != rs && rs.size() > 0) {
+                    list.addAll(rs);
+                }
                 continue;
             }
 
             MetaFileEntity mf = createMetaFileEntity(meta);
+            mf.setDirid(dirid);
+            mf.setEventid(StringHelper.getGuid());
             mf.setName(fileName);
             mf.setExtName(extName);
-            mf.setSizes(FileHelper.sizeToMb(f.length()));
-            mf.setPath(f.getPath().substring(subPath.length()));
+            mf.setPath(f.getPath().substring(start));
+
             if (isShp) {
+                List<String> shpFiles = getShpFiles(f.getPath());
                 mf.setTab(fileName);
-                mf.setGuid(getShpMd5(f.getPath()));
+                mf.setSizes(getFilesSize(shpFiles));
+                mf.setGuid(getFilesMd5(shpFiles));
             } else {
+                mf.setSizes(FileHelper.sizeToMb(f.length()));
                 mf.setGuid(FileHelper.getFileMd5(f.getPath()));
             }
             list.add(mf);
@@ -285,7 +296,6 @@
      */
     private MetaFileEntity createMetaFileEntity(MetaFileEntity meta) {
         MetaFileEntity mf = new MetaFileEntity();
-        mf.setEventid(StringHelper.getGuid());
         mf.setDirid(meta.getDirid());
         mf.setDepid(meta.getDepid());
         mf.setVerid(meta.getVerid());
@@ -296,80 +306,128 @@
     }
 
     /**
-     * 鑾峰彇SHP鏂囦欢鐨凪D5鐮�
+     * 鏍规嵁鏂囦欢璺緞鑾峰彇鐩綍ID
      */
-    private String getShpMd5(String shpPath) {
-        List<String> files = new ArrayList<>();
-        files.add(shpPath);
+    private int getDirByPath(String filePath, DirEntity dir, List<DirEntity> dirs) {
+        if (0 != dir.getPid() || null == dirs || dirs.isEmpty()) {
+            return dir.getId();
+        }
+        if ("/".equals(File.separator)) {
+            filePath = filePath.replace("/", "\\");
+        }
+
+        for (DirEntity entity : dirs) {
+            if (filePath.contains(entity.getFullName())) {
+                return entity.getId();
+            }
+        }
+
+        return dir.getId();
+    }
+
+    /**
+     * 鑾峰彇SHP鏂囦欢闆嗗悎
+     */
+    private  List<String> getShpFiles(String shpPath) {
+        List<String> list = new ArrayList<>();
+        list.add(shpPath);
 
         for (int i = 1, c = StaticData.SHP_EXTENSION.size(); i < c; i++) {
             String path = shpPath.replace(".shp", StaticData.SHP_EXTENSION.get(i));
 
             File f = new File(path);
             if (f.exists() && !f.isDirectory()) {
-                files.add(path);
+                list.add(path);
             }
         }
 
-        return getFilesMd5(files);
+        return list;
     }
 
     /**
-     * 鑾峰彇GDB鏂囦欢鐨凪D5鐮�
+     * 鑾峰彇GDB鏂囦欢闆嗗悎
      */
-    private String getGdbMd5(String path) {
+    private List<String> getGdbFiles(String path) {
+        List<String> list = new ArrayList<>();
+
         File[] files = new File(path).listFiles();
         if (null == files || files.length == 0) {
-            return StringHelper.getGuid();
+            return list;
         }
-
-        List<String> list = new ArrayList<>();
         for (File f : files) {
             list.add(f.getPath());
         }
 
-        return getFilesMd5(list);
+        return list;
     }
 
     /**
      * 鑾峰彇澶氭枃浠剁殑MD5
      */
-    private String getFilesMd5(List<String> list) {
-        StringBuilder sb = new StringBuilder();
-        for (String str : list) {
-            String md5 = FileHelper.getFileMd5(str);
+    private String getFilesMd5(List<String> files) {
+        List<String> list = new ArrayList<>();
+        for (String file : files) {
+            String md5 = FileHelper.getFileMd5(file);
             if (null != md5) {
-                sb.append(sb + ",");
+                list.add(md5);
             }
         }
 
-        if (sb.length() > 0) {
-            return FileHelper.getStringMd5(sb.toString());
+        if (list.size() > 0) {
+            String str = StringHelper.join(list, ",");
+            return FileHelper.getStringMd5(str);
         }
 
         return StringHelper.getGuid();
     }
 
-    private void getGdbFiles(String shpPath, List<String> list) {
-        File file = new File(shpPath);
-
-        File[] files = file.listFiles();
-        if (null == files || files.length == 0) {
-            return;
+    /**
+     * 鑾峰彇澶氭枃浠剁殑澶у皬
+     */
+    private double getFilesSize(List<String> files) {
+        long size = 0L;
+        for (String file : files) {
+            File f = new File(file);
+            if (f.exists() && !f.isDirectory()) {
+                size += f.length();
+            }
         }
 
-        for (File f : files) {
-            if (!f.isDirectory()) {
-                continue;
-            }
+        return FileHelper.sizeToMb(size);
+    }
 
-            if (isGdbFile(f)) {
-                list.add(f.getPath());
-                continue;
-            }
-
-            getGdbFiles(f.getPath(), list);
+    /**
+     * 鑾峰彇GDB鏂囦欢鏄犲皠
+     */
+    private List<MetaFileEntity> getGdbMappers(File f, MetaFileEntity meta, int dirid, int start) {
+        List<String> tabs = GdbHelper.getTabNames(f.getPath());
+        if (null == tabs || tabs.size() == 0) {
+            return null;
         }
+
+        String fileName = FileHelper.getFileName(f.getPath());
+        String extName = FileHelper.getExtension(fileName);
+
+        List<String> gdbFiles = getGdbFiles(f.getPath());
+        String md5 = getFilesMd5(gdbFiles);
+        double sizes = getFilesSize(gdbFiles);
+
+        List<MetaFileEntity> list = new ArrayList<>();
+        for (String tab : tabs) {
+            MetaFileEntity mf = createMetaFileEntity(meta);
+            mf.setDirid(dirid);
+            mf.setEventid(StringHelper.getGuid());
+            mf.setName(fileName);
+            mf.setExtName(extName);
+            mf.setSizes(FileHelper.sizeToMb(f.length()));
+            mf.setPath(f.getPath().substring(start));
+            mf.setTab(tab);
+            mf.setSizes(sizes);
+            mf.setGuid(md5);
+            list.add(mf);
+        }
+
+        return list;
     }
 
     /**

--
Gitblit v1.9.3