diff --git a/detection/src/main/java/com/njcn/gather/device/service/impl/PqDevServiceImpl.java b/detection/src/main/java/com/njcn/gather/device/service/impl/PqDevServiceImpl.java index 45308d5e..c52c4a30 100644 --- a/detection/src/main/java/com/njcn/gather/device/service/impl/PqDevServiceImpl.java +++ b/detection/src/main/java/com/njcn/gather/device/service/impl/PqDevServiceImpl.java @@ -778,7 +778,7 @@ public class PqDevServiceImpl extends ServiceImpl implements } int count = this.count(queryWrapper); if (count > 0) { - throw new BusinessException(DetectionResponseEnum.PQ_DEV_REPEAT); + throw new BusinessException(DetectionResponseEnum.PQ_DEV_REPEAT, "【" + param.getName() + "】被检设备已存在"); } } @@ -1179,10 +1179,23 @@ public class PqDevServiceImpl extends ServiceImpl implements @Transactional public boolean importContrastDev(List contrastDevExcelList, String patternId, String planId) { if (CollUtil.isNotEmpty(contrastDevExcelList)) { - List monitorList = new ArrayList<>(); - List oldDevList = contrastDevExcelList.stream().map(devExcel -> { + // 根据设备名称分组 + Map> listMap = contrastDevExcelList.stream() + .collect(Collectors.groupingBy(ContrastDevExcel::getName, LinkedHashMap::new, Collectors.toList())); + List oldDevList = new ArrayList<>(listMap.size()); + List finalMonitorList = new ArrayList<>(); + for (Map.Entry> entry : listMap.entrySet()) { + String name = entry.getKey(); + List devExcelList = entry.getValue(); + // 监测点数据 + List pqMonitorExcelList = devExcelList.stream() + .map(ContrastDevExcel::getPqMonitorExcelList) + .filter(Objects::nonNull) + .flatMap(List::stream) + .collect(Collectors.toList()); + // 取第一条为设备基本信息 + ContrastDevExcel devExcel = devExcelList.get(0); PqDev pqDev = BeanUtil.copyProperties(devExcel, PqDev.class); - if (pqDev.getEncryptionFlag() == 1) { if (StrUtil.isNotBlank(pqDev.getSeries()) && StrUtil.isNotBlank(pqDev.getDevKey())) { pqDev.setSeries(EncryptionUtil.encodeString(1, pqDev.getSeries())); @@ -1194,43 +1207,47 @@ public class PqDevServiceImpl extends ServiceImpl implements DevType devType = devTypeService.getByName(pqDev.getDevType()); if (ObjectUtil.isNull(devType)) { throw new BusinessException(DetectionResponseEnum.DEV_TYPE_NOT_EXIST); - } else { - pqDev.setDevType(devType.getId()); - - Integer devChns = devType.getDevChns(); - List numList = devExcel.getPqMonitorExcelList().stream().map(monitorExcel -> monitorExcel.getNum()).collect(Collectors.toList()); - if (CollUtil.isNotEmpty(numList)) { - Integer max = CollectionUtil.max(numList); - Integer min = CollectionUtil.min(numList); - if (min < 1 || max > devChns) { - throw new BusinessException(DetectionResponseEnum.MONITOR_NUM_OUT_OF_RANGE); - } - if (min == max && numList.size() > 1) { - throw new BusinessException(DetectionResponseEnum.MONITOR_NUM_REPEAT); - } - } + } + // 校验监测点数量 + int devChns = devType.getDevChns(); + if (pqMonitorExcelList.size() != devChns) { + throw new BusinessException(DetectionResponseEnum.IMPORT_DATA_FAIL, "【" + name + "】的设备类型必须具备" + devChns + "个监测点信息!"); + } + List numList = pqMonitorExcelList.stream().map(PqMonitorExcel::getNum).collect(Collectors.toList()); + // 判断是否有重复的num + Set uniqueNumSet = new HashSet<>(numList); + if (uniqueNumSet.size() != numList.size()) { + throw new BusinessException(DetectionResponseEnum.MONITOR_NUM_REPEAT); + } + Integer max = CollectionUtil.max(numList); + Integer min = CollectionUtil.min(numList); + if (min < 1 || max > devChns) { + throw new BusinessException(DetectionResponseEnum.MONITOR_NUM_OUT_OF_RANGE); } + pqDev.setDevType(devType.getId()); pqDev.setImportFlag(1); pqDev.setId(UUID.randomUUID().toString().replaceAll("-", "")); pqDev.setCreateId(pqDev.getName()); //导入时设备序列号默认与设备名称相同 - - StringBuilder sb = new StringBuilder(); - for (int i = 0; i < devExcel.getPqMonitorExcelList().size(); i++) { - PqMonitor monitor = BeanUtil.copyProperties(devExcel.getPqMonitorExcelList().get(i), PqMonitor.class); - if (StrUtil.isBlank(monitor.getName())) { - continue; - } - sb.append(monitor.getNum() + StrUtil.COMMA); + List monitorList = new ArrayList<>(); + // 根据num排序 + pqMonitorExcelList.sort(Comparator.comparingInt(PqMonitorExcel::getNum)); + for (PqMonitorExcel pqMonitorExcel : pqMonitorExcelList) { + PqMonitor monitor = BeanUtil.copyProperties(pqMonitorExcel, PqMonitor.class); monitor.setDevId(pqDev.getId()); monitorList.add(monitor); } - if (sb.length() > 0) { - pqDev.setInspectChannel(sb.replace(sb.length() - 1, sb.length(), "").toString()); + StringBuilder inspectChannelBuilder = new StringBuilder(); + for (int i = 1; i <= devChns; i++) { + inspectChannelBuilder.append(i); + if (i < devChns) { + inspectChannelBuilder.append(","); + } } - return pqDev; - }).collect(Collectors.toList()); - + pqDev.setInspectChannel(inspectChannelBuilder.toString()); + oldDevList.add(pqDev); + finalMonitorList.addAll(monitorList); + } //逆向可视化 this.reverseVisualizeProvinceDev(oldDevList, patternId); @@ -1259,7 +1276,7 @@ public class PqDevServiceImpl extends ServiceImpl implements PqDev newDev = newDevList.stream().filter(dev -> dev.getHarmSysId().equals(oldDev.getHarmSysId())).findFirst().orElse(null); if (ObjectUtil.isNotNull(newDev)) { newDevList.remove(newDev); - monitorList.stream() + finalMonitorList.stream() .filter(monitor -> monitor.getDevId().equals(newDev.getId())) .forEach(monitor -> monitor.setDevId(oldDev.getId())); BeanUtil.copyProperties(newDev, oldDev, "id"); @@ -1287,8 +1304,8 @@ public class PqDevServiceImpl extends ServiceImpl implements .in("pq_monitor.Dev_Id", devIdList); pqMonitorService.remove(wrapper); } - pqMonitorService.reverseVisualizeMonitor(monitorList); - pqMonitorService.saveBatch(monitorList); + pqMonitorService.reverseVisualizeMonitor(finalMonitorList); + pqMonitorService.saveBatch(finalMonitorList); return true; } return false; @@ -1404,11 +1421,6 @@ public class PqDevServiceImpl extends ServiceImpl implements pqDev.setDelegate(delegateDictData.getId()); } } -// pqDev.setTimeCheckResult(TimeCheckResultEnum.UNKNOWN.getValue()); -// pqDev.setFactorCheckResult(FactorCheckResultEnum.UNKNOWN.getValue()); -// pqDev.setCheckState(CheckStateEnum.UNCHECKED.getValue()); -// pqDev.setReportState(DevReportStateEnum.UNCHECKED.getValue()); -// pqDev.setCheckResult(CheckResultEnum.UNCHECKED.getValue()); pqDev.setState(DataStateEnum.ENABLE.getCode()); }); }