pcPointStep.setCreateTime(DateUtils.getNowDate());
pcPointStep.setUpdateTime(DateUtils.getNowDate());
pcPointStepMapper.insertPcPointStep(pcPointStep);

List<Long> deviceIds = pcPointStep.getDeviceIds();
List<Long> deviceSetting = pcPointStep.getDeviceSetting();
Long pcProcessDetailId = pcPointStep.getId();

for (int i = 0; i < deviceIds.size(); i++) {
    PcPointStepDevice pcPointStepDevice = new PcPointStepDevice();
    pcPointStepDevice.setStepId(pcProcessDetailId);
    pcPointStepDevice.setGroupId(pcPointStep.getGroupId());
    pcPointStepDevice.setDeviceId(deviceIds.get(i));
    Long coordinateId = deviceSetting.get(i);
    String coordinate = "[]";
    if (coordinateId > 0) {
        coordinate = pcCoordinateStagingMapper.selectPcCoordinateStagingById(coordinateId).getCoordinate();
    }
    pcPointStepDevice.setCoordinate(coordinate);
    pcPointStepDeviceMapper.insertPcPointStepDevice(pcPointStepDevice);
}
pcPointStep.setUpdateTime(DateUtils.getNowDate());
pcPointStepMapper.updatePcPointStep(pcPointStep);
//先查询出所有的记录步骤关联通道信息
PcPointStepDevice pcPointStepDevice1 = new PcPointStepDevice();
pcPointStepDevice1.setGroupId(pcPointStep.getGroupId());
List<PcPointStepDevice> pcPointStepDevices = pcPointStepDeviceMapper.selectPcPointStepDeviceList(pcPointStepDevice1);
//删除之前关联的通道信息
pcPointStepDeviceMapper.deletePcPointStepDeviceByPcPointStepId(pcPointStep.getId());

List<Long> deviceIds = pcPointStep.getDeviceIds();
List<Long> deviceSetting = pcPointStep.getDeviceSetting();
List<Long> pcPointStepDeviceIds = pcPointStep.getPcPointStepDeviceIds();
Long pcProcessDetailId = pcPointStep.getId();

for (int i = 0; i < deviceIds.size(); i++) {
    PcPointStepDevice pcPointStepDevice = new PcPointStepDevice();
    if (deviceSetting.get(i) == -1) {
        Long deviceId = deviceIds.get(i);
        Long stepDeviceId = pcPointStepDeviceIds.get(i);
        PcPointStepDevice pcPointStepDevice2;
        if (stepDeviceId != 0) {
            pcPointStepDevice2 = pcPointStepDevices.stream().filter(o -> o.getId().equals(stepDeviceId)).findFirst().get();
            pcPointStepDevice2.setDeviceId(deviceId);
        } else {
            pcPointStepDevice2 = pcPointStepDevices.stream().filter(o -> Objects.equals(o.getDeviceId(), deviceId)).findFirst().get();
        }
        pcPointStepDeviceMapper.insertPcPointStepDevice(pcPointStepDevice2);
        continue;
    }

    pcPointStepDevice.setStepId(pcProcessDetailId);
    pcPointStepDevice.setGroupId(pcPointStep.getGroupId());
    pcPointStepDevice.setDeviceId(deviceIds.get(i));
    Long coordinateId = deviceSetting.get(i);
    String coordinate = "[]";
    if (coordinateId > 0) {
        coordinate = pcCoordinateStagingMapper.selectPcCoordinateStagingById(coordinateId).getCoordinate();
    }
    pcPointStepDevice.setCoordinate(coordinate);
    pcPointStepDeviceMapper.insertPcPointStepDevice(pcPointStepDevice);
}
int seconds = 0;
PcMonitoringTask pcMonitoringTask = pcMonitoringTaskService.selectPcMonitoringTaskById(callbackResult.getTaskId());
while (redisUtil.hHasKey(PC_POINT_STEP_HANDLE_KEY + "_mark" ,callbackResult.getPointId() + "_" + callbackResult.getVideoId())) {
    log.info("=++++++++=======++++++++=======++++++++=======++++++++=======++++++++======:当前步骤正在处理,正在等待 === taskId:{} - pointId:{} - videoId:{}", callbackResult.getTaskId(), callbackResult.getPointId(), callbackResult.getVideoId());
    seconds++;
    if (seconds > pcMonitoringTask.getTimeInterval()){
        log.info("=++++++++=======++++++++=======++++++++=======++++++++=======++++++++======:当前步骤正在处理,等待超时 === taskId:{} - pointId:{} - videoId:{}", callbackResult.getTaskId(), callbackResult.getPointId(), callbackResult.getVideoId());
        return;
    }
}

redisUtil.hset(PC_POINT_STEP_HANDLE_KEY + "_mark",callbackResult.getPointId() + "_" + callbackResult.getVideoId(), "running");
List<PcPointStep> pcPointSteps = pcPointStepMapper.selectPcPointStepByGroupId(callbackResult.getPointId());
System.out.println(JSONObject.toJSONString(pcPointSteps));
int baffleNum = 0;
PcPoint pcPoint = pcPointMapper.selectPcPointById(callbackResult.getPointId());

  //************************
  //        步骤处理
  //************************


