From 68f80a7451a126335153ec00bb6cb520a5ae1f8d Mon Sep 17 00:00:00 2001
From: guoshilong <123456>
Date: Mon, 13 Nov 2023 17:02:45 +0800
Subject: [PATCH] 定时任务逻辑修改

---
 src/main/java/com/dji/sample/wayline/service/impl/WaylineJobServiceImpl.java |  244 +++++++++++++++++-------------------------------
 1 files changed, 87 insertions(+), 157 deletions(-)

diff --git a/src/main/java/com/dji/sample/wayline/service/impl/WaylineJobServiceImpl.java b/src/main/java/com/dji/sample/wayline/service/impl/WaylineJobServiceImpl.java
index 0c72c27..8ae67ba 100644
--- a/src/main/java/com/dji/sample/wayline/service/impl/WaylineJobServiceImpl.java
+++ b/src/main/java/com/dji/sample/wayline/service/impl/WaylineJobServiceImpl.java
@@ -1,9 +1,12 @@
 package com.dji.sample.wayline.service.impl;
 
 import com.alibaba.druid.stat.TableStat;
+import com.alibaba.fastjson.JSON;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
+import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
+import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.dji.sample.common.error.CommonErrorEnum;
 import com.dji.sample.common.model.CustomClaim;
@@ -281,108 +284,6 @@
             return response;
         }
 
-
-        //新增多条数据
-        //开始日期
-//        String parentId = "";
-//        for (int i = 0; i < param.getExecuteStartTimeArr().size(); i++) {
-//            List<Long> timeArr = param.getExecuteStartTimeArr().get(i);
-//
-//            long beginTimeLong = 0L;
-//            long endTimeLong = 0L;
-//            LocalDateTime beginTime = null;
-//            if (i == 0) {
-//                //父,开始-结束为整个区间
-//                //开始日期
-//                LocalDate startDate = LocalDate.ofInstant(Instant.ofEpochSecond(param.getTaskDays().get(0)), ZoneId.systemDefault());
-//                beginTimeLong = LocalDateTime.of(startDate, LocalTime.ofInstant(Instant.ofEpochSecond(param.getExecuteStartTimeArr().get(0).get(0)), ZoneId.systemDefault()))
-//                        .atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
-//
-//                //结束日期
-//                LocalDate endDate = LocalDate.ofInstant(Instant.ofEpochSecond(param.getTaskDays().get(1)), ZoneId.systemDefault());
-//                endTimeLong = LocalDateTime.of(endDate, LocalTime.ofInstant(Instant.ofEpochSecond(param.getExecuteStartTimeArr().get(param.getExecuteStartTimeArr().size() - 1).get(0)), ZoneId.systemDefault()))
-//                        .atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
-//            } else {
-//                //子,开始-结束为当前区间
-//                //开始日期
-//                LocalDate date = LocalDate.ofInstant(Instant.ofEpochSecond(timeArr.get(0)), ZoneId.systemDefault());
-//                //任务执行开始时间
-//                beginTime = LocalDateTime.of(date, LocalTime.ofInstant(Instant.ofEpochSecond(timeArr.get(0)), ZoneId.systemDefault()));
-//                //任务结束时间
-//                LocalDateTime endTime = timeArr.size() > 1 && Objects.nonNull(timeArr.get(1)) ?
-//                        LocalDateTime.of(date, LocalTime.ofInstant(Instant.ofEpochSecond(timeArr.get(1)), ZoneId.systemDefault())) : beginTime;
-//
-//                beginTimeLong = beginTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
-//                endTimeLong = endTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
-//            }
-//
-//
-//            //保存数据
-//            WaylineJobEntity waylineJobEntity = WaylineJobEntity.builder()
-//                    .parentId(parentId.equals("") ? null : parentId)
-//                    .jobId(UUID.randomUUID().toString())
-//                    .name(param.getName())
-//                    .dockSn(param.getDockSn())
-//                    .fileId(param.getFileId())
-//                    .username(customClaim.getUsername())
-//                    .workspaceId(customClaim.getWorkspaceId())
-//
-//                    .beginTime(beginTimeLong)
-//                    .endTime(endTimeLong)
-//                    .status(WaylineJobStatusEnum.PENDING.getVal())
-//                    .taskType(param.getTaskType().getVal())
-//                    .waylineType(param.getWaylineType().getVal())
-//                    .outOfControlAction(param.getOutOfControlAction())
-//                    .batteryCapacity(param.getMinBatteryCapacity())
-//                    .rthAltitude(param.getRthAltitude())
-//                    .mediaCount(0)
-//                    .repFreVal(param.getRepFreVal())
-//                    .repFreType(param.getRepFreType())
-//                    .repRuleType(param.getRepRuleType())
-//                    .repRuleVal(param.getRepRuleVal())
-//                    .executeTimeArr(param.getTaskPeriods())
-//                    .executeStartTimeArr(param.getExecuteStartTimeArr())
-//                    .build();
-//
-//            Optional<WaylineJobDTO> waylineJobOpt = insertWaylineJob(waylineJobEntity);
-//
-//            if (waylineJobOpt.isEmpty()) {
-//                throw new SQLException("任务创建失败");
-//            }
-//
-//
-//            if (i == 0) {
-//                //把第一个当作父
-//                parentId = waylineJobEntity.getId().toString();
-//            }
-//            WaylineJobDTO waylineJob = waylineJobOpt.get();
-//
-//            //条件任务
-//            if (param.getTaskType() == WaylineTaskTypeEnum.CONDITION) {
-//                //如果是条件任务类型,需要在任务参数中添加条件。
-//                waylineJob.setConditions(
-//                        WaylineTaskConditionDTO.builder()
-//                                .executableConditions(Objects.nonNull(param.getMinStorageCapacity()) ? WaylineTaskExecutableConditionDTO.builder().storageCapacity(param.getMinStorageCapacity()).build() : null)
-//                                .readyConditions(WaylineTaskReadyConditionDTO.builder()
-//                                        .batteryCapacity(param.getMinBatteryCapacity())
-//                                        .beginTime(beginTimeLong)
-//                                        .endTime(endTimeLong)
-//                                        .build())
-//                                .build());
-//            } else {
-//                waylineJob.setBeginTime(beginTime);
-//            }
-//
-//            if (waylineJobOpt.isEmpty()) {
-//                throw new SQLException("任务创建失败");
-//            }
-//
-//            ResponseResult response = this.publishOneFlightTask(waylineJob);
-//            if (ResponseResult.CODE_SUCCESS != response.getCode()) {
-//                return response;
-//            }
-//        }
-
         return ResponseResult.success();
     }
 
