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/InquiryService.java |  187 ++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 179 insertions(+), 8 deletions(-)

diff --git a/src/main/java/com/lf/server/service/show/InquiryService.java b/src/main/java/com/lf/server/service/show/InquiryService.java
index e2b2e46..0d310f1 100644
--- a/src/main/java/com/lf/server/service/show/InquiryService.java
+++ b/src/main/java/com/lf/server/service/show/InquiryService.java
@@ -1,12 +1,26 @@
 package com.lf.server.service.show;
 
+import com.lf.server.entity.all.StaticData;
+import com.lf.server.entity.ctrl.DownloadTileEntity;
 import com.lf.server.entity.ctrl.ShpRecordEntity;
+import com.lf.server.entity.data.DownloadEntity;
 import com.lf.server.entity.data.MetaFileEntity;
-import com.lf.server.helper.FileHelper;
-import com.lf.server.helper.GdalHelper;
-import com.lf.server.helper.StringHelper;
+import com.lf.server.entity.data.PublishEntity;
+import com.lf.server.entity.sys.UserEntity;
+import com.lf.server.helper.*;
+import com.lf.server.service.data.DownloadService;
+import com.lf.server.service.data.PublishService;
+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.stereotype.Service;
 
+import javax.annotation.Resource;
+import java.io.File;
+import java.io.FileInputStream;
+import java.util.ArrayList;
+import java.util.Date;
 import java.util.List;
 
 /**
@@ -15,16 +29,24 @@
  */
 @Service
 public class InquiryService {
+    @Resource
+    PathHelper pathHelper;
+
+    @Resource
+    PublishService publishService;
+
+    @Resource
+    DownloadService downloadService;
+
+    private final static Log log = LogFactory.getLog(InquiryService.class);
+
     /**
      * 璇诲彇Shp绗竴鏉¤褰曠殑WKT
-     *
-     * @param list
-     * @return
      */
     public ShpRecordEntity readShpFirstRecord(List<MetaFileEntity> list) {
         String fileName = null;
         for (MetaFileEntity mf : list) {
-            if (mf.getName().toLowerCase().indexOf(".shp") > -1) {
+            if (mf.getName().toLowerCase().contains(".shp")) {
                 fileName = mf.getPath();
                 break;
             }
@@ -33,9 +55,158 @@
             return null;
         }
 
-        ShpRecordEntity sr = GdalHelper.readShpFirstRecord(fileName);
+        ShpRecordEntity sr = ShpHelper.readShpFirstRecord(fileName);
         FileHelper.deleteFiles(list);
 
         return sr;
     }
+
+    /**
+     * 鎵撳寘鐡︾墖
+     */
+    public String zipTiles(DownloadTileEntity dt, PublishEntity pub, UserEntity ue) {
+        if (!isTilePathExist(pub)) {
+            return null;
+        }
+
+        List<File> list = findTiles(dt, pub);
+        if (list.isEmpty()) {
+            return null;
+        }
+
+        String tempName = StringHelper.YMDHMS2_FORMAT.format(new Date());
+        String zipFile = pathHelper.getDownloadFullPath() + File.separator + tempName + ".zip";
+
+        ZipFile zip = Zip4jHelper.createZipFile(zipFile, dt.getPwd());
+        ZipParameters params = Zip4jHelper.getZipParams(true);
+        addFiles(zip, params, list, pub.getPath() + File.separator);
+
+        String dbPwd = Md5Helper.reverse(Md5Helper.generate(dt.getPwd()));
+        DownloadEntity de = getDownloadEntity(ue, zipFile, dbPwd);
+        int rows = downloadService.insert(de);
+        if (de.getId() > 0) {
+            insertPubDown(pub, de, ue);
+        }
+
+        return rows > 0 ? de.getGuid() : null;
+    }
+
+    /**
+     * 鐡︾墖璺緞鏄惁瀛樺湪
+     */
+    private boolean isTilePathExist(PublishEntity pub) {
+        String tilePath = pathHelper.getConfig().getTilePath() + pub.getPath();
+
+        File f = new File(tilePath);
+        if (!f.exists() || !f.isDirectory()) {
+            return false;
+        }
+
+        pub.setPath(tilePath);
+
+        return true;
+    }
+
+    /**
+     * 鏌ユ壘鐡︾墖
+     */
+    private List<File> findTiles(DownloadTileEntity dt, PublishEntity pub) {
+        List<File> list = new ArrayList<>();
+
+        File view = new File(pub.getPath() + File.separator + "view.htm");
+        if (view.exists() && !view.isDirectory()) {
+            list.add(view);
+        }
+
+        for (int i = 0; i < StaticData.I23; i++) {
+            List<File> files = findTilesByZoom(dt, pub, i);
+            if (files.size() > 0) {
+                list.addAll(files);
+            }
+        }
+
+        return list;
+    }
+
+    /**
+     * 鏍规嵁灞傛鏌ユ壘鐡︾墖
+     */
+    private List<File> findTilesByZoom(DownloadTileEntity dt, PublishEntity pub, int zoom) {
+        List<File> list = new ArrayList<>();
+        File f = new File(pub.getPath() + File.separator + zoom);
+        if (!f.exists() || !f.isDirectory()) {
+            return list;
+        }
+
+        int[] leftTop = deg2num(zoom, dt.getXmin(), dt.getYmax());
+        int[] rightBottom = deg2num(zoom, dt.getXmax(), dt.getYmin());
+
+        for (int x = leftTop[0]; x <= rightBottom[0]; x++) {
+            for (int y = leftTop[1]; y <= rightBottom[1]; y++) {
+                String pngPath = String.format("%s\\%d\\%d\\%d.png", pub.getPath(), zoom, x, y);
+
+                File pngFile = new File(pngPath);
+                if (pngFile.exists()) {
+                    list.add(pngFile);
+                }
+            }
+        }
+
+        return list;
+    }
+
+    /**
+     * 瑙掑害杞暟鍊�
+     */
+    private int[] deg2num(int zoom, double x, double y) {
+        double yRad = Math.toRadians(y);
+        double n = Math.pow(2.0, zoom);
+
+        int xTile = (int) ((x + 180.0) / 360.0 * n);
+        int yTile = (int) ((1.0 - Math.log(Math.tan(yRad) + (1 / Math.cos(yRad))) / Math.PI) / 2.0 * n);
+
+        return new int[]{xTile, yTile};
+    }
+
+    /**
+     * 娣诲姞鏂囦欢鑷冲帇缂╁寘
+     */
+    private void addFiles(ZipFile zip, ZipParameters params, List<File> list, String basePath) {
+        for (File f : list) {
+            try {
+                params.setFileNameInZip(f.getPath().replace(basePath, ""));
+                zip.addStream(new FileInputStream(f), params);
+            } catch (Exception ex) {
+                log.error(ex.getMessage(), ex);
+            }
+        }
+    }
+
+    /**
+     * 鑾峰彇涓嬭浇瀹炰綋绫�
+     */
+    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-缁熻鎶ュ憡锛�7-闄勪欢鏂囦欢锛�8-鐡︾墖鏂囦欢
+        de.setType(8);
+        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;
+    }
+
+    /**
+     * 鎻掑叆鏁版嵁鍙戝竷-涓嬭浇琛�
+     */
+    private void insertPubDown(PublishEntity pub, DownloadEntity de, UserEntity ue) {
+        publishService.insertPubDown(pub.getId(), de.getId(), ue.getId());
+    }
 }

--
Gitblit v1.9.3