public void truckHeadResultHandle(PcPointRecord pcPointRecord, PcPointStep pcPointStep, PcPointRecordCallbackResult callbackResult, SftpConfig sftpConfig, List<PcPointStep> pcPointSteps, PcPoint pcPoint, PcAiModel pcAiModel,String type,String targetType) {
  List<ModelResult> result = callbackResult.getResult();
  List<ModelResult> collect = result.stream().filter(o -> type.equals(o.getLabel()) && o.getScore() > Double.parseDouble(pcAiModel.getSimilarity())).collect(Collectors.toList());
  PcPointStepDevice pcPointStepDevice = new PcPointStepDevice();
  pcPointStepDevice.setDeviceId(callbackResult.getVideoId());
  pcPointStepDevice.setGroupId(callbackResult.getPointId());
  pcPointStepDevice.setStepId(pcPointStep.getId());
  List<PcPointStepDevice> pcPointStepDevices = pcPointStepDeviceMapper.selectPcPointStepDeviceList(pcPointStepDevice);
  if (pcPointStepDevices.isEmpty()) {
      return;
  }
  PcPointStepDevice pcPointStepDeviceInfo = pcPointStepDevices.get(0);

  Map<String, List<ModelResult>> coincideModelResultsType = getCoincideModelResultsType(collect, pcPointStepDeviceInfo);
  if (!coincideModelResultsType.isEmpty()) {
      String imagePath = callbackResult.getImagePath();
      String s = scpDownFile(sftpConfig, imagePath);
      String frameImgPath = imageResultHandle(coincideModelResultsType, s, pcPointStep, pcPointStepDeviceInfo);
      boolean isDone = checkStepIsDone(pcPointRecord, pcPointStep, callbackResult.getTaskId());
      if (pcPointStep.getStartFlag() == 1 && !isDone) {
          Date date = DateUtils.parseDate(callbackResult.getTime());
          PcPointRecord pcPointRecordInsertParams = PcPointRecord.builder().build();
          String processIsRunningKey =  callbackResult.getTaskId() + "_" + callbackResult.getPointId();
          boolean addProcessIsRunning = redisUtil.hsetnx(PC_POINT_STEP_HANDLE_KEY + URLConstant.PROCESS_IS_RUNNING, URLConstant.PC_POINT_RECORD + processIsRunningKey, pcPointRecordInsertParams);
          if (addProcessIsRunning){
              pcPointRecordMapper.insertPcPointRecord(pcPointRecordInsertParams);
              pcPointRecord = pcPointRecordInsertParams;
              redisUtil.hset(PC_POINT_STEP_HANDLE_KEY + URLConstant.PROCESS_IS_RUNNING, URLConstant.PC_POINT_RECORD + processIsRunningKey, pcPointRecord);
              PcPointRecordDetail pcPointRecordDetail = PcPointRecordDetail.builder().build();
              log.info(">>>>>>>>>>>>>pcPointRecord:{}",JSONObject.toJSONString(pcPointRecord));
              log.info("》》》》》》》》》》》》》》》》》》》》》》》》:{}",PC_POINT_STEP_HANDLE_KEY + "_" + pcPointRecord.getId());
              boolean hSetNx = redisUtil.hsetnx(PC_POINT_STEP_HANDLE_KEY + "_" + pcPointRecord.getId(), URLConstant.RESULT_STEP + pcPointStep.getId() + "_" + callbackResult.getTaskId(), URLConstant.END);
              if (hSetNx) {
                  pcPointRecordDetailMapper.insertPcPointRecordDetail(pcPointRecordDetail);
              }
          }
      } else if (pcPointStep.getEndFlag() == 1) {
          redisUtil.hset(PC_POINT_STEP_HANDLE_KEY + "_" + pcPointRecord.getId(), URLConstant.CAR_LEAVE + callbackResult.getPointId() + "_" + callbackResult.getTaskId(), DateUtils.getTime());
      } else {
          if (!checkStepIsDone(pcPointRecord, pcPointStep, callbackResult.getTaskId())) {
              PcPointRecordDetail pcPointRecordDetail = PcPointRecordDetail.builder().build();
              log.info(">>>>>>>>>>>>>pcPointRecord:{}",JSONObject.toJSONString(pcPointRecord));
              log.info("》》》》》》》》》》》》》》》》》》》》》》》》:{}",PC_POINT_STEP_HANDLE_KEY + "_" + pcPointRecord.getId());
              boolean hSetNx = redisUtil.hsetnx(PC_POINT_STEP_HANDLE_KEY + "_" + pcPointRecord.getId(), URLConstant.RESULT_STEP + pcPointStep.getId() + "_" + callbackResult.getTaskId(), URLConstant.END);
              if (hSetNx){
                  pcPointRecordDetailMapper.insertPcPointRecordDetail(pcPointRecordDetail);
              }
          }
      }
      if (pcPointStep.getStartFlag() == 1) {
          redisUtil.hset(PC_POINT_STEP_HANDLE_KEY + "_" + pcPointRecord.getId(), URLConstant.BAFFLE_RULE_CHECK + callbackResult.getPointId() + "_" + callbackResult.getTaskId(), callbackResult.getTime());
          redisUtil.hset(PC_POINT_STEP_HANDLE_KEY + "_" + pcPointRecord.getId(), URLConstant.GUARDIAN_PERSION_DUTY_RULE_CHECK + callbackResult.getPointId() + "_" + callbackResult.getTaskId(), callbackResult.getTime()); 
      }
      if (pcPointStep.getEndFlag() != 1) {
          redisUtil.hsetnx(PC_POINT_STEP_HANDLE_KEY + "_" + pcPointRecord.getId(), URLConstant.RESULT_STEP + pcPointStep.getId() + "_" + callbackResult.getTaskId(), URLConstant.END);
      }

      File file = new File(s);
      if (file.exists()) {
          file.delete();
      }
  } else {
      if (pcPointStep.getEndFlag() == 1) {
          if (redisUtil.hHasKey(PC_POINT_STEP_HANDLE_KEY + "_" + pcPointRecord.getId(), URLConstant.CAR_LEAVE + callbackResult.getPointId() + "_" + callbackResult.getTaskId())) {
              Object hget = redisUtil.hget(PC_POINT_STEP_HANDLE_KEY + "_" + pcPointRecord.getId(), URLConstant.CAR_LEAVE + callbackResult.getPointId() + "_" + callbackResult.getTaskId());
              long i = DateUtils.getDifferenceSeconds(DateUtils.parseDate(callbackResult.getTime()), DateUtils.parseDate(hget));
              if (i > pcPointStep.getMonitorPrefixSecond()) {
                  if (!redisUtil.hHasKey(PC_POINT_STEP_HANDLE_KEY + "_" + pcPointRecord.getId(), URLConstant.CONNECT_PIPES_CORRECT + callbackResult.getPointId() + "_" + callbackResult.getTaskId())) {
                      return;
                  }
                  String imagePath = callbackResult.getImagePath();
                  String s = scpDownFile(sftpConfig, imagePath);
                  String frameImgPath = imageResultHandle(coincideModelResultsType, s, pcPointStep, pcPointStepDeviceInfo);
                  for (PcPointStep pointStep : pcPointSteps) {
                      if (!redisUtil.hHasKey(PC_POINT_STEP_HANDLE_KEY + "_" + pcPointRecord.getId(), URLConstant.RESULT_STEP + pointStep.getId() + "_" + callbackResult.getTaskId())) {
                          Long alarmState = URLConstant.FALSE_STATUS;
                          Long dealState = URLConstant.DEAL_STATE_NOT_DEAL; 
                          if (URLConstant.TEUCK_HEAD.equals(pointStep.getLabel())) {
                              alarmState = URLConstant.TRUE_STATUS;
                              dealState = URLConstant.DEAL_STATE_NOT_NEED_DEAL;
                          }
                          if (URLConstant.GUARDIAN_PERSION_DUTY.equals(pointStep.getLabel())) {
                              if (redisUtil.hHasKey(PC_POINT_STEP_HANDLE_KEY + "_" + pcPointRecord.getId(), URLConstant.GUARDIAN_PERSION_DUTY_ALARM + callbackResult.getPointId() + "_" + callbackResult.getTaskId())) {
                                  alarmState = URLConstant.TRUE_STATUS;
                                  dealState = URLConstant.DEAL_STATE_NOT_NEED_DEAL;
                              }
                          }
                          if (URLConstant.REAR_CAR_PATROL_PERSION.equals(pointStep.getLabel())) {
                              if (redisUtil.hHasKey(PC_POINT_STEP_HANDLE_KEY + "_" + pcPointRecord.getId(), URLConstant.REAR_CAR_PATROL_PERSION_ALARM + callbackResult.getPointId() + "_" + callbackResult.getTaskId())) {
                                  alarmState = URLConstant.TRUE_STATUS;
                                  dealState = URLConstant.DEAL_STATE_NOT_NEED_DEAL;
                              }
                          }
                          if (URLConstant.SAFETY_SLING.equals(pointStep.getLabel())) {
                              alarmState = URLConstant.TRUE_STATUS;
                              dealState = URLConstant.DEAL_STATE_NOT_NEED_DEAL;
                              if (redisUtil.hHasKey(PC_POINT_STEP_HANDLE_KEY + "_" + pcPointRecord.getId(), URLConstant.NO_ALARM_IMAGE_PATH + pcPointStep.getId() + "_" + callbackResult.getTaskId())) {
                                  frameImgPath = redisUtil.hget(PC_POINT_STEP_HANDLE_KEY + "_" + pcPointRecord.getId(), URLConstant.NO_ALARM_IMAGE_PATH + pcPointStep.getId() + "_" + callbackResult.getTaskId()).toString();
                              }
                          }

                          if (alarmState.equals(URLConstant.FALSE_STATUS)) {
                              pcPointRecord.setAlarmState(URLConstant.FALSE_STATUS);
                          }
                          PcPointRecordDetail pcPointRecordDetail = PcPointRecordDetail.builder().build();

                          boolean hSetNx = redisUtil.hsetnx(PC_POINT_STEP_HANDLE_KEY + "_" + pcPointRecord.getId(), URLConstant.RESULT_STEP + pointStep.getId() + "_" + callbackResult.getTaskId(), URLConstant.END);
                          if (hSetNx){
                              pcPointRecordDetailMapper.insertPcPointRecordDetail(pcPointRecordDetail);
                              if (pcPointRecordDetail.getAlarmState() == 2) {
                                  PcMonitoringDeviceResult build = PcMonitoringDeviceResult.builder().build();
                                  pcMonitoringDeviceResultService.insertPcMonitoringDeviceResult(build);

                                  PcMonitoringDevice pcMonitoringDevice = pcMonitoringDeviceService.selectPcMonitoringDeviceById(callbackResult.getVideoId());
                                  aiTaskService.publishAlarmInformation(build, pcPoint.getDeptName(), pointStep.getModelName(), pcMonitoringDevice.getName());
                              }
                          }
                      }
                  }
                  String processIsRunningKey =  callbackResult.getTaskId() + "_" + callbackResult.getPointId();
                  redisUtil.hdel(PC_POINT_STEP_HANDLE_KEY + URLConstant.PROCESS_IS_RUNNING, URLConstant.PC_POINT_RECORD + processIsRunningKey);
                  redisUtil.del(PC_POINT_STEP_HANDLE_KEY + "_" + pcPointRecord.getId());

                  pcPointRecord.setEndTime(DateUtils.parseDate(callbackResult.getTime()));
                  pcPointRecord.setPointState(3L);
                  pcPointRecordMapper.updatePcPointRecord(pcPointRecord);
              }
          }
      }
  }
}

