diff --git a/src/main/java/com/example/device/controller/DeviceStatus.java b/src/main/java/com/example/device/controller/DeviceStatus.java index c9b1d4b..214f2a1 100644 --- a/src/main/java/com/example/device/controller/DeviceStatus.java +++ b/src/main/java/com/example/device/controller/DeviceStatus.java @@ -171,7 +171,7 @@ public class DeviceStatus { * */ - @Scheduled(cron = "0 30 23 * * ?") + @Scheduled(cron = "0 30 11,15,17,23 * * ?") public void calculateSuccessRate() { LocalDateTime todayStart = LocalDateTime.now().truncatedTo(ChronoUnit.DAYS); @@ -182,10 +182,14 @@ public class DeviceStatus { status.setStartTime(todayStart); status.setTime(currentTime); List listStatus = statusService.list(status); + //根据时间分组 Map> map = listStatus.stream() .collect(Collectors.groupingBy(Status -> Status.getTime().getHour())); + //根据类型分组 + Map> maps = listStatus.stream().filter(iteam->iteam.getType()!=null).collect(Collectors.groupingBy(Status::getType)); + String filePath=CreateNamedExcel(); - excelExportService.exportDataToExcel(map,filePath); + excelExportService.exportDataToExcel(map,maps,filePath); } diff --git a/src/main/java/com/example/device/entity/Device.java b/src/main/java/com/example/device/entity/Device.java index f07cd01..722847c 100644 --- a/src/main/java/com/example/device/entity/Device.java +++ b/src/main/java/com/example/device/entity/Device.java @@ -48,8 +48,18 @@ public class Device { this.deviceIp = deviceIp; } + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + private String deviceNo; private String deviceName; private String deviceIp; + private String type; + } \ No newline at end of file diff --git a/src/main/java/com/example/device/entity/Status.java b/src/main/java/com/example/device/entity/Status.java index 5c60048..2c963c3 100644 --- a/src/main/java/com/example/device/entity/Status.java +++ b/src/main/java/com/example/device/entity/Status.java @@ -18,9 +18,49 @@ public class Status { return startTime; } + public Status(long id, String deviceNo, String deviceName, int deviceStatus, LocalDateTime time, LocalDateTime startTime, String deviceIp, String successRate, String lostRate, String direction, String production, String model, String network, String content, String type) { + this.id = id; + this.deviceNo = deviceNo; + this.deviceName = deviceName; + this.deviceStatus = deviceStatus; + this.time = time; + this.startTime = startTime; + this.deviceIp = deviceIp; + this.successRate = successRate; + this.lostRate = lostRate; + this.direction = direction; + this.production = production; + this.model = model; + this.network = network; + this.content = content; + this.type = type; + } + public void setStartTime(LocalDateTime startTime) { this.startTime = startTime; } + + @Override + public String toString() { + return "Status{" + + "id=" + id + + ", deviceNo='" + deviceNo + '\'' + + ", deviceName='" + deviceName + '\'' + + ", deviceStatus=" + deviceStatus + + ", time=" + time + + ", startTime=" + startTime + + ", deviceIp='" + deviceIp + '\'' + + ", successRate='" + successRate + '\'' + + ", lostRate='" + lostRate + '\'' + + ", direction='" + direction + '\'' + + ", production='" + production + '\'' + + ", model='" + model + '\'' + + ", network='" + network + '\'' + + ", content='" + content + '\'' + + ", type='" + type + '\'' + + '}'; + } + public LocalDateTime getTime() { return time; } @@ -67,20 +107,6 @@ public class Status { private String deviceNo; - @Override - public String toString() { - return "Status{" + - "id=" + id + - ", deviceNo='" + deviceNo + '\'' + - ", deviceName='" + deviceName + '\'' + - ", deviceStatus=" + deviceStatus + - ", time=" + time + - ", startTime=" + startTime + - ", deviceIp=" + deviceIp + - '}'; - } - - private String deviceName; private int deviceStatus; @@ -157,8 +183,18 @@ public class Status { private String model; + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + private String network; private String content; + private String type; + } diff --git a/src/main/java/com/example/device/service/ExcelExportService.java b/src/main/java/com/example/device/service/ExcelExportService.java index 15bd045..7c21144 100644 --- a/src/main/java/com/example/device/service/ExcelExportService.java +++ b/src/main/java/com/example/device/service/ExcelExportService.java @@ -10,16 +10,16 @@ import java.io.IOException; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.time.temporal.ChronoUnit; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; +import java.util.*; +import java.util.stream.Collectors; @Service public class ExcelExportService { - public void exportDataToExcel(Map> ipMap, String filePath) { + public void exportDataToExcel(Map> ipMap,Map >categoryMap, String filePath) { Workbook workbook = new XSSFWorkbook(); - Sheet sheet = workbook.createSheet("设备故障率统计页"); + Map> map = new TreeMap<>(ipMap); + Sheet sheet = workbook.createSheet("设备故障率分时统计页"); int rowNum = 0; Row row0 = sheet.createRow(0); row0.createCell(0).setCellValue("设备名称"); @@ -39,7 +39,7 @@ public class ExcelExportService { int i=0; boolean flag = true; Row row1= sheet.createRow(1); - for (Map.Entry> entry : ipMap.entrySet()) { + for (Map.Entry> entry : map.entrySet()) { row0.createCell(2*i+7).setCellValue(entry.getKey()+"时"); sheet.addMergedRegion(new CellRangeAddress(0, 0, 2*i+7, 2*i+8)); row1.createCell(2*i+7).setCellValue("在线率"); @@ -67,12 +67,123 @@ public class ExcelExportService { i++; flag = false; } + Sheet sheets = workbook.createSheet("设备故障率汇总页"); + createSheet(sheets,categoryMap); + Sheet sheet1 = workbook.createSheet("设备故障率分时汇总页"); + createSheet1(sheet1,ipMap); try (FileOutputStream outputStream = new FileOutputStream(filePath)) { workbook.write(outputStream); } catch (IOException e) { e.printStackTrace(); } System.out.println("excel写入成功!!!!!!"); + + } + public void createSheet1(Sheet sheet,Map> ipMap){ + Map> map = new TreeMap<>(ipMap); + int rowNum = 0; + Row row0 = sheet.createRow(rowNum); + row0.createCell(0).setCellValue("设备分类"); + sheet.addMergedRegion(new CellRangeAddress(0, 1, 0, 0)); + int i=0; + boolean flag = true; + Row row1= sheet.createRow(1); + for (Map.Entry> entry : map.entrySet()) { + row0.createCell(3 * i + 1).setCellValue(entry.getKey() + "时"); + sheet.addMergedRegion(new CellRangeAddress(0, 0, 3 * i + 1, 3 * i + 3)); + row1.createCell(3 * i + 1).setCellValue("总统计数"); + row1.createCell(3 * i + 2).setCellValue("在线统计数"); + row1.createCell(3 * i + 3).setCellValue("在线率"); + rowNum = 2; + List groupItem = entry.getValue(); + Map> maps = groupItem.stream().filter(iteam -> iteam.getType() != null).collect(Collectors.groupingBy(Status::getType)); + for (Map.Entry> entrys : maps.entrySet()) { + List groupItems = entrys.getValue(); + long b = groupItems.stream().filter(iteam -> iteam.getDeviceStatus() == 1).count(); +// for (Status ignored : groupItems) { + int a = rowNum++; + Row row = sheet.getRow(a); // 获取指定索引的行 + if (row == null) { // 如果行不存在,则创建新行 + row = sheet.createRow(a); + } + HashMap itemTypeMap = new HashMap<>(); + itemTypeMap.put("1", "高清网络枪型固定摄像机"); + itemTypeMap.put("2", "高清网络球形摄像机"); + itemTypeMap.put("3", "桥下高清网络球形摄像机"); + itemTypeMap.put("4", "360°全景摄像机"); + itemTypeMap.put("5", "180°全景摄像机"); + itemTypeMap.put("6", "门架式可变信息标志"); + itemTypeMap.put("7", "雨棚可变信息标志"); + itemTypeMap.put("8", "站前悬臂式可变信息标志"); + itemTypeMap.put("9", "气象检测器"); + itemTypeMap.put("10", "路段语音广播系统"); + itemTypeMap.put("11", "护栏碰撞预警系统"); + itemTypeMap.put("12", "毫米波雷达"); + itemTypeMap.put("13", "合流区预警系统"); + itemTypeMap.put("14", "激光疲劳唤醒"); + itemTypeMap.put("15", "一类交通量调查站"); + itemTypeMap.put("16", "智能行车诱导系统"); + itemTypeMap.put("17", "智能设备箱"); + String type = groupItems.get(0).getType(); + String description = itemTypeMap.get(type); + if (flag) { + row.createCell(0).setCellValue(description); + } + row.createCell(3 * i + 1).setCellValue(groupItems.size()); + row.createCell(3 * i + 2).setCellValue(b); + row.createCell(3 * i + 3).setCellValue(String.format("%.2f%%", (double) b / groupItems.size() * 100)); + } + i++; + flag = false; + // } + } + + } + + public void createSheet(Sheet sheet,Map >categoryMap){ + int rowNum = 0; + Row row = sheet.createRow(rowNum); + row.createCell(0).setCellValue("设备分类"); + row.createCell(1).setCellValue("总统计数"); + row.createCell(2).setCellValue("在线统计数"); + row.createCell(3).setCellValue("在线率"); + rowNum = 1; + for (Map.Entry> entry : categoryMap.entrySet()) { + List groupItems = entry.getValue(); + long a=groupItems.stream().filter(iteam->iteam.getDeviceStatus()==1).count(); + row = sheet.createRow(rowNum++); + HashMap itemTypeMap = new HashMap<>(); + itemTypeMap.put("1", "高清网络枪型固定摄像机"); + itemTypeMap.put("2", "高清网络球形摄像机"); + itemTypeMap.put("3", "桥下高清网络球形摄像机"); + itemTypeMap.put("4", "360°全景摄像机"); + itemTypeMap.put("5", "180°全景摄像机"); + itemTypeMap.put("6", "门架式可变信息标志"); + itemTypeMap.put("7", "雨棚可变信息标志"); + itemTypeMap.put("8", "站前悬臂式可变信息标志"); + itemTypeMap.put("9", "气象检测器"); + itemTypeMap.put("10", "路段语音广播系统"); + itemTypeMap.put("11", "护栏碰撞预警系统"); + itemTypeMap.put("12", "毫米波雷达"); + itemTypeMap.put("13", "合流区预警系统"); + itemTypeMap.put("14", "激光疲劳唤醒"); + itemTypeMap.put("15", "一类交通量调查站"); + itemTypeMap.put("16", "智能行车诱导系统"); + itemTypeMap.put("17", "智能设备箱"); + String type = groupItems.get(0).getType(); + String description = itemTypeMap.get(type); + row.createCell(0).setCellValue(description); + row.createCell(1).setCellValue(groupItems.size()); + row.createCell(2).setCellValue(a); + row.createCell(3).setCellValue(String.format("%.2f%%", (double) a / groupItems.size() * 100)); + + } + } + + + + + } diff --git a/src/main/resources/mapping/StatusMapper.xml b/src/main/resources/mapping/StatusMapper.xml index 055cef6..08a93a2 100644 --- a/src/main/resources/mapping/StatusMapper.xml +++ b/src/main/resources/mapping/StatusMapper.xml @@ -16,6 +16,7 @@ + @@ -77,7 +78,7 @@