From b1bdad2ff512e73a2dd9049f2ccbddd4634969f7 Mon Sep 17 00:00:00 2001
From: 13693261870 <252740454@qq.com>
Date: 星期二, 01 十一月 2022 15:51:58 +0800
Subject: [PATCH] 1

---
 src/main/java/com/lf/server/config/InitConfig.java              |    1 
 src/main/resources/mapper/data/DownloadMapper.xml               |    4 +
 src/main/java/com/lf/server/controller/show/MarkController.java |   44 +++++++++++++-
 src/main/java/com/lf/server/helper/WebHelper.java               |   11 ++-
 src/main/java/com/lf/server/service/show/MarkService.java       |   75 +++++++++++++++++++++---
 data/db_fn.sql                                                  |    6 +-
 src/main/resources/mapper/sys/AttachMapper.xml                  |    2 
 src/main/java/com/lf/server/helper/FileHelper.java              |   17 +++++
 src/main/java/com/lf/server/mapper/data/DownloadMapper.java     |    9 +++
 src/main/java/com/lf/server/service/data/DownloadService.java   |    5 +
 src/main/java/com/lf/server/service/all/UploadService.java      |    2 
 11 files changed, 153 insertions(+), 23 deletions(-)

diff --git a/data/db_fn.sql b/data/db_fn.sql
index 9857e0a..1b57438 100644
--- a/data/db_fn.sql
+++ b/data/db_fn.sql
@@ -251,9 +251,9 @@
 
 select st_astext(geom) from bd.dlg_25w_hfcl limit 10;
 
-
-
-
+select * from lf.sys_attach where 1=1 limit 1
+select * from lf.sys_download;
+select * from lf.sys_user;
 
 
 
diff --git a/src/main/java/com/lf/server/config/InitConfig.java b/src/main/java/com/lf/server/config/InitConfig.java
index 43dda52..3a0a113 100644
--- a/src/main/java/com/lf/server/config/InitConfig.java
+++ b/src/main/java/com/lf/server/config/InitConfig.java
@@ -9,6 +9,7 @@
 import com.lf.server.entity.all.ResAuthEntity;
 import com.lf.server.entity.bd.DlgAgnpEntity;
 import com.lf.server.helper.AesHelper;
+import com.lf.server.helper.FileHelper;
 import com.lf.server.helper.PathHelper;
 import com.lf.server.mapper.bd.DlgAgnpMapper;
 import com.lf.server.service.all.BaseQueryService;
diff --git a/src/main/java/com/lf/server/controller/show/MarkController.java b/src/main/java/com/lf/server/controller/show/MarkController.java
index ec1bf6e..0094e8e 100644
--- a/src/main/java/com/lf/server/controller/show/MarkController.java
+++ b/src/main/java/com/lf/server/controller/show/MarkController.java
@@ -5,9 +5,12 @@
 import com.lf.server.entity.all.HttpStatus;
 import com.lf.server.entity.all.ResponseMsg;
 import com.lf.server.entity.ctrl.MarkJsonEntity;
+import com.lf.server.entity.data.DownloadEntity;
 import com.lf.server.entity.show.MarkEntity;
 import com.lf.server.entity.sys.UserEntity;
+import com.lf.server.helper.StringHelper;
 import com.lf.server.helper.WebHelper;
+import com.lf.server.service.data.DownloadService;
 import com.lf.server.service.show.MarkService;
 import com.lf.server.service.sys.TokenService;
 import io.swagger.annotations.Api;
@@ -34,6 +37,9 @@
 
     @Autowired
     TokenService tokenService;
+
+    @Autowired
+    DownloadService downloadService;
 
     @SysLog()
     @ApiOperation(value = "鍒嗛〉鏌ヨ骞惰繑鍥炶褰曟暟")
@@ -238,15 +244,47 @@
                 return fail("鎵句笉鍒版爣缁樻暟鎹�", null);
             }
 