public void baffleResultHandle(PcPointRecord pcPointRecord, PcPointStep pcPointStep, PcPointRecordCallbackResult callbackResult, SftpConfig sftpConfig, int baffleNum, PcPoint pcPoint, PcAiModel pcAiModel,String type,String targetType) {
  String imagePath = callbackResult.getImagePath();
  List<ModelResult> result = callbackResult.getResult();
  List<ModelResult> collect = result.stream().filter(o -> type.equals(o.getLabel()) && o.getScore() > Double.parseDouble(pcAiModel.getSimilarity())).collect(Collectors.toList());
  PcPointStepDevice pcPointStepDevice = new PcPointStepDevice();
  pcPointStepDevice.setDeviceId(callbackResult.getVideoId());
  pcPointStepDevice.setGroupId(callbackResult.getPointId());
  pcPointStepDevice.setStepId(pcPointStep.getId());
  List<PcPointStepDevice> pcPointStepDevices = pcPointStepDeviceMapper.selectPcPointStepDeviceList(pcPointStepDevice);
  if (pcPointStepDevices.isEmpty()) {
      return;
  }
  PcPointStepDevice pcPointStepDeviceInfo = pcPointStepDevices.get(0);
  Map<String, List<ModelResult>> coincideModelResultsType = getCoincideModelResultsType(collect, pcPointStepDeviceInfo);
  PcPointRecordDetail pcPointRecordDetail = null;
  String keys = PC_POINT_STEP_HANDLE_KEY + "_" + pcPointRecord.getId() + URLConstant.RESULT_STEP + pcPointStep.getId() + "_" + callbackResult.getTaskId();
  log.info("#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#:baffleType:{},keys:{}", baffleNum, keys);

  boolean isAlarm = false;
  if (baffleNum <= 1) {
      if (!coincideModelResultsType.isEmpty()) {
          String s = scpDownFile(sftpConfig, imagePath);
          String frameImgPath = imageResultHandle(coincideModelResultsType, s, pcPointStep, pcPointStepDeviceInfo);
          pcPointRecordDetail = PcPointRecordDetail.builder().build();

          File file = new File(s);
          if (file.exists()) {
              file.delete();
          }
      } else {
          if (redisUtil.hHasKey(PC_POINT_STEP_HANDLE_KEY + "_" + pcPointRecord.getId(), URLConstant.BAFFLE_RULE_CHECK + callbackResult.getPointId() + "_" + callbackResult.getTaskId())) {
              Object hget = redisUtil.hget(PC_POINT_STEP_HANDLE_KEY + "_" + pcPointRecord.getId(), URLConstant.BAFFLE_RULE_CHECK + callbackResult.getPointId() + "_" + callbackResult.getTaskId());
              long i = DateUtils.getDifferenceSeconds(DateUtils.parseDate(callbackResult.getTime()), DateUtils.parseDate(hget));
              if (i > pcPointStep.getMonitorPrefixSecond()) {
                  String s = scpDownFile(sftpConfig, imagePath);
                  String frameImgPath = imageResultHandle(coincideModelResultsType, s, pcPointStep, pcPointStepDeviceInfo);
                  pcPointRecordDetail = PcPointRecordDetail.builder().build();
                  File file = new File(s);
                  if (file.exists()) {
                      file.delete();
                  }
              }
          }
      }
      boolean baffleIsDone = checkStepIsDone(pcPointRecord, pcPointStep, callbackResult.getTaskId());
      if (!baffleIsDone && pcPointRecordDetail != null) {
          if (redisUtil.hsetnx(PC_POINT_STEP_HANDLE_KEY + "_" + pcPointRecord.getId(), URLConstant.RESULT_STEP + pcPointStep.getId() + "_" + callbackResult.getTaskId(), URLConstant.END)){
              isAlarm = pcPointRecordDetail.getAlarmState() == 2;
              pcPointRecordDetailMapper.insertPcPointRecordDetail(pcPointRecordDetail);
              boolean bafflePlaceAlready = !isAlarm;
              redisUtil.hset(PC_POINT_STEP_HANDLE_KEY + "_" + pcPointRecord.getId(),
                      URLConstant.BAFFLE_PLACE_ALREADY + callbackResult.getPointId() + "_" + callbackResult.getTaskId(), bafflePlaceAlready);
          }
      }

  } else {
      if (!coincideModelResultsType.isEmpty()) {
          String s = scpDownFile(sftpConfig, imagePath);
          String frameImgPath = imageResultHandle(coincideModelResultsType, s, pcPointStep, pcPointStepDeviceInfo);
          pcPointRecordDetail = PcPointRecordDetail.builder().build();
          File file = new File(s);
          if (file.exists()) {
              file.delete();
          }
      }
      boolean bafflePlaceAlready = false;
      if (redisUtil.hHasKey(PC_POINT_STEP_HANDLE_KEY + "_" + pcPointRecord.getId(), URLConstant.BAFFLE_PLACE_ALREADY + callbackResult.getPointId() + "_" + callbackResult.getTaskId())){
          bafflePlaceAlready = Boolean.parseBoolean(redisUtil.hget(PC_POINT_STEP_HANDLE_KEY + "_" + pcPointRecord.getId(), URLConstant.BAFFLE_PLACE_ALREADY + callbackResult.getPointId() + "_" + callbackResult.getTaskId()).toString());
      }

      boolean pipesReset = redisUtil.hHasKey(PC_POINT_STEP_HANDLE_KEY + "_" + pcPointRecord.getId(), URLConstant.PIPES_RESET + pcPointRecord.getId() + "_" + callbackResult.getTaskId());

      if (pipesReset && pcPointRecordDetail != null){
          if (!bafflePlaceAlready){
              pcPointRecordDetail.setAlarmState(URLConstant.FALSE_STATUS);
              pcPointRecordDetail.setDealState(URLConstant.DEAL_STATE_NOT_DEAL);
          }

          boolean baffleIsDone = checkStepIsDone(pcPointRecord, pcPointStep, callbackResult.getTaskId()); 
          if (!baffleIsDone && redisUtil.hsetnx(PC_POINT_STEP_HANDLE_KEY + "_" + pcPointRecord.getId(), URLConstant.RESULT_STEP + pcPointStep.getId() + "_" + callbackResult.getTaskId(), URLConstant.END)){
              isAlarm = pcPointRecordDetail.getAlarmState() == 2;
              pcPointRecordDetailMapper.insertPcPointRecordDetail(pcPointRecordDetail);
          }
      }
  }
  if (pcPointRecordDetail != null && isAlarm) {
      if (Objects.equals(pcPointRecordDetail.getAlarmState(), URLConstant.FALSE_STATUS)) {
          if (Objects.equals(pcPointRecord.getAlarmState(), URLConstant.TRUE_STATUS)) {
              pcPointRecord.setAlarmState(URLConstant.FALSE_STATUS);
              pcPointRecord.setDealState(URLConstant.DEAL_STATE_NOT_DEAL);
              pcPointRecordMapper.updatePcPointRecord(pcPointRecord);
          }
          PcMonitoringDeviceResult build = PcMonitoringDeviceResult.builder().build();
          pcMonitoringDeviceResultService.insertPcMonitoringDeviceResult(build);
          PcMonitoringDevice pcMonitoringDevice = pcMonitoringDeviceService.selectPcMonitoringDeviceById(callbackResult.getVideoId());
          aiTaskService.publishAlarmInformation(build, pcPoint.getDeptName(), pcPointStep.getModelName(), pcMonitoringDevice.getName());
      }
  }
}

