From 77565e7990bdeacac09c63efffdd51dd8214b81c Mon Sep 17 00:00:00 2001
From: rain <167982779@qq.com>
Date: Fri, 14 Jun 2024 13:57:11 +0800
Subject: [PATCH] 将未推送字段更新到图斑表,db推送以图斑字段is_push为依据

---
 src/main/java/com/dji/sample/patches/utils/TimerUtil.java |  186 ++++++++++++++++++++++++++++++++--------------
 1 files changed, 128 insertions(+), 58 deletions(-)

diff --git a/src/main/java/com/dji/sample/patches/utils/TimerUtil.java b/src/main/java/com/dji/sample/patches/utils/TimerUtil.java
index e769198..4c9cccc 100644
--- a/src/main/java/com/dji/sample/patches/utils/TimerUtil.java
+++ b/src/main/java/com/dji/sample/patches/utils/TimerUtil.java
@@ -1,12 +1,11 @@
 package com.dji.sample.patches.utils;
 
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
-import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.dji.sample.common.model.CustomClaim;
-import com.dji.sample.common.model.ResponseResult;
 import com.dji.sample.media.dao.IFileMapper;
 import com.dji.sample.media.model.MediaFileEntity;
 import com.dji.sample.patches.config.pojo.PatchesConfigPojo;
+import com.dji.sample.patches.dao.GetPatchesMapper;
 import com.dji.sample.patches.dao.ShpToDataSourceMapper;
 import com.dji.sample.patches.model.entity.LotInfo;
 import com.dji.sample.patches.service.GetPatchesService;
@@ -24,19 +23,16 @@
 import org.springframework.http.*;
 import org.springframework.scheduling.annotation.Scheduled;
 import org.springframework.stereotype.Component;
+import org.springframework.transaction.annotation.Transactional;
 import org.springframework.util.LinkedMultiValueMap;
 import org.springframework.util.MultiValueMap;
 import org.springframework.web.client.RestTemplate;
 import org.springframework.web.multipart.MultipartFile;
 
-import javax.xml.transform.Result;
 import java.nio.file.StandardCopyOption;
 import java.time.LocalDateTime;
 
 import java.io.*;
-import java.net.HttpURLConnection;
-import java.net.URL;
-import java.nio.charset.StandardCharsets;
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.Paths;
@@ -46,6 +42,8 @@
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
 import java.util.stream.Collectors;
 
 import static com.dji.sample.patches.utils.MultipartFileTOFileUtil.convert;
@@ -67,6 +65,9 @@
     private ITbFJService tbFJService;
     @Autowired
     private GetPatchesService getPatchesService;
+
+    @Autowired
+    private GetPatchesMapper patchesMapper;
     @Autowired
     private IWaylineJobService waylineJobService;
     @Autowired
@@ -81,7 +82,7 @@
      */
     @Scheduled(cron = "0 0 0 * * ?")
     public void myTask() throws Exception {
-        String taskId="";
+        String taskId = "";
         String workspaceId = "4a574db8-4ad3-48f7-9f16-3edbcd8056e1";
 //        获取未规划的图斑集合
         List<List<LotInfo>> lists = getNoPlan();
@@ -90,43 +91,74 @@
             long timestamp = System.currentTimeMillis();
             String waylineName = "云飞行调查" + timestamp;
             //根据获取的图斑集合获得kmz航线文件
-            MultipartFile multipartFile = getFile(waylineName, list);
+            MultipartFile multipartFile = null;
+            try {
+                multipartFile = getFile(waylineName, list);
+            } catch (IOException e) {
+                throw new RuntimeException(e);
+            }
             //上传航线文件
             backWayline(multipartFile, waylineName, workspaceId, backclaim().getUsername());
             //将为规划的图斑状态更新为已规划
             updatePatchesStatu(list);
+//            waylineJobService.publishFlightTask(JobParam("656c62f4-cb86-4911-930d-b38089829348"),backclaim());
         }
