diff --git a/zc-business/src/main/java/com/zc/business/service/impl/DcSdhsEventServiceImpl.java b/zc-business/src/main/java/com/zc/business/service/impl/DcSdhsEventServiceImpl.java index b8569033..9b4fff59 100644 --- a/zc-business/src/main/java/com/zc/business/service/impl/DcSdhsEventServiceImpl.java +++ b/zc-business/src/main/java/com/zc/business/service/impl/DcSdhsEventServiceImpl.java @@ -737,6 +737,7 @@ public class DcSdhsEventServiceImpl implements IDcSdhsEventService { public void exportAccidentModelAnalysis(XWPFDocument doc, DcSdhsEventQuery dcSdhsEventQuery) { // 初始化一个空的结果映射 Map> result = accidentModelAnalysis(dcSdhsEventQuery); + if (result != null && result.size() != UniversalEnum.ZERO.getNumber()) { // 计算最大车辆类型数量 int maxVehicleTypes = 5; @@ -839,7 +840,7 @@ public class DcSdhsEventServiceImpl implements IDcSdhsEventService { // 换行 createLineBreak(doc); - +} } /** @@ -944,7 +945,9 @@ public class DcSdhsEventServiceImpl implements IDcSdhsEventService { String hourString = String.format("%02d", hour); hourlyCounts.merge(hourString, 0L, Long::sum); } - return hourlyCounts; + Map hourlyCountsTreeMap = new TreeMap<>(hourlyCounts); + + return hourlyCountsTreeMap; } else if (dcSdhsEventQuery.getType().equals("1")) { // 将查询开始时间和结束时间转换为 LocalDateTime LocalDateTime queryStart = dcSdhsEventQuery.getStartTime().toInstant().atZone(zoneId).toLocalDateTime(); @@ -972,7 +975,9 @@ public class DcSdhsEventServiceImpl implements IDcSdhsEventService { String dateString = date.format(formatter); dailyCounts.merge(dateString, 0L, Long::sum); } - return dailyCounts; + // 将键值对放入TreeMap中,TreeMap会自动按键排序 + Map dailyCountsTreeMap = new TreeMap<>(dailyCounts); + return dailyCountsTreeMap; } else { // 将查询开始时间和结束时间转换为 LocalDateTime LocalDateTime queryStart = dcSdhsEventQuery.getStartTime().toInstant().atZone(zoneId).toLocalDateTime(); @@ -1000,7 +1005,9 @@ public class DcSdhsEventServiceImpl implements IDcSdhsEventService { monthlyCounts.merge(monthString, 0L, Long::sum); currentMonth = currentMonth.plusMonths(1); } - return monthlyCounts; + // 将键值对放入TreeMap中,TreeMap会自动按键排序 + Map monthlyCountsTreeMap = new TreeMap<>(monthlyCounts); + return monthlyCountsTreeMap; } } @@ -1074,118 +1081,119 @@ public class DcSdhsEventServiceImpl implements IDcSdhsEventService { name = "wordTemplate/accidentTimeMonthAnalysis.docx"; } + if (currentYearData != null && currentYearData.size() != UniversalEnum.ZERO.getNumber()) { // 创建表格 - XWPFTable table = doc.createTable(rows, 3); - CTTblWidth infoTableWidth = table.getCTTbl().addNewTblPr().addNewTblW(); - infoTableWidth.setType(STTblWidth.DXA); - infoTableWidth.setW(BigInteger.valueOf(UniversalEnum.NINE_THOUSAND_AND_SEVENTY_TWO.getNumber())); + XWPFTable table = doc.createTable(rows, 3); + CTTblWidth infoTableWidth = table.getCTTbl().addNewTblPr().addNewTblW(); + infoTableWidth.setType(STTblWidth.DXA); + infoTableWidth.setW(BigInteger.valueOf(UniversalEnum.NINE_THOUSAND_AND_SEVENTY_TWO.getNumber())); // 表头 - setTableFonts(table.getRow(0).getCell(0), "时段"); - setTableFonts(table.getRow(0).getCell(1), "今年同期"); - setTableFonts(table.getRow(0).getCell(2), "去年同期"); + setTableFonts(table.getRow(0).getCell(0), "时段"); + setTableFonts(table.getRow(0).getCell(1), "今年同期"); + setTableFonts(table.getRow(0).getCell(2), "去年同期"); // 定义一个从数字到日期字符串的映射 - Map indexToDateMap = new HashMap<>(); - for (int i = 0; i <= 32; i++) { - String dateString = String.format("%02d", i); - indexToDateMap.put(i, dateString); - } - String dayString = ""; + Map indexToDateMap = new HashMap<>(); + for (int i = 0; i <= 32; i++) { + String dateString = String.format("%02d", i); + indexToDateMap.put(i, dateString); + } + String dayString = ""; // 填充表格数据 - for (int i = 0; i < rows - 1; i++) { + for (int i = 0; i < rows - 1; i++) { - if (type.equals("2")) { - dayString = indexToDateMap.get(i); // 获取 i 对应的日期字符串 - } else { - dayString = indexToDateMap.get(i + 1); // 获取 i 对应的日期字符串 + if (type.equals("2")) { + dayString = indexToDateMap.get(i); // 获取 i 对应的日期字符串 + } else { + dayString = indexToDateMap.get(i + 1); // 获取 i 对应的日期字符串 - } + } - setTableFonts(table.getRow(i + 1).getCell(0), (dayString) + stingType); - Long currentYearValue = currentYearData.getOrDefault(dayString, 0L); - setTableFonts(table.getRow(i + 1).getCell(1), String.valueOf(currentYearValue)); - Long lastYearValue = lastYearData.getOrDefault(dayString, 0L); - setTableFonts(table.getRow(i + 1).getCell(2), String.valueOf(lastYearValue)); - } + setTableFonts(table.getRow(i + 1).getCell(0), (dayString) + stingType); + Long currentYearValue = currentYearData.getOrDefault(dayString, 0L); + setTableFonts(table.getRow(i + 1).getCell(1), String.valueOf(currentYearValue)); + Long lastYearValue = lastYearData.getOrDefault(dayString, 0L); + setTableFonts(table.getRow(i + 1).getCell(2), String.valueOf(lastYearValue)); + } // 图表部分 - try { + try { - InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(name); + InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(name); - XWPFDocument copiedTemplate = new XWPFDocument(inputStream); + XWPFDocument copiedTemplate = new XWPFDocument(inputStream); - List charts = copiedTemplate.getCharts(); - XWPFChart chart = charts.get(0); - XSSFWorkbook workbook = chart.getWorkbook(); - XSSFSheet sheet = workbook.getSheetAt(UniversalEnum.ZERO.getNumber()); - // 更新图表数据 - for (int i = UniversalEnum.ZERO.getNumber(); i < rows - 1; i++) { - if (type.equals("2")) { - dayString = indexToDateMap.get(i); // 获取 i 对应的日期字符串 - } else { - dayString = indexToDateMap.get(i + 1); // 获取 i 对应的日期字符串 + List charts = copiedTemplate.getCharts(); + XWPFChart chart = charts.get(0); + XSSFWorkbook workbook = chart.getWorkbook(); + XSSFSheet sheet = workbook.getSheetAt(UniversalEnum.ZERO.getNumber()); + // 更新图表数据 + for (int i = UniversalEnum.ZERO.getNumber(); i < rows - 1; i++) { + if (type.equals("2")) { + dayString = indexToDateMap.get(i); // 获取 i 对应的日期字符串 + } else { + dayString = indexToDateMap.get(i + 1); // 获取 i 对应的日期字符串 - } - sheet.getRow(i + 1).getCell(0).setCellValue((dayString) + stingType); - int rowIndex = i + UniversalEnum.ONE.getNumber(); // 计算行索引 - Row row = sheet.getRow(rowIndex); - if (row == null) { - // 如果行不存在,则创建新行 - row = sheet.createRow(rowIndex); - } + } + sheet.getRow(i + 1).getCell(0).setCellValue((dayString) + stingType); + int rowIndex = i + UniversalEnum.ONE.getNumber(); // 计算行索引 + Row row = sheet.getRow(rowIndex); + if (row == null) { + // 如果行不存在,则创建新行 + row = sheet.createRow(rowIndex); + } - // 现在可以安全地访问或创建单元格了 - Cell cell = row.getCell(UniversalEnum.ONE.getNumber()); - if (cell == null) { - // 通常情况下,getCell 会创建新的单元格,但这里我们显式地检查以防万一 - cell = row.createCell(UniversalEnum.ONE.getNumber(), CellType.NUMERIC); - } - // 设置单元格的值 - sheet.getRow(i + 1).getCell(1).setCellValue(currentYearData.get(dayString)); - int rowIndexTwo = i + 1 + UniversalEnum.ONE.getNumber(); // 计算行索引 - Row rowTwo = sheet.getRow(rowIndexTwo); - if (rowTwo == null) { - // 如果行不存在,则创建新行 - rowTwo = sheet.createRow(rowIndexTwo); - } + // 现在可以安全地访问或创建单元格了 + Cell cell = row.getCell(UniversalEnum.ONE.getNumber()); + if (cell == null) { + // 通常情况下,getCell 会创建新的单元格,但这里我们显式地检查以防万一 + cell = row.createCell(UniversalEnum.ONE.getNumber(), CellType.NUMERIC); + } + // 设置单元格的值 + sheet.getRow(i + 1).getCell(1).setCellValue(currentYearData.get(dayString)); + int rowIndexTwo = i + 1 + UniversalEnum.ONE.getNumber(); // 计算行索引 + Row rowTwo = sheet.getRow(rowIndexTwo); + if (rowTwo == null) { + // 如果行不存在,则创建新行 + rowTwo = sheet.createRow(rowIndexTwo); + } - // 现在可以安全地访问或创建单元格了 - Cell cellTwo = row.getCell(UniversalEnum.TWO.getNumber()); - if (cellTwo == null) { - // 通常情况下,getCell 会创建新的单元格,但这里我们显式地检查以防万一 - cell = row.createCell(UniversalEnum.TWO.getNumber(), CellType.NUMERIC); + // 现在可以安全地访问或创建单元格了 + Cell cellTwo = row.getCell(UniversalEnum.TWO.getNumber()); + if (cellTwo == null) { + // 通常情况下,getCell 会创建新的单元格,但这里我们显式地检查以防万一 + cell = row.createCell(UniversalEnum.TWO.getNumber(), CellType.NUMERIC); + } + sheet.getRow(i + 1).getCell(2).setCellValue(lastYearData.get(dayString)); + //sheet.getRow(i+UniversalEnum.ONE.getNumber()).getCell(UniversalEnum.ONE.getNumber()).setCellValue(Long.parseLong(entries.get(i).getValue().toString())); } - sheet.getRow(i + 1).getCell(2).setCellValue(lastYearData.get(dayString)); - //sheet.getRow(i+UniversalEnum.ONE.getNumber()).getCell(UniversalEnum.ONE.getNumber()).setCellValue(Long.parseLong(entries.get(i).getValue().toString())); - } /* for (int i = 0; i < 24; i++) { sheet.getRow(i + 1).getCell(0).setCellValue((i) + "点"); sheet.getRow(i + 1).getCell(1).setCellValue(currentYearData.get(i)); sheet.getRow(i + 1).getCell(2).setCellValue(lastYearData.get(i)); }*/ - for (int i = sheet.getLastRowNum(); i > currentYearData.size(); i--) { - sheet.removeRow(sheet.getRow(i)); - } - // 更新图表 - PoiUtil.wordExportChar(chart, "事故时间分析", new String[]{"今年同期", "去年同期"}, sheet); - - // 合并图表到Word文档 - mergeChart(chart, doc); + for (int i = sheet.getLastRowNum(); i > currentYearData.size(); i--) { + sheet.removeRow(sheet.getRow(i)); + } + // 更新图表 + PoiUtil.wordExportChar(chart, "事故时间分析", new String[]{"今年同期", "去年同期"}, sheet); - // 关闭模板文档 - copiedTemplate.close(); - } catch (Exception e) { - e.printStackTrace(); - } - //换行 - createLineBreak(doc); + // 合并图表到Word文档 + mergeChart(chart, doc); + // 关闭模板文档 + copiedTemplate.close(); + } catch (Exception e) { + e.printStackTrace(); + } + //换行 + createLineBreak(doc); + } } @@ -1871,97 +1879,100 @@ public class DcSdhsEventServiceImpl implements IDcSdhsEventService { List exit = map.get("exit"); int rows = entrance.size(); String name = "wordTemplate/tollGateEntrance.docx"; + if (map != null && map.size() != UniversalEnum.ZERO.getNumber()) { + + PoiUtil.createHeading2(doc, "收费站出入口车流量"); // 创建表格 - XWPFTable table = doc.createTable(rows+1, 3); - CTTblWidth infoTableWidth = table.getCTTbl().addNewTblPr().addNewTblW(); - infoTableWidth.setType(STTblWidth.DXA); - infoTableWidth.setW(BigInteger.valueOf(UniversalEnum.NINE_THOUSAND_AND_SEVENTY_TWO.getNumber())); + XWPFTable table = doc.createTable(rows + 1, 3); + CTTblWidth infoTableWidth = table.getCTTbl().addNewTblPr().addNewTblW(); + infoTableWidth.setType(STTblWidth.DXA); + infoTableWidth.setW(BigInteger.valueOf(UniversalEnum.NINE_THOUSAND_AND_SEVENTY_TWO.getNumber())); // 表头 - setTableFonts(table.getRow(0).getCell(0), "收费站"); - setTableFonts(table.getRow(0).getCell(1), "入口"); - setTableFonts(table.getRow(0).getCell(2), "出口"); + setTableFonts(table.getRow(0).getCell(0), "收费站"); + setTableFonts(table.getRow(0).getCell(1), "入口"); + setTableFonts(table.getRow(0).getCell(2), "出口"); // 填充表格数据 - for (int i = 0; i < rows ; i++) { + for (int i = 0; i < rows; i++) { - setTableFonts(table.getRow(i + 1).getCell(0), (entrance.get(i).getFacilityName())); - Integer entranceValue = entrance.get(i).getTrafficVolume(); - setTableFonts(table.getRow(i + 1).getCell(1), String.valueOf(entranceValue)); - Integer exitValue = exit.get(i).getTrafficVolume(); - setTableFonts(table.getRow(i + 1).getCell(2), String.valueOf(exitValue)); - } + setTableFonts(table.getRow(i + 1).getCell(0), (entrance.get(i).getFacilityName())); + Integer entranceValue = entrance.get(i).getTrafficVolume(); + setTableFonts(table.getRow(i + 1).getCell(1), String.valueOf(entranceValue)); + Integer exitValue = exit.get(i).getTrafficVolume(); + setTableFonts(table.getRow(i + 1).getCell(2), String.valueOf(exitValue)); + } // 图表部分 - try { - - InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(name); + try { + InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(name); - XWPFDocument copiedTemplate = new XWPFDocument(inputStream); - List charts = copiedTemplate.getCharts(); - XWPFChart chart = charts.get(0); - XSSFWorkbook workbook = chart.getWorkbook(); - XSSFSheet sheet = workbook.getSheetAt(UniversalEnum.ZERO.getNumber()); - // 更新图表数据 - for (int i = UniversalEnum.ZERO.getNumber(); i < rows; i++) { + XWPFDocument copiedTemplate = new XWPFDocument(inputStream); - sheet.getRow(i + 1).getCell(0).setCellValue(entrance.get(i).getFacilityName()); - int rowIndex = i + UniversalEnum.ONE.getNumber(); // 计算行索引 - Row row = sheet.getRow(rowIndex); - if (row == null) { - // 如果行不存在,则创建新行 - row = sheet.createRow(rowIndex); - } + List charts = copiedTemplate.getCharts(); + XWPFChart chart = charts.get(0); + XSSFWorkbook workbook = chart.getWorkbook(); + XSSFSheet sheet = workbook.getSheetAt(UniversalEnum.ZERO.getNumber()); + // 更新图表数据 + for (int i = UniversalEnum.ZERO.getNumber(); i < rows; i++) { + + sheet.getRow(i + 1).getCell(0).setCellValue(entrance.get(i).getFacilityName()); + int rowIndex = i + UniversalEnum.ONE.getNumber(); // 计算行索引 + Row row = sheet.getRow(rowIndex); + if (row == null) { + // 如果行不存在,则创建新行 + row = sheet.createRow(rowIndex); + } - // 现在可以安全地访问或创建单元格了 - Cell cell = row.getCell(UniversalEnum.ONE.getNumber()); - if (cell == null) { - // 通常情况下,getCell 会创建新的单元格,但这里我们显式地检查以防万一 - cell = row.createCell(UniversalEnum.ONE.getNumber(), CellType.NUMERIC); - } - // 设置单元格的值 - sheet.getRow(i + 1).getCell(1).setCellValue( entrance.get(i).getTrafficVolume()); - int rowIndexTwo = i + 1 + UniversalEnum.ONE.getNumber(); // 计算行索引 - Row rowTwo = sheet.getRow(rowIndexTwo); - if (rowTwo == null) { - // 如果行不存在,则创建新行 - rowTwo = sheet.createRow(rowIndexTwo); - } + // 现在可以安全地访问或创建单元格了 + Cell cell = row.getCell(UniversalEnum.ONE.getNumber()); + if (cell == null) { + // 通常情况下,getCell 会创建新的单元格,但这里我们显式地检查以防万一 + cell = row.createCell(UniversalEnum.ONE.getNumber(), CellType.NUMERIC); + } + // 设置单元格的值 + sheet.getRow(i + 1).getCell(1).setCellValue(entrance.get(i).getTrafficVolume()); + int rowIndexTwo = i + 1 + UniversalEnum.ONE.getNumber(); // 计算行索引 + Row rowTwo = sheet.getRow(rowIndexTwo); + if (rowTwo == null) { + // 如果行不存在,则创建新行 + rowTwo = sheet.createRow(rowIndexTwo); + } - // 现在可以安全地访问或创建单元格了 - Cell cellTwo = row.getCell(UniversalEnum.TWO.getNumber()); - if (cellTwo == null) { - // 通常情况下,getCell 会创建新的单元格,但这里我们显式地检查以防万一 - cell = row.createCell(UniversalEnum.TWO.getNumber(), CellType.NUMERIC); + // 现在可以安全地访问或创建单元格了 + Cell cellTwo = row.getCell(UniversalEnum.TWO.getNumber()); + if (cellTwo == null) { + // 通常情况下,getCell 会创建新的单元格,但这里我们显式地检查以防万一 + cell = row.createCell(UniversalEnum.TWO.getNumber(), CellType.NUMERIC); + } + sheet.getRow(i + 1).getCell(2).setCellValue(exit.get(i).getTrafficVolume()); + //sheet.getRow(i+UniversalEnum.ONE.getNumber()).getCell(UniversalEnum.ONE.getNumber()).setCellValue(Long.parseLong(entries.get(i).getValue().toString())); } - sheet.getRow(i + 1).getCell(2).setCellValue(exit.get(i).getTrafficVolume()); - //sheet.getRow(i+UniversalEnum.ONE.getNumber()).getCell(UniversalEnum.ONE.getNumber()).setCellValue(Long.parseLong(entries.get(i).getValue().toString())); - } /* for (int i = 0; i < 24; i++) { sheet.getRow(i + 1).getCell(0).setCellValue((i) + "点"); sheet.getRow(i + 1).getCell(1).setCellValue(currentYearData.get(i)); sheet.getRow(i + 1).getCell(2).setCellValue(lastYearData.get(i)); }*/ - for (int i = sheet.getLastRowNum(); i > entrance.size(); i--) { - sheet.removeRow(sheet.getRow(i)); - } - // 更新图表 - PoiUtil.wordExportChar(chart, "收费站出入口车流量", new String[]{"入口", "出口"}, sheet); + for (int i = sheet.getLastRowNum(); i > entrance.size(); i--) { + sheet.removeRow(sheet.getRow(i)); + } + // 更新图表 + PoiUtil.wordExportChar(chart, "收费站出入口车流量", new String[]{"入口", "出口"}, sheet); - // 合并图表到Word文档 - mergeChart(chart, doc); + // 合并图表到Word文档 + mergeChart(chart, doc); - // 关闭模板文档 - copiedTemplate.close(); - } catch (Exception e) { - e.printStackTrace(); + // 关闭模板文档 + copiedTemplate.close(); + } catch (Exception e) { + e.printStackTrace(); + } + //换行 + createLineBreak(doc); } - //换行 - createLineBreak(doc); - } /** @@ -1977,6 +1988,7 @@ public class DcSdhsEventServiceImpl implements IDcSdhsEventService { List exit = map.get("jinanDirection"); int rows = entrance.size(); String name = "wordTemplate/frameDataAnalysis.docx"; + if (map != null && map.size() != UniversalEnum.ZERO.getNumber()) { // 创建表格 XWPFTable table = doc.createTable(rows+1, 3); @@ -2068,7 +2080,7 @@ public class DcSdhsEventServiceImpl implements IDcSdhsEventService { } //换行 createLineBreak(doc); - +} }