public void guardianPersonDutyResultHandle(PcPointRecord pcPointRecord, PcPointStep pcPointStep, PcPointRecordCallbackResult callbackResult, SftpConfig sftpConfig, PcPoint pcPoint, PcAiModel pcAiModel,String type,String targetType) {
  String imagePath = callbackResult.getImagePath();
  List<ModelResult> result = callbackResult.getResult();
  List<ModelResult> collect = result.stream().filter(o -> type.equals(o.getLabel()) && o.getScore() > Double.parseDouble(pcAiModel.getSimilarity())).collect(Collectors.toList());

  PcPointStepDevice pcPointStepDevice = new PcPointStepDevice();
  pcPointStepDevice.setDeviceId(callbackResult.getVideoId());
  pcPointStepDevice.setGroupId(callbackResult.getPointId());
  pcPointStepDevice.setStepId(pcPointStep.getId());
  List<PcPointStepDevice> pcPointStepDevices = pcPointStepDeviceMapper.selectPcPointStepDeviceList(pcPointStepDevice);
  if (pcPointStepDevices.isEmpty()) {
      return;
  }
  PcPointStepDevice pcPointStepDeviceInfo = pcPointStepDevices.get(0);
  Map<String, List<ModelResult>> coincideModelResultsType = getCoincideModelResultsType(collect, pcPointStepDeviceInfo);
  PcPointRecordDetail pcPointRecordDetail = null;
  boolean isAlarm = false;
  if (coincideModelResultsType.isEmpty()) {
      String s = scpDownFile(sftpConfig, imagePath);
      String frameImgPath = imageResultHandle(coincideModelResultsType, s, pcPointStep, pcPointStepDeviceInfo);

      if (redisUtil.hHasKey(PC_POINT_STEP_HANDLE_KEY + "_" + pcPointRecord.getId(), URLConstant.GUARDIAN_PERSION_DUTY_RULE_CHECK + callbackResult.getPointId() + "_" + callbackResult.getTaskId())) {
          Object hget = redisUtil.hget(PC_POINT_STEP_HANDLE_KEY + "_" + pcPointRecord.getId(), URLConstant.GUARDIAN_PERSION_DUTY_RULE_CHECK + callbackResult.getPointId() + "_" + callbackResult.getTaskId());
          long i = DateUtils.getDifferenceSeconds(DateUtils.parseDate(callbackResult.getTime()), DateUtils.parseDate(hget));
          if (i > pcPointStep.getMonitorPrefixSecond()) {
              pcPointRecordDetail = PcPointRecordDetail.builder().build();
              isAlarm = true;
          }
      }
      File file = new File(s);
      if (file.exists()) {
          file.delete();
      }
  } else {
      redisUtil.hset(PC_POINT_STEP_HANDLE_KEY + "_" + pcPointRecord.getId(), URLConstant.GUARDIAN_PERSION_DUTY_ALARM + callbackResult.getPointId() + "_" + callbackResult.getTaskId(), false);
      redisUtil.hset(PC_POINT_STEP_HANDLE_KEY + "_" + pcPointRecord.getId(), URLConstant.GUARDIAN_PERSION_DUTY_RULE_CHECK + callbackResult.getPointId() + "_" + callbackResult.getTaskId(), DateUtils.getDate());
  }
  if (isAlarm && !checkStepIsDone(pcPointRecord, pcPointStep, callbackResult.getTaskId()) && redisUtil.hsetnx(PC_POINT_STEP_HANDLE_KEY + "_" + pcPointRecord.getId(), URLConstant.RESULT_STEP + pcPointStep.getId() + "_" + callbackResult.getTaskId(), URLConstant.END)) {
      pcPointRecordDetailMapper.insertPcPointRecordDetail(pcPointRecordDetail);
      if (Objects.equals(pcPointRecordDetail.getAlarmState(), URLConstant.FALSE_STATUS)) {
          if (Objects.equals(pcPointRecord.getAlarmState(), URLConstant.TRUE_STATUS)) {
              pcPointRecord.setAlarmState(URLConstant.FALSE_STATUS);
              pcPointRecord.setDealState(URLConstant.DEAL_STATE_NOT_DEAL);
              pcPointRecordMapper.updatePcPointRecord(pcPointRecord);
          }
          PcMonitoringDeviceResult build = PcMonitoringDeviceResult.builder().build();
          pcMonitoringDeviceResultService.insertPcMonitoringDeviceResult(build);
          PcMonitoringDevice pcMonitoringDevice = pcMonitoringDeviceService.selectPcMonitoringDeviceById(callbackResult.getVideoId());
          aiTaskService.publishAlarmInformation(build, pcPoint.getDeptName(), pcPointStep.getModelName(), pcMonitoringDevice.getName());
      }
      redisUtil.hdel(PC_POINT_STEP_HANDLE_KEY + "_" + pcPointRecord.getId(), URLConstant.GUARDIAN_PERSION_DUTY_ALARM + callbackResult.getPointId() + "_" + callbackResult.getTaskId());
      redisUtil.hdel(PC_POINT_STEP_HANDLE_KEY + "_" + pcPointRecord.getId(), URLConstant.GUARDIAN_PERSION_DUTY_RULE_CHECK + callbackResult.getPointId() + "_" + callbackResult.getTaskId());
  }
}


public void connectPipesCorrectResultHandle(PcPointRecord pcPointRecord, PcPointStep pcPointStep, PcPointRecordCallbackResult callbackResult, SftpConfig sftpConfig, PcPoint pcPoint, PcAiModel pcAiModel,String type,String targetType) {
  String imagePath = callbackResult.getImagePath();
  List<ModelResult> result = callbackResult.getResult();
  List<ModelResult> collect = result.stream().filter(o -> type.equals(o.getLabel()) && o.getScore() > Double.parseDouble(pcAiModel.getSimilarity())).collect(Collectors.toList());
  List<ModelResult> tsCollect = result.stream().filter(o -> "ts_" + type.equals(o.getLabel()) && o.getScore() > Double.parseDouble(pcAiModel.getSimilarity())).collect(Collectors.toList());

  collect.addAll(tsCollect);

  if (collect.isEmpty()) {
      return;
  }
  PcPointStepDevice pcPointStepDevice = new PcPointStepDevice();
  pcPointStepDevice.setDeviceId(callbackResult.getVideoId());
  pcPointStepDevice.setGroupId(callbackResult.getPointId());
  pcPointStepDevice.setStepId(pcPointStep.getId());
  List<PcPointStepDevice> pcPointStepDevices = pcPointStepDeviceMapper.selectPcPointStepDeviceList(pcPointStepDevice);
  if (pcPointStepDevices.isEmpty()) {
      return;
  }
  PcPointStepDevice pcPointStepDeviceInfo = pcPointStepDevices.get(0);

  Map<String, List<ModelResult>> coincideModelResultsType = getCoincideModelResultsType(collect, pcPointStepDeviceInfo);
  PcPointRecordDetail pcPointRecordDetail = null;

  if (!coincideModelResultsType.isEmpty()) {
      String s = scpDownFile(sftpConfig, imagePath);
      String frameImgPath = imageResultHandle(coincideModelResultsType, s, pcPointStep, pcPointStepDeviceInfo);
      pcPointRecordDetail = PcPointRecordDetail.builder().build();
      File file = new File(s);
      if (file.exists()) {
          file.delete();
      }
  }

  if (pcPointRecordDetail != null && !checkStepIsDone(pcPointRecord, pcPointStep, callbackResult.getTaskId()))  {
      boolean hSetNx = redisUtil.hsetnx(PC_POINT_STEP_HANDLE_KEY + "_" + pcPointRecord.getId(),
              URLConstant.RESULT_STEP + pcPointStep.getId() + "_" + callbackResult.getTaskId(),
              URLConstant.END);
      if (hSetNx){
          pcPointRecordDetailMapper.insertPcPointRecordDetail(pcPointRecordDetail);
          if (Objects.equals(pcPointRecordDetail.getAlarmState(), URLConstant.FALSE_STATUS)) {
              if (Objects.equals(pcPointRecord.getAlarmState(), URLConstant.TRUE_STATUS)) {
                  pcPointRecord.setAlarmState(URLConstant.FALSE_STATUS);
                  pcPointRecord.setDealState(URLConstant.DEAL_STATE_NOT_DEAL);
                  pcPointRecordMapper.updatePcPointRecord(pcPointRecord);
              }
              PcMonitoringDeviceResult build = PcMonitoringDeviceResult.builder().build();
              pcMonitoringDeviceResultService.insertPcMonitoringDeviceResult(build);

              PcMonitoringDevice pcMonitoringDevice = pcMonitoringDeviceService.selectPcMonitoringDeviceById(callbackResult.getVideoId());
              aiTaskService.publishAlarmInformation(build, pcPoint.getDeptName(), pcPointStep.getModelName(), pcMonitoringDevice.getName());
          }
          redisUtil.hset(PC_POINT_STEP_HANDLE_KEY + "_" + pcPointRecord.getId(), URLConstant.CONNECT_PIPES_CORRECT + callbackResult.getPointId() + "_" + callbackResult.getTaskId(), DateUtils.getTime());
      }
  }
}

