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/PipelineService.java |  204 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 201 insertions(+), 3 deletions(-)

diff --git a/src/main/java/com/lf/server/service/show/PipelineService.java b/src/main/java/com/lf/server/service/show/PipelineService.java
index 959ebd5..68caf48 100644
--- a/src/main/java/com/lf/server/service/show/PipelineService.java
+++ b/src/main/java/com/lf/server/service/show/PipelineService.java
@@ -1,11 +1,27 @@
 package com.lf.server.service.show;
 
+import com.lf.server.entity.all.StaticData;
+import com.lf.server.entity.data.DownloadEntity;
 import com.lf.server.entity.show.PipelineEntity;
+import com.lf.server.entity.sys.UserEntity;
+import com.lf.server.helper.*;
+import com.lf.server.mapper.data.DownloadMapper;
 import com.lf.server.mapper.show.PipelineMapper;
+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.gdal.ogr.*;
+import org.gdal.osr.SpatialReference;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
+import java.io.File;
+import java.lang.reflect.Field;
+import java.util.ArrayList;
+import java.util.Date;
 import java.util.List;
+import java.util.Map;
 
 /**
  * 绠¢亾鍒嗘瀽鏈嶅姟绫�
@@ -14,7 +30,22 @@
 @Service
 public class PipelineService implements PipelineMapper {
     @Autowired
+    PathHelper pathHelper;
+
+    @Autowired
+    DownloadMapper downloadMapper;
+
+    @Autowired
     PipelineMapper pipelineMapper;
+
+    private final static Log log = LogFactory.getLog(PipelineService.class);
+
+    @Override
+    public List<PipelineEntity> selectPipelines(String name) {
+        name = StringHelper.getLikeUpperStr(name);
+
+        return pipelineMapper.selectPipelines(name);
+    }
 
     @Override
     public List<PipelineEntity> selectSegNames() {
@@ -26,8 +57,175 @@
         return pipelineMapper.selectPipeAnalysis(tab, gid);
     }
 
-    @Override
-    public List<PipelineEntity> selectAnalysisResult(String tab, Integer gid) {
-        return pipelineMapper.selectAnalysisResult(tab, gid);
+    /**
+     * 鍒涘缓Zip鍖�
+     *
+     * @param ue  鐢ㄦ埛瀹炰綋
+     * @param map 绠¢亾鍒嗘瀽鏁版嵁闆嗗悎
+     * @param pwd 瀵嗙爜
+     * @return 涓嬭浇鏂囦欢GUID
+     */
+    public String createZipFile(UserEntity ue, Map<String, List<PipelineEntity>> 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);
+        }
+        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(true);
+        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;
+    }
+
+    /**
+     * 鍒涘缓GDB
+     */
+    public static void createGdb(String filePath, Map<String, List<PipelineEntity>> map) throws Exception {
+        Driver driver = null;
+        DataSource dataSource = null;
+        try {
+            driver = ogr.GetDriverByName("FileGDB");
+            if (null == driver) {
+                log.error("PipelineService.createGdb.driver(FileGDB) is null.");
+                return;
+            }
+            dataSource = driver.CreateDataSource(filePath, null);
+            if (null == dataSource) {
+                log.error("PipelineService.createGdb.dataSource is null. " + filePath);
+                return;
+            }
+
+            for (String key : map.keySet()) {
+                Layer layer = null;
+                try {
+                    List<PipelineEntity> list = map.get(key);
+                    layer = createLayer(dataSource, key.replace(".", "_"), list.get(0));
+
+                    List<Field> fields = new ArrayList<>();
+                    getFields(PipelineEntity.class, fields);
+
+                    GdbHelper.addLayerField(layer, fields);
+                    setLayerData(layer, fields, list);
+                } finally {
+                    if (null != layer) {
+                        layer.delete();
+                    }
+                }
+            }
+
+            dataSource.SyncToDisk();
+            dataSource.FlushCache();
+        } catch (Exception ex) {
+            log.error(ex.getMessage(), ex);
+            throw ex;
+        } finally {
+            GdbHelper.delete(dataSource, driver);
+        }
+    }
+
+    /**
+     * 鍒涘缓鍥惧眰
+     */
+    private static Layer createLayer(DataSource dataSource, String tab, PipelineEntity pe) {
+        int geomType = pe.getWkt().contains("POINT") ? ogr.wkbMultiPoint : ogr.wkbMultiLineString;
+
+        SpatialReference sr = new SpatialReference();
+        sr.ImportFromEPSG(4490);
+
+        return dataSource.CreateLayer(tab, sr, geomType, null);
+    }
+
+    /**
+     * 鑾峰彇瀛楁
+     */
+    private static void getFields(Class clazz, List<Field> list) {
+        try {
+            Field[] fields = clazz.getDeclaredFields();
+            for (Field f : fields) {
+                if (StaticData.PIPE_EXCLUDE_FIELDS.contains(f.getName())) {
+                    continue;
+                }
+
+                f.setAccessible(true);
+                list.add(f);
+            }
+
+            if (!StaticData.OBJECT.equals(clazz.getSuperclass().getName())) {
+                getFields(clazz.getSuperclass(), list);
+            }
+        } catch (Exception ex) {
+            //
+        }
+    }
+
+    /**
+     * 璁剧疆鍥惧眰鏁版嵁
+     */
+    private static void setLayerData(Layer layer, List<Field> fields, List<PipelineEntity> list) throws Exception {
+        for (PipelineEntity t : list) {
+            Feature f = new Feature(layer.GetLayerDefn());
+
+            String wkt = t.getWkt();
+            if (!wkt.contains("MULTI")) {
+                wkt = wkt.replace("POINT", "MULTIPOINT").replace("LINESTRING", "MULTILINESTRING(") + (wkt.contains("LINESTRING") ? ")" : "");
+            }
+
+            Geometry geom = Geometry.CreateFromWkt(wkt);
+            f.SetGeometry(geom);
+
+            GdbHelper.setFeatureData(f, fields, t);
+            layer.CreateFeature(f);
+        }
+    }
+
+    /**
+     * 娣诲姞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 DownloadEntity getDownloadEntity(UserEntity ue, String file, String pwd) throws Exception {
+        DownloadEntity de = new DownloadEntity();
+        de.setName(FileHelper.getFileName(file));
+        // 1-Shp鏂囦欢锛�2-涓撻鍥撅紝3-鍏冩暟鎹紝4-涓氬姟鏁版嵁锛�5-绠¢亾鍒嗘瀽锛�6-缁熻鎶ュ憡锛�7-闄勪欢鏂囦欢锛�8-鐡︾墖鏂囦欢
+        de.setType(5);
+        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;
     }
 }

--
Gitblit v1.9.3