-            String str = markService.downloadShp(ue, list, req, res);
+            String guid = markService.downloadShp(ue, list, req, res);
+            if (StringHelper.isNull(guid)) {
+                return fail("鐢熸垚ShapeFile鏂囦欢澶辫触", null);
+            }
 
-            return success(str);
+            return success(guid);
         } catch (Exception ex) {
             return fail(ex.getMessage(), null);
         }
     }
 
-    public void downloadFile(String guid) {
+    @SysLog()
+    @ApiOperation(value = "涓嬭浇鏂囦欢")
+    @ApiImplicitParams({
+            @ApiImplicitParam(name = "guid", value = "鏂囦欢GUID", dataType = "String", paramType = "query")
+    })
+    @RequestMapping(value = "/downloadFile", method = RequestMethod.GET)
+    public void downloadFile(String guid, HttpServletRequest req, HttpServletResponse res) {
+        try {
+            UserEntity ue = tokenService.getCurrentUser(req);
+            if (ue == null) {
+                WebHelper.write2Page(res, WebHelper.getErrJson(HttpStatus.UNAUTHORIZED, "鐢ㄦ埛鏈櫥褰�"));
+            }
 
+            DownloadEntity de = downloadService.selectByGuid(guid);
+            if (de == null) {
+                WebHelper.write2Page(res, WebHelper.getErrJson(HttpStatus.NOT_FOUND, "鏂囦欢涓嶅瓨鍦�"));
+            }
+
+            de.setDcount(de.getDcount() + 1);
+            de.setDownloadUser(ue.getId());
+            int rows = downloadService.update(de);
+
+            String filePath = markService.getDownloadFilePath(de);
+            WebHelper.download(filePath, de.getName(), res);
+        } catch (Exception ex) {
+            try {
+                WebHelper.write2Page(res, WebHelper.getErrJson(HttpStatus.UNAUTHORIZED, ex.getMessage()));
+            } catch (Exception e) {
+                log.error(e.getMessage(), e);
+            }
+        }
     }
 }
diff --git a/src/main/java/com/lf/server/helper/FileHelper.java b/src/main/java/com/lf/server/helper/FileHelper.java
index acad188..196a825 100644
--- a/src/main/java/com/lf/server/helper/FileHelper.java
+++ b/src/main/java/com/lf/server/helper/FileHelper.java
@@ -259,4 +259,21 @@
 
         file.delete();
     }
+
+    /**
+     * 鑾峰彇鐩稿璺緞
+     *
+     * @param file 鏂囦欢
+     * @return 鐩稿璺緞
+     */
+    public static String getRelativePath(String file) {
+        if (StringHelper.isEmpty(file)) {
+            return null;
+        }
+
+        int idx = file.lastIndexOf(File.separator);
+        int start = file.lastIndexOf(File.separator, idx - 1);
+
+        return file.substring(start + 1);
+    }
 }
diff --git a/src/main/java/com/lf/server/helper/WebHelper.java b/src/main/java/com/lf/server/helper/WebHelper.java
index 5a43d7d..9957740 100644
--- a/src/main/java/com/lf/server/helper/WebHelper.java
+++ b/src/main/java/com/lf/server/helper/WebHelper.java
@@ -286,12 +286,15 @@
     /**
      * 涓嬭浇鏂囦欢
      *
-     * @param file 鏂囦欢
-     * @param res  鍝嶅簲
+     * @param file     鏂囦欢
+     * @param fileName 鏂囦欢鍚�
+     * @param res      鍝嶅簲
      * @throws Exception 寮傚父
      */
