From 71726a1cb05790d6841595ef7daef5173f2cddfa Mon Sep 17 00:00:00 2001
From: 13693261870 <252740454@qq.com>
Date: 星期三, 29 三月 2023 09:43:11 +0800
Subject: [PATCH] 1

---
 src/main/java/com/lf/server/service/show/DataLibService.java |  467 +++++++++++++++++++++++++++++++++++++++++++++++----------
 1 files changed, 380 insertions(+), 87 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 e54acfa..adf6c3c 100644
--- a/src/main/java/com/lf/server/service/show/DataLibService.java
+++ b/src/main/java/com/lf/server/service/show/DataLibService.java
@@ -1,7 +1,10 @@
 package com.lf.server.service.show;
 
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.lf.server.entity.all.BaseEntity;
+import com.lf.server.entity.ctrl.DownloadReqEntity;
 import com.lf.server.entity.data.DownloadEntity;
+import com.lf.server.entity.sys.AttachEntity;
 import com.lf.server.entity.sys.UserEntity;
 import com.lf.server.helper.*;
 import com.lf.server.mapper.all.BasicMapper;
@@ -40,31 +43,36 @@
     private final static Log log = LogFactory.getLog(DataLibService.class);
 
     /**
-     * 鏌ヨDB涓孩鍑虹殑鍗曚綅ID
+     * 鏌ヨDB涓孩鍑虹殑鍗曚綅缂栫爜
      */