@@ -608,19 +509,7 @@
 
     @Override
     public PaginationData<WaylineJobDTO> getJobsByWorkspaceId(String workspaceId, long page, long pageSize, WaylineJobQueryParam waylineJobQueryParam) {
-//        Page<WaylineJobEntity> pageData = mapper.selectPage(
-//                new Page<WaylineJobEntity>(page, pageSize),
-//                new LambdaQueryWrapper<WaylineJobEntity>()
-//                        .eq(WaylineJobEntity::getWorkspaceId, workspaceId)
-//                        .eq(waylineJobDTO.getTaskType() !=null, WaylineJobEntity::getTaskType, waylineJobDTO.getTaskType())
-//                        .eq(waylineJobDTO.getStatus()!=null, WaylineJobEntity::getStatus, waylineJobDTO.getStatus())
-//                        .like(StringUtils.hasText(waylineJobDTO.getJobName()),WaylineJobEntity::getName,waylineJobDTO.getJobName())
-//
-//
-//                        .orderByDesc(WaylineJobEntity::getId));
-
         Page<WaylineJobEntity> pageData = mapper.getPage(new Page<WaylineJobEntity>(page, pageSize), waylineJobQueryParam, workspaceId);
-
 
         List<WaylineJobDTO> records = pageData.getRecords()
                 .stream()
@@ -628,6 +517,18 @@
                 .collect(Collectors.toList());
 
         return new PaginationData<WaylineJobDTO>(records, new Pagination(pageData));
+    }
+
+    @Override
+    public List<WaylineJobDTO> getChildrenJobs(String workspaceId, WaylineJobQueryParam waylineJobQueryParam) {
+
+        List<WaylineJobEntity> list= mapper.getJobs(workspaceId, waylineJobQueryParam);
+        List<WaylineJobDTO> records =list
+                .stream()
+                .map(this::entity2Dto)
+                .collect(Collectors.toList());
+
+        return records;
     }
 
 
