From d1a6b45132c7df365812237281e91f41f00fe90e Mon Sep 17 00:00:00 2001
From: 13693261870 <252740454@qq.com>
Date: 星期六, 14 九月 2024 16:56:57 +0800
Subject: [PATCH] 完成基础功能开发

---
 src/main/java/com/se/simu/service/SedbService.java |  121 ++++++++++++++++++++++++++++++++++++----
 1 files changed, 108 insertions(+), 13 deletions(-)

diff --git a/src/main/java/com/se/simu/service/SedbService.java b/src/main/java/com/se/simu/service/SedbService.java
index dc50c89..20a4267 100644
--- a/src/main/java/com/se/simu/service/SedbService.java
+++ b/src/main/java/com/se/simu/service/SedbService.java
@@ -6,6 +6,7 @@
 import cn.hutool.json.JSONUtil;
 import com.se.simu.domain.SeDb;
 import com.se.simu.domain.SeField;
+import com.se.simu.domain.SeFile;
 import com.se.simu.domain.SeLayer;
 import com.se.simu.helper.RsaHelper;
 import com.se.simu.helper.ShpHelper;
@@ -18,6 +19,11 @@
 
 import javax.annotation.Resource;
 import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+import java.net.URLConnection;
 import java.util.*;
 import java.util.stream.Collectors;
 
@@ -58,26 +64,42 @@
     @Value("#{'${sedb.sysFields}'}")
     List<String> sysFields;
 
+    @Value("${sedb.demName}")
+    String demName;
+
     String password;
 
     @Resource
     RestTemplate restTemplate;
 
-    public boolean test() throws Exception {
-        // 469538.6536261877,4416744.922022615,469853.14714664617,4417049.378602433
-        String bbox = "116.64388473935195,39.884315914604464,116.64754729082588,39.887069143903496";
-
+    public boolean test(String bbox, String taskName) throws Exception {
         String token = getToken();
-
         SeDb db = getSeDb(token);
         db.setBbox(bbox);
 
         List<SeLayer> layers = getLayers(token, db);
         queryData(token, db, layers);
 
-        createShps(inPath + "\\20240913", layers);
+        String basePath = inPath + File.separator + taskName;
+        createPath(basePath);
+        createShps(basePath, layers);
+
+        SeDb fileDb = getFileDb(token);
+        String fileId = getFileId(token, fileDb.getDbid());
+        List<SeFile> files = getFileNames(token, fileDb.getDbid(), fileId);
+
+        String filePath = inPath + File.separator + fileDb.getDbid();
+        downloadFiles(token, filePath, files, fileDb.getDbid(), fileId);
 
         return true;
+    }
+
+    private void createPath(String basePath) {
+        File f = new File(basePath);
+        if (f.exists() && f.isDirectory()) {
+            FileUtil.del(f);
+        }
+        f.mkdirs();
     }
 
     public String getToken() throws Exception {
@@ -119,7 +141,7 @@
         List<SeDb> list = JSONUtil.toList(data, SeDb.class);
         if (CollectionUtils.isEmpty(list)) return null;
 
-        return list.stream().filter(db -> dbName.equals(db.getName())).findFirst().orElse(null);
+        return list.stream().filter(db -> null != db.getName() && db.getName().contains(dbName)).findFirst().orElse(null);
     }
 
     public List<SeLayer> getLayers(String token, SeDb db) {
@@ -213,12 +235,6 @@
     }
 
     public void createShps(String basePath, List<SeLayer> layers) throws Exception {
-        File f = new File(basePath);
-        if (f.exists() && f.isDirectory()) {
-            FileUtil.del(f);
-        }
-        f.mkdirs();
-
         for (SeLayer layer : layers) {
             String path = String.format("%s\\%s.shp", basePath, layer.getShpName());
             if (!ShpHelper.createShp(path, layer)) {
@@ -226,4 +242,83 @@
             }
         }
     }
+
+    public SeDb getFileDb(String token) {
+        String uri = String.format("%sfile-service/docdb/query/canview?token=%s", host, token);
+        JSONObject obj = restTemplate.getForObject(uri, JSONObject.class);
+        JSONArray data = obj.getJSONArray("data");
+
+        List<SeDb> list = JSONUtil.toList(data, SeDb.class);
+        if (CollectionUtils.isEmpty(list)) return null;
+
+        return list.stream().filter(db -> null != db.getName() && db.getName().contains(dbName)).findFirst().orElse(null);
+    }
+
+    public String getFileId(String token, String dbid) {
+        String uri = String.format("%sfile-service/doc/catagory/file/query?token=%s&dbid=%s&catagory=%s&count=%d&start=%d&like=",
+                host, token, dbid, "image", 9999, 1);
+
+        JSONObject obj = restTemplate.getForObject(uri, JSONObject.class);
+        JSONArray items = obj.getJSONObject("data").getJSONArray("items");
+
+        for (int i = 0, c = items.size(); i < c; i++) {
+            JSONObject jb = items.getJSONObject(i);
+            if (demName.equals(jb.getStr("filename"))) {
+                return jb.getStr("fileid");
+            }
+        }
+
+        return null;
+    }
+
+    public List<SeFile> getFileNames(String token, String dbid, String fileId) {
+        String uri = String.format("%sfile-service/doc/cluster/struct/list?token=%s&dbid=%s&cluster_fileid=%s&onlychild=true&folder_stairs=",
+                host, token, dbid, fileId);
+
+        JSONObject obj = restTemplate.getForObject(uri, JSONObject.class);
+        JSONArray data = obj.getJSONArray("data");
+
+        return JSONUtil.toList(data, SeFile.class);
+    }
+
+    public void downloadFiles(String token, String path, List<SeFile> files, String dbid, String fileId) throws IOException {
+        File f = new File(path);
+        if (!f.exists() || !f.isDirectory()) {
+            f.mkdirs();
+        }
+
+        for (SeFile seFile : files) {
+            String filePath = path + File.separator + seFile.getName();
+            f = new File(filePath);
+            if (f.exists() && f.length() == seFile.getSize()) {
+                continue;
+            }
+            if (f.exists() && f.length() < seFile.getSize()) {
+                f.delete();
+            }
+
+            String uri = String.format("%sfile-service/fileparser/cluster/download/%s?token=%s&dbid=%s&cluster_fileid=%s",
+                    host, seFile.getName(), token, dbid, fileId);
+            downloadFile(uri, filePath);
+        }
+    }
+
+    private void downloadFile(String uri, String filePath) throws IOException {
+        URL url = new URL(uri);
+        URLConnection conn = url.openConnection();
+        InputStream is = conn.getInputStream();
+
+        byte[] buffer = new byte[1024];
+        FileOutputStream fs = new FileOutputStream(filePath);
+
+        int read = 0, sum = 0;
+        while ((read = is.read(buffer)) != -1) {
+            sum += read;
+            fs.write(buffer, 0, read);
+        }
+
+        fs.flush();
+        fs.close();
+        is.close();
+    }
 }

--
Gitblit v1.9.3