public void safetySlingResultHandle(PcPointRecord pcPointRecord, PcPointStep pcPointStep, PcPointRecordCallbackResult callbackResult, SftpConfig sftpConfig, PcPoint pcPoint, PcAiModel pcAiModel,String type,String targetType) {
  String imagePath = callbackResult.getImagePath();
  List<ModelResult> result = callbackResult.getResult();
  List<ModelResult> collect = result.stream().filter(o -> type.equals(o.getLabel()) && o.getScore() > Double.parseDouble(pcAiModel.getSimilarity())).collect(Collectors.toList());
  List<ModelResult> peopleCollect = result.stream().filter(o -> targetType.equals(o.getLabel()) && o.getScore() > Double.parseDouble(pcAiModel.getSimilarity())).collect(Collectors.toList());
  PcPointStepDevice pcPointStepDevice = new PcPointStepDevice();
  pcPointStepDevice.setDeviceId(callbackResult.getVideoId());
  pcPointStepDevice.setGroupId(callbackResult.getPointId());
  pcPointStepDevice.setStepId(pcPointStep.getId());
  List<PcPointStepDevice> pcPointStepDevices = pcPointStepDeviceMapper.selectPcPointStepDeviceList(pcPointStepDevice);
  if (pcPointStepDevices.isEmpty()) {
      return;
  }
  PcPointStepDevice pcPointStepDeviceInfo = pcPointStepDevices.get(0);

  Map<String, List<ModelResult>> coincideModelResultsType = getCoincideModelResultsType(collect, pcPointStepDeviceInfo); 
  Map<String, List<ModelResult>> peopleCoincideModelResultsType = getCoincideModelResultsType(peopleCollect, pcPointStepDeviceInfo);

  Map<String, List<ModelResult>> allResult = coincideModelResultsType.entrySet().stream()
          .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (v1, v2) -> v2, () -> new HashMap<>(peopleCoincideModelResultsType)));


  int safetyLineNum = 0;
  int peopleNum = 0;

  for (int i = 0; i < coincideModelResultsType.size(); i++) {
      List<ModelResult> modelResults = coincideModelResultsType.get(i + "");
      safetyLineNum += modelResults.size();
  }


  for (int i = 0; i < peopleCoincideModelResultsType.size(); i++) {
      List<ModelResult> modelResults = peopleCoincideModelResultsType.get(i + "");
      peopleNum += modelResults.size();
  }


  if (peopleCoincideModelResultsType.isEmpty()) {
      redisUtil.hset(PC_POINT_STEP_HANDLE_KEY + "_" + pcPointRecord.getId(),
              URLConstant.SAFETY_SLING_RESULT_RULE_CHECKING + pcPointStep.getId() + "_" + callbackResult.getTaskId(),
              DateUtils.getTime());
      return;
  }

  PcPointRecordDetail pcPointRecordDetail;
  boolean isHasSafetySlingResultRuleChecking = redisUtil.hHasKey(PC_POINT_STEP_HANDLE_KEY + "_" + pcPointRecord.getId(),
          URLConstant.SAFETY_SLING_RESULT_RULE_CHECKING + pcPointStep.getId() + "_" + callbackResult.getTaskId());

  if (safetyLineNum < peopleNum) {
      String frameImgPath;
      if (!redisUtil.hHasKey(PC_POINT_STEP_HANDLE_KEY + "_" + pcPointRecord.getId(), URLConstant.ALARM_IMAGE_PATH + pcPointStep.getId() + "_" + callbackResult.getTaskId())) {
          String s = scpDownFile(sftpConfig, imagePath);
          frameImgPath = imageResultHandle(allResult, s, pcPointStep, pcPointStepDeviceInfo);
          redisUtil.hset(PC_POINT_STEP_HANDLE_KEY + "_" + pcPointRecord.getId(), URLConstant.ALARM_IMAGE_PATH + pcPointStep.getId() + "_" + callbackResult.getTaskId(), frameImgPath);
      } else {
          frameImgPath = redisUtil.hget(PC_POINT_STEP_HANDLE_KEY + "_" + pcPointRecord.getId(), URLConstant.ALARM_IMAGE_PATH + pcPointStep.getId() + "_" + callbackResult.getTaskId()).toString();
      }
      Long monitorPrefixSecond = pcPointStep.getMonitorPrefixSecond();
      if (isHasSafetySlingResultRuleChecking) {
          Object safetySlingResultRuleChecking = redisUtil.hget(PC_POINT_STEP_HANDLE_KEY + "_" + pcPointRecord.getId(), URLConstant.SAFETY_SLING_RESULT_RULE_CHECKING + pcPointStep.getId() + "_" + callbackResult.getTaskId());
          long times = DateUtils.getDifferenceSeconds(DateUtils.parseDate(callbackResult.getTime()), DateUtils.parseDate(safetySlingResultRuleChecking));
          if (times > monitorPrefixSecond && !checkStepIsDone(pcPointRecord, pcPointStep, callbackResult.getTaskId())) {
              pcPointRecordDetail = PcPointRecordDetail.builder().pointId(callbackResult.getPointId())
                      .deviceId(callbackResult.getVideoId())
                      .modelId(pcPointStep.getModelId())
                      .stepId(pcPointStep.getId())
                      .stepName(pcPointStep.getStepName())
                      .recordId(pcPointRecord.getId())
                      .alarmState(URLConstant.FALSE_STATUS)
                      .dealState(URLConstant.DEAL_STATE_NOT_DEAL)
                      .imgUrl(frameImgPath)
                      .startFlag(pcPointStep.getStartFlag())
                      .endFlag(pcPointStep.getEndFlag())
                      .detectTime(DateUtils.parseDate(callbackResult.getTime())).build();


              boolean hSetNx = redisUtil.hsetnx(PC_POINT_STEP_HANDLE_KEY + "_" + pcPointRecord.getId(), URLConstant.RESULT_STEP + pcPointStep.getId() + "_" + callbackResult.getTaskId(), URLConstant.END);
              if (hSetNx){
                  pcPointRecordDetailMapper.insertPcPointRecordDetail(pcPointRecordDetail);

                  if (Objects.equals(pcPointRecord.getAlarmState(), URLConstant.TRUE_STATUS)) {
                      pcPointRecord.setAlarmState(URLConstant.FALSE_STATUS);
                      pcPointRecord.setDealState(URLConstant.DEAL_STATE_NOT_DEAL);
                      pcPointRecordMapper.updatePcPointRecord(pcPointRecord);
                  }
                  PcMonitoringDeviceResult build = PcMonitoringDeviceResult.builder().build();
                  pcMonitoringDeviceResultService.insertPcMonitoringDeviceResult(build);
                  PcMonitoringDevice pcMonitoringDevice = pcMonitoringDeviceService.selectPcMonitoringDeviceById(callbackResult.getVideoId());
                  aiTaskService.publishAlarmInformation(build, pcPoint.getDeptName(), pcPointStep.getModelName(), pcMonitoringDevice.getName());

              }
          }
      } else {
          redisUtil.hset(PC_POINT_STEP_HANDLE_KEY + "_" + pcPointRecord.getId(),
                  URLConstant.SAFETY_SLING_RESULT_RULE_CHECKING + pcPointStep.getId() + "_" + callbackResult.getTaskId(),
                  DateUtils.getTime());
      }
  } else {
      if (!redisUtil.hHasKey(PC_POINT_STEP_HANDLE_KEY + "_" + pcPointRecord.getId(), URLConstant.NO_ALARM_IMAGE_PATH + pcPointStep.getId() + "_" + callbackResult.getTaskId())) {
          String s = scpDownFile(sftpConfig, imagePath);
          String frameImgPath = imageResultHandle(coincideModelResultsType, s, pcPointStep, pcPointStepDeviceInfo);
          redisUtil.hset(PC_POINT_STEP_HANDLE_KEY + "_" + pcPointRecord.getId(),
                  URLConstant.NO_ALARM_IMAGE_PATH + pcPointStep.getId() + "_" + callbackResult.getTaskId(),
                  frameImgPath);
      }
      redisUtil.hset(PC_POINT_STEP_HANDLE_KEY + "_" + pcPointRecord.getId(),
              URLConstant.SAFETY_SLING_RESULT_RULE_CHECKING + pcPointStep.getId() + "_" + callbackResult.getTaskId(),
              DateUtils.getTime());
  }
}