@@ -794,17 +695,7 @@
 
     @Override
     public WaylineJobEntity getLatestJob(String workspaceId, WaylineJobQueryParam waylineJobQueryParam) {
-//        List<WaylineJobEntity> waylineJobEntities = mapper.selectList(new LambdaQueryWrapper<>(WaylineJobEntity.class)
-//                .eq(WaylineJobEntity::getWorkspaceId, workspaceId)
-//                .eq(WaylineJobEntity::getDockSn, waylineJobQueryParam.getDockSn())
-//                //获取状态为待执行
-//                        .eq(WaylineJobEntity::getStatus,1)
-//                .orderByDesc(WaylineJobEntity::getBeginTime)
-//        );
-
         WaylineJobEntity waylineJobEntity = mapper.getLatest(workspaceId, waylineJobQueryParam);
-
-
         return waylineJobEntity;
     }
 
@@ -858,38 +749,40 @@
         takeoffToPointParam.setMaxSpeed(10.0);
         takeoffToPointParam.setExitWaylineWhenRcLost(WaylineRcLostActionEnum.EXECUTE_RC_LOST_ACTION);
 
-        //起飞
-        ResponseResult responseResult = controlService.takeoffToPoint(sn, takeoffToPointParam);
-
-        if (responseResult.getCode() != ResponseResult.CODE_SUCCESS) {
-            return responseResult;
-        }
-        //向目标点飞行
-
-        FlyToPointParam flyToPointParam = new FlyToPointParam();
-        flyToPointParam.setMaxSpeed(14);
-        List<PointDTO> pointDTOS = new ArrayList<>();
-        targetList.forEach(pointPOJO -> {
-            PointDTO pointDTO = new PointDTO();
-            pointDTO.setHeight(150.0);
-            pointDTO.setLongitude(pointPOJO.getLon());
-            pointDTO.setLatitude(pointPOJO.getLat());
-
-            pointDTOS.add(pointDTO);
-        });
-        flyToPointParam.setPoints(pointDTOS);
-        ResponseResult responseResult1 = controlService.flyToPoint(sn, flyToPointParam);
-
-        if (responseResult1.getCode() != ResponseResult.CODE_SUCCESS) {
-            return responseResult1;
-        }
-
-        //返航
-        //return_home
-
-
         return ResponseResult.success();
+//        //起飞
+//        ResponseResult responseResult = controlService.takeoffToPoint(sn, takeoffToPointParam);
+//
+//        if (responseResult.getCode() != ResponseResult.CODE_SUCCESS) {
+//            return responseResult;
+//        }
+//        //向目标点飞行
+//
+//        FlyToPointParam flyToPointParam = new FlyToPointParam();
+//        flyToPointParam.setMaxSpeed(14);
+//        List<PointDTO> pointDTOS = new ArrayList<>();
+//        targetList.forEach(pointPOJO -> {
+//            PointDTO pointDTO = new PointDTO();
+//            pointDTO.setHeight(150.0);
+//            pointDTO.setLongitude(pointPOJO.getLon());
+//            pointDTO.setLatitude(pointPOJO.getLat());
+//
+//            pointDTOS.add(pointDTO);
+//        });
+//        flyToPointParam.setPoints(pointDTOS);
+//        ResponseResult responseResult1 = controlService.flyToPoint(sn, flyToPointParam);
+//
+//        if (responseResult1.getCode() != ResponseResult.CODE_SUCCESS) {
+//            return responseResult1;
+//        }
+//
+//        //返航
+//        //return_home
+//
+//
+//        return ResponseResult.success();
     }
