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/MarkService.java |  142 +++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 123 insertions(+), 19 deletions(-)

diff --git a/src/main/java/com/lf/server/service/show/MarkService.java b/src/main/java/com/lf/server/service/show/MarkService.java
index d54f484..a5ab635 100644
--- a/src/main/java/com/lf/server/service/show/MarkService.java
+++ b/src/main/java/com/lf/server/service/show/MarkService.java
@@ -1,26 +1,24 @@
 package com.lf.server.service.show;
 
+import com.lf.server.entity.all.StaticData;
 import com.lf.server.entity.ctrl.MarkJsonEntity;
+import com.lf.server.entity.data.DownloadEntity;
+import com.lf.server.entity.data.MetaEntity;
+import com.lf.server.entity.data.MetaFileEntity;
 import com.lf.server.entity.show.MarkEntity;
 import com.lf.server.entity.sys.UserEntity;
-import com.lf.server.helper.GdalHelper;
-import com.lf.server.helper.PathHelper;
-import com.lf.server.helper.StringHelper;
-import com.lf.server.helper.WebHelper;
+import com.lf.server.helper.*;
 import com.lf.server.mapper.show.MarkMapper;
-import com.lf.server.service.data.UploaderService;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
+import com.lf.server.service.data.DownloadService;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
-import org.springframework.web.bind.annotation.RequestBody;
 
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import java.io.File;
 import java.util.ArrayList;
+import java.util.Date;
 import java.util.List;
-import java.util.Random;
 
 /**
  * 鏍囩粯
@@ -33,6 +31,9 @@
 
     @Autowired
     PathHelper pathHelper;
+
+    @Autowired
+    DownloadService downloadService;
 
     @Override
     public Integer selectCount(Integer uid) {
@@ -84,27 +85,64 @@
         return markMapper.updates(list);
     }
 
-    public void downloadShp(UserEntity ue, List<MarkJsonEntity> list, HttpServletRequest req, HttpServletResponse res) {
-        String path = getShpDir(ue);
+    /**
+     * 涓嬭浇ShapeFile鏂囦欢
+     *
+     * @param ue   鐢ㄦ埛瀹炰綋
+     * @param list 鏍囩粯JSON瀹炰綋绫婚泦鍚�
+     * @return GUID
+     * @throws Exception 寮傚父
+     */
+    public String downloadShp(UserEntity ue, List<MarkJsonEntity> list) throws Exception {
+        String path = pathHelper.getTempPath();
+        createShapeFiles(ue, list, path);
 
+        File[] files = new File(path).listFiles();
+        if (files == null || files.length == 0) {
+            return null;
+        }
+
+        String zip = getZipPath();
+        ZipHelper.zip(zip, path);
+        FileHelper.deleteDir(path);
+
+        String guid = FileHelper.getFileMd5(zip);
+        DownloadEntity entity = downloadService.selectByGuid(guid);
+        if (entity != null) {
+            return entity.getGuid();
+        }
+
+        DownloadEntity de = getDownloadEntity(ue, zip);
+        int rows = downloadService.insert(de);
+
+        return rows > 0 ? de.getGuid() : null;
+    }
+
+    /**
+     * 鍒涘缓shp鏂囦欢
+     */
+    private String createShapeFiles(UserEntity ue, List<MarkJsonEntity> list, String path) {
         List<MarkJsonEntity> points = getMarkByType(list, "POINT");
         if (points.size() > 0) {
-            String pointFile = GdalHelper.createShp(points, path, "POINT");
+            ShpHelper.createShp(points, path, "POINT");
         }
         List<MarkJsonEntity> lines = getMarkByType(list, "LINESTRING");
         if (lines.size() > 0) {
-            String lineFile = GdalHelper.createShp(lines, path, "LINESTRING");
+            ShpHelper.createShp(lines, path, "LINESTRING");
         }
         List<MarkJsonEntity> polygons = getMarkByType(list, "POLYGON");
         if (polygons.size() > 0) {
-            String polygonFile = GdalHelper.createShp(polygons, path, "POLYGON");
+            ShpHelper.createShp(polygons, path, "POLYGON");
         }
 
-        //
+        return path;
     }
 