public void rearCarPatrolPersonResultHandle(PcPointRecord pcPointRecord, PcPointStep pcPointStep, PcPointRecordCallbackResult callbackResult, SftpConfig sftpConfig, PcPoint pcPoint, PcAiModel pcAiModel,String type,String targetType) {

  String imagePath = callbackResult.getImagePath();
  List<ModelResult> result = callbackResult.getResult();
  List<ModelResult> collect = result.stream().filter(o -> type.equals(o.getLabel()) && o.getScore() > Double.parseDouble(pcAiModel.getSimilarity())).collect(Collectors.toList());

  PcPointStepDevice pcPointStepDevice = new PcPointStepDevice();
  pcPointStepDevice.setDeviceId(callbackResult.getVideoId());
  pcPointStepDevice.setGroupId(callbackResult.getPointId());
  pcPointStepDevice.setStepId(pcPointStep.getId());
  List<PcPointStepDevice> pcPointStepDevices = pcPointStepDeviceMapper.selectPcPointStepDeviceList(pcPointStepDevice);
  if (pcPointStepDevices.isEmpty()) {
      return;
  }

  PcPointStepDevice pcPointStepDeviceInfo = pcPointStepDevices.get(0);

  Map<String, List<ModelResult>> coincideModelResultsType = getCoincideModelResultsType(collect, pcPointStepDeviceInfo);
  PcPointRecordDetail pcPointRecordDetail = null;
  boolean isAlarm = false;
  boolean connectPipesCorrect = redisUtil.hHasKey(PC_POINT_STEP_HANDLE_KEY + "_" + pcPointRecord.getId(), URLConstant.CONNECT_PIPES_CORRECT + callbackResult.getPointId() + "_" + callbackResult.getTaskId());
  String state = "continue";
  if (connectPipesCorrect) {
      Object connectPipesCorrectTime = redisUtil.hget(PC_POINT_STEP_HANDLE_KEY + "_" + pcPointRecord.getId(), URLConstant.CONNECT_PIPES_CORRECT + callbackResult.getPointId() + "_" + callbackResult.getTaskId());
      long i = DateUtils.getDifferenceSeconds(DateUtils.parseDate(callbackResult.getTime()), DateUtils.parseDate(connectPipesCorrectTime));
      if (coincideModelResultsType.isEmpty()) {
          if (i > pcPointStep.getMonitorPrefixSecond()) {

              state = "alarm";
          }
      } else {
          if (i > pcPointStep.getMonitorPrefixSecond()) {

              state = "alarm";
          } else {
              state = "normal";
          }
      }
  }
  if ("continue".equals(state)) {
      return;
  }

  Long alarmState = URLConstant.TRUE_STATUS;
  Long dealState = URLConstant.DEAL_STATE_NOT_NEED_DEAL; 
  if ("alarm".equals(state)) {

      alarmState = URLConstant.FALSE_STATUS;
      dealState = URLConstant.DEAL_STATE_NOT_DEAL;
      isAlarm = true;
  }

  String s = scpDownFile(sftpConfig, imagePath);
  String frameImgPath = imageResultHandle(coincideModelResultsType, s, pcPointStep, pcPointStepDeviceInfo);
  boolean isDone = checkStepIsDone(pcPointRecord, pcPointStep, callbackResult.getTaskId());
  if (!isDone) {
      pcPointRecordDetail = PcPointRecordDetail.builder().build();

      boolean hSetNx = redisUtil.hsetnx(PC_POINT_STEP_HANDLE_KEY + "_" + pcPointRecord.getId(), URLConstant.RESULT_STEP + pcPointStep.getId() + "_" + callbackResult.getTaskId(), URLConstant.END);
      if (hSetNx){
          pcPointRecordDetailMapper.insertPcPointRecordDetail(pcPointRecordDetail);

          if (!isAlarm) {
              return;
          }

          if (Objects.equals(pcPointRecordDetail.getAlarmState(), URLConstant.FALSE_STATUS)) {
              if (Objects.equals(pcPointRecord.getAlarmState(), URLConstant.TRUE_STATUS)) {

                  pcPointRecord.setAlarmState(URLConstant.FALSE_STATUS);
                  pcPointRecord.setDealState(URLConstant.DEAL_STATE_NOT_DEAL);
                  pcPointRecordMapper.updatePcPointRecord(pcPointRecord);
              }


              PcMonitoringDeviceResult build = PcMonitoringDeviceResult.builder().build();
              pcMonitoringDeviceResultService.insertPcMonitoringDeviceResult(build);

              PcMonitoringDevice pcMonitoringDevice = pcMonitoringDeviceService.selectPcMonitoringDeviceById(callbackResult.getVideoId());
              aiTaskService.publishAlarmInformation(build, pcPoint.getDeptName(), pcPointStep.getModelName(), pcMonitoringDevice.getName());
          }
      }
  }

  File file = new File(s);
  if (file.exists()) {
      file.delete();
  }
}


private void manholeCoverCloseResultHandle(PcPointRecord pcPointRecord, PcPointStep pcPointStep, PcPointRecordCallbackResult callbackResult, SftpConfig sftpConfig, PcPoint pcPoint, PcAiModel pcAiModel,String type,String targetType) {

  String imagePath = callbackResult.getImagePath();
  List<ModelResult> result = callbackResult.getResult();
  List<ModelResult> collect = result.stream().filter(o -> type.equals(o.getLabel()) && o.getScore() > Double.parseDouble(pcAiModel.getSimilarity())).collect(Collectors.toList());
  List<ModelResult> tsCollect = result.stream().filter(o -> "ts_" + type.equals(o.getLabel()) && o.getScore() > Double.parseDouble(pcAiModel.getSimilarity())).collect(Collectors.toList());
  collect.addAll(tsCollect);

  PcPointStepDevice pcPointStepDevice = new PcPointStepDevice();
  pcPointStepDevice.setDeviceId(callbackResult.getVideoId());
  pcPointStepDevice.setGroupId(callbackResult.getPointId());
  pcPointStepDevice.setStepId(pcPointStep.getId());
  List<PcPointStepDevice> pcPointStepDevices = pcPointStepDeviceMapper.selectPcPointStepDeviceList(pcPointStepDevice);
  if (pcPointStepDevices.isEmpty()) {
      return;
  }

  PcPointStepDevice pcPointStepDeviceInfo = pcPointStepDevices.get(0);

  Map<String, List<ModelResult>> coincideModelResultsType = getCoincideModelResultsType(collect, pcPointStepDeviceInfo);

  PcPointRecordDetail pcPointRecordDetail = null;
  if (redisUtil.hHasKey(PC_POINT_STEP_HANDLE_KEY + "_" + pcPointRecord.getId(), URLConstant.PIPES_RESET + pcPointRecord.getId() + "_" + callbackResult.getTaskId())) {
      String s = scpDownFile(sftpConfig, imagePath);
      String frameImgPath = imageResultHandle(coincideModelResultsType, s, pcPointStep, pcPointStepDeviceInfo);
      if (!coincideModelResultsType.isEmpty()) {
          pcPointRecordDetail = PcPointRecordDetail.builder().build();
      } else {
          pcPointRecordDetail = PcPointRecordDetail.builder().build();
      }

      File file = new File(s);
      if (file.exists()) {
          file.delete();
      }

      boolean isDone = checkStepIsDone(pcPointRecord, pcPointStep, callbackResult.getTaskId());
      if (pcPointRecordDetail != null && !isDone) {
          boolean hSetNx = redisUtil.hsetnx(PC_POINT_STEP_HANDLE_KEY + "_" + pcPointRecord.getId(), URLConstant.RESULT_STEP + pcPointStep.getId() + "_" + callbackResult.getTaskId(), URLConstant.END);
          if (hSetNx){
              pcPointRecordDetailMapper.insertPcPointRecordDetail(pcPointRecordDetail);
              if (Objects.equals(pcPointRecordDetail.getAlarmState(), URLConstant.FALSE_STATUS)) {
                  if (Objects.equals(pcPointRecord.getAlarmState(), URLConstant.TRUE_STATUS)) {
                      pcPointRecord.setAlarmState(URLConstant.FALSE_STATUS);
                      pcPointRecord.setDealState(URLConstant.DEAL_STATE_NOT_DEAL);
                      pcPointRecordMapper.updatePcPointRecord(pcPointRecord);
                  }
                  PcMonitoringDeviceResult build = PcMonitoringDeviceResult.builder().build();
                  pcMonitoringDeviceResultService.insertPcMonitoringDeviceResult(build);
                  PcMonitoringDevice pcMonitoringDevice = pcMonitoringDeviceService.selectPcMonitoringDeviceById(callbackResult.getVideoId());
                  aiTaskService.publishAlarmInformation(build, pcPoint.getDeptName(), pcPointStep.getModelName(), pcMonitoringDevice.getName());
              }
          }
      }
  }
}
private void operateResetResultResetHandle(PcPointRecord pcPointRecord, PcPointStep pcPointStep, PcPointRecordCallbackResult callbackResult, SftpConfig sftpConfig, String type, PcPoint pcPoint, PcAiModel pcAiModel,String type,String targetType) {

  String imagePath = callbackResult.getImagePath();
  if (!redisUtil.hHasKey(PC_POINT_STEP_HANDLE_KEY + "_" + pcPointRecord.getId(), URLConstant.CONNECT_PIPES_CORRECT + callbackResult.getPointId() + "_" + callbackResult.getTaskId())) {
      return;
  }

  List<ModelResult> result = callbackResult.getResult();
  List<ModelResult> collect = result.stream().filter(o -> type.equals(o.getLabel()) && o.getScore() > Double.parseDouble(pcAiModel.getSimilarity())).collect(Collectors.toList());


  PcPointStepDevice pcPointStepDevice = new PcPointStepDevice();
  pcPointStepDevice.setDeviceId(callbackResult.getVideoId());
  pcPointStepDevice.setGroupId(callbackResult.getPointId());
  pcPointStepDevice.setStepId(pcPointStep.getId());
  List<PcPointStepDevice> pcPointStepDevices = pcPointStepDeviceMapper.selectPcPointStepDeviceList(pcPointStepDevice);
  if (pcPointStepDevices.isEmpty()) {
      return;
  }
  PcPointStepDevice pcPointStepDeviceInfo = pcPointStepDevices.get(0);
  PcPointRecordDetail pcPointRecordDetail = null;
  if ("pipes".equals(type)){
      String coordinate = pcPointStepDeviceInfo.getCoordinate();
      com.alibaba.fastjson.JSONArray coordinateArray = JSONObject.parseArray(coordinate);

      boolean pipesReset = true;
      Map<String, List<ModelResult>> coincideModelResultsTypes = new HashMap<>();


      List<ModelResult> collect1 = new ArrayList<>();
      for (Object o : coordinateArray) {
          pcPointStepDeviceInfo.setCoordinate(Collections.singletonList(o).toString());

          Map<String, List<ModelResult>> coincideModelResultsType = getCoincideModelResultsType(collect, pcPointStepDeviceInfo);

          if (coincideModelResultsType.isEmpty()) {
              pipesReset = false;
              break;
          }
          List<ModelResult> collect2 = coincideModelResultsType.values().stream().flatMap(List::stream).collect(Collectors.toList());
          collect1.addAll(collect2);
      }


      if (pipesReset){
          Map<String, List<ModelResult>> coincideModelResultsType = new HashMap<>();
          coincideModelResultsType.put("0",collect1);
          String s = scpDownFile(sftpConfig, imagePath);
          String frameImgPath = imageResultHandle(coincideModelResultsType, s, pcPointStep, pcPointStepDeviceInfo);
          pcPointRecordDetail = PcPointRecordDetail.builder().build();

          File file = new File(s);
          if (file.exists()) {
              file.delete();
          }
      }
  }else {
      Map<String, List<ModelResult>> coincideModelResultsType = getCoincideModelResultsType(collect, pcPointStepDeviceInfo);

      if (!coincideModelResultsType.isEmpty()) {
          String s = scpDownFile(sftpConfig, imagePath);
          String frameImgPath = imageResultHandle(coincideModelResultsType, s, pcPointStep, pcPointStepDeviceInfo);
          pcPointRecordDetail = PcPointRecordDetail.builder().build();

          File file = new File(s);
          if (file.exists()) {
              file.delete();
          }
      }
  }

  boolean isDone = checkStepIsDone(pcPointRecord, pcPointStep, callbackResult.getTaskId());
  if (pcPointRecordDetail != null && !isDone) {
      boolean hSetNx = redisUtil.hsetnx(PC_POINT_STEP_HANDLE_KEY + "_" + pcPointRecord.getId(), URLConstant.RESULT_STEP + pcPointStep.getId() + "_" + callbackResult.getTaskId(), URLConstant.END);
      if (hSetNx){
          pcPointRecordDetailMapper.insertPcPointRecordDetail(pcPointRecordDetail);

          if (Objects.equals(pcPointRecordDetail.getAlarmState(), URLConstant.FALSE_STATUS)) {
              if (Objects.equals(pcPointRecord.getAlarmState(), URLConstant.TRUE_STATUS)) {

                  pcPointRecord.setAlarmState(URLConstant.FALSE_STATUS);
                  pcPointRecord.setDealState(URLConstant.DEAL_STATE_NOT_DEAL);
                  pcPointRecordMapper.updatePcPointRecord(pcPointRecord);
              }


              PcMonitoringDeviceResult build = PcMonitoringDeviceResult.builder().build();
              pcMonitoringDeviceResultService.insertPcMonitoringDeviceResult(build);
              PcMonitoringDevice pcMonitoringDevice = pcMonitoringDeviceService.selectPcMonitoringDeviceById(callbackResult.getVideoId());
              aiTaskService.publishAlarmInformation(build, pcPoint.getDeptName(), pcPointStep.getModelName(), pcMonitoringDevice.getName());
          }
      }

      if ("pipes".equals(type)) {
          redisUtil.hsetnx(PC_POINT_STEP_HANDLE_KEY + "_" + pcPointRecord.getId(), URLConstant.PIPES_RESET + pcPointRecord.getId() + "_" + callbackResult.getTaskId(), URLConstant.END);
      }
  }
}