-        List<List<MediaFileEntity>> list = getNoadd();
-        List<List<MediaFileEntity>> medias = convertToLists(list);
-        for (List<MediaFileEntity> media : medias) {
-            //清空FJ表
-            tbFJService.deleteData();
-            for (MediaFileEntity mediaFile : media) {
-                //获取媒体文件名里的dkbh和taskId
-                String dkbh = getDkbh(mediaFile.getFileName());
-                taskId = getTaskId(mediaFile.getFileName());
-                //获取该媒体文件的图斑信息
-                LotInfo lotInfo = getPatchesService.getLotinfo(dkbh, workspaceId);
-                tbFJService.insertOneData(mediaFile, lotInfo);
-                //更新媒体文件的是否添加状态
-                updateMediaStatu(mediaFile);
 
+    }
+
+    @Scheduled(cron = "0 0 0 * * ?")
+    public void myTask2() {
+        ExecutorService executor = Executors.newSingleThreadExecutor();
+        executor.execute(() -> {
+            try {
+                dbOperation();
+            } catch (Exception e) {
+                throw new RuntimeException(e);
             }
+        });
+        executor.shutdown();
+    }
+
+    @Transactional
+    public void dbOperation() throws Exception {
+        String workspaceId = "4a574db8-4ad3-48f7-9f16-3edbcd8056e1";
+        String taskId = "";
+        String dkbh = null;
+        List<List<LotInfo>> lists = convertToLists(getNoPush(workspaceId));
+        for (List<LotInfo> lotInfo : lists) {
+            for (LotInfo lotInfo1 : lotInfo) {
+                List<MediaFileEntity> media = getPatchesService.listPohto(lotInfo1.getDkbh(), workspaceId);
+                //清空FJ表
+                tbFJService.deleteData();
+                for (MediaFileEntity mediaFile : media) {
+                    //获取媒体文件名里的dkbh和taskId
+                    dkbh = getDkbh(mediaFile.getFileName());
+                    //获取该媒体文件的图斑信息
+                    LotInfo lotInfo2 = getPatchesService.getLotinfo(dkbh, workspaceId);
+                    taskId=lotInfo2.getTaskId();
+                    tbFJService.insertOneData(mediaFile, lotInfo2);
+                    getPatchesService.patchesPushed(taskId, dkbh, workspaceId);
+                }
+            }
+            //存储db到服务器
             dbSave(territoryConfigPojo.getResult(), territoryConfigPojo.getResultsave(), taskId);
+
             //发送请求给第三方接口
-            sendPostWithFileAndParameter(territoryConfigPojo.getResult(), taskId);
+            sendPostWithFileAndParameter(territoryConfigPojo.getResult(), taskId, dkbh, workspaceId);
+
         }
     }
 
     /**
      * 创建航线
+     *
      * @param waylineName
      * @param list
      * @return
      * @throws IOException
      */
     public MultipartFile getFile(String waylineName, List<LotInfo> list) throws IOException {
-        List<PointPO> coordinates = GeoToolsUtil.getRoutePointOrder(list, 28.62703, 115.867719);
+        List<PointPO> coordinates = GeoToolsUtil.getRoutePointOrder(list, 28.62452712442823, 115.85666327144976);
         XMLTemplateModel xmlModel = XMLTemplateModel.init(coordinates, list);
         CreateWaylineFileUtils.createWaylineFile(xmlModel, patchesConfigPojo.getTemplate(), patchesConfigPojo.getTargetTemplate(), patchesConfigPojo.getWaylines(), patchesConfigPojo.getTargetWaylines());
         // 压缩文件夹中的内容
@@ -137,6 +169,7 @@
 
     /**
      * 将航线上传Oss
+     *
      * @param multipartFile
      * @param waylineName
      * @param workspaceId
@@ -144,7 +177,7 @@
      * @return
      */
     public String backWayline(MultipartFile multipartFile, String waylineName, String workspaceId, String username) {
-        waylineFileService.importKmzFile(multipartFile, workspaceId, username);
+        waylineFileService.importKmzFileBack(multipartFile, workspaceId, username);
         WaylineFileEntity entity = waylineFileService.selectByName(waylineName);
         try {
             waylineFileService.getObjectUrl(workspaceId, entity.getWaylineId());
@@ -181,17 +214,14 @@
         }
     }
 
-    public void updateMediaStatu(MediaFileEntity mediaFile) {
-            mediaFile.setIsadd(1);
-            fileMapper.updateById(mediaFile);
+    public List<List<LotInfo>> getNoPush(String workspaceId) {
+        List<LotInfo> list = patchesMapper.selectList(new LambdaQueryWrapper<LotInfo>()
+                .eq(LotInfo::getIsPush, 0)
+                .eq(LotInfo::getInvestigate,1)
+                .eq(LotInfo::getWorkspaceId, workspaceId));
+        return groupTasks(list);
     }
 
-    public List<List<MediaFileEntity>> getNoadd() {
-        List<MediaFileEntity> list = fileMapper.selectList(new LambdaQueryWrapper<MediaFileEntity>()
-                .eq(MediaFileEntity::getIsadd, 0).like(MediaFileEntity::getFileName, "~"));
-        List<List<MediaFileEntity>> groupedFilenames = groupTasks(list);
-        return groupedFilenames;
-    }
 
     public CustomClaim backclaim() {
         CustomClaim claim = new CustomClaim();
@@ -200,6 +230,31 @@
         claim.setWorkspaceId("4a574db8-4ad3-48f7-9f16-3edbcd8056e1");
         claim.setUserType(1);
         return claim;
+    }
+
+    public static CreateJobParam JobParam(String waylineId) {
+        long timestamp = System.currentTimeMillis();
+        List<List<Long>> listOfLists = new ArrayList<>();
+        List<Long> sublist = new ArrayList<>();
+        sublist.add(1715941980L); // 添加整数值
+        listOfLists.add(sublist);
+        List<Long> list = new ArrayList<>();
+        list.add(1715941980L);
+        CreateJobParam param = new CreateJobParam();
+        param.setName("云飞行调查" + timestamp);
+        param.setExecuteStartTimeArr(listOfLists);
+        param.setTaskType(TIMED);
+        param.setOutOfControlAction(0);
+        param.setRepFreType(3);
+        param.setRepFreVal(1);
+        param.setRepRuleType(1);
+        param.setRthAltitude(80);
+        param.setWaylineType(WAYPOINT);
+        param.setTaskPeriods(listOfLists);
+        param.setTaskDays(list);
+        param.setFileId(waylineId);
+        param.setDockSn("4TADKCMB0010016");
+        return param;
     }
 
     public static <T> List<List<T>> convertToLists(List<List<T>> listOfLists) {
@@ -212,7 +267,19 @@
 
 
     // 对任务ID进行分组
-    public static List<List<MediaFileEntity>> groupTasks(List<MediaFileEntity> mediaFiles) {
+    public static List<List<LotInfo>> groupTasks(List<LotInfo> lotInfos) {
+        Map<String, List<LotInfo>> groupedTasks = new HashMap<>();
+        for (LotInfo lotInfo : lotInfos) {
+            String taskId = lotInfo.getTaskId();
+            if (!groupedTasks.containsKey(taskId)) {
+                groupedTasks.put(taskId, new ArrayList<>());
+            }
+            groupedTasks.get(taskId).add(lotInfo);
+        }
+        return new ArrayList<>(groupedTasks.values());
+    }
+
+    public static List<List<MediaFileEntity>> groupTask(List<MediaFileEntity> mediaFiles) {
         Map<String, List<MediaFileEntity>> groupedTasks = new HashMap<>();
         for (MediaFileEntity mediaFile : mediaFiles) {
             String taskId = getTaskId(mediaFile.getFileName());
@@ -241,39 +308,41 @@
 
     /**
      * 将成果发送post请求到第三方接口
+     *
      * @param filePath
      * @param taskId
      * @throws IOException
      */
-    public static void sendPostWithFileAndParameter(String filePath, String taskId) throws IOException {
+    public void sendPostWithFileAndParameter(String filePath, String taskId, String dkbh, String workspaceId) throws IOException {
         // 创建 RestTemplate 实例
-        try{
-        RestTemplate restTemplate = new RestTemplate();
+        try {
+            RestTemplate restTemplate = new RestTemplate();
 
-        // 读取文件内容为字节数组
-        byte[] fileContent = readFileToBytes(filePath);
+            // 读取文件内容为字节数组
+            byte[] fileContent = readFileToBytes(filePath);
 
-        // 构建请求体
-        MultiValueMap<String, Object> body = buildRequestBody(taskId, fileContent, filePath);
+            // 构建请求体
+            MultiValueMap<String, Object> body = buildRequestBody(taskId, fileContent, filePath);
 
-        // 设置请求头
-        HttpHeaders headers = new HttpHeaders();
-        headers.setContentType(MediaType.MULTIPART_FORM_DATA);
-        headers.set("X-SHARE-TOKEN", "981D9B822A9A09DE28B66243AA330DC666E1A82826B58EC3FE63FEB1EC8AA190");
-        headers.set("X-SHARE-AK", "gwc952d483-bfe5-476b-8431-ae15fe94c006");
+            // 设置请求头
+            HttpHeaders headers = new HttpHeaders();
+            headers.setContentType(MediaType.MULTIPART_FORM_DATA);
+            headers.set("X-SHARE-TOKEN", "981D9B822A9A09DE28B66243AA330DC666E1A82826B58EC3FE63FEB1EC8AA190");
+            headers.set("X-SHARE-AK", "gwc952d483-bfe5-476b-8431-ae15fe94c006");
 
-        // 构建请求实体
-        HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers);
+            // 构建请求实体
+            HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers);
 
-        // 发送请求
-                ResponseEntity<String> result=restTemplate.exchange(
-                "http://39.98.48.180:8087/landCloudWork/artifact/media/upload.action",
-//                "http://localhost:6789/territory/tbdkjbxx/upload",
-                HttpMethod.POST,
-                requestEntity,
-                String.class);
-        }catch (Exception e){
-            throw new IllegalArgumentException("db推送失败"+e.getMessage());
+            // 发送请求
+            ResponseEntity<String> response = restTemplate.exchange(
+                  "http://39.98.48.180:8087/landCloudWork/artifact/media/upload.action",
+//                    "http://localhost:6789/territory/tbdkjbxx/upload",
+                    HttpMethod.POST,
+                    requestEntity,
+                    String.class);
+            System.out.println(response);
+        } catch (Exception e) {
+            throw new IllegalArgumentException("db推送失败" + e.getMessage());
         }
     }
 
@@ -296,6 +365,7 @@
 
     /**
      * 将上传的db保存一份到本地
+     *
      * @param path
      * @param folder
      * @param taskId

--
Gitblit v1.9.3