From ed8c7a5effd0d423ce1118b680ecdca6fe732609 Mon Sep 17 00:00:00 2001
From: 13693261870 <252740454@qq.com>
Date: 星期三, 02 七月 2025 16:43:13 +0800
Subject: [PATCH] Merge branch 'master' of http://192.168.11.205:9000/r/P2022036_Service

---
 src/main/java/com/lf/server/service/show/DataLibService.java |  434 ++++++++++++++++++++++++++++++++----------------------
 1 files changed, 258 insertions(+), 176 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 39a578b..5abe6ac 100644
--- a/src/main/java/com/lf/server/service/show/DataLibService.java
+++ b/src/main/java/com/lf/server/service/show/DataLibService.java
@@ -1,20 +1,24 @@
 package com.lf.server.service.show;
 
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.lf.server.entity.ctrl.DownloadReqEntity;
+import com.lf.server.entity.ctrl.KeyValueEntity;
 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;
 import com.lf.server.mapper.all.GeomBaseMapper;
 import com.lf.server.mapper.data.DownloadMapper;
 import com.lf.server.service.all.BaseQueryService;
-import com.lf.server.service.data.DownloadService;
+import com.lf.server.service.data.MetaService;
 import net.lingala.zip4j.ZipFile;
 import net.lingala.zip4j.model.ZipParameters;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
+import org.springframework.util.StringUtils;
 
 import java.io.File;
 import java.util.*;
@@ -24,15 +28,16 @@
  * @author WWW
  */
 @Service
