Browse Source

----优化代码

main
mengff 11 months ago
parent
commit
c8620a7415
  1. 8
      src/main/java/com/example/device/controller/DeviceStatus.java
  2. 10
      src/main/java/com/example/device/entity/Device.java
  3. 64
      src/main/java/com/example/device/entity/Status.java
  4. 123
      src/main/java/com/example/device/service/ExcelExportService.java
  5. 3
      src/main/resources/mapping/StatusMapper.xml

8
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() { public void calculateSuccessRate() {
LocalDateTime todayStart = LocalDateTime.now().truncatedTo(ChronoUnit.DAYS); LocalDateTime todayStart = LocalDateTime.now().truncatedTo(ChronoUnit.DAYS);
@ -182,10 +182,14 @@ public class DeviceStatus {
status.setStartTime(todayStart); status.setStartTime(todayStart);
status.setTime(currentTime); status.setTime(currentTime);
List<Status> listStatus = statusService.list(status); List<Status> listStatus = statusService.list(status);
//根据时间分组
Map<Integer, List<Status>> map = listStatus.stream() Map<Integer, List<Status>> map = listStatus.stream()
.collect(Collectors.groupingBy(Status -> Status.getTime().getHour())); .collect(Collectors.groupingBy(Status -> Status.getTime().getHour()));
//根据类型分组
Map<String, List<Status>> maps = listStatus.stream().filter(iteam->iteam.getType()!=null).collect(Collectors.groupingBy(Status::getType));
String filePath=CreateNamedExcel(); String filePath=CreateNamedExcel();
excelExportService.exportDataToExcel(map,filePath); excelExportService.exportDataToExcel(map,maps,filePath);
} }

10
src/main/java/com/example/device/entity/Device.java

@ -48,8 +48,18 @@ public class Device {
this.deviceIp = deviceIp; this.deviceIp = deviceIp;
} }
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
private String deviceNo; private String deviceNo;
private String deviceName; private String deviceName;
private String deviceIp; private String deviceIp;
private String type;
} }

64
src/main/java/com/example/device/entity/Status.java

@ -18,9 +18,49 @@ public class Status {
return startTime; 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) { public void setStartTime(LocalDateTime startTime) {
this.startTime = 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() { public LocalDateTime getTime() {
return time; return time;
} }
@ -67,20 +107,6 @@ public class Status {
private String deviceNo; 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 String deviceName;
private int deviceStatus; private int deviceStatus;
@ -157,8 +183,18 @@ public class Status {
private String model; private String model;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
private String network; private String network;
private String content; private String content;
private String type;
} }

123
src/main/java/com/example/device/service/ExcelExportService.java

@ -10,16 +10,16 @@ import java.io.IOException;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit; import java.time.temporal.ChronoUnit;
import java.util.ArrayList; import java.util.*;
import java.util.List; import java.util.stream.Collectors;
import java.util.Map;
@Service @Service
public class ExcelExportService { public class ExcelExportService {
public void exportDataToExcel(Map<Integer, List<Status>> ipMap, String filePath) { public void exportDataToExcel(Map<Integer, List<Status>> ipMap,Map<String, List<Status> >categoryMap, String filePath) {
Workbook workbook = new XSSFWorkbook(); Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("设备故障率统计页"); Map<Integer, List<Status>> map = new TreeMap<>(ipMap);
Sheet sheet = workbook.createSheet("设备故障率分时统计页");
int rowNum = 0; int rowNum = 0;
Row row0 = sheet.createRow(0); Row row0 = sheet.createRow(0);
row0.createCell(0).setCellValue("设备名称"); row0.createCell(0).setCellValue("设备名称");
@ -39,7 +39,7 @@ public class ExcelExportService {
int i=0; int i=0;
boolean flag = true; boolean flag = true;
Row row1= sheet.createRow(1); Row row1= sheet.createRow(1);
for (Map.Entry<Integer, List<Status>> entry : ipMap.entrySet()) { for (Map.Entry<Integer, List<Status>> entry : map.entrySet()) {
row0.createCell(2*i+7).setCellValue(entry.getKey()+"时"); row0.createCell(2*i+7).setCellValue(entry.getKey()+"时");
sheet.addMergedRegion(new CellRangeAddress(0, 0, 2*i+7, 2*i+8)); sheet.addMergedRegion(new CellRangeAddress(0, 0, 2*i+7, 2*i+8));
row1.createCell(2*i+7).setCellValue("在线率"); row1.createCell(2*i+7).setCellValue("在线率");
@ -67,12 +67,123 @@ public class ExcelExportService {
i++; i++;
flag = false; flag = false;
} }
Sheet sheets = workbook.createSheet("设备故障率汇总页");
createSheet(sheets,categoryMap);
Sheet sheet1 = workbook.createSheet("设备故障率分时汇总页");
createSheet1(sheet1,ipMap);
try (FileOutputStream outputStream = new FileOutputStream(filePath)) { try (FileOutputStream outputStream = new FileOutputStream(filePath)) {
workbook.write(outputStream); workbook.write(outputStream);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
System.out.println("excel写入成功!!!!!!"); System.out.println("excel写入成功!!!!!!");
}
public void createSheet1(Sheet sheet,Map<Integer, List<Status>> ipMap){
Map<Integer, List<Status>> 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<Integer, List<Status>> 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<Status> groupItem = entry.getValue();
Map<String, List<Status>> maps = groupItem.stream().filter(iteam -> iteam.getType() != null).collect(Collectors.groupingBy(Status::getType));
for (Map.Entry<String, List<Status>> entrys : maps.entrySet()) {
List<Status> 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<String, String> 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<String, List<Status> >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<String, List<Status>> entry : categoryMap.entrySet()) {
List<Status> groupItems = entry.getValue();
long a=groupItems.stream().filter(iteam->iteam.getDeviceStatus()==1).count();
row = sheet.createRow(rowNum++);
HashMap<String, String> 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));
}
} }
} }

3
src/main/resources/mapping/StatusMapper.xml

@ -16,6 +16,7 @@
<result column="model" jdbcType="VARCHAR" property="model"/> <result column="model" jdbcType="VARCHAR" property="model"/>
<result column="network" jdbcType="VARCHAR" property="network"/> <result column="network" jdbcType="VARCHAR" property="network"/>
<result column="content" jdbcType="VARCHAR" property="content"/> <result column="content" jdbcType="VARCHAR" property="content"/>
<result column="type" jdbcType="VARCHAR" property="type"/>
</resultMap> </resultMap>
@ -77,7 +78,7 @@
</sql> </sql>
<select id="listStatus" parameterType="com.example.device.entity.Status" resultMap="BaseResultMap"> <select id="listStatus" parameterType="com.example.device.entity.Status" resultMap="BaseResultMap">
select s.id, s.device_no, s.device_name, s.device_status, s.time, d.device_ip,s.success_rate,s.lost_rate,d.direction,d.production,d.model,d.network,d.content select s.id, s.device_no, s.device_name, s.device_status, s.time, d.device_ip,s.success_rate,s.lost_rate,d.direction,d.production,d.model,d.network,d.content,d.type
from status s from status s
LEFT JOIN device d on s.device_ip = d.device_ip LEFT JOIN device d on s.device_ip = d.device_ip
<where> <where>

Loading…
Cancel
Save