From 2db1aa88e8ab53096a936163d686b90d8e056a99 Mon Sep 17 00:00:00 2001
From: rain <167982779@qq.com>
Date: Wed, 21 Aug 2024 23:18:33 +0800
Subject: [PATCH] 国土对接返回信息加密

---
 src/main/java/com/dji/sample/territory/service/impl/TbDkjbxxServiceImpl.java |  121 +++++++++++++++++++++++++++------------
 1 files changed, 83 insertions(+), 38 deletions(-)

diff --git a/src/main/java/com/dji/sample/territory/service/impl/TbDkjbxxServiceImpl.java b/src/main/java/com/dji/sample/territory/service/impl/TbDkjbxxServiceImpl.java
index c2ac1e0..3bfce17 100644
--- a/src/main/java/com/dji/sample/territory/service/impl/TbDkjbxxServiceImpl.java
+++ b/src/main/java/com/dji/sample/territory/service/impl/TbDkjbxxServiceImpl.java
@@ -8,16 +8,21 @@
 import com.dji.sample.patches.model.entity.LotInfo;
 import com.dji.sample.patches.utils.GeoToolsUtil;
 import com.dji.sample.patches.utils.MultipartFileTOFileUtil;
+import com.dji.sample.patches.utils.PointPO;
+import com.dji.sample.patches.utils.TimerUtil;
 import com.dji.sample.patches.xml.mode.XMLTemplateModel;
 import com.dji.sample.patches.xml.utils.CreateWaylineFileUtils;
 import com.dji.sample.territory.dao.ITbDkjbxxMapper;
 import com.dji.sample.territory.dao.ITbFjMapper;
 import com.dji.sample.territory.model.entity.TbDkjbxxEntity;
 import com.dji.sample.territory.model.entity.TbFjEntity;
+import com.dji.sample.territory.model.entity.param.UploadUrlParam;
+import com.dji.sample.territory.pojo.TerritoryConfigPojo;
 import com.dji.sample.territory.service.ITbDkjbxxService;
 import org.locationtech.jts.geom.Coordinate;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
 import org.springframework.util.FileCopyUtils;
 import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.multipart.MultipartFile;
@@ -25,11 +30,16 @@
 import java.io.*;
 import java.net.HttpURLConnection;
 import java.net.URL;
+import java.net.URLConnection;
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Objects;
+import java.util.UUID;
 
 import static com.dji.sample.patches.utils.MultipartFileTOFileUtil.convert;
 import static com.dji.sample.patches.utils.ZipUtil.zipFolder;
+import static com.dji.sample.territory.utils.CoordinateSystemUtil.*;
+
 
 /**
  * @PROJECT_NAME: drone
@@ -38,13 +48,16 @@
  * @DATE: 2024/4/10 11:19
  */
 @Service