+@SuppressWarnings("ALL")
 public class DataLibService {
     @Autowired
     PathHelper pathHelper;
 
     @Autowired
-    DownloadMapper downloadMapper;
+    MetaService metaService;
 
     @Autowired
-    DownloadService downloadService;
+    DownloadMapper downloadMapper;
 
     @Autowired
     BaseQueryService baseQueryService;
@@ -42,35 +47,77 @@
     /**
      * 鏌ヨ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) {
-            try {
-                GeomBaseMapper<?> baseMapper = ClassHelper.getGeoBaseMapper(enity);
-                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");
-
-                Integer srid = baseQueryService.getSrid(baseMapper);
-                if (null != srid) {
-                    wrapper.apply(String.format("ST_Intersects(ST_PolygonFromText('%s', %d), geom)", wkt, srid));
-                }
-
-                List<String> ids = baseMapper.selectObjs(wrapper);
-
-                addDepCodes(rs, ids);
-            } catch (Exception ex) {
-                log.error(ex.getMessage(), ex);
+        for (String entity : dr.getEntities()) {
+            GeomBaseMapper<?> baseMapper = ClassHelper.getGeoBaseMapper(entity);
+            if (null == baseMapper) {
+                continue;
             }
+
+            QueryWrapper wrapper = getWrapper4DbOverflow(ue, dr);
+            Integer srid = baseQueryService.getSrid(baseMapper);
+            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;
+    }
+
+    /**
+     * 鏌ヨDB涓孩鍑虹殑鍗曚綅缂栫爜-灞炴�ф煡璇�
+     */
+    public List<String> selectDbOverflowDep4Prop(UserEntity ue, DownloadReqEntity dr) {
+        List<String> rs = new ArrayList<>();
+        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;
+    }
+
+    /**
+     * 鏌ヨ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;
     }
 
     /**
@@ -82,6 +129,9 @@
         }
 
         for (String id : ids) {
+            if (StringHelper.isEmpty(id)) {
+                continue;
+            }
             if (!rs.contains(id)) {
                 rs.add(id);
             }
@@ -89,106 +139,121 @@
     }
 
     /**
-     * 鍒涘缓Zip鍖�
+     * 璇锋眰DB鏁版嵁涓嬭浇
      */
-    public String createZipFile(UserEntity ue, List<String> entities, String depcode, String dirs, String wkt, String pwd) throws Exception {
-        Map<String, List<?>> map = queryData(entities, depcode, dirs, wkt);
-        if (map.size() == 0) {
+    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;
         }
 
         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 gdbPath = tempPath + File.separator + "tabs.gdb";
 
-        File file = new File(filePath);
-        if (file.exists() && file.isDirectory()) {
-            FileHelper.deleteDir(filePath);
+        File gdbFile = new File(gdbPath);
+        if (gdbFile.exists() && gdbFile.isDirectory()) {
+            FileHelper.deleteDir(gdbPath);
         }
-        GdbHelper.createGdb(filePath, map);
+        GdbHelper.createGdb(gdbPath, dataMap);
 
-        String zipName = tempName + ".gdb.zip";
-        String zipFile = pathHelper.getDownloadFullPath() + File.separator + zipName;
+        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)
+        metaService.addAnnex(zip, params, annexMap);
 
-        ZipFile zip = Zip4jHelper.createZipFile(zipFile, pwd);
-        ZipParameters params = Zip4jHelper.getZipParams();
-        addZipFiles(zip, params, file.listFiles());
+        String dbPwd = Md5Helper.reverse(Md5Helper.generate(dr.getPwd()));
+        DownloadEntity de = getDownloadEntity(ue, zipFile, dbPwd);
+        int rows = downloadMapper.insert(de);
 
-        String dbPwd = Md5Helper.reverse(Md5Helper.generate(pwd));
-        DownloadEntity downloadEntity = getDownloadEntity(ue, zipFile, dbPwd);
-        int rows = downloadMapper.insert(downloadEntity);
-
-        return rows > 0 ? downloadEntity.getGuid() : null;
+        return rows > 0 ? de.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) {
+    private void queryData(DownloadReqEntity dr, Map<String, List<?>> dataMap, Map<String, List<AttachEntity>> annexMap) {
+        for (String entity : dr.getEntities()) {
             try {
-                GeomBaseMapper<?> baseMapper = ClassHelper.getGeoBaseMapper(enity);
+                BasicMapper baseMapper = ClassHelper.getBasicMapper(entity);
                 if (null == baseMapper) {
                     continue;
                 }
 
-                QueryWrapper wrapper = createWrapper(baseMapper, depcode, dirs, wkt);
-                List<?> list = baseMapper.selectList(wrapper);
-                if (null == list || list.size() == 0) {
-                    continue;
-                }
-
-                if (!map.containsKey(enity)) {
-                    map.put(enity, list);
-                }
+                QueryWrapper wrapper = createQueryWrapper(baseMapper, dr);
+                metaService.addData(entity, baseMapper, wrapper, dataMap, annexMap);
             } 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, *");
+    private <T> QueryWrapper<T> createQueryWrapper(BasicMapper baseMapper, DownloadReqEntity dr) {
+        QueryWrapper<T> wrapper = new QueryWrapper<T>();
 
-            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");
+        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) {
+            wrapper.select("ST_AsText(geom) as geom, *");
+            if (!StringHelper.isEmpty(dr.getWkt())) {
+                Integer srid = baseQueryService.getSrid((GeomBaseMapper) baseMapper);
+                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;
     }
 
     /**
-     * 娣诲姞Zip鏂囦欢
+     * 鍒涘缓闄勪欢 *
      */
-    private void addZipFiles(ZipFile zip, ZipParameters params, File[] files) {
-        if (null == files || files.length == 0) {
+    private void createAnnex(String annexPath, Map<String, List<AttachEntity>> annexMap) {
+        if (annexMap.size() == 0) {
             return;
         }
 
-        for (File f : files) {
-            try {
-                zip.addFile(f, params);
-            } catch (Exception ex) {
-                log.error(ex.getMessage(), ex);
+        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();
+            }
+
+            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);
+                }
             }
         }
     }
@@ -196,10 +261,10 @@
     /**
      * 鑾峰彇涓嬭浇瀹炰綋绫�
      */
-    private DownloadEntity getDownloadEntity(UserEntity ue, String file, String pwd) throws Exception {
+    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-缁熻鎶ュ憡
+        // 1-Shp鏂囦欢锛�2-涓撻鍥撅紝3-鍏冩暟鎹紝4-涓氬姟鏁版嵁锛�5-绠¢亾鍒嗘瀽锛�6-缁熻鎶ュ憡锛�7-闄勪欢鏂囦欢锛�8-鐡︾墖鏂囦欢
         de.setType(4);
         de.setSizes(FileHelper.sizeToMb(new File(file).length()));
         de.setDepid(ue.getDepid());
@@ -215,68 +280,6 @@
     }
 
     /**
-     * 鎵撳寘DB鏁版嵁
-     */
-    public String zipDbData(UserEntity ue, String name, String depcode, String dirs, String filter, String pwd) throws Exception {
-        BasicMapper baseMapper = ClassHelper.getBasicMapper(name);
-        if (baseMapper == null) {
-            return null;
-        }
-
-        QueryWrapper wrapper = new QueryWrapper();
-        baseQueryService.addFilterWrapper(wrapper, filter);
-        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;
-        }
-
-        Map<String, List<?>> map = new HashMap<>(1);
-        map.put(name, list);
-
-        return zipData(ue, map, pwd);
-    }
-
-    /**
-     * 鎵撳寘鏁版嵁
-     */
-    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);
-        }
-        GdbHelper.createGdb(filePath, map);
-
-        String zipName = tempName + ".gdb.zip";
-        String zipFile = pathHelper.getDownloadFullPath() + File.separator + zipName;
-
-        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;
-    }
-
-    /**
      * 澶勭悊缂栫爜
      */
     public static String copeCodes(String codes, String field) {
@@ -284,15 +287,23 @@
             return null;
         }
 
