From 3430a3dd889ba51d06cbdb77014aa63d0a070e48 Mon Sep 17 00:00:00 2001
From: rain <167982779@qq.com>
Date: Fri, 16 Aug 2024 16:38:08 +0800
Subject: [PATCH] 优化国土对接接口

---
 src/main/java/com/dji/sample/patches/utils/TimerUtil.java |  194 +++++++++++++++++++++++++++---------------------
 1 files changed, 110 insertions(+), 84 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 ce3d2a0..b8c1fe2 100644
--- a/src/main/java/com/dji/sample/patches/utils/TimerUtil.java
+++ b/src/main/java/com/dji/sample/patches/utils/TimerUtil.java
@@ -2,6 +2,10 @@
 
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.dji.sample.common.model.CustomClaim;
+import com.dji.sample.droneairport.dao.DbUploadMapper;
+import com.dji.sample.droneairport.model.Entity.DbUploadEntity;
+import com.dji.sample.manage.dao.IDeviceMapper;
+import com.dji.sample.manage.model.entity.DeviceEntity;
 import com.dji.sample.media.dao.IFileMapper;
 import com.dji.sample.media.model.MediaFileEntity;
 import com.dji.sample.patches.config.pojo.PatchesConfigPojo;
@@ -53,6 +57,7 @@
 
 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.SM3.sm3;
 import static com.dji.sample.wayline.model.enums.WaylineTaskTypeEnum.TIMED;
 import static com.dji.sample.wayline.model.enums.WaylineTemplateTypeEnum.WAYPOINT;
 
@@ -78,7 +83,10 @@
     private TerritoryConfigPojo territoryConfigPojo;
     @Autowired
     private IWaylineJobBreakPointService jobBreakPointService;