String screen_url = configService.selectConfigByKey(URLConstant.SCREEN_URL);
if (null == screen_url || screen_url.trim().length() < 1 || screen_url.equals("screen_url"))
    return;
ConcurrentHashMap<String, ConcurrentHashMap<String, ConcurrentHashMap<String, WebSocketServer>>> webSocketSet = webSocketServer.webSocketSet;
if (CollectionUtil.isEmpty(webSocketSet))
    return;
List<Long> parentDeptIdList = sysDeptService.getParentDeptIdList(monitoringDeviceResult.getDeptId());

ConcurrentHashMap<String, ConcurrentHashMap<String, WebSocketServer>> apiScreen = webSocketSet.get("screenAlarm");
if (CollectionUtil.isNotEmpty(apiScreen)) {
    Map<String, ConcurrentHashMap<String, WebSocketServer>> sendUserMap = apiScreen.entrySet().stream().filter(l -> parentDeptIdList.contains(userService.selectUserById(Long.valueOf(l.getKey())).getDeptId())).collect(Collectors.toMap(l -> l.getKey(), k -> k.getValue(), (oldValue, newValue) -> oldValue));
    sendUserMap.entrySet().stream().forEach(entry -> {
        try {
            String is_or_not_important_alarm = configService.selectConfigByKey(URLConstant.IS_OR_NOT_IMPORTANT_ALARM);
            List<String> isOrNotImportantAlarm = StringUtils.isNotBlank(is_or_not_important_alarm) ? Arrays.asList(is_or_not_important_alarm.split(",")) : null;
            Map<String, Object> hashMap = new HashMap<>();
            hashMap.put("importantAlarm", !Optional.ofNullable(isOrNotImportantAlarm).isPresent() ? false : isOrNotImportantAlarm.contains(String.valueOf(monitoringDeviceResult.getAiModelId())));

            entry.getValue().entrySet().stream().forEach(entry_1 -> publishAlarmInformation(entry_1.getValue(), gson.toJson(hashMap), "apiScreen", "publishAlarmInformation"));
        } catch (Exception e) {
            logger.error("失败" + e);
        }
    });
}