-        List<String> list = codesAsList(codes);
-        removeDuplicate(list);
-        setRightLike(list, field);
+        //List<String> list = Arrays.asList(codes.split(","));
+        //List<String> list = codesAsList(codes);
+        //list = copeDirCodes(list);
+        //setRightLike(list, field);
 
-        return StringHelper.join(list, " or ");
+        //return "(" + StringHelper.join(list, " or ") + ")";
+
+        List<String> list = new ArrayList<>();
+        for (String dir : codes.split(",")) {
+            list.add("^" + dir);
+        }
+
+        return field + " ~ '" + StringHelper.join(list, "|") + "'";
     }
 
     /**
-     * 鍗曚綅缂栫爜杞泦鍚�
+     * 鍗曚綅缂栫爜杞泦鍚� *
      */
     private static List<String> codesAsList(String codes) {
         List<String> list = Arrays.asList(codes.split(","));
@@ -305,42 +316,113 @@
     }
 
     /**
-     * 鍘婚櫎閲嶅
+     * 澶勭悊鐩綍缂栫爜 *
      */
-    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);
+    private static List<String> copeDirCodes(List<String> list) {
+        List<String> prjList = getDirCodesByLen(list, 0, 2);
+        List<String> appList = getDirCodesByLen(list, 3, 30);
+        if (prjList.isEmpty() && appList.isEmpty()) {
+            return list;
+        }
+        if (prjList.isEmpty()) {
+            return appList;
+        }
+        if (appList.isEmpty()) {
+            return prjList;
+        }
+
+        return filterCodes(prjList, appList);
+    }
+
+    /**
+     * 鏍规嵁闀垮害鑾峰彇鍗曚綅缂栫爜 *
+     */
+    private static List<String> getDirCodesByLen(List<String> list, int start, int end) {
+        List<String> rs = new ArrayList<>();
+        for (String code : list) {
+            if (StringHelper.isEmpty(code)) {
                 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;
+            if (code.length() >= start && code.length() <= end) {
+                rs.add(code);
             }
         }
 
-        return -1;
+        return rs;
     }
 
     /**
-     * 璁剧疆 鍙砽ike
+     * 杩囨护椤圭洰缂栫爜 *
+     */
+    private static List<String> filterCodes(List<String> prjList, List<String> appList) {
+        int i = 0;
+        while (i < appList.size()) {
+            boolean flag = false;
+            for (String prj : prjList) {
+                if (appList.get(i).startsWith(prj)) {
+                    flag = true;
+                    break;
+                }
+            }
+
+            if (!flag) {
+                appList.remove(i);
+                continue;
+            }
+            i++;
+        }
+
+        return appList;
+    }
+
+    /**
+     * 璁剧疆 鍙砽ike *
      */
     private static void setRightLike(List<String> list, String field) {
+        if (list.isEmpty()) {
+            list.add("1 = 2");
+            return;
+        }
+
         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);
         }
     }
+
+    /**
+     * 鏍规嵁璧勬枡绫诲埆鑾峰彇鐩綍缂栫爜
+     */
+    public String getDirsByTypes(String types, String dirs, String field) {
+        if (StringUtils.isEmpty(types) && StringUtils.isEmpty(dirs)) {
+            return null;
+        }
+
+        if (!StringUtils.isEmpty(types)) {
+            List<KeyValueEntity> list = baseQueryService.selectDirsByTypes(types.split(","), copeCodes(dirs, "code"));
+            if (null == list || list.isEmpty()) {
+                return "1 = 2";
+            }
+
+            return copeDirs(list, field);
+        }
+
+        return copeCodes(dirs, field);
+    }
+
+    /**
+     * 澶勭悊鐩綍缂栫爜锛歞ircode ~ '^0A|^0E'
+     */
+    public static String copeDirs(List<KeyValueEntity> kvs, String field) {
+        List<String> list = new ArrayList<>();
+        for (KeyValueEntity kv : kvs) {
+            String[] dirs = kv.getValue().split(",");
+            for (String dir : dirs) {
+                list.add("^" + dir);
+            }
+        }
+
+        return field + " ~ '" + StringHelper.join(list, "|") + "'";
+    }
 }

--
Gitblit v1.9.3