From 05ffa1a7f490e1e81d6fbf1ee948db50b1fff6fc Mon Sep 17 00:00:00 2001
From: 13693261870 <252740454@qq.com>
Date: 星期二, 14 二月 2023 16:02:52 +0800
Subject: [PATCH] 修改depcode、dircode值

---
 src/main/java/com/lf/server/service/all/BaseUploadService.java |  371 +++++++++++++++++++++++++++++++++++++++-------------
 1 files changed, 277 insertions(+), 94 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 f0a614d..8e0fdb6 100644
--- a/src/main/java/com/lf/server/service/all/BaseUploadService.java
+++ b/src/main/java/com/lf/server/service/all/BaseUploadService.java
@@ -1,8 +1,12 @@
 package com.lf.server.service.all;
 
+import com.lf.server.entity.all.StaticData;
 import com.lf.server.entity.data.DirEntity;
 import com.lf.server.entity.data.MetaFileEntity;
+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;
@@ -23,20 +27,11 @@
  */
 @Service
 public class BaseUploadService {
-    private final static String XLS = ".xls";
-
-    private final static String MDB = ".mdb";
-
-    private final static String GDB = ".gdb";
-
-    private final static String SHP_ZIP = ".shp.zip";
-
-    private final static String GDB_ZIP = ".gdb.zip";
-
-    private final static String ZIP = ".zip";
+    @Autowired
+    protected DirService dirService;
 
     @Autowired
-    public PathHelper pathHelper;
+    protected PathHelper pathHelper;
 
     public final Log log = LogFactory.getLog(getClass());
 
@@ -157,7 +152,9 @@
             mf.setExtName(extName);
             mf.setSizes(FileHelper.sizeToMb(f.length()));
             mf.setPath(subPath + File.separator + fileName);
-            // mf.setGuid(FileHelper.getFileMd5(f.getPath()))
+            if (!StaticData.ZIP.equals(extName)) {
+                mf.setGuid(FileHelper.getFileMd5(f.getPath()));
+            }
 
             list.add(mf);
         }
@@ -190,124 +187,289 @@
     /**
      * 鏌ヨ鏄犲皠
      */
-    public List<MetaFileEntity> selectMappers(List<MetaFileEntity> metas, DirEntity dir, String epsgCode) {
-        List<MetaFileEntity> list = new ArrayList<>();
-        for (MetaFileEntity meta : metas) {
-            //
+    public List<MetaFileEntity> selectMappers(UserEntity ue, String path, DirEntity dir, VerEntity ver, String epsgCode) {
+        List<MetaFileEntity> metas = selectFiles(path, StaticData.ALL_EXTENSION);
+        if (null == metas || metas.isEmpty()) {
+            return null;
         }
+
+        List<MetaFileEntity> list = new ArrayList<>();
+        List<DirEntity> dirs = dirService.selectDirRecursive(dir.getName());
+        for (MetaFileEntity meta : metas) {
+            meta.setEventid(StringHelper.getGuid());
+            meta.setDircode(dir.getCode());
+            meta.setDepcode(ue.getDepcode());
+            meta.setVerid(ver.getId());
+            meta.setCreateUser(ue.getId());
+            meta.setEpsgCode(epsgCode);
+
+            if (StaticData.ZIP.equals(meta.getExtName())) {
+                List<MetaFileEntity> subs = getMapperFiles(path, dir, dirs, meta);
+                if (null != subs && subs.size() > 0) {
+                    list.addAll(subs);
+                    continue;
+                }
+            }
+
+            list.add(meta);
+        }
+        setMetaType(list);
 
         return list;
     }
 
-    /*private List<TabMapperEntity> getMappers(String zipPath, File[] files) {
-        String temp = pathHelper.getConfig().getTempPath();
+    /**
+     * 鑾峰彇鏄犲皠鏂囦欢
+     */
+    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;
+        }
 
-        List<TabMapperEntity> list = new ArrayList<>();
+        File zipFolder = new File(pathHelper.getConfig().getTempPath() + File.separator + path + "_zip");
+        if (!zipFolder.exists() || !zipFolder.isDirectory()) {
+            zipFolder.mkdirs();
+        }
+
+        String subPath = zipFolder + File.separator + meta.getName().toLowerCase().replace(".zip", "");
+        ZipHelper.unzip(zipFile, subPath);
+
+        List<File> files = new ArrayList<>();
+        getFilesByPath(files, subPath);
+
+        return getMapperFiles(files, dir, dirs, meta, tempPath.length() + 1);
+    }
+
+    /**
+     * 鏍规嵁璺緞鑾峰彇鏂囦欢
+     */
+    private void getFilesByPath(List<File> list, String path) {
+        File file = new File(path);
+        if (!file.isDirectory()) {
+            String extName = FileHelper.getExtension(file);
+            if (StaticData.ALL_EXTENSION.contains(extName)) {
+                list.add(file);
+            }
+            return;
+        }
+
+        if (isGdbFile(file)) {
+            list.add(file);
+            return;
+        }
+
+        File[] files = file.listFiles();
+        if (null == files) {
+            return;
+        }
         for (File f : files) {
-            String fileName = FileHelper.getFileName(f.getPath());
-            if (fileName.contains(XLS)) {
-                String path = f.getPath().replace(temp + File.separator, "");
-                list.add(new TabMapperEntity(fileName, "xls", null, path));
+            if (f.isDirectory()) {
+                getFilesByPath(list, f.getPath());
+            } else {
+                String extName = FileHelper.getExtension(f);
+                if (StaticData.ALL_EXTENSION.contains(extName)) {
+                    list.add(f);
+                }
+            }
+        }
+    }
+
+    /**
+     * 鑾峰彇鏄犲皠鏂囦欢
+     */
+    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);
+            if (!isGdb && f.isDirectory()) {
                 continue;
             }