+
 
     @Override
     public ResponseResult checkNextJob(WaylineJobDTO job)  throws SQLException  {
@@ -898,7 +791,10 @@
         params.setJobId(job.getJobId());
 
 
-        WaylineJobEntity currentJob = mapper.selectOne(Wrappers.query(params));
+        WaylineJobEntity job1 = mapper.selectOne(Wrappers.query(params));
+
+        WaylineJobEntity currentJob = JSON.parseObject(JSON.toJSONString(job1), WaylineJobEntity.class);
+
         if (currentJob == null) {
             return ResponseResult.error("该任务不存在");
         }
@@ -913,7 +809,7 @@
 
         long currentTime = System.currentTimeMillis();
         for (int i = 0; i < executeStartTimeArr.size(); i++) {
-            Long startTime = Long.valueOf(executeStartTimeArr.get(i).get(0));
+            Long startTime = executeStartTimeArr.get(i).get(0)*1000;
             //如果当前时间小于开始时间
             if (currentTime < startTime) {
                 //当前索引就是下一次要执行的时间
@@ -921,6 +817,11 @@
                 break;
             }
         }
+
+        if (indexTime == -1) {
+            return ResponseResult.success();
+        }
+
 
         List<Long> timeArr = currentJob.getExecuteStartTimeArr().get(indexTime);
         LocalDate date = LocalDate.ofInstant(Instant.ofEpochSecond(timeArr.get(0)), ZoneId.systemDefault());
@@ -1002,6 +903,34 @@
     }
 
 
+
+
+    @Override
+    public WaylineJobCountDTO patrolStatistics(String workspaceId,String queryTime) {
+        WaylineJobCountDTO waylineJobCountDTO = new WaylineJobCountDTO();
+        List<WaylineJobEntity> list = mapper.patrolStatistics(workspaceId,queryTime);
+        if (!CollectionUtils.isEmpty(list)){
+            waylineJobCountDTO.setTotalNumber(list.size());
+            long totalTime = list.stream().mapToLong(s -> s.getEndTime() - s.getBeginTime()).sum() / 1000;
+            StringBuffer buffer = new StringBuffer();
+            long h = totalTime / 3600;
+            long m = (totalTime % 3600) / 60;
+            buffer.append(h).append(" h ");
+            buffer.append(m).append(" min");
+            waylineJobCountDTO.setTotalDuration(buffer.toString());
+        }
+        return waylineJobCountDTO;
+    }
+
+    @Override
+    public void updateJobCollect(WaylineJobEntity waylineJob) {
+        new LambdaUpdateChainWrapper<>(mapper)
+                .eq(WaylineJobEntity::getJobId,waylineJob.getJobId())
+                .set(WaylineJobEntity::getCollectStatus,waylineJob.getCollectStatus())
+                .set(WaylineJobEntity::getUserId,waylineJob.getUserId())
+                .update();
+    }
+
     private void pauseJob(String workspaceId, String dockSn, String jobId, WaylineJobStatusEnum statusEnum) {
         if (WaylineJobStatusEnum.PAUSED == statusEnum && jobId.equals(waylineRedisService.getPausedWaylineJobId(dockSn))) {
             waylineRedisService.setPausedWaylineJob(dockSn, jobId);
@@ -1070,7 +999,8 @@
                 .waylineType(WaylineTemplateTypeEnum.find(entity.getWaylineType()))
                 .rthAltitude(entity.getRthAltitude())
                 .outOfControlAction(entity.getOutOfControlAction())
-                .mediaCount(entity.getMediaCount());
+                .mediaCount(entity.getMediaCount())
+                .hasChildren(entity.getHasChildren());
 
         if (Objects.nonNull(entity.getEndTime())) {
             builder.endTime(LocalDateTime.ofInstant(Instant.ofEpochMilli(entity.getEndTime()), ZoneId.systemDefault()));

--
Gitblit v1.9.3