diff --git a/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/deviceoperationrecord/DeviceOperationRecordServiceImpl.java b/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/deviceoperationrecord/DeviceOperationRecordServiceImpl.java index cb2b5d1f7..5050a3a2a 100644 --- a/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/deviceoperationrecord/DeviceOperationRecordServiceImpl.java +++ b/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/deviceoperationrecord/DeviceOperationRecordServiceImpl.java @@ -159,46 +159,7 @@ public class DeviceOperationRecordServiceImpl implements DeviceOperationRecordSe @Override public List deviceOperationPageList(DeviceTotalTimeRecordReqVO pageReqVO) { - - applyPeriodIfNecessary(pageReqVO); - - List deviceList = - deviceOperationRecordMapper.selectDeviceListFromMySQL(pageReqVO); - - if (CollectionUtils.isEmpty(deviceList)) { - return Collections.emptyList(); - } - - List deviceIds = deviceList.stream() - .map(DeviceTotalTimeRecordRespVO::getId) - .collect(Collectors.toList()); - - List> statsList = - deviceOperationRecordMapper.selectDeviceStatsFromTD( - deviceIds, - pageReqVO.getStartTime(), - pageReqVO.getEndTime() - ); - - Map tdStatsMap = convertStatsListToMap(statsList); - - List result = new ArrayList<>(); - for (DeviceTotalTimeRecordRespVO device : deviceList) { - DeviceTotalTimeRecordRespVO stats = tdStatsMap.get(device.getId()); - if (stats != null) { - device.setTotalOfflineTime(stats.getTotalOfflineTime()); - device.setTotalRunningTime(stats.getTotalRunningTime()); - device.setTotalStandbyTime(stats.getTotalStandbyTime()); - device.setTotalFaultTime(stats.getTotalFaultTime()); - } else { - device.setTotalOfflineTime(0); - device.setTotalRunningTime(0); - device.setTotalStandbyTime(0); - device.setTotalFaultTime(0); - } - result.add(device); - } - + List result = queryDeviceStatsRaw(pageReqVO); calculateAndSetConvertedValues(result, pageReqVO); result.sort(Comparator.comparingDouble(this::parsePercentValue).reversed()); return result; @@ -455,20 +416,26 @@ public class DeviceOperationRecordServiceImpl implements DeviceOperationRecordSe //参数构造 applyPeriodIfNecessary(pageReqVO); - List records = deviceOperationPageList(pageReqVO); + List records = queryDeviceStatsRaw(pageReqVO); DeviceOperationOverviewRespVO respVO = new DeviceOperationOverviewRespVO(); - double totalRunningHours = records.stream().mapToDouble(DeviceTotalTimeRecordRespVO::getTotalRunningTime).sum(); - double totalStandbyHours = records.stream().mapToDouble(DeviceTotalTimeRecordRespVO::getTotalStandbyTime).sum(); - double totalFaultHours = records.stream().mapToDouble(DeviceTotalTimeRecordRespVO::getTotalFaultTime).sum(); - double totalOfflineHours = records.stream().mapToDouble(DeviceTotalTimeRecordRespVO::getTotalOfflineTime).sum(); + double totalRunningSeconds = records.stream().mapToDouble(DeviceTotalTimeRecordRespVO::getTotalRunningTime).sum(); + double totalStandbySeconds = records.stream().mapToDouble(DeviceTotalTimeRecordRespVO::getTotalStandbyTime).sum(); + double totalFaultSeconds = records.stream().mapToDouble(DeviceTotalTimeRecordRespVO::getTotalFaultTime).sum(); + double totalOfflineSeconds = records.stream().mapToDouble(DeviceTotalTimeRecordRespVO::getTotalOfflineTime).sum(); - double onlineHours = totalRunningHours + totalStandbyHours + totalFaultHours; - double totalHours = onlineHours + totalOfflineHours; - double utilizationRate = onlineHours > 0 ? totalRunningHours / onlineHours * 100 : 0D; - double powerOnRate = totalHours > 0 ? onlineHours / totalHours * 100 : 0D; - double faultRate = onlineHours > 0 ? totalFaultHours / onlineHours * 100 : 0D; - double standbyRate = onlineHours > 0 ? totalStandbyHours / onlineHours * 100 : 0D; + double onlineSeconds = totalRunningSeconds + totalStandbySeconds + totalFaultSeconds; + double totalSeconds = onlineSeconds + totalOfflineSeconds; + double utilizationRate = calculatePercent(totalRunningSeconds, onlineSeconds); + double powerOnRate = calculatePercent(onlineSeconds, totalSeconds); + double faultRate = calculatePercent(totalFaultSeconds, onlineSeconds); + double standbyRate = calculatePercent(totalStandbySeconds, onlineSeconds); + + double totalRunningHours = secondsToHours(totalRunningSeconds); + double totalStandbyHours = secondsToHours(totalStandbySeconds); + double totalFaultHours = secondsToHours(totalFaultSeconds); + double totalOfflineHours = secondsToHours(totalOfflineSeconds); + double totalHours = secondsToHours(totalSeconds); respVO.setMetrics(Arrays.asList( DeviceOperationOverviewRespVO.MetricItem.builder().key("utilizationRate").icon("ep:pie-chart").value(round2(utilizationRate)).unit("%").change(0D).build(), @@ -554,6 +521,23 @@ public class DeviceOperationRecordServiceImpl implements DeviceOperationRecordSe return denominator > 0 ? round2(numerator / denominator * 100) : 0D; } + private double calculatePercent(double numerator, double denominator) { + return denominator > 0 ? numerator / denominator * 100D : 0D; + } + + private double calculateUtilizationRate(DeviceTotalTimeRecordRespVO record) { + if (record == null) { + return 0D; + } + double runningSeconds = record.getTotalRunningTime(); + double onlineSeconds = runningSeconds + record.getTotalStandbyTime() + record.getTotalFaultTime(); + return round2(calculatePercent(runningSeconds, onlineSeconds)); + } + + private double secondsToHours(double seconds) { + return round2(seconds / 3600D); + } + private double round2(double value) { return Math.round(value * 100D) / 100D; } @@ -585,7 +569,7 @@ public class DeviceOperationRecordServiceImpl implements DeviceOperationRecordSe return DeviceOperationOverviewRespVO.TimelineRowItem.builder() .id(String.valueOf(record.getId())) .name(record.getDeviceName()) - .utilizationRate(parsePercentValue(record)) + .utilizationRate(calculateUtilizationRate(record)) .segments(segments) .build(); }).collect(Collectors.toList()); @@ -604,6 +588,47 @@ public class DeviceOperationRecordServiceImpl implements DeviceOperationRecordSe return rows.subList(fromIndex, toIndex); } + private List queryDeviceStatsRaw(DeviceTotalTimeRecordReqVO pageReqVO) { + applyPeriodIfNecessary(pageReqVO); + + List deviceList = + deviceOperationRecordMapper.selectDeviceListFromMySQL(pageReqVO); + + if (CollectionUtils.isEmpty(deviceList)) { + return Collections.emptyList(); + } + + List deviceIds = deviceList.stream() + .map(DeviceTotalTimeRecordRespVO::getId) + .collect(Collectors.toList()); + + List> statsList = + deviceOperationRecordMapper.selectDeviceStatsFromTD( + deviceIds, + pageReqVO.getStartTime(), + pageReqVO.getEndTime() + ); + + Map tdStatsMap = convertStatsListToMap(statsList); + List result = new ArrayList<>(); + for (DeviceTotalTimeRecordRespVO device : deviceList) { + DeviceTotalTimeRecordRespVO stats = tdStatsMap.get(device.getId()); + if (stats != null) { + device.setTotalOfflineTime(stats.getTotalOfflineTime()); + device.setTotalRunningTime(stats.getTotalRunningTime()); + device.setTotalStandbyTime(stats.getTotalStandbyTime()); + device.setTotalFaultTime(stats.getTotalFaultTime()); + } else { + device.setTotalOfflineTime(0); + device.setTotalRunningTime(0); + device.setTotalStandbyTime(0); + device.setTotalFaultTime(0); + } + result.add(device); + } + return result; + } + private List buildTimelineSegments(List timelineRecords, LocalDateTime start, LocalDateTime end,