From 71fc7c05b3c641bf898fd84975d6f036a05f829b Mon Sep 17 00:00:00 2001
From: zhaowenyu <35589726@qq.com>
Date: Thu, 04 Dec 2025 09:09:49 +0800
Subject: [PATCH] 2025-12-03 zwy 删除任务时判断循环任务task_type=3的,不判断设备是否在线情况直接删除

---
 ard-sample/src/main/java/com/ard/sample/wayline/service/impl/WaylineJobServiceImpl.java |  105 +++++++++++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 95 insertions(+), 10 deletions(-)

diff --git a/ard-sample/src/main/java/com/ard/sample/wayline/service/impl/WaylineJobServiceImpl.java b/ard-sample/src/main/java/com/ard/sample/wayline/service/impl/WaylineJobServiceImpl.java
index 8b649e9..930ca2e 100644
--- a/ard-sample/src/main/java/com/ard/sample/wayline/service/impl/WaylineJobServiceImpl.java
+++ b/ard-sample/src/main/java/com/ard/sample/wayline/service/impl/WaylineJobServiceImpl.java
@@ -65,6 +65,7 @@
 @Service
 @Transactional
 @Slf4j
+@SuppressWarnings("all")
 public class WaylineJobServiceImpl implements IWaylineJobService {
 
     @Autowired
@@ -489,9 +490,9 @@
      * @throws IllegalArgumentException 如果任务状态不正确,抛出非法参数异常
      */
     @Override
-    public void cancelFlightTask(Collection<String> jobIds) {
+    public ResponseResult cancelFlightTask(Collection<String> jobIds) {
         List<WaylineJobDTO> waylineJobs = getJobsByConditions(jobIds, WaylineJobStatusEnum.PENDING);
-
+//        ResponseResult.error(errorCode, CommonErrorEnum.getMsgByCode(errorCode)));
 //        Set<String> waylineJobIds = waylineJobs.stream().map(WaylineJobDTO::getJobId).collect(Collectors.toSet());
 //        // Check if the task status is correct.
 //        boolean isErr = !jobIds.removeAll(waylineJobIds) || !jobIds.isEmpty();
@@ -501,13 +502,85 @@
 ////                    + Arrays.toString(jobIds.toArray()));
 //        }
 
-        // Group job id by dock sn.
+        // 按 dock sn 对作业 ID 进行分组。
         Map<String, List<String>> dockJobs = waylineJobs.stream()
                 .collect(Collectors.groupingBy(WaylineJobDTO::getDockSn,
                         Collectors.mapping(WaylineJobDTO::getJobId, Collectors.toList())));
-        dockJobs.forEach((dockSn, idList) -> this.publishCancelTask(dockSn, idList));
+//        dockJobs.forEach((dockSn, idList) -> this.publishCancelTask(dockSn, idList));
+        for(Map.Entry<String, List<String>> entry : dockJobs.entrySet()) {
+            String docksn = entry.getKey();
+            List<String> jobIdList = entry.getValue();
+           ResponseResult r =   publishCancelTask(docksn, jobIdList);
+           String a = r.getMessage();
+            return r ;
+        }
 
+        return ResponseResult.success();
     }
+
+// region 代码备份
+//    @Override
+//    public void cancelFlightTask(Collection<String> jobIds) {
+//        List<WaylineJobDTO> waylineJobs = getJobsByConditions(jobIds, WaylineJobStatusEnum.PENDING);
+//
+////        Set<String> waylineJobIds = waylineJobs.stream().map(WaylineJobDTO::getJobId).collect(Collectors.toSet());
+////        // Check if the task status is correct.
+////        boolean isErr = !jobIds.removeAll(waylineJobIds) || !jobIds.isEmpty();
+////        if (isErr) {
+////            throw new IllegalArgumentException("任务开始后,无法取消。" + Arrays.toString(jobIds.toArray()));
+//////            throw new IllegalArgumentException("These tasks have an incorrect status and cannot be canceled. "
+//////                    + Arrays.toString(jobIds.toArray()));
+////        }
+//
+//        // Group job id by dock sn.
+//        Map<String, List<String>> dockJobs = waylineJobs.stream()
+//                .collect(Collectors.groupingBy(WaylineJobDTO::getDockSn,
+//                        Collectors.mapping(WaylineJobDTO::getJobId, Collectors.toList())));
+//        dockJobs.forEach((dockSn, idList) -> this.publishCancelTask(dockSn, idList));
+//
+//    }
+
+//@Override
+//public void publishCancelTask(String dockSn, List<String> jobIds) {
+//
+//    boolean isOnline = deviceService.checkDeviceOnline(dockSn);
+//    if (!isOnline) {
+//        Integer errorCode = CommonErrorEnum.DEVICE_OFFLINE.getErrorCode();
+//
+//        //return ResponseResult.error(errorCode, CommonErrorEnum.getMsgByCode(errorCode)));
+//        throw new RuntimeException(CommonErrorEnum.DEVICE_OFFLINE.getErrorMsg());
+//    }
+//    String topic = TopicConst.THING_MODEL_PRE + TopicConst.PRODUCT + dockSn + TopicConst.SERVICES_SUF;
+//
+//    CommonTopicResponse<Object> response = CommonTopicResponse.builder()
+//            .tid(UUID.randomUUID().toString())
+//            .bid(UUID.randomUUID().toString())
+//            .timestamp(System.currentTimeMillis())
+//            .data(Map.of(MapKeyConst.FLIGHT_IDS, jobIds))
+//            .method(WaylineMethodEnum.FLIGHT_TASK_CANCEL.getMethod())
+//            .build();
+//
+//    ServiceReply serviceReply = messageSender.publishWithReply(topic, response);
+//    if (ResponseResult.CODE_SUCCESS != serviceReply.getResult()) {
+//        String errorMsg = CommonErrorEnum.getMsgByCode(serviceReply.getResult());
+//        log.info("取消飞行任务失败 ====> 设备sn: {}, 错误码: {}, {}", dockSn, serviceReply.getResult(), errorMsg);
+//        //throw new RuntimeException("Failed to cancel the wayline job of " + dockSn);
+//        throw new RuntimeException("设备:" + dockSn + "取消飞行任务失败,错误码:" + serviceReply.getResult() + "," + errorMsg);
+//    }
+//
+//    for (String jobId : jobIds) {
+//        this.updateJob(WaylineJobDTO.builder()
+//                .jobId(jobId)
+//                .status(WaylineJobStatusEnum.CANCEL.getVal())
+//                .completedTime(LocalDateTime.now())
+//                .build());
+//        RedisOpsUtils.zRemove(RedisConst.WAYLINE_JOB_TIMED_EXECUTE, RedisConst.DELIMITER + dockSn + RedisConst.DELIMITER + jobId);
+//        // 原流程是修改表中数据的status状态值,现在的精力是直接删除
+//        this.deleteTask(jobId);
+//    }
+//
+//}
+// endregion
 
     /**
      * 发布取消任务
@@ -517,10 +590,22 @@
      * @throws RuntimeException 如果设备离线或取消任务失败,则抛出运行时异常
      */
     @Override
-    public void publishCancelTask(String dockSn, List<String> jobIds) {
+    public ResponseResult publishCancelTask(String dockSn, List<String> jobIds) {
+        for(String jobId : jobIds) {
+            Optional<WaylineJobDTO> waylineJobDTO = this.getJobByJobId(jobId);
+            if (waylineJobDTO.isEmpty()) {
+                return ResponseResult.error("任务不存在,jobId: " + jobId);
+            }
+            if (waylineJobDTO.get().getTaskType() == 3) {
+                // 已经执行完的子任务是否删除,或者清掉父任务ID,和正常任务一起显示在列表中。
+                this.deleteTask(jobId);
+                return ResponseResult.success();
+            }
+        }
         boolean isOnline = deviceService.checkDeviceOnline(dockSn);
         if (!isOnline) {
-            throw new RuntimeException(CommonErrorEnum.DEVICE_OFFLINE.getErrorMsg());
+            Integer errorCode = CommonErrorEnum.DEVICE_OFFLINE.getErrorCode();
+            return ResponseResult.error(errorCode, CommonErrorEnum.getMsgByCode(errorCode));
         }
         String topic = TopicConst.THING_MODEL_PRE + TopicConst.PRODUCT + dockSn + TopicConst.SERVICES_SUF;
 
@@ -536,8 +621,7 @@
         if (ResponseResult.CODE_SUCCESS != serviceReply.getResult()) {
             String errorMsg = CommonErrorEnum.getMsgByCode(serviceReply.getResult());
             log.info("取消飞行任务失败 ====> 设备sn: {}, 错误码: {}, {}", dockSn, serviceReply.getResult(), errorMsg);
-            //throw new RuntimeException("Failed to cancel the wayline job of " + dockSn);
-            throw new RuntimeException("设备:" + dockSn + "取消飞行任务失败,错误码:" + serviceReply.getResult() + "," + errorMsg);
+            return ResponseResult.error(serviceReply.getResult(), errorMsg);
         }
 
         for (String jobId : jobIds) {
@@ -550,7 +634,7 @@
             // 原流程是修改表中数据的status状态值,现在的精力是直接删除
             this.deleteTask(jobId);
         }
-
+        return ResponseResult.success();
     }
 
     @Override
@@ -570,7 +654,8 @@
     public List<WaylineJobDTO> getJobsByConditions(Collection<String> jobIds, WaylineJobStatusEnum status) {
         return mapper.selectList(
                         new LambdaQueryWrapper<WaylineJobEntity>()
-                                .eq(Objects.nonNull(status), WaylineJobEntity::getStatus, status.getVal())
+                                // 不过滤状态,
+                                //.eq(Objects.nonNull(status), WaylineJobEntity::getStatus, status.getVal())
                                 .and(!CollectionUtils.isEmpty(jobIds),
                                         wrapper -> jobIds.forEach(id -> wrapper.eq(WaylineJobEntity::getJobId, id).or())))
                 .stream()

--
Gitblit v1.9.3