From 05df28d78537cf07407ed867bc1b79bfd3750fee Mon Sep 17 00:00:00 2001
From: 13693261870 <252740454@qq.com>
Date: 星期一, 03 四月 2023 10:50:28 +0800
Subject: [PATCH] 1

---
 src/main/java/com/lf/server/service/show/DataLibService.java |  353 +++++++++++++++++-----------------------------------------
 1 files changed, 102 insertions(+), 251 deletions(-)

diff --git a/src/main/java/com/lf/server/service/show/DataLibService.java b/src/main/java/com/lf/server/service/show/DataLibService.java
index adf6c3c..0bdeb1c 100644
--- a/src/main/java/com/lf/server/service/show/DataLibService.java
+++ b/src/main/java/com/lf/server/service/show/DataLibService.java
@@ -20,6 +20,7 @@
 import org.springframework.stereotype.Service;
 
 import java.io.File;
+import java.io.FileInputStream;
 import java.util.*;
 
 /**
@@ -59,23 +60,19 @@
     public List<String> selectDbOverflowDep4Wkt(UserEntity ue, DownloadReqEntity dr) {
         List<String> rs = new ArrayList<>();
         for (String entity : dr.getEntities()) {
-            try {
-                GeomBaseMapper<?> baseMapper = ClassHelper.getGeoBaseMapper(entity);
-                if (null == baseMapper) {
-                    continue;
-                }
-
-                QueryWrapper wrapper = getWrapper4DbOverflow(ue, dr);
-                Integer srid = baseQueryService.getSrid(baseMapper);
-                if (null != srid) {
-                    wrapper.apply(String.format("ST_Intersects(ST_PolygonFromText('%s', %d), geom)", dr.getWkt(), srid));
-                }
-
-                List<String> ids = baseMapper.selectObjs(wrapper);
-                addDepCodes(rs, ids);
-            } catch (Exception ex) {
-                log.error(ex.getMessage(), ex);
+            GeomBaseMapper<?> baseMapper = ClassHelper.getGeoBaseMapper(entity);
+            if (null == baseMapper) {
+                continue;
             }
+
+            QueryWrapper wrapper = getWrapper4DbOverflow(ue, dr);
+            Integer srid = baseQueryService.getSrid(baseMapper);
+            if (null != srid) {
+                wrapper.apply(String.format("ST_Intersects(ST_PolygonFromText('%s', %d), geom)", dr.getWkt(), srid));
+            }
+
+            List<String> ids = baseMapper.selectObjs(wrapper);
+            addDepCodes(rs, ids);
         }
 
         return rs;
@@ -86,19 +83,14 @@
      */
     public List<String> selectDbOverflowDep4Prop(UserEntity ue, DownloadReqEntity dr) {
         List<String> rs = new ArrayList<>();
-        try {
-            BasicMapper<?> baseMapper = ClassHelper.getBasicMapper(dr.getEntities().get(0));
-            if (null == baseMapper) {
-                return rs;
-            }
-
-            QueryWrapper wrapper = getWrapper4DbOverflow(ue, dr);
-
-            List<String> ids = baseMapper.selectObjs(wrapper);
-            addDepCodes(rs, ids);
-        } catch (Exception ex) {
-            log.error(ex.getMessage(), ex);
+        BasicMapper<?> baseMapper = ClassHelper.getBasicMapper(dr.getEntities().get(0));
+        if (null == baseMapper) {
+            return rs;
         }
+
+        QueryWrapper wrapper = getWrapper4DbOverflow(ue, dr);
+        List<String> ids = baseMapper.selectObjs(wrapper);
+        addDepCodes(rs, ids);
 
         return rs;
     }
@@ -150,25 +142,45 @@
     /**
      * 璇锋眰DB鏁版嵁涓嬭浇
      */
-    public String downloadDbReq(UserEntity ue, DownloadReqEntity dr) {
+    public String downloadDbReq(UserEntity ue, DownloadReqEntity dr) throws Exception {
         Map<String, List<?>> dataMap = new HashMap<>(2);
         Map<String, List<AttachEntity>> annexMap = new HashMap<>(2);
-
         queryData(dr, dataMap, annexMap);
         if (dataMap.size() == 0) {
             return null;
         }
 
-        return null;
+        String tempName = StringHelper.YMDHMS2_FORMAT.format(new Date());
+        String tempPath = pathHelper.getTempPath(tempName);
+        String gdbPath = tempPath + File.separator + "tabs.gdb";
+
+        File gdbFile = new File(gdbPath);
+        if (gdbFile.exists() && gdbFile.isDirectory()) {
+            FileHelper.deleteDir(gdbPath);
+        }
+        GdbHelper.createGdb(gdbPath, dataMap);
+
+        String zipFile = pathHelper.getDownloadFullPath() + File.separator + tempName + ".gdb.zip";
+        ZipFile zip = Zip4jHelper.createZipFile(zipFile, dr.getPwd());
+        ZipParameters params = Zip4jHelper.getZipParams(true);
+        zip.addFolder(new File(gdbPath), params);
+        // zip.addFolder(new File(annexPath), params)
+        addAnnex(zip, params, annexMap);
+
+        String dbPwd = Md5Helper.reverse(Md5Helper.generate(dr.getPwd()));
+        DownloadEntity downloadEntity = getDownloadEntity(ue, zipFile, dbPwd);
+        int rows = downloadMapper.insert(downloadEntity);
+
+        return rows > 0 ? downloadEntity.getGuid() : null;
     }
 
     /**
      * 鏌ヨ鏁版嵁+闄勪欢
      */
     private void queryData(DownloadReqEntity dr, Map<String, List<?>> dataMap, Map<String, List<AttachEntity>> annexMap) {
-        for (String enity : dr.getEntities()) {
+        for (String entity : dr.getEntities()) {
             try {
-                BasicMapper baseMapper = ClassHelper.getBasicMapper(enity);
+                BasicMapper baseMapper = ClassHelper.getBasicMapper(entity);
                 if (null == baseMapper) {
                     continue;
                 }
@@ -179,25 +191,28 @@
                     continue;
                 }
 
-                String tab = BaseQueryService.getTabName(baseMapper);
-                if (!dataMap.containsKey(enity)) {
-                    dataMap.put(enity, list);
+                if (!dataMap.containsKey(entity)) {
+                    dataMap.put(entity, list);
                 } else {
-                    dataMap.get(enity).addAll(list);
+                    dataMap.get(entity).addAll(list);
                 }
                 if (wrapper.isEmptyOfWhere()) {
                     wrapper.apply("1 = 1");
                 }
+                if ("bborehole".equals(entity)) {
+                    wrapper.last("limit 100");
+                }
 
+                String tab = BaseQueryService.getTabName(baseMapper);
                 List<AttachEntity> annex = baseMapper.selectAnnex(tab, wrapper);
                 if (null == annex || annex.isEmpty()) {
                     continue;
                 }
 
-                if (!annexMap.containsKey(enity)) {
-                    annexMap.put(enity, annex);
+                if (!annexMap.containsKey(entity)) {
+                    annexMap.put(tab.replace(".", "_"), annex);
                 } else {
-                    annexMap.get(enity).addAll(annex);
+                    annexMap.get(tab.replace(".", "_")).addAll(annex);
                 }
             } catch (Exception ex) {
                 log.error(ex.getMessage(), ex);
@@ -236,92 +251,66 @@
     }
 
     /**
-     * 璇锋眰DB鏁版嵁涓嬭浇-绌洪棿鏌ヨ
+     * 鍒涘缓闄勪欢 *
      */
-    public String downloadDbReq4Wkt(UserEntity ue, DownloadReqEntity dr) throws Exception {
-        String depcode = null == dr.getDepcodes() || dr.getDepcodes().isEmpty() ? null : dr.getDepcodes().get(0);
-        Map<String, List<?>> map = queryData(dr.getEntities(), depcode, dr.getDirs(), dr.getWkt());
-        if (map.size() == 0) {
-            return null;
+    private void createAnnex(String annexPath, Map<String, List<AttachEntity>> annexMap) {
+        if (annexMap.size() == 0) {
+            return;
         }
 
-        String tempName = StringHelper.YMDHMS2_FORMAT.format(new Date());
-        String tempPath = pathHelper.getTempPath(tempName);
-        // String filePath = "D:\\LF\\temp\\20221219202706\\2022.gdb"
-        String filePath = tempPath + File.separator + tempName + ".gdb";
+        String uploadPath = pathHelper.getConfig().getUploadPath();
+        for (String key : annexMap.keySet()) {
+            String targetPath = annexPath + File.separator + key;
+            File targetFile = new File(targetPath);
+            if (!targetFile.exists() || !targetFile.isDirectory()) {
+                targetFile.mkdirs();
+            }
 
-        File file = new File(filePath);
-        if (file.exists() && file.isDirectory()) {
-            FileHelper.deleteDir(filePath);
+            for (AttachEntity ae : annexMap.get(key)) {
+                try {
+                    File srcFile = new File(uploadPath + File.separator + ae.getPath());
+                    if (!srcFile.exists() || srcFile.isDirectory()) {
+                        continue;
+                    }
+
+                    File destFile = new File(targetPath + File.separator + ae.getName());
+                    if (destFile.exists() && !destFile.isDirectory()) {
+                        continue;
+                    }
+
+                    FileHelper.copyFile(srcFile, destFile);
+                } catch (Exception ex) {
+                    log.error(ex.getMessage(), ex);
+                }
+            }
         }
-        GdbHelper.createGdb(filePath, map);
-
-        String zipName = tempName + ".gdb.zip";
-        String zipFile = pathHelper.getDownloadFullPath() + File.separator + zipName;
-
-        ZipFile zip = Zip4jHelper.createZipFile(zipFile, dr.getPwd());
-        ZipParameters params = Zip4jHelper.getZipParams(true);
-        addZipFiles(zip, params, file.listFiles());
-
-        String dbPwd = Md5Helper.reverse(Md5Helper.generate(dr.getPwd()));
-        DownloadEntity downloadEntity = getDownloadEntity(ue, zipFile, dbPwd);
-        int rows = downloadMapper.insert(downloadEntity);
-
-        return rows > 0 ? downloadEntity.getGuid() : null;
     }
 
     /**
-     * 鏌ヨ鏁版嵁
+     * 娣诲姞闄勪欢
      */
-    private Map<String, List<?>> queryData(List<String> entities, String depcode, String dirs, String wkt) {
-        Map<String, List<?>> map = new HashMap<>(5);
-        for (String enity : entities) {
-            try {
-                GeomBaseMapper<?> baseMapper = ClassHelper.getGeoBaseMapper(enity);
-                if (null == baseMapper) {
-                    continue;
-                }
+    private void addAnnex(ZipFile zip, ZipParameters params, Map<String, List<AttachEntity>> annexMap) {
+        List<String> files = new ArrayList<>();
+        String uploadPath = pathHelper.getConfig().getUploadPath();
+        for (String key : annexMap.keySet()) {
+            for (AttachEntity ae : annexMap.get(key)) {
+                try {
+                    File srcFile = new File(uploadPath + File.separator + ae.getPath());
+                    if (!srcFile.exists() || srcFile.isDirectory()) {
+                        continue;
+                    }
+                    if (files.contains(srcFile.getPath())) {
+                        continue;
+                    }
 
-                QueryWrapper wrapper = createWrapper(baseMapper, depcode, dirs, wkt);
-                List<?> list = baseMapper.selectList(wrapper);
-                if (null == list || list.size() == 0) {
-                    continue;
+                    files.add(srcFile.getPath());
+                    params.setFileNameInZip("annex" + File.separator + key + File.separator + ae.getName());
+                    zip.addStream(new FileInputStream(srcFile), params);
+                } catch (Exception ex) {
+                    log.error(ex.getMessage(), ex);
                 }
-
-                if (!map.containsKey(enity)) {
-                    map.put(enity, list);
-                }
-            } catch (Exception ex) {
-                log.error(ex.getMessage(), ex);
             }
         }
-
-        return map;
-    }
-
-    /**
-     * 鍒涘缓QueryWrapper
-     */
-    private QueryWrapper createWrapper(BasicMapper baseMapper, String depcode, String dirs, String wkt) {
-        QueryWrapper wrapper = new QueryWrapper();
-        if (baseMapper instanceof GeomBaseMapper) {
-            wrapper.select("ST_AsText(geom) as geom, *");
-
-            Integer srid = baseQueryService.getSrid((GeomBaseMapper) baseMapper);
-            if (null != srid) {
-                wrapper.apply(String.format("ST_Intersects(ST_PolygonFromText('%s', %d), geom)", wkt, srid));
-            }
-        }
-        if (!StringHelper.isEmpty(depcode)) {
-            wrapper.likeRight("depid", depcode);
-        }
-
-        dirs = DataLibService.copeCodes(dirs, "dirid");
-        if (!StringHelper.isEmpty(dirs)) {
-            wrapper.apply(dirs);
-        }
-
-        return wrapper;
     }
 
     /**
@@ -343,85 +332,6 @@
         // de.setGeom(null)
 
         return de;
-    }
-
-    /**
-     * 璇锋眰DB鏁版嵁涓嬭浇-灞炴�ф煡璇�
-     */
-    public String downloadDbReq4Prop(UserEntity ue, DownloadReqEntity dr) throws Exception {
-        String dirs = dr.getDirs();
-        String entity = dr.getEntities().get(0);
-        String depcode = null == dr.getDepcodes() || dr.getDepcodes().isEmpty() ? null : dr.getDepcodes().get(0);
-
-        BasicMapper baseMapper = ClassHelper.getBasicMapper(entity);
-        if (baseMapper == null) {
-            return null;
-        }
-
-        QueryWrapper wrapper = new QueryWrapper();
-        baseQueryService.addFilterWrapper(wrapper, dr.getFilter());
-        if (baseMapper instanceof GeomBaseMapper) {
-            wrapper.select("ST_AsText(geom) as geom, *");
-        }
-        if (!StringHelper.isEmpty(depcode)) {
-            wrapper.likeRight("depid", depcode);
-        }
-
-        dirs = DataLibService.copeCodes(dirs, "dirid");
-        if (!StringHelper.isEmpty(dirs)) {
-            wrapper.apply(dirs);
-        }
-
-        List<?> list = baseMapper.selectList(wrapper);
-        if (null == list || 0 == list.size()) {
-            return null;
-        }
-
-        String tab = BaseQueryService.getTabName(baseMapper);
-        String ids = getAnnexFilter(StringHelper.isEmpty(depcode) && StringHelper.isEmpty(dirs) && StringHelper.isEmpty(dr.getFilter()), list);
-        List<AttachEntity> annex = baseQueryService.selectAnnexByTab(tab, ids);
-
-        Map<String, List<?>> map = new HashMap<>(1);
-        map.put(entity, list);
-
-        return zipData(ue, map, annex, dr.getPwd());
-    }
-
-    /**
-     * 鎵撳寘鏁版嵁
-     */
-    private String zipData(UserEntity ue, Map<String, List<?>> map, List<AttachEntity> annex, String pwd) throws Exception {
-        String tempName = StringHelper.YMDHMS2_FORMAT.format(new Date());
-        String tempPath = pathHelper.getTempPath(tempName);
-        String gdbPath = tempPath + File.separator + tempName + ".gdb";
-        String annexPath = tempPath + File.separator + "annex";
-
-        File file = new File(gdbPath);
-        if (file.exists() && file.isDirectory()) {
-            FileHelper.deleteDir(gdbPath);
-        }
-        file = new File(annexPath);
-        if (!file.exists() || !file.isDirectory()) {
-            file.mkdirs();
-        }
-
-        GdbHelper.createGdb(gdbPath, map);
-        createAnnex(annexPath, annex);
-
-        String zipName = tempName + ".gdb.zip";
-        String zipFile = pathHelper.getDownloadFullPath() + File.separator + zipName;
-
-        ZipFile zip = Zip4jHelper.createZipFile(zipFile, pwd);
-        ZipParameters params = Zip4jHelper.getZipParams(true);
-        // addZipFiles(zip, params, file.listFiles())
-        zip.addFolder(new File(gdbPath), params);
-        zip.addFolder(new File(annexPath), params);
-
-        String dbPwd = Md5Helper.reverse(Md5Helper.generate(pwd));
-        DownloadEntity downloadEntity = getDownloadEntity(ue, zipFile, dbPwd);
-        int rows = downloadMapper.insert(downloadEntity);
-
-        return rows > 0 ? downloadEntity.getGuid() : null;
     }
 
     /**
@@ -489,65 +399,6 @@
         for (int i = 0, c = list.size(); i < c; i++) {
             String str = String.format("%s like '%s%%'", field, list.get(i));
             list.set(i, str);
-        }
-    }
-
-    /**
-     * 娣诲姞Zip鏂囦欢
-     */
-    private void addZipFiles(ZipFile zip, ZipParameters params, File[] files) {
-        if (null == files || files.length == 0) {
-            return;
-        }
-
-        for (File f : files) {
-            try {
-                zip.addFile(f, params);
-            } catch (Exception ex) {
-                log.error(ex.getMessage(), ex);
-            }
-        }
-    }
-
-    /**
-     * 鑾峰彇鏌ヨ闄勪欢鏉′欢
-     */
-    private String getAnnexFilter(boolean flag, List<?> list) {
-        if (flag) {
-            return null;
-        }
-
-        List<Integer> gids = new ArrayList<>();
-        for (Object obj : list) {
-            BaseEntity be = (BaseEntity) obj;
-            gids.add(be.getGid());
-        }
-
-        return StringHelper.join(gids, ",");
-    }
-
-    /**
-     * 娣诲姞闄勪欢鏂囦欢
-     */
-    private void createAnnex(String targetPath, List<AttachEntity> list) {
-        if (null == list || list.isEmpty()) {
-            return;
-        }
-
-        int i = 1;
-        String uploadPath = pathHelper.getConfig().getUploadPath();
-        for (AttachEntity attach : list) {
-            try {
-                File srcFile = new File(uploadPath + File.separator + attach.getPath());
-                if (!srcFile.exists() || srcFile.isDirectory()) {
-                    continue;
-                }
-
-                File destFile = new File(targetPath + File.separator + i++ + "_" + attach.getName());
-                FileHelper.copyFile(srcFile, destFile);
-            } catch (Exception ex) {
-                log.error(ex.getMessage(), ex);
-            }
         }
     }
 }

--
Gitblit v1.9.3