Browse Source

设备状态柱状图按时间和类型24小时制

develop
王兴琳 1 month ago
parent
commit
9ab8fa6e7f
  1. 62
      zc-business/src/main/java/com/zc/business/controller/DcDeviceOnlineController.java
  2. 6
      zc-business/src/main/java/com/zc/business/domain/OnlineSum.java

62
zc-business/src/main/java/com/zc/business/controller/DcDeviceOnlineController.java

@ -39,6 +39,8 @@ import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.chrono.ChronoLocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.*;
@ -158,7 +160,67 @@ public class DcDeviceOnlineController extends BaseController {
}
return AjaxResult.success(maps);
}
@ApiOperation("设备状态柱状图按时间和类型24小时制")
@GetMapping("/listHourSystem")
public AjaxResult getStatusListHourSystem(
@RequestParam("startTime") String startTime,
@RequestParam("time") String endTime,
@RequestParam("type") String[] types,
@RequestParam(value = "direction", required = false) String direction) {
try {
// 解析时间
LocalDateTime start = LocalDateTime.parse(startTime, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
LocalDateTime end = LocalDateTime.parse(endTime, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
// 校验时间范围
if (start.isAfter(end)) {
return AjaxResult.error("开始时间不能晚于结束时间");
}
// 查询数据
List<OnlineSum> onlineSums = onlineSumService.queryByDeviceTypeAndDate(types, start.toLocalDate(), end.toLocalDate(), direction);
// 按设备类型分组
Map<String, List<OnlineSum>> stringListMap = onlineSums.stream().collect(Collectors.groupingBy(OnlineSum::getDeviceType));
// 构建结果
Map<String, Map<String, Map<String, String>>> maps = new HashMap<>();
for (String type : types) {
Map<String, Map<String, String>> dailyMap = new TreeMap<>();
List<OnlineSum> listOfType = stringListMap.getOrDefault(type, new ArrayList<>());
// 按日期分组
Map<LocalDate, List<OnlineSum>> localDateListMap = listOfType.stream().collect(Collectors.groupingBy(OnlineSum::getStatisticalDate));
// 按日期统计
for (LocalDate date = start.toLocalDate(); date.isBefore(end.toLocalDate().plusDays(1)); date = date.plusDays(1)) {
Map<String, String> hourlyMap = new TreeMap<>();
List<OnlineSum> dayList = localDateListMap.getOrDefault(date, new ArrayList<>());
// 按小时统计
for (int hour = 0; hour < 24; hour++) {
LocalDateTime startHour = LocalDateTime.of(date, LocalTime.of(hour, 0));
LocalDateTime endHour = startHour.plusHours(1);
List<OnlineSum> hourList = dayList.stream()
.filter(os -> os.getStatisticalDate().isAfter(ChronoLocalDate.from(startHour)) && os.getStatisticalDate().isBefore(ChronoLocalDate.from(endHour)))
.collect(Collectors.toList());
double averageRate = hourList.stream().mapToDouble(OnlineSum::getOnlineRate).average().orElse(0.0);
hourlyMap.put(startHour.toString(), MathUtil.doubleTwoDecimalStr(averageRate) + "%");
}
dailyMap.put(date.toString(), hourlyMap);
}
maps.put(type, dailyMap);
}
return AjaxResult.success(maps);
} catch (Exception e) {
return AjaxResult.error("查询设备状态柱状图数据失败");
}
}
/**
* 查询设备在线率
*

6
zc-business/src/main/java/com/zc/business/domain/OnlineSum.java

@ -36,9 +36,9 @@ public class OnlineSum implements java.io.Serializable {
private double offlineRate;//离线率
@Excel(name = "使用状态", dictType = "dc_device_use")
private String useState;
@DateTimeFormat(pattern = "yyyy-MM-dd")
@JsonFormat(pattern = "yyyy-MM-dd")
@Excel(name = "统计日期", width = 30, dateFormat = "yyyy-MM-dd")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@Excel(name = "统计日期", width = 30, dateFormat = "yyyy-MM-dd HH:mm:ss")
private LocalDate statisticalDate;//统计日期
private String deviceType;//设备类型
private int totalCount;//当天总次数

Loading…
Cancel
Save