-    public List<String> selectDbOverflowDep(UserEntity ue, List<String> entities, String wkt) {
+    public List<String> selectDbOverflowDep(UserEntity ue, DownloadReqEntity dr) {
+        if (!StringHelper.isEmpty(dr.getWkt())) {
+            return selectDbOverflowDep4Wkt(ue, dr);
+        }
+
+        return selectDbOverflowDep4Prop(ue, dr);
+    }
+
+    /**
+     * 鏌ヨDB涓孩鍑虹殑鍗曚綅缂栫爜-绌洪棿鏌ヨ
+     */
+    public List<String> selectDbOverflowDep4Wkt(UserEntity ue, DownloadReqEntity dr) {
         List<String> rs = new ArrayList<>();
-        for (String enity : entities) {
+        for (String entity : dr.getEntities()) {
             try {
-                GeomBaseMapper<?> baseMapper = ClassHelper.getGeoBaseMapper(enity);
+                GeomBaseMapper<?> baseMapper = ClassHelper.getGeoBaseMapper(entity);
                 if (null == baseMapper) {
                     continue;
                 }
 
-                QueryWrapper wrapper = new QueryWrapper();
-                wrapper.select("depid");
-                // wrapper.gt("depid", 0); wrapper.apply(String.format("depid != ALL(fn_rec_array(%d, 'dep'))", ue.getDepid()))
-                wrapper.apply("depid is not null and depid not like '" + ue.getDepcode() + "%'");
-                wrapper.groupBy("depid");
-
+                QueryWrapper wrapper = getWrapper4DbOverflow(ue, dr);
                 Integer srid = baseQueryService.getSrid(baseMapper);
                 if (null != srid) {
-                    wrapper.apply(String.format("ST_Intersects(ST_PolygonFromText('%s', %d), geom)", wkt, srid));
+                    wrapper.apply(String.format("ST_Intersects(ST_PolygonFromText('%s', %d), geom)", dr.getWkt(), srid));
                 }
 
                 List<String> ids = baseMapper.selectObjs(wrapper);
-
-                addDepIds(rs, ids);
+                addDepCodes(rs, ids);
             } catch (Exception ex) {
                 log.error(ex.getMessage(), ex);
             }
@@ -74,14 +82,65 @@
     }
 
     /**
-     * 娣诲姞鍗曚綅ID
+     * 鏌ヨDB涓孩鍑虹殑鍗曚綅缂栫爜-灞炴�ф煡璇�
      */
-    private void addDepIds(List<String> rs, List<String> ids) {
+    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);
+        }
+
+        return rs;
+    }
+
+    /**
+     * 鏌ヨDB涓孩鍑哄崟浣嶇紪鐮佺殑鏌ヨ鍖呰鍣�
+     */
+    private <T> QueryWrapper<T> getWrapper4DbOverflow(UserEntity ue, DownloadReqEntity dr) {
+        QueryWrapper<T> wrapper = new QueryWrapper<T>();
+        wrapper.select("depid");
+        wrapper.groupBy("depid");
+        wrapper.apply("depid is not null and depid not like '" + ue.getDepcode() + "%'");
+
+        if (null != dr.getIds() && dr.getIds().size() > 0) {
+            wrapper.apply(String.format("gid in (%s)", StringHelper.join(dr.getIds(), ",")));
+        } else {
+            baseQueryService.addFilterWrapper(wrapper, dr.getFilter());
+        }
+
+        String dirs = copeCodes(dr.getDirs(), "dirid");
+        if (!StringHelper.isEmpty(dirs)) {
+            wrapper.apply(dirs);
+        }
+        if (!StringHelper.isEmpty(dr.getDepcode())) {
+            wrapper.likeRight("depid", dr.getDepcode());
+        }
+
+        return wrapper;
+    }
+
+    /**
+     * 娣诲姞鍗曚綅缂栫爜
+     */
+    private void addDepCodes(List<String> rs, List<String> ids) {
         if (null == ids || ids.isEmpty()) {
             return;
         }
 
         for (String id : ids) {
+            if (StringHelper.isEmpty(id)) {
+                continue;
+            }
             if (!rs.contains(id)) {
                 rs.add(id);
             }
@@ -89,10 +148,99 @@
     }
 
     /**
-     * 鍒涘缓Zip鍖�
+     * 璇锋眰DB鏁版嵁涓嬭浇
      */
-    public String createZipFile(UserEntity ue, List<String> entities, String wkt, String pwd) throws Exception {
-        Map<String, List<?>> map = queryData(entities, wkt);
+    public String downloadDbReq(UserEntity ue, DownloadReqEntity dr) {
+        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;
+    }
+
+    /**
+     * 鏌ヨ鏁版嵁+闄勪欢
+     */
+    private void queryData(DownloadReqEntity dr, Map<String, List<?>> dataMap, Map<String, List<AttachEntity>> annexMap) {
+        for (String enity : dr.getEntities()) {
+            try {
+                BasicMapper baseMapper = ClassHelper.getBasicMapper(enity);
+                if (null == baseMapper) {
+                    continue;
+                }
+
+                QueryWrapper wrapper = createQueryWrapper(baseMapper, dr);
+                List list = baseMapper.selectList(wrapper);
+                if (null == list || list.size() == 0) {
+                    continue;
+                }
+
+                String tab = BaseQueryService.getTabName(baseMapper);
+                if (!dataMap.containsKey(enity)) {
+                    dataMap.put(enity, list);
+                } else {
+                    dataMap.get(enity).addAll(list);
+                }
+                if (wrapper.isEmptyOfWhere()) {
+                    wrapper.apply("1 = 1");
+                }
+
+                List<AttachEntity> annex = baseMapper.selectAnnex(tab, wrapper);
+                if (null == annex || annex.isEmpty()) {
+                    continue;
+                }
+
+                if (!annexMap.containsKey(enity)) {
+                    annexMap.put(enity, annex);
+                } else {
+                    annexMap.get(enity).addAll(annex);
+                }
+            } catch (Exception ex) {
+                log.error(ex.getMessage(), ex);
+            }
+        }
+    }
+
+    /**
+     * 鍒涘缓鏌ヨ鍖呰鍣�
+     */
+    private <T> QueryWrapper<T> createQueryWrapper(BasicMapper baseMapper, DownloadReqEntity dr) {
+        QueryWrapper<T> wrapper = new QueryWrapper<T>();
+
+        String dirs = copeCodes(dr.getDirs(), "dirid");
+        if (!StringHelper.isEmpty(dirs)) {
+            wrapper.apply(dirs);
+        }
+        if (!StringHelper.isEmpty(dr.getDepcode())) {
+            // wrapper.apply(String.format("depid like '%s'", StringHelper.getRightLike(dr.getDepcode())))
+            wrapper.likeRight("depid", dr.getDepcode());
+        }
+        if (baseMapper instanceof GeomBaseMapper && !StringHelper.isEmpty(dr.getWkt())) {
+            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)", dr.getWkt(), srid));
+            }
+        }
+        if (null != dr.getIds() && dr.getIds().size() > 0) {
+            wrapper.apply(String.format("gid in (%s)", StringHelper.join(dr.getIds(), ",")));
+        } else {
+            baseQueryService.addFilterWrapper(wrapper, dr.getFilter());
+        }
+
+        return wrapper;
+    }
+
+    /**
+     * 璇锋眰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;
         }
@@ -111,11 +259,11 @@
         String zipName = tempName + ".gdb.zip";
         String zipFile = pathHelper.getDownloadFullPath() + File.separator + zipName;
 
-        ZipFile zip = Zip4jHelper.createZipFile(zipFile, pwd);
-        ZipParameters params = Zip4jHelper.getZipParams();
+        ZipFile zip = Zip4jHelper.createZipFile(zipFile, dr.getPwd());
+        ZipParameters params = Zip4jHelper.getZipParams(true);
         addZipFiles(zip, params, file.listFiles());
 
-        String dbPwd = Md5Helper.reverse(Md5Helper.generate(pwd));
+        String dbPwd = Md5Helper.reverse(Md5Helper.generate(dr.getPwd()));
         DownloadEntity downloadEntity = getDownloadEntity(ue, zipFile, dbPwd);
         int rows = downloadMapper.insert(downloadEntity);
 
@@ -125,7 +273,7 @@
     /**
      * 鏌ヨ鏁版嵁
      */
-    private Map<String, List<?>> queryData(List<String> entities, String wkt) {
+    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 {
@@ -134,7 +282,7 @@
                     continue;
                 }
 
-                QueryWrapper wrapper = createWrapper(baseMapper, wkt);
+                QueryWrapper wrapper = createWrapper(baseMapper, depcode, dirs, wkt);
                 List<?> list = baseMapper.selectList(wrapper);
                 if (null == list || list.size() == 0) {
                     continue;
@@ -154,16 +302,194 @@
     /**
      * 鍒涘缓QueryWrapper
      */
-    private QueryWrapper createWrapper(GeomBaseMapper baseMapper, String wkt) {
+    private QueryWrapper createWrapper(BasicMapper baseMapper, String depcode, String dirs, String wkt) {
         QueryWrapper wrapper = new QueryWrapper();
-        wrapper.select("ST_AsText(geom) as geom, *");
+        if (baseMapper instanceof GeomBaseMapper) {
+            wrapper.select("ST_AsText(geom) as geom, *");
 
-        Integer srid = baseQueryService.getSrid(baseMapper);
-        if (null != srid) {
-            wrapper.apply(String.format("ST_Intersects(ST_PolygonFromText('%s', %d), geom)", wkt, srid));
+            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;
+    }
+
+    /**
+     * 鑾峰彇涓嬭浇瀹炰綋绫�
+     */
+    private DownloadEntity getDownloadEntity(UserEntity ue, String file, String pwd) {
+        DownloadEntity de = new DownloadEntity();
+        de.setName(FileHelper.getFileName(file));
+        // 1-Shp鏂囦欢锛�2-涓撻鍥撅紝3-鍏冩暟鎹紝4-涓氬姟鏁版嵁锛�5-绠¢亾鍒嗘瀽锛�6-缁熻鎶ュ憡
+        de.setType(4);
+        de.setSizes(FileHelper.sizeToMb(new File(file).length()));
+        de.setDepid(ue.getDepid());
+        de.setDcount(0);
+        de.setPwd(pwd);
+        de.setUrl(FileHelper.getRelativePath(file));
+        de.setDescr("涓氬姟鏁版嵁鏂囦欢");
+        de.setGuid(FileHelper.getFileMd5(file));
+        de.setCreateUser(ue.getId());
+        // 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;
+    }
+
+    /**
+     * 澶勭悊缂栫爜
+     */
+    public static String copeCodes(String codes, String field) {
+        if (StringHelper.isEmpty(codes) || StringHelper.isSqlInjection(codes)) {
+            return null;
+        }
+
+        List<String> list = codesAsList(codes);
+        removeDuplicate(list);
+        setRightLike(list, field);
+
+        return "(" + StringHelper.join(list, " or ") + ")";
+    }
+
+    /**
+     * 鍗曚綅缂栫爜杞泦鍚�
+     */
+    private static List<String> codesAsList(String codes) {
+        List<String> list = Arrays.asList(codes.split(","));
+        Set set = new HashSet(list);
+
+        List<String> newList = new ArrayList<>(set);
+        Collections.sort(newList);
+
+        return newList;
+    }
+
+    /**
+     * 鍘婚櫎閲嶅
+     */
+    private static void removeDuplicate(List<String> list) {
+        int i = 0;
+        while (i < list.size()) {
+            int j = findStr(list, i);
+            if (j > -1) {
+                list.remove(j);
+                continue;
+            }
+
+            i++;
+        }
+    }
+
+    /**
+     * 鏌ユ壘瀛楃涓�
+     */
+    private static int findStr(List<String> list, int i) {
+        String source = list.get(i);
+        for (int j = i + 1, c = list.size(); j < c; j++) {
+            if (list.get(j).startsWith(source)) {
+                return j;
+            }
+        }
+
+        return -1;
+    }
+
+    /**
+     * 璁剧疆 鍙砽ike
+     */
+    private static void setRightLike(List<String> list, String field) {
+        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);
+        }
     }
 
     /**
@@ -184,77 +510,44 @@
     }
 
     /**
-     * 鑾峰彇涓嬭浇瀹炰綋绫�
+     * 鑾峰彇鏌ヨ闄勪欢鏉′欢
      */
-    private DownloadEntity getDownloadEntity(UserEntity ue, String file, String pwd) throws Exception {
-        DownloadEntity de = new DownloadEntity();
-        de.setName(FileHelper.getFileName(file));
-        // 1-Shp鏂囦欢锛�2-涓撻鍥撅紝3-鍏冩暟鎹紝4-涓氬姟鏁版嵁
-        de.setType(4);
-        de.setSizes(FileHelper.sizeToMb(new File(file).length()));
-        de.setDepid(ue.getDepid());
-        de.setDcount(0);
-        de.setPwd(pwd);
-        de.setUrl(FileHelper.getRelativePath(file));
-        de.setDescr("涓氬姟鏁版嵁鏂囦欢");
-        de.setGuid(FileHelper.getFileMd5(file));
-        de.setCreateUser(ue.getId());
-        // de.setGeom(null)
-
-        return de;
-    }
-
-    /**
-     * 鎵撳寘DB鏁版嵁
-     */
-    public String zipDbData(UserEntity ue, String name, String filter, String pwd) throws Exception {
-        BasicMapper baseMapper = ClassHelper.getBasicMapper(name);
-        if (baseMapper == null) {
+    private String getAnnexFilter(boolean flag, List<?> list) {
+        if (flag) {
             return null;
         }
 
-        QueryWrapper wrapper = new QueryWrapper();
-        baseQueryService.addFilterWrapper(wrapper, filter);
-        if (baseMapper instanceof GeomBaseMapper) {
-            wrapper.select("ST_AsText(geom) as geom, *");
+        List<Integer> gids = new ArrayList<>();
+        for (Object obj : list) {
+            BaseEntity be = (BaseEntity) obj;
+            gids.add(be.getGid());
         }
 
-        List<?> list = baseMapper.selectList(wrapper);
-        if (null == list || 0 == list.size()) {
-            return null;
-        }
-
-        Map<String, List<?>> map = new HashMap<>(1);
-        map.put(name, list);
-
-        return zipData(ue, map, pwd);
+        return StringHelper.join(gids, ",");
     }
 
     /**
-     * 鎵撳寘鏁版嵁
+     * 娣诲姞闄勪欢鏂囦欢
      */
-    private String zipData(UserEntity ue, Map<String, List<?>> map, String pwd) throws Exception {
-        String tempName = StringHelper.YMDHMS2_FORMAT.format(new Date());
-        String tempPath = pathHelper.getTempPath(tempName);
-        String filePath = tempPath + File.separator + tempName + ".gdb";
-
-        File file = new File(filePath);
-        if (file.exists() && file.isDirectory()) {
-            FileHelper.deleteDir(filePath);
+    private void createAnnex(String targetPath, List<AttachEntity> list) {
+        if (null == list || list.isEmpty()) {
+            return;
         }
-        GdbHelper.createGdb(filePath, map);
 
-        String zipName = tempName + ".gdb.zip";
-        String zipFile = pathHelper.getDownloadFullPath() + File.separator + zipName;
+        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;
+                }
 
-        ZipFile zip = Zip4jHelper.createZipFile(zipFile, pwd);
-        ZipParameters params = Zip4jHelper.getZipParams();
-        addZipFiles(zip, params, file.listFiles());
-
-        String dbPwd = Md5Helper.reverse(Md5Helper.generate(pwd));
-        DownloadEntity downloadEntity = getDownloadEntity(ue, zipFile, dbPwd);
-        int rows = downloadMapper.insert(downloadEntity);
-
-        return rows > 0 ? downloadEntity.getGuid() : null;
+                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