-@DS("sqlite")
+@DS("sqlite-resource")
 public class TbDkjbxxServiceImpl extends ServiceImpl<ITbDkjbxxMapper, TbDkjbxxEntity> implements ITbDkjbxxService {
-    private static final String RESOURCE_FILE_PATH = "DB/resource_db.db";
     @Autowired
-    private ITbDkjbxxMapper mapper;
+    private ITbDkjbxxMapper iTbDkjbxxMapper;
+    @Autowired
+    private TbFjServiceImpl tbFjService;
     @Autowired
     private PatchesConfigPojo patchesConfigPojo;
+    @Autowired
+    private TerritoryConfigPojo territoryConfigPojo;
 
     /**
      * 上传DB文件并覆盖之前DB文件,自动读取数据到本地数据库
@@ -52,15 +65,19 @@
      * @param file
      * @return
      */
-    public ResponseResult<String> uploadFile(MultipartFile file) {
+    public ResponseResult uploadFile(MultipartFile file) {
         if (file.isEmpty()) {
-            return ResponseResult.error("所上传的文件为空");
+            throw new IllegalArgumentException("上传文件为空");
+        }
+        boolean fileName = Objects.requireNonNull(file.getOriginalFilename()).endsWith("db");
+        if (!fileName) {
+            throw new IllegalArgumentException("检查文件是否为db文件");
         }
         try {
             // 获取上传的文件输入流
             InputStream inputStream = file.getInputStream();
             // 创建输出流,将文件内容写入资源文件
-            OutputStream outputStream = new FileOutputStream(RESOURCE_FILE_PATH);
+            OutputStream outputStream = new FileOutputStream(territoryConfigPojo.getResource());
             byte[] buffer = new byte[1024];
             int bytesRead;
             while ((bytesRead = inputStream.read(buffer)) != -1) {
@@ -71,57 +88,85 @@
             return ResponseResult.success("文件上传成功");
         } catch (IOException e) {
             e.printStackTrace();
-            return ResponseResult.error(e.getMessage()); // 将异常信息包含在响应中返回给客户端
+            return ResponseResult.error(e.getMessage());
         }
     }
 
+    @Transactional
     public MultipartFile listFile(String workspaceId, String waylineName, double airportLat, double airportLon) throws IOException {
-        List<TbDkjbxxEntity> list = mapper.selectList(null);
+        List<TbDkjbxxEntity> list = iTbDkjbxxMapper.selectList(null);
         List<LotInfo> info = dbConvertToEntity(list);
-        Coordinate[] coordinates = GeoToolsUtil.getRoutePointOrder(info, airportLat, airportLon);
+        List<PointPO> coordinates = GeoToolsUtil.getRoutePointOrder(info, airportLat, airportLon);
+        // 创建XML模板模型
         XMLTemplateModel xmlModel = XMLTemplateModel.init(coordinates, info);
         CreateWaylineFileUtils.createWaylineFile(xmlModel, patchesConfigPojo.getTemplate(), patchesConfigPojo.getTargetTemplate(), patchesConfigPojo.getWaylines(), patchesConfigPojo.getTargetWaylines());
-        // 压缩文件夹中的内容
+        // 压缩文件夹中的内容到KMZ文件
         String destKMZFile = patchesConfigPojo.getDestKMZFile() + waylineName + ".kmz"; // 输出的KMZ文件路径
         zipFolder(patchesConfigPojo.getSourceDir(), destKMZFile);
-        MultipartFile multipartFile = convert(new File(destKMZFile));
-        return multipartFile;
+        // 将压缩文件转换为MultipartFile对象
+        return convert(new File(destKMZFile));
     }
 
+    @Override
+    public void uploadUrl(String url, String taskName) {
+        if (url.isEmpty()) {
+            throw new IllegalArgumentException("上传文件为空");
+        }
+        String saveFilePath = territoryConfigPojo.getResource(); // 要保存文件的本地路径
+        try {
+            downloadFile(url, saveFilePath);
+        } catch (IOException e) {
+            throw new IllegalArgumentException("下载db文件发生错误" + e);
+        }
+        TimerUtil util = new TimerUtil();
+        util.dbSave(territoryConfigPojo.getResource(), territoryConfigPojo.getResourcesave(), taskName);
+
+    }
+
+    /**
+     * 根据地址下载文件
+     *
+     * @param fileUrl
+     * @param saveFilePath
+     * @throws IOException
+     */
+    public static void downloadFile(String fileUrl, String saveFilePath) throws IOException {
+        // 创建URL对象
+        URL url = new URL(fileUrl);
+
+        // 打开连接
+        try (BufferedInputStream in = new BufferedInputStream(url.openStream());
+             FileOutputStream fileOutputStream = new FileOutputStream(saveFilePath)) {
+            byte[] dataBuffer = new byte[1024];
+            int bytesRead;
+            while ((bytesRead = in.read(dataBuffer, 0, 1024)) != -1) {
+                fileOutputStream.write(dataBuffer, 0, bytesRead);
+            }
+        }
+    }
+
+    /**
+     * 将TbDkjbxxEntity类型的列表转换为LotInfo类型的列表。
+     *
+     * @param list TbDkjbxxEntity类型的列表,表示数据库中的实体列表。
+     * @return List<LotInfo>类型的列表,表示转换后的业务实体列表。
+     */
     private List<LotInfo> dbConvertToEntity(List<TbDkjbxxEntity> list) {
-        List<LotInfo> infos =new ArrayList<>();
+        List<LotInfo> infos = new ArrayList<>();
         for (TbDkjbxxEntity file : list) {
+            // 使用Builder模式构建LotInfo对象
             LotInfo.LotInfoBuilder builder = LotInfo.builder();
             if (file != null) {
-                builder.bsm(file.getBsm())
-                        .bz(file.getBz())
-                        .dkmj(file.getDkmj())
-                        .dkmc(file.getDkmc())
-                        .dkbh(file.getDkbh())
-                        .xmc(file.getXmc())
-                        .xzb(file.getXzb())
-                        .sfbhzdk(file.getSfbhzdk())
-                        .sjlx(file.getSjlx())
-                        .dkfw(file.getDkfw())
-                        .xzqdm(file.getXzqdm())
-                        .yzb(file.getYzb())
-                        .kzxx(file.getKzxx())
-                        .dklx(file.getDklx())
+                // 从TbDkjbxxEntity实体中提取信息,构建LotInfo对象
+                builder.bsm(file.getFId())
+                        .dkbh(file.getFTbbh())
+                        .dkfw(file.getFShape())
+                        .xzqdm(file.getFXzqdm())
                         .build();
+                // 将构建好的LotInfo对象添加到infos列表中
                 infos.add(builder.build());
             }
         }
         return infos;
-    }
-    public void insert(List<LotInfo> list) {
-        for (LotInfo lotInfo : list) {
-            TbDkjbxxEntity entity = new TbDkjbxxEntity();
-            entity.setBsm(lotInfo.getBsm());
-            entity.setDkbh(lotInfo.getDkbh());
-            entity.setXzb(lotInfo.getXzb());
-            entity.setYzb(lotInfo.getYzb());
-            entity.setDkfw(lotInfo.getDkfw());
-            mapper.insert(entity);
-        }
     }
 }
\ No newline at end of file

--
Gitblit v1.9.3