-    public static void download(String file, HttpServletResponse res) throws Exception {
-        String fileName = URLEncoder.encode(FileHelper.getFileName(file), "UTF-8");
+    public static void download(String file, String fileName, HttpServletResponse res) throws Exception {
+        if (StringHelper.isNull(fileName)) {
+            fileName = URLEncoder.encode(FileHelper.getFileName(file), "UTF-8");
+        }
 
         // 璁剧疆鍝嶅簲澶翠腑鏂囦欢鐨勪笅杞芥柟寮忎负闄勪欢鏂瑰紡锛屼互鍙婅缃枃浠跺悕
         res.setHeader("Content-Disposition", "attachment; filename=" + fileName);
diff --git a/src/main/java/com/lf/server/mapper/data/DownloadMapper.java b/src/main/java/com/lf/server/mapper/data/DownloadMapper.java
index 8646b22..a96d271 100644
--- a/src/main/java/com/lf/server/mapper/data/DownloadMapper.java
+++ b/src/main/java/com/lf/server/mapper/data/DownloadMapper.java
@@ -1,6 +1,7 @@
 package com.lf.server.mapper.data;
 
 import com.lf.server.entity.data.DownloadEntity;
+import com.lf.server.entity.sys.AttachEntity;
 import org.apache.ibatis.annotations.Mapper;
 import org.springframework.stereotype.Repository;
 
@@ -47,6 +48,14 @@
     public DownloadEntity selectById(int id);
 
     /**
+     * 鏍规嵁Guid鏌ヨ
+     *
+     * @param guid
+     * @return
+     */
+    public DownloadEntity selectByGuid(String guid);
+
+    /**
      * 鎻掑叆涓�鏉�
      *
      * @param entity
diff --git a/src/main/java/com/lf/server/service/all/UploadService.java b/src/main/java/com/lf/server/service/all/UploadService.java
index d6dd101..0706bc0 100644
--- a/src/main/java/com/lf/server/service/all/UploadService.java
+++ b/src/main/java/com/lf/server/service/all/UploadService.java
@@ -115,7 +115,7 @@
             }
 
             String file = pathHelper.getConfig().getUploadPath() + File.separator + entity.getPath();
-            WebHelper.download(file, res);
+            WebHelper.download(file, entity.getName(), res);
         } catch (Exception ex) {
             try {
                 String msg = JSON.toJSONString(new ResponseMsg<String>(HttpStatus.ERROR, "鏂囦欢涓嬭浇鍑洪敊"));
diff --git a/src/main/java/com/lf/server/service/data/DownloadService.java b/src/main/java/com/lf/server/service/data/DownloadService.java
index 0830114..d13a5cf 100644
--- a/src/main/java/com/lf/server/service/data/DownloadService.java
+++ b/src/main/java/com/lf/server/service/data/DownloadService.java
@@ -42,6 +42,11 @@
     }
 
     @Override
+    public DownloadEntity selectByGuid(String guid) {
+        return downloadMapper.selectByGuid(guid);
+    }
+
+    @Override
     public Integer insert(DownloadEntity entity) {
         return downloadMapper.insert(entity);
     }
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 b1cb186..4fa6894 100644
--- a/src/main/java/com/lf/server/service/show/MarkService.java
+++ b/src/main/java/com/lf/server/service/show/MarkService.java
@@ -1,10 +1,12 @@
 package com.lf.server.service.show;
 
 import com.lf.server.entity.ctrl.MarkJsonEntity;
+import com.lf.server.entity.data.DownloadEntity;
 import com.lf.server.entity.show.MarkEntity;
 import com.lf.server.entity.sys.UserEntity;
 import com.lf.server.helper.*;
 import com.lf.server.mapper.show.MarkMapper;
+import com.lf.server.service.data.DownloadService;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
@@ -25,6 +27,9 @@
 
     @Autowired
     PathHelper pathHelper;
+
+    @Autowired
+    DownloadService downloadService;
 
     @Override
     public Integer selectCount(Integer uid) {
@@ -76,8 +81,42 @@
         return markMapper.updates(list);
     }
 
+    /**
+     * 涓嬭浇ShapeFile鏂囦欢
+     *
+     * @param ue   鐢ㄦ埛瀹炰綋
+     * @param list 鏍囩粯JSON瀹炰綋绫婚泦鍚�
+     * @param req  璇锋眰
+     * @param res  鍝嶅簲
+     * @return GUID
+     * @throws Exception 寮傚父
+     */
     public String downloadShp(UserEntity ue, List<MarkJsonEntity> list, HttpServletRequest req, HttpServletResponse res) throws Exception {
         String parent = pathHelper.getTempPath(ue.getId());
+
+        String path = createShapeFiles(ue, list, parent);
+        File[] files = new File(path).listFiles();
+        if (files == null || files.length == 0) {
+            return null;
+        }
+
+        String zip = getZip();
+        ZipHelper.zip(zip, parent);
+        FileHelper.deleteDir(parent);
+
+        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;
+    }
+
+    private String createShapeFiles(UserEntity ue, List<MarkJsonEntity> list, String parent) {
         String path = getShpDir(ue, parent);
 
         List<MarkJsonEntity> points = getMarkByType(list, "POINT");
@@ -93,17 +132,7 @@
             String polygonFile = GdalHelper.createShp(polygons, path, "POLYGON");
         }
 
-        File[] files = new File(path).listFiles();
-        if (files == null || files.length == 0) {
-            return "Shp鏂囦欢鐢熸垚澶辫触";
-        }
-
-        String zip = getZip();
-        ZipHelper.zip(zip, parent);
-        //download(res, zip);
-        FileHelper.deleteDir(parent);
-
-        return "";
+        return path;
     }
 
     private String getShpDir(UserEntity ue, String parent) {
@@ -142,5 +171,29 @@
         return path;
     }
 
+    private DownloadEntity getDownloadEntity(UserEntity ue, String file) throws Exception {
+        DownloadEntity de = new DownloadEntity();
+        de.setName(FileHelper.getFileName(file));
+        de.setType(1);
+        de.setDepid(ue.getDepid());
+        de.setDcount(0);
+        // de.setPwd(null)
+        de.setUrl(FileHelper.getRelativePath(file));
+        de.setDescr("涓嬭浇ShapeFile鏂囦欢");
+        de.setGuid(FileHelper.getFileMd5(file));
+        de.setCreateUser(ue.getId());
+        // de.setGeom(null)
 
+        return de;
+    }
+
+    /**
+     * 鑾峰彇涓嬭浇鏂囦欢璺緞
+     *
+     * @param de 涓嬭浇瀹炰綋绫�
+     * @return 涓嬭浇鏂囦欢璺緞
+     */
+    public String getDownloadFilePath(DownloadEntity de) {
+        return pathHelper.getConfig().getDownloadPath() + File.separator + de.getUrl();
+    }
 }