public Map<String, Object> getAlarmStatistics(Long userId, String url) {
  Map<String, Object> mapList = new HashMap<>();
  PcMonitoringDeviceResult pcMonitoringDeviceResult = new PcMonitoringDeviceResult();
  SysUser sysUser = iSysUserService.selectUserById(userId);
  if (userId != 1L) { 
      List<Long> deptIdList = sysDeptService.getSubDeptIdList(sysUser.getDeptId());
      pcMonitoringDeviceResult.setDeptIds(deptIdList); 
  }
  Long allCountResult = pcMonitoringDeviceResultMapper.selectPcMonitoringDeviceResultCount(pcMonitoringDeviceResult);
  mapList.put("all", allCountResult);
  Date weekStartTime = LocalDateTimeUtils.convert(LocalDateTimeUtils.weekStartTime());
  Date last7DaysEndTime = LocalDateTimeUtils.convert(LocalDateTimeUtils.last7DaysEndTime());
  Map<String, Object> params = new HashMap<>();
  params.put("beginCreateDate", weekStartTime);
  params.put("endCreateDate", last7DaysEndTime);
  pcMonitoringDeviceResult.setParams(params);

  Long weekCountResult = pcMonitoringDeviceResultMapper.selectPcMonitoringDeviceResultCount(pcMonitoringDeviceResult);
  mapList.put("week", weekCountResult);
  Date last7DaysStartTime = LocalDateTimeUtils.convert(LocalDateTimeUtils.last7DaysStartTime());
  List<String> betweenDays = DateUtils.getBetweenDays(last7DaysStartTime, last7DaysEndTime);
  params.put("beginCreateDate", last7DaysStartTime);
  pcMonitoringDeviceResult.setParams(params);
  Map<String, Object> listMap1 = pcMonitoringDeviceResultMapper.selectPcMonitoringDeviceResultWeek(pcMonitoringDeviceResult);
  Map<String, Object> weeks = new LinkedHashMap<>();
  for (String betweenDay : betweenDays) {
      String value = "0";
      if (Optional.ofNullable(listMap1.get(betweenDay)).isPresent())
          value = new Gson().toJson(new Gson().fromJson(String.valueOf(listMap1.get(betweenDay)), Map.class).get("count"));
      weeks.put(betweenDay.substring(5).replace("-", "."), (int) Double.parseDouble(value));
  }
  mapList.put("detail", weeks);
  return mapList;
}
public static void scpFile(SftpConfig sftpConfig ,String remote, String local) {
  try {
      long startTime = System.currentTimeMillis();
      SshClient client = SshClient.setUpDefaultClient();
      client.start();
      ClientSession session = client.connect(sftpConfig.getUserName(), sftpConfig.getHost(), sftpConfig.getPort()).verify().getSession();
      session.addPasswordIdentity(sftpConfig.getPassword());
      boolean isSuccess = session.auth().verify().isSuccess();
      // 认证成功
      if (isSuccess) {
          long middleTime = System.currentTimeMillis();
          ScpClientCreator creator = ScpClientCreator.instance();
          ScpClient scpClient = creator.createScpClient(session);
          scpClient.download(remote,local);
          if (scpClient != null) {
              scpClient = null;
          }
          if (session != null && session.isOpen()) {
              session.close();
          }
          if (client != null && client.isOpen()) {
              client.stop();
              client.close();
          }
      }
      long endTime = System.currentTimeMillis();
  }catch (Exception e){
      log.error(e.getMessage());
  }
  }

  private static long countNum = 0;
  public static ChannelSftp connect(SftpConfig sftpConfig) {
  ChannelSftp sftp = null;
  try {
      JSch jsch = new JSch();
      jsch.getSession(sftpConfig.getUserName(), sftpConfig.getHost(), sftpConfig.getPort());
      Session sshSession = jsch.getSession(sftpConfig.getUserName(), sftpConfig.getHost(), sftpConfig.getPort());
      sshSession.setPassword(sftpConfig.getPassword());
      Properties sshConfig = new Properties();
      sshConfig.put("StrictHostKeyChecking", "no");
      sshSession.setConfig(sshConfig);
      sshSession.connect();
      Channel channel = sshSession.openChannel("sftp");
      channel.connect();
      sftp = (ChannelSftp) channel;
  } catch (Exception e) {
      try {
          countNum += 1;
          if (sftpConfig.getCount() == countNum) {
              throw new RuntimeException(e);
          }
          Thread.sleep(sftpConfig.getSleepTime());
          connect(sftpConfig);
      } catch (InterruptedException e1) {
          throw new RuntimeException(e1);
      }
  }
  return sftp;
  }
  public static void upload(String directory, String uploadFile, SftpConfig sftpConfig) {
  ChannelSftp sftp = connect(sftpConfig);
  try {
      sftp.cd(directory);
  } catch (Exception e) {
      try {
          sftp.mkdir(directory);
          sftp.cd(directory);
      } catch (Exception e1) {
          throw new RuntimeException("ftp创建文件路径失败" + directory);
      }
  }
  File file = new File(uploadFile);
  InputStream inputStream=null;
  try {
      inputStream = Files.newInputStream(file.toPath());
      sftp.put(inputStream, file.getName());
  } catch (Exception e) {
      throw new RuntimeException("sftp异常" + e);
  } finally {
      disConnect(sftp);
      closeStream(inputStream,null);
  }
  }

  public static void download(String directory, String downloadFile, String saveFile, SftpConfig sftpConfig) {
  OutputStream output = null;
  try {
      File localDirFile = new File(saveFile);
      if (!localDirFile.exists()) {
          localDirFile.mkdirs();
      }
      ChannelSftp sftp = connect(sftpConfig);
      sftp.cd(directory);
      output = Files.newOutputStream(new File(saveFile.concat(File.separator).concat(downloadFile)).toPath());
      sftp.get(downloadFile, output);
      disConnect(sftp);
  } catch (Exception e) {
      throw new RuntimeException("文件下载出现异常,[{}]", e);
  } finally {
      closeStream(null,output);
  }
  }
  public static void getFileDir(String remoteFilePath, String localDirPath, SftpConfig sftpConfig) throws Exception {
  File localDirFile = new File(localDirPath);
  if (!localDirFile.exists()) {
      localDirFile.mkdirs();
  }
  ChannelSftp channelSftp = connect(sftpConfig);
  Vector<ChannelSftp.LsEntry> lsEntries = channelSftp.ls(remoteFilePath);
  for (ChannelSftp.LsEntry entry : lsEntries) {
      String fileName = entry.getFilename();
      if (checkFileName(fileName)) {
          continue;
      }
      String remoteFileName = getRemoteFilePath(remoteFilePath, fileName);
      channelSftp.get(remoteFileName, localDirPath);
  }
  disConnect(channelSftp);
  }
  private static void closeStream(InputStream inputStream, OutputStream outputStream) {
  if (outputStream != null) {
      try {
          outputStream.close();
      } catch (IOException e) {
          e.printStackTrace();
      }
  }
  if(inputStream != null){
      try {
          inputStream.close();
      } catch (IOException e) {
          e.printStackTrace();
      }
  }
  }

  private static boolean checkFileName(String fileName) {
  if (".".equals(fileName) || "..".equals(fileName)) {
      return true;
  }
  return false;
  }

  private static String getRemoteFilePath(String remoteFilePath, String fileName) {
  if (remoteFilePath.endsWith("/")) {
      return remoteFilePath.concat(fileName);
  } else {
      return remoteFilePath.concat("/").concat(fileName);
  }
  }

  public static void delete(String directory, String deleteFile, ChannelSftp sftp) {
  try {
      sftp.cd(directory);
      sftp.rm(deleteFile);
  } catch (Exception e) {
      throw new RuntimeException(e);
  }
  }
  public static List<String> listFiles(String directory, SftpConfig sftpConfig) throws SftpException {
  ChannelSftp sftp = connect(sftpConfig);
  List fileNameList = new ArrayList();
  try {
      sftp.cd(directory);
  } catch (SftpException e) {
      return fileNameList;
  }
  Vector vector = sftp.ls(directory);
  for (int i = 0; i < vector.size(); i++) {
      if (vector.get(i) instanceof ChannelSftp.LsEntry) {
          ChannelSftp.LsEntry lsEntry = (ChannelSftp.LsEntry) vector.get(i);
          String fileName = lsEntry.getFilename();
          if (".".equals(fileName) || "..".equals(fileName)) {
              continue;
          }
          fileNameList.add(fileName);
      }
  }
  disConnect(sftp);
  return fileNameList;
  }
  public static void disConnect(ChannelSftp sftp) {
  try {
      sftp.disconnect();
      sftp.getSession().disconnect();
  } catch (Exception e) {
      e.printStackTrace();
  }
  }


public static String downloadFromSftp(String remoteFile,String localFile,SftpConfig sftpConfig){
    ChannelSftp connect = connect(sftpConfig);
    try {
        connect.get(remoteFile,localFile);
    }catch (Exception e){
        log.error(e.getMessage());
    }finally {
        connect.disconnect();
    }
    if (new File(localFile).exists()){
        return localFile;
    }
    return null;
}

public LinkedHashMap<String, Object> workOrderStatistics(PcWoTask pcWoTask) {
Date last7DaysStartTime = LocalDateTimeUtils.convert(LocalDateTimeUtils.last7DaysStartTime());
Date last7DaysEndTime = LocalDateTimeUtils.convert(LocalDateTimeUtils.last7DaysEndTime());

List<String> betweenDays = DateUtils.getBetweenDays(last7DaysStartTime, last7DaysEndTime);
Map<String,Object> params = new HashMap<>();
params.put("beginDate",last7DaysStartTime);
params.put("endDate",last7DaysEndTime);
pcWoTask.setParams(params);

List<PcWoTask> pcWoTasks = pcWoTaskMapper.selectPcWoTaskListForFormatDate(pcWoTask);
Map<String, List<PcWoTask>> collectForAiModel = pcWoTasks.stream().collect(Collectors.groupingBy(PcWoTask::getPcAiModelName));

LinkedHashMap<String,Object> echartsRes = new LinkedHashMap<>();
echartsRes.put("all",pcWoTasks.size());
echartsRes.put("unprocessed",pcWoTasks.stream().filter(a -> a.getWoTaskStatus() == 0).count());

//周天数数据
List<String> weeks = new ArrayList<>();
for (String betweenDay : betweenDays) {
    weeks.add(DateUtils.dateToWeek(DateUtils.parseDate(betweenDay)));
}
echartsRes.put("weeks",weeks);

//组装每天的每个模型的工单数量
Map<String,List<Integer>> numsDetail = new HashMap<>();

collectForAiModel.forEach((modelName, pcWoTasksForModels) -> {
    List<Integer> workOrderNums = new ArrayList<>();
    for (String betweenDay : betweenDays) {
        int nums = 0;
        for (PcWoTask pcWoTasksForModel : pcWoTasksForModels) {
            if (betweenDay.equals(pcWoTasksForModel.getCreateDate())){
                nums +=1;
            }
        }
        workOrderNums.add(nums);
    }
    numsDetail.put(modelName,workOrderNums);
});
echartsRes.put("detail",numsDetail);
return echartsRes;
}
文档更新时间: 2023-12-18 03:25   作者:JeffreyCheung