-    private String getShpDir(UserEntity ue) {
-        String path = pathHelper.getTempPath(ue.getId()) + File.separator + WebHelper.getRandomInt(100000, 1000000);
+    /**
+     * 鑾峰彇shp鐩綍
+     */
+    private String getShpDir(UserEntity ue, String parent) {
+        String path = parent + File.separator + StringHelper.YMDHMS2_FORMAT.format(new Date());
 
         File file = new File(path);
         if (!file.exists() && !file.isDirectory()) {
@@ -114,17 +152,83 @@
         return path;
     }
 
+    /**
+     * 鑾峰彇鏍囩粯绫诲瀷
+     */
     private List<MarkJsonEntity> getMarkByType(List<MarkJsonEntity> list, String type) {
-        List<MarkJsonEntity> rs = new ArrayList<MarkJsonEntity>();
+        List<MarkJsonEntity> rs = new ArrayList<>();
         for (MarkJsonEntity mark : list) {
             if (StringHelper.isEmpty(mark.getWkt())) {
                 continue;
             }
-            if (mark.getWkt().indexOf(type) > -1) {
+            if (mark.getWkt().contains(type)) {
                 rs.add(mark);
             }
         }
 
         return rs;
     }
+
+    /**
+     * 鑾峰彇zip璺緞
+     */
+    private String getZipPath() {
+        String path = pathHelper.getDownloadFullPath() + File.separator + StringHelper.YMDHMS2_FORMAT.format(new Date()) + ".zip";
+
+        File file = new File(path);
+        if (file.exists() && !file.isDirectory()) {
+            file.delete();
+        }
+
+        return path;
+    }
+
+    /**
+     * 鑾峰彇涓嬭浇瀹炰綋绫�
+     */
+    private DownloadEntity getDownloadEntity(UserEntity ue, String file) throws Exception {
+        DownloadEntity de = new DownloadEntity();
+        de.setName(FileHelper.getFileName(file));
+        // 1-Shp鏂囦欢锛�2-涓撻鍥撅紝3-鍏冩暟鎹紝4-涓氬姟鏁版嵁锛�5-绠¢亾鍒嗘瀽锛�6-缁熻鎶ュ憡锛�7-闄勪欢鏂囦欢锛�8-鐡︾墖鏂囦欢
+        de.setType(1);
+        de.setSizes(FileHelper.sizeToMb(new File(file).length()));
+        de.setDepid(ue.getDepid());
+        de.setDcount(0);
+        // de.setPwd(null)
+        de.setUrl(FileHelper.getRelativePath(file));
+        de.setDescr("Shp鏂囦欢");
+        de.setGuid(FileHelper.getFileMd5(file));
+        de.setCreateUser(ue.getId());
+        // de.setGeom(null)
+
+        return de;
+    }
+
+    /**
+     * 鑾峰彇涓嬭浇鏂囦欢璺緞
+     */
+    public String getDownloadFilePath(DownloadEntity de) {
+        return pathHelper.getConfig().getDownloadPath() + File.separator + de.getUrl();
+    }
+
+    /**
+     * 璇诲彇ShapeFile鏂囦欢鑾峰彇Mark瀹炰綋绫�
+     */
+    public List<MarkJsonEntity> readShpForMarks(List<MetaFileEntity> list) {
+        String fileName = null;
+        for (MetaFileEntity mf : list) {
+            if (mf.getName().toLowerCase().contains(StaticData.SHP)) {
+                fileName = mf.getPath();
+                break;
+            }
+        }
+        if (StringHelper.isEmpty(fileName)) {
+            return null;
+        }
+
+        List<MarkJsonEntity> mjeList = ShpHelper.readShpForMarks(fileName);
+        FileHelper.deleteFiles(list);
+
+        return mjeList;
+    }
 }

--
Gitblit v1.9.3