diff --git a/src/main/resources/mapper/data/DownloadMapper.xml b/src/main/resources/mapper/data/DownloadMapper.xml
index 229f015..39af069 100644
--- a/src/main/resources/mapper/data/DownloadMapper.xml
+++ b/src/main/resources/mapper/data/DownloadMapper.xml
@@ -29,6 +29,10 @@
         select * from lf.sys_download where id = #{id}
     </select>
 
+    <select id="selectByGuid" resultType="com.lf.server.entity.data.DownloadEntity">
+        select * from lf.sys_download where guid = #{guid} limit 1
+    </select>
+
     <insert id="insert" parameterType="com.lf.server.entity.data.DownloadEntity">
        insert into lf.sys_download
        (name,type,depid,dcount,pwd,url,descr,guid,create_user,create_time,download_user,download_time,geom,bak)
diff --git a/src/main/resources/mapper/sys/AttachMapper.xml b/src/main/resources/mapper/sys/AttachMapper.xml
index 82ddca6..6aeea6f 100644
--- a/src/main/resources/mapper/sys/AttachMapper.xml
+++ b/src/main/resources/mapper/sys/AttachMapper.xml
@@ -30,7 +30,7 @@
     </select>
 
     <select id="selectByGuid" resultType="com.lf.server.entity.sys.AttachEntity">
-        select * from lf.sys_attach where guid = #{guid}
+        select * from lf.sys_attach where guid = #{guid} limit 1
     </select>
 
     <insert id="insert" parameterType="com.lf.server.entity.sys.AttachEntity">

--
Gitblit v1.9.3