Pu Zhibing
2025-01-01 03bab7e7f1c86cc27ffa984d61c7bd2d9bd3a373
guns-admin/src/main/java/com/stylefeng/guns/modular/system/service/impl/PatrolTaskServiceImpl.java
@@ -38,9 +38,12 @@
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
/**
 * @author zhibing.pu
@@ -183,8 +186,18 @@
      
      //获取当前任务中的车船信息
      List<TaskDetailVehicles> taskDetailVehicles = taskDetailVehiclesService.selectList(new EntityWrapper<TaskDetailVehicles>().eq("task_detail_id", id));
      int errNum = 0;
      for (TaskDetailVehicles taskDetailVehicle : taskDetailVehicles) {
      //根据企业id分组用于定义多线上处理
      Set<String> collect = taskDetailVehicles.stream().map(TaskDetailVehicles::getCompanyId).collect(Collectors.toSet());
      //定义线程池
      ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(collect.size(), collect.size(), 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>());
      //定义多线程阻塞
      CountDownLatch countDownLatch = new CountDownLatch(collect.size());
      for (String companyId : collect) {
         List<TaskDetailVehicles> vehicles = taskDetailVehicles.stream().filter(s -> s.getCompanyId().equals(companyId)).collect(Collectors.toList());
         threadPoolExecutor.execute(new Runnable() {
            @Override
            public void run() {
               for (TaskDetailVehicles taskDetailVehicle : vehicles) {
         try {
            taskDetailVehicle.setStartExecutionTime(LocalDateTime.now());
            Integer taskDetailVehicleId = taskDetailVehicle.getId();
@@ -234,7 +247,7 @@
                  String fileId = VideoGateway.cameraShot2018(terminalId, videoChannel, 0);
                  if(null == fileId){
                     log.error("2018年协议发送拍摄指令失败:terminalId-->{},videoChannel-->{}", terminalId, videoChannel);
                     throw new RuntimeException();
                              throw new RuntimeException("2018年协议发送拍摄指令失败:terminalId-->" + terminalId + ",videoChannel-->" + videoChannel);
                  }else{
                     //等待15秒
                     Thread.sleep(30000);
@@ -246,7 +259,7 @@
                        fileId = VideoGateway.cameraShot2018(terminalId, videoChannel, 0);
                        if(null == fileId){
                           log.error("2018年协议发送拍摄指令失败:terminalId-->{},videoChannel-->{}", terminalId, videoChannel);
                           throw new Exception();
                                    throw new RuntimeException("2018年协议发送拍摄指令失败:terminalId-->" + terminalId + ",videoChannel-->" + videoChannel);
                        }else{
                           //等待15秒
                           Thread.sleep(30000);
@@ -254,7 +267,7 @@
                           fileUrl = VideoGateway.getCameraShotByFileId2018(terminalId, fileId, 0);
                           if(null == fileUrl){
                              log.error("2018年协议车辆获取拍摄图片失败:terminalId-->{},fileId-->{}", terminalId, fileId);
                              throw new Exception();
                                       throw new RuntimeException("2018年协议车辆获取拍摄图片失败:terminalId-->" + terminalId + ",fileId-->" + fileId);
                           }else{
                              taskDetailVehiclesChannel.setImageUrl(fileUrl);
                              taskDetailVehiclesChannel.setSysCreateTime(LocalDateTime.now());
@@ -286,7 +299,7 @@
                  String fileId = VideoGateway.cameraShot2023(vehicleId, vehicleType, terminalId, videoChannel, 0);
                  if(null == fileId){
                     log.error("2023年协议发送拍摄指令失败:terminalId-->{},videoChannel-->{}", terminalId, videoChannel);
                     throw new Exception();
                              throw new RuntimeException("2023年协议发送拍摄指令失败:terminalId-->" + terminalId + ",videoChannel-->" + videoChannel);
                  }else{
                     //等待15秒
                     Thread.sleep(30000);
@@ -298,7 +311,7 @@
                        fileId = VideoGateway.cameraShot2023(vehicleId, vehicleType, terminalId, videoChannel, 0);
                        if(null == fileId){
                           log.error("2023年协议发送拍摄指令失败:terminalId-->{},videoChannel-->{}", terminalId, videoChannel);
                           throw new Exception();
                                    throw new RuntimeException("2023年协议发送拍摄指令失败:terminalId-->" + terminalId + ",videoChannel-->" + videoChannel);
                        }else{
                           //等待15秒
                           Thread.sleep(30000);
@@ -306,7 +319,7 @@
                           fileUrl = VideoGateway.getCameraShotByFileId2023(fileId, 0);
                           if(null == fileUrl){
                              log.error("2023年协议车辆获取拍摄图片失败:terminalId-->{},fileId-->{}", terminalId, fileId);
                              throw new Exception();
                                       throw new RuntimeException("2023年协议车辆获取拍摄图片失败:terminalId-->" + terminalId + ",fileId-->" + fileId);
                           }else{
                              taskDetailVehiclesChannel.setImageUrl(fileUrl);
                              taskDetailVehiclesChannel.setSysCreateTime(LocalDateTime.now());
@@ -344,11 +357,22 @@
            taskDetailVehicle.setEndExecutionTime(LocalDateTime.now());
            taskDetailVehicle.setStatus(5);
            taskDetailVehiclesService.updateById(taskDetailVehicle);
            errNum++;
         }
      }
      
      taskDetail.setStatus(0 == errNum ? 3 : 4);
               //当前线程执行完成后减少等待数
               countDownLatch.countDown();
            }
         });
      }
      try {
         countDownLatch.await();
      } catch (InterruptedException e) {
         throw new RuntimeException(e);
      }
      int count = taskDetailVehiclesService.selectCount(new EntityWrapper<TaskDetailVehicles>().eq("task_detail_id", id).eq("status", 5));
      taskDetail.setStatus(count > 0 ? 4 : 3);
      taskDetailService.updateById(taskDetail);
   }