-            if (fileName.contains(MDB)) {
-                String path = f.getPath().replace(temp + File.separator, "");
-                List<String> tabs = MdbHelper.getTabNames(f.getPath());
-                for (String tab : tabs) {
-                    list.add(new TabMapperEntity(fileName, "mdb", tab, path));
+
+            String fileName = FileHelper.getFileName(f.getPath());
+            String extName = FileHelper.getExtension(fileName);
+            if (!StaticData.ALL_EXTENSION.contains(extName) || fileName.startsWith("~")) {
+                continue;
+            }
+
+            String dircode = getDirByPath(f.getPath(), fileName, dir, dirs);
+            boolean isShp = StaticData.SHP.equals(extName);
+            if (isGdb) {
+                List<MetaFileEntity> rs = getGdbMappers(f, meta, dircode, start);
+                if (null != rs && rs.size() > 0) {
+                    list.addAll(rs);
                 }
                 continue;
             }
-            if (fileName.contains(SHP_ZIP)) {
-                String subPath = zipPath + File.separator + f.getName().toLowerCase().replace(".zip", "");
-                ZipHelper.unzip(f.getPath(), subPath);
-                getShpFiles(f.getName(), subPath, list);
-                continue;
+
+            MetaFileEntity mf = createMetaFileEntity(meta);
+            mf.setDircode(dircode);
+            mf.setEventid(StringHelper.getGuid());
+            mf.setName(fileName);
+            mf.setExtName(extName);
+            mf.setPath(f.getPath().substring(start));
+
+            if (isShp) {
+                List<String> shpFiles = getShpFiles(f.getPath());
+                mf.setTab(fileName.replace(StaticData.SHP, ""));
+                mf.setSizes(getFilesSize(shpFiles));
+                mf.setGuid(getFilesMd5(shpFiles));
+            } else {
+                mf.setSizes(FileHelper.sizeToMb(f.length()));
+                mf.setGuid(FileHelper.getFileMd5(f.getPath()));
             }
-            if (fileName.contains(GDB_ZIP)) {
-                String subPath = zipPath + File.separator + f.getName().toLowerCase().replace(".zip", "");
-                ZipHelper.unzip(f.getPath(), subPath);
-                getGdbFiles(f.getName(), subPath, list);
-                continue;
+            list.add(mf);
+        }
+
+        return list;
+    }
+
+    /**
+     * 鍒涘缓鍏冩暟鎹枃浠跺疄浣撶被
+     */
+    private MetaFileEntity createMetaFileEntity(MetaFileEntity meta) {
+        MetaFileEntity mf = new MetaFileEntity();
+        mf.setDircode(meta.getDircode());
+        mf.setDepcode(meta.getDepcode());
+        mf.setVerid(meta.getVerid());
+        mf.setCreateUser(meta.getCreateUser());
+        mf.setEpsgCode(meta.getEpsgCode());
+
+        return mf;
+    }
+
+    /**
+     * 鏍规嵁鏂囦欢璺緞鑾峰彇鐩綍ID
+     */
+    private String getDirByPath(String filePath,String fileName, DirEntity dir, List<DirEntity> dirs) {
+        if (0 != dir.getPid() || null == dirs || dirs.isEmpty()) {
+            return dir.getCode();
+        }
+        if (StaticData.SLASH.equals(File.separator)) {
+            filePath = filePath.replace("/", "\\");
+        }
+
+        for (DirEntity entity : dirs) {
+            if (filePath.contains(entity.getFullName() + "\\" + fileName)) {
+                return entity.getCode();
             }
-            if (fileName.contains(ZIP)) {
-                // 鏆傛椂涓嶅疄鐜�
+        }
+
+        return dir.getCode();
+    }
+
+    /**
+     * 鑾峰彇SHP鏂囦欢闆嗗悎
+     */
+    private List<String> getShpFiles(String shpPath) {
+        List<String> list = new ArrayList<>();
+        list.add(shpPath);
+
+        for (int i = 0, c = StaticData.SHP_EXT.size(); i < c; i++) {
+            String path = shpPath.replace(".shp", StaticData.SHP_EXT.get(i));
+
+            File f = new File(path);
+            if (f.exists() && !f.isDirectory()) {
+                list.add(path);
             }
         }
 
         return list;
     }
 
-    private void getShpFiles(String sourceName, String subPath, List<TabMapperEntity> list) {
-        List<String> files = new ArrayList<>();
-        getShpFiles(subPath, files);
+    /**
+     * 鑾峰彇GDB鏂囦欢闆嗗悎
+     */
+    private List<String> getGdbFiles(String path) {
+        List<String> list = new ArrayList<>();
 
-        String root = pathHelper.getConfig().getTempPath() + File.separator;
-        for (String file : files) {
-            String name = FileHelper.getFileName(file);
-            String path = file.replace(root, "");
-            list.add(new TabMapperEntity(sourceName, "shp", name, path));
-        }
-    }
-
-    private void getShpFiles(String shpPath, List<String> list) {
-        File file = new File(shpPath);
-
-        File[] files = file.listFiles();
+        File[] files = new File(path).listFiles();
         if (null == files || files.length == 0) {
-            return;
+            return list;
         }
-
         for (File f : files) {
-            if (f.isDirectory()) {
-                getShpFiles(f.getPath(), list);
-                continue;
-            }
-
-            if (f.getName().toLowerCase().endsWith(".shp")) {
-                list.add(f.getPath());
-            }
+            list.add(f.getPath());
         }
+
+        return list;
     }
 
-    private void getGdbFiles(String sourceName, String subPath, List<TabMapperEntity> list) {
-        List<String> files = new ArrayList<>();
-        getGdbFiles(subPath, files);
-
-        String root = pathHelper.getConfig().getTempPath() + File.separator;
+    /**
+     * 鑾峰彇澶氭枃浠剁殑MD5
+     */
+    private String getFilesMd5(List<String> files) {
+        List<String> list = new ArrayList<>();
         for (String file : files) {
-            String path = file.replace(root, "");
-            List<String> tabs = GdbHelper.getTabNames(file);
-            for (String tab : tabs) {
-                list.add(new TabMapperEntity(sourceName, "gdb", tab, path));
+            String md5 = FileHelper.getFileMd5(file);
+            if (null != md5) {
+                list.add(md5);
             }
         }
+
+        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, String dircode, 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.setDircode(dircode);
+            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;
+    }
+
+    /**
+     * 鏄�/鍚︿负GDB鏂囦欢
+     */
     private boolean isGdbFile(File f) {
-        if (f.getName().toLowerCase().endsWith(GDB)) {
+        if (f.isDirectory() && f.getName().toLowerCase().endsWith(StaticData.GDB)) {
             File[] files = f.listFiles();
             if (null == files || files.length == 0) {
                 return false;
@@ -324,7 +486,28 @@
     }
 
     /**
-     * 鑾峰彇鍙傛暟
+     * 璁剧疆鍏冩暟鎹枃浠剁殑绫诲瀷
+     */
+    private void setMetaType(List<MetaFileEntity> list) {
+        for (MetaFileEntity mf : list) {
+            if (null != mf.getExtName()) {
+                mf.setType(mf.getExtName().replace(".", ""));
+            }
+        }
+    }
+
+    /**
+     * 澶勭悊鐩綍
+     */
+    public void copePath(List<MetaFileEntity> list) {
+        String basePath = pathHelper.getConfig().getTempPath() + File.separator;
+        for (MetaFileEntity mf : list) {
+            mf.setPath(mf.getPath().replace(basePath, ""));
+        }
+    }
+
+    /**
+     * 鑾峰彇鍙傛暟 *
      * Enumeration<String> headers = req.getHeaderNames();
      * Enumeration<String> attributes = req.getAttributeNames();
      */

--
Gitblit v1.9.3