-
+    @Autowired
+    private IDeviceMapper deviceMapper;
+    @Autowired
+    private DbUploadMapper dbUploadMapper;
     /**
      * 定时器,将没有规划的图斑生成航线,并将航线飞完后的成果数据进行保存推送
      *
@@ -249,6 +257,9 @@
         String taskId = "";
         String dkbh = null;
         List<List<LotInfo>> lists = convertToLists(getNoPush(workspaceId));
+        if (lists.isEmpty()){
+            return;
+        }
         for (List<LotInfo> lotInfo : lists) {
             for (LotInfo lotInfo1 : lotInfo) {
                 List<MediaFileEntity> media = getPatchesService.listPohto(lotInfo1.getDkbh(), workspaceId);
@@ -273,29 +284,55 @@
         }
     }
     @Scheduled(cron = "0 3 17 * * ?")
-    public void GtdbOperation() throws Exception {
-        String taskId = "";
-        String dkbh = null;
-        List<List<LotInfo>> lists = convertToLists(getNoPush());
-        for (List<LotInfo> lotInfo : lists) {
-            for (LotInfo lotInfo1 : lotInfo) {
-                List<MediaFileEntity> media = getPatchesService.listPohto(lotInfo1.getDkbh());
-                //清空FJ表
-                tbFJService.deleteData();
-                for (MediaFileEntity mediaFile : media) {
-                    //获取媒体文件名里的dkbh和taskId
-                    dkbh = getDkbh(mediaFile.getFileName());
-                    //获取该媒体文件的图斑信息
-                    LotInfo lotInfo2 = getPatchesService.getLotinfo(dkbh);
-                    taskId = lotInfo2.getTaskId();
-                    tbFJService.insertOneData(mediaFile, lotInfo2);
-                    getPatchesService.patchesPushed(taskId, dkbh);
-                }
-            }
-            //存储db到服务器
-            dbSaveGt(territoryConfigPojo.getResult(), territoryConfigPojo.getResultGtsave(), taskId);
+   public void GtdbOperation() throws Exception {
+    String taskId = "";
+    String dkbh = null;
+    String code="";
+    int count = 0;
+    Set<String> dkbhSet = new HashSet<>(); // 用于存储不同的dkbh
+    List<List<LotInfo>> lists = convertToLists(getNoPush());
+        if (lists.isEmpty()){
+            return;
         }
+        tbFJService.deleteData();
+    for (List<LotInfo> lotInfo : lists) {
+        for (LotInfo lotInfo1 : lotInfo) {
+            List<MediaFileEntity> media = getPatchesService.listPohto(lotInfo1.getDkbh());
+            // 清空FJ表
+            for (MediaFileEntity mediaFile : media) {
+                // 获取媒体文件名里的dkbh和taskId
+                dkbh = getDkbh(mediaFile.getFileName());
+                dkbhSet.add(dkbh); // 添加dkbh到set中
+                // 获取该媒体文件的图斑信息
+                LotInfo lotInfo2 = getPatchesService.getLotinfo(dkbh);
+                taskId = lotInfo2.getTaskId();
+                code=lotInfo2.getXzqdm();
+                count++;
+                // 插入数据到数据库
+                tbFJService.insertOneData(mediaFile, lotInfo2);
+                getPatchesService.patchesPushed(taskId, dkbh);
+            }
+        }
+
+        // 存储db到服务器
+        String dbName = dbSaveGt(territoryConfigPojo.getResult(), territoryConfigPojo.getResultGtsave(), taskId);
+        DbUploadEntity entity = new DbUploadEntity();
+        File file = new File(territoryConfigPojo.getResult());
+        byte[] bytesArray = Files.readAllBytes(file.toPath());
+        long currentTimeMillis = System.currentTimeMillis();
+        entity.setTimestamp(currentTimeMillis);
+        entity.setSize(getFileSize(file))  ;
+        entity.setHash(sm3(bytesArray));
+        entity.setCount(dkbhSet.size());// 统计dkbh的数量
+        entity.setUrl(territoryConfigPojo.getResultGtsave()+"/"+dbName);
+        entity.setRegioncode(code);
+        entity.setExtension(".db");
+        entity.setAttachmentCount(count);
+        entity.setTaskId(taskId);
+        dbUploadMapper.insert(entity);
     }
+}
+
 
     /**
      * 创建航线
@@ -375,7 +412,7 @@
     public List<List<LotInfo>> getNoPush(String workspaceId) {
         List<LotInfo> list = patchesMapper.selectList(new LambdaQueryWrapper<LotInfo>()
                 .eq(LotInfo::getIsPush, 0)
-                .eq(LotInfo::getType, 1)
+                .eq(LotInfo::getType, 2)
                 .eq(LotInfo::getInvestigate, 1)
                 .eq(LotInfo::getWorkspaceId, workspaceId));
         return groupTasks(list);
@@ -383,7 +420,7 @@
     public List<List<LotInfo>> getNoPush() {
         List<LotInfo> list = patchesMapper.selectList(new LambdaQueryWrapper<LotInfo>()
                 .eq(LotInfo::getIsPush, 0)
-                .eq(LotInfo::getType, 0)
+                .eq(LotInfo::getType, 1)
                 .eq(LotInfo::getInvestigate, 1));
         return groupTasks(list);
     }
@@ -525,13 +562,13 @@
         return Files.readAllBytes(path);
     }
 
-    public static void sendPostWithParameters(String name, String fileId, String time, List<Long> taskDays, List<List<Long>> taskPeriods, String jobId) throws IOException {
+    public  void sendPostWithParameters(String name, String fileId, String time, List<Long> taskDays, List<List<Long>> taskPeriods, String jobId,String docksn) throws IOException {
         try {
             RestTemplate restTemplate = new RestTemplate();
 
             // 构建请求体
-            String jsonBody = buildRequestBody(name, fileId, time, taskDays, taskPeriods, jobId);
-
+            String jsonBody = buildRequestBody(name, fileId, time, taskDays, taskPeriods, jobId,docksn);
+            String workspaceId =getWorkspaceIdByDocksn(docksn);
             // 设置请求头
             HttpHeaders headers = new HttpHeaders();
             headers.setContentType(MediaType.APPLICATION_JSON);
@@ -546,49 +583,19 @@
             // 发送请求
             ResponseEntity<String> response = restTemplate.exchange(
 //                    "https://wrj.shuixiongit.com/drone-api/wayline/api/v1/workspaces/4a574db8-4ad3-48f7-9f16-3edbcd8056e1/flight-tasks",
-                    "http://localhost:6789/wayline/api/v1/workspaces/4a574db8-4ad3-48f7-9f16-3edbcd8056e1/flight-tasks",
+                    "http://localhost:6789/wayline/api/v1/workspaces/"+workspaceId+"/flight-tasks",
                     HttpMethod.POST,
                     requestEntity,
                     String.class);
-            System.out.println(response);
-            System.out.println(response);
-            System.out.println(response);
-            System.out.println(response);
-            System.out.println(response);
-            System.out.println(response);
-            System.out.println(response);
-            System.out.println(response);
-            System.out.println(response);
-            System.out.println(response);
-            System.out.println(response);
-            System.out.println(response);
-            System.out.println(response);
-            System.out.println(response);
-            System.out.println(response);
-            System.out.println(response);
-            System.out.println(response);
-            System.out.println(response);
-            System.out.println(response);
-            System.out.println(response);
-            System.out.println(response);
-            System.out.println(response);
-            System.out.println(response);
-            System.out.println(response);
-            System.out.println(response);
-            System.out.println(response);
-            System.out.println(response);
-            System.out.println(response);
-            System.out.println(response);
-            System.out.println(response);
-            System.out.println(response);
-            System.out.println(response);
-            System.out.println(response);
-            System.out.println(response);
         } catch (Exception e) {
             throw new IllegalArgumentException("Request failed: " + e.getMessage());
         }
     }
 
+    public String getWorkspaceIdByDocksn(String docksn){
+      DeviceEntity entity=  deviceMapper.selectOne(new LambdaQueryWrapper<DeviceEntity>().eq(DeviceEntity::getDeviceSn,docksn));
+      return entity.getWorkspaceId();
+    }
     public static void sendPostWithParameters(String name, String fileId, String time, List<Long> taskDays, List<List<Long>> taskPeriods) throws IOException {
         try {
             RestTemplate restTemplate = new RestTemplate();
@@ -652,7 +659,7 @@
                 + "}", name, fileId, select_execute_time, taskDays, taskPeriods);
     }
 
-    private static String buildRequestBody(String name, String fileId, String select_execute_time, List<Long> taskDays, List<List<Long>> taskPeriods, String jobid) {
+    private static String buildRequestBody(String name, String fileId, String select_execute_time, List<Long> taskDays, List<List<Long>> taskPeriods, String jobid, String dockSn) {
         // 将 List 和 String 类型的参数格式化为 JSON 字符串
         String taskDaysJson = taskDays != null ? taskDays.toString() : "[]";
         String taskPeriodsJson = taskPeriods != null ? taskPeriods.toString() : "[]";
@@ -661,7 +668,7 @@
         return String.format("{"
                 + "\"name\":\"%s\","
                 + "\"file_id\":\"%s\","
-                + "\"dock_sn\":\"4TADKCM0010016\","
+                + "\"dock_sn\":\"%s\","
                 + "\"task_type\":1,"
                 + "\"select_execute_time\":\"%s\","
                 + "\"rth_altitude\":100,"
@@ -679,8 +686,9 @@
                 + "\"task_periods\":%s,"
                 + "\"wayline_type\":0,"
                 + "\"job_id\":\"%s\""
-                + "}", name, fileId, select_execute_time, taskDaysJson, taskPeriodsJson, jobid);
+                + "}", name, fileId, dockSn, select_execute_time, taskDaysJson, taskPeriodsJson, jobid);
     }
+
 
     /**
      * 将上传的db保存一份到本地
@@ -717,30 +725,17 @@
             throw new RuntimeException("Db文件复制失败", e); // 捕获并抛出原始异常
         }
     }
-    public void dbSaveGt(String path, String folder, String taskId) {
-        String sourceFilePath = path; // 源文件路径
-        String targetFolderPath = fileFold(folder,taskId).getPath(); // 目标文件夹路径
-
+    public String dbSaveGt(String path, String folder, String taskId) {
         try {
-            // 创建目标文件夹
-            File targetFolder = new File(targetFolderPath);
-            if (!targetFolder.exists()) {
-                targetFolder.mkdirs();
-            }
-
-            // 获取当前时间
-            LocalDateTime currentTime = LocalDateTime.now();
-
-            // 格式化时间,生成文件名
-            String timeName = currentTime.format(DateTimeFormatter.ofPattern("HHmmss"));
-            String fileName = timeName + "_" + taskId + ".db";
+            String fileName = taskId + ".db";
             // 构建目标文件路径
-            String targetFilePath = targetFolderPath + File.separator + fileName;
+            String targetFilePath = folder + File.separator + fileName;
 
-            // 复制文件到目标文件夹
-            Path sourcePath = Paths.get(sourceFilePath);
+            // 复制文件到目标位置
+            Path sourcePath = Paths.get(path);
             Path targetPath = Paths.get(targetFilePath);
             Files.copy(sourcePath, targetPath, StandardCopyOption.REPLACE_EXISTING);
+            return fileName;
         } catch (IOException e) {
             throw new RuntimeException("Db文件复制失败", e); // 捕获并抛出原始异常
         }
@@ -801,6 +796,21 @@
         // 将毫秒级时间戳转换为秒级时间戳
         return timestamp / 1000;
     }
+    public static long getNextDayEightAMTimestamp(int hour) {
+        // 获取当前时间
+        LocalDateTime now = LocalDateTime.now();
+
+        // 获取第二天的日期并设置时间为08:00
+        LocalDateTime nextDayEightAM = now.plusDays(1).withHour(8).withMinute(0).withSecond(0).withNano(0);
+
+        // 根据传入的hour参数增加小时
+        LocalDateTime adjustedTime = nextDayEightAM.plusHours(hour);
+
+        // 转换为时间戳(秒级)
+        ZonedDateTime zonedDateTime = adjustedTime.atZone(ZoneId.systemDefault());
+        return zonedDateTime.toEpochSecond();
+    }
+
 
     public static Long addOneHourToTimestamp(Long timestampInSeconds, long hours, long min) {
         // 将输入的秒级时间戳转换为Instant对象
@@ -847,4 +857,20 @@
         // 格式化时间,生成当前时间
         return currentTime.format(DateTimeFormatter.ofPattern("MMddHHmmssSSS"));
     }
+    public static long getFileSize(File file) {
+        if (file == null || !file.exists() || !file.isFile()) {
+            return -1L; // 文件不存在或不是普通文件
+        }
+        return file.length();
+    }
+
+    public static void main(String[] args) {
+        File file = new File("DB/result_db.db");
+        long size = getFileSize(file);
+        System.out.println("文件大小: " + size + " 字节");
+    }
+    public static void  getFileInfo(File file){
+        long size=getFileSize(file);
+
+    }
 }

--
Gitblit v1.9.3