diff --git a/zc-business/pom.xml b/zc-business/pom.xml index c0017e0b..6b870a73 100644 --- a/zc-business/pom.xml +++ b/zc-business/pom.xml @@ -47,6 +47,31 @@ com.ruoyi ruoyi-system + + + org.apache.poi + poi-ooxml + 4.1.2 + + + org.apache.poi + poi-ooxml-schemas + 4.1.2 + + + org.apache.poi + poi + 4.1.2 + + + + + com.deepoove + poi-tl + 1.9.1 + + com.zc 1.0.0 diff --git a/zc-business/src/main/java/com/zc/business/controller/WordController.java b/zc-business/src/main/java/com/zc/business/controller/WordController.java new file mode 100644 index 00000000..b2879914 --- /dev/null +++ b/zc-business/src/main/java/com/zc/business/controller/WordController.java @@ -0,0 +1,1050 @@ +package com.zc.business.controller; + +import com.ruoyi.common.config.RuoYiConfig; +import com.ruoyi.common.core.domain.AjaxResult; +import com.zc.business.mapper.DcTrafficIncidentsMapper; +import com.zc.business.utils.PoiUtil; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import org.apache.poi.xssf.usermodel.XSSFSheet; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; +import org.apache.poi.xwpf.usermodel.*; +import org.openxmlformats.schemas.wordprocessingml.x2006.main.*; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import javax.servlet.http.HttpServletResponse; +import java.io.InputStream; +import java.math.BigInteger; +import java.text.SimpleDateFormat; +import java.time.OffsetDateTime; +import java.time.format.DateTimeFormatter; +import java.util.*; + +import static com.zc.business.utils.PoiUtil.*; + +/** + * @Description 通行情况快报 + * + * @author liuwenge + * @date 2024/3/26 11:13 + */ +@Api(tags = "通行情况快报") +@RestController +@RequestMapping("/business/word") +public class WordController { + + @Autowired + private WeatherForecastController weatherForecastController; + @Autowired + private DcTrafficIncidentsMapper dcTrafficIncidentsMapper; + + + @PostMapping("/trafficSituationReport") + @ApiOperation("导出通行情况快报") + public void trafficSituationReport(HttpServletResponse response){ + try { + SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); + String date = df.format(new Date()); + String outputPath = RuoYiConfig.getDownloadPath() +"/济菏高速公路通行情况快报" + date + ".docx"; + // 读取Word模板 + InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("wordTemplate/wordTemplate.docx"); + XWPFDocument newDoc = new XWPFDocument(inputStream); + + // 创建并插入带超链接的目录 + insertTOC(newDoc); + + + //换页 + XWPFParagraph breakPara = newDoc.createParagraph(); + breakPara.setAlignment(ParagraphAlignment.CENTER); + CTP ctP = breakPara.getCTP(); + CTR ctr = ctP.addNewR(); + CTBr ctBr = ctr.addNewBr(); + ctBr.setType(STBrType.PAGE); + + + + PoiUtil.createHeading1(newDoc,"总体情况"); + /** 天气情况统计 */ + weather(newDoc); + /** 交通管制情况 */ + trafficControl(newDoc); + /** 交通事故 */ + trafficAccident(newDoc); + /** 出入口车流量 */ + trafficFlow(newDoc); + + + + PoiUtil.createHeading1(newDoc,"收费站运行情况"); + /** 封闭原因统计 */ + trafficControlReasons(newDoc); + /** 封闭数量统计 */ + closedQuantity(newDoc); + PoiUtil.createHeading1(newDoc,"交通事故情况"); + /** 交通事故统计 */ + accidentType(newDoc); + + + PoiUtil.createHeading1(newDoc,"拥堵情况"); + /** 拥堵地点 */ + congestionLocation(newDoc); + /** 拥堵时长 */ + congestionDuration(newDoc); + /** 拥堵长度 */ + congestionMileage(newDoc); + + + + // 保存新生成的Word文件 +// FileOutputStream out = new FileOutputStream(outputPath); +// out.close(); + + // 不保存直接返回文件流 + newDoc.write(response.getOutputStream()); + + + // 关闭文档 + newDoc.close(); + System.out.println("生成通行情况快报成功!"); + } catch (Exception e) { + e.printStackTrace(); + } + } + + /** + * @Description 天气情况统计 + * + * @author liuwenge + * @date 2024/3/26 15:39 + * @param + * @return void + */ + + public void weather(XWPFDocument doc) { + + PoiUtil.createHeading2(doc,"天气情况统计"); + + XWPFTable table = doc.createTable(9, 25); + //列宽自动分割 + CTTblWidth infoTableWidth = table.getCTTbl().addNewTblPr().addNewTblW(); + infoTableWidth.setType(STTblWidth.DXA); + infoTableWidth.setW(BigInteger.valueOf(9072)); + + AjaxResult ajaxResult = weatherForecastController.hourlyWeather(); + if (ajaxResult.get("code").equals(200)) { + Map>> data = (Map>>) ajaxResult.get("data"); + data.keySet().forEach(key ->{ + if ("hourlyWeather1".equals(key)){ + setTableFonts(table.getRow(1).getCell(0), "长清区"); + List> weatherList = data.get(key); + for (int i = 0; i < weatherList.size(); i++) { + OffsetDateTime offsetDateTime = OffsetDateTime.parse(weatherList.get(i).get("fxTime").toString()); + setTableFonts(table.getRow(0).getCell(i+1),offsetDateTime.format(DateTimeFormatter.ofPattern("dd日HH时"))); + setTableFonts(table.getRow(1).getCell(i+1), weatherList.get(i).get("text").toString()); + } + } else if ("hourlyWeather2".equals(key)){ + setTableFonts(table.getRow(2).getCell(0), "平阴县"); + List> weatherList = data.get(key); + for (int i = 0; i < weatherList.size(); i++) { + setTableFonts(table.getRow(2).getCell(i+1), weatherList.get(i).get("text").toString()); + } + } else if ("hourlyWeather3".equals(key)){ + setTableFonts(table.getRow(3).getCell(0), "东平县"); + List> weatherList = data.get(key); + for (int i = 0; i < weatherList.size(); i++) { + setTableFonts(table.getRow(3).getCell(i+1), weatherList.get(i).get("text").toString()); + } + } else if ("hourlyWeather4".equals(key)){ + setTableFonts(table.getRow(4).getCell(0), "汶上县"); + List> weatherList = data.get(key); + for (int i = 0; i < weatherList.size(); i++) { + setTableFonts(table.getRow(4).getCell(i+1), weatherList.get(i).get("text").toString()); + } + } else if ("hourlyWeather5".equals(key)){ + setTableFonts(table.getRow(5).getCell(0), "梁山县"); + List> weatherList = data.get(key); + for (int i = 0; i < weatherList.size(); i++) { + setTableFonts(table.getRow(5).getCell(i+1), weatherList.get(i).get("text").toString()); + } + } else if ("hourlyWeather6".equals(key)){ + setTableFonts(table.getRow(6).getCell(0), "嘉祥县"); + List> weatherList = data.get(key); + for (int i = 0; i < weatherList.size(); i++) { + setTableFonts(table.getRow(6).getCell(i+1), weatherList.get(i).get("text").toString()); + } + } else if ("hourlyWeather7".equals(key)){ + setTableFonts(table.getRow(7).getCell(0), "巨野县"); + List> weatherList = data.get(key); + for (int i = 0; i < weatherList.size(); i++) { + setTableFonts(table.getRow(7).getCell(i+1), weatherList.get(i).get("text").toString()); + } + } else if ("hourlyWeather8".equals(key)){ + setTableFonts(table.getRow(8).getCell(0), "郓城县"); + List> weatherList = data.get(key); + for (int i = 0; i < weatherList.size(); i++) { + setTableFonts(table.getRow(8).getCell(i+1), weatherList.get(i).get("text").toString()); + } + } + }); + } + + //换行 + createLineBreak(doc); + + + } + + + /** + * @Description 交通管制情况 + * + * @author liuwenge + * @date 2024/3/26 17:36 + * @param doc + * @return void + */ + public void trafficControl(XWPFDocument doc) { + PoiUtil.createHeading2(doc, "交通管制情况"); + + + SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + + + Calendar calendar = Calendar.getInstance(); + //将天至1 + calendar.set(Calendar.DAY_OF_MONTH, 1); + //将小时至0 + calendar.set(Calendar.HOUR_OF_DAY, 0); + //将分钟至0 + calendar.set(Calendar.MINUTE, 0); + //将秒至0 + calendar.set(Calendar.SECOND,0); + //将毫秒至0 + calendar.set(Calendar.MILLISECOND, 0); + //获得当前月第一天 + Date date = calendar.getTime(); + String startDate = dateFormat.format(date); + String endDate = dateFormat.format(new Date()); + + + List> tollStationAnalysisList = dcTrafficIncidentsMapper.selectTollStationAnalysisByFacility(null,startDate,endDate); + if (tollStationAnalysisList != null && tollStationAnalysisList.size() > 0) { + + XWPFTable table = doc.createTable(11, 4); + //列宽自动分割 + CTTblWidth infoTableWidth = table.getCTTbl().addNewTblPr().addNewTblW(); + infoTableWidth.setType(STTblWidth.DXA); + infoTableWidth.setW(BigInteger.valueOf(9072)); + + setTableFonts(table.getRow(0).getCell(1), "封闭"); + setTableFonts(table.getRow(0).getCell(2), "限行"); + setTableFonts(table.getRow(0).getCell(3), "间隔放行"); + + //excel + for (int i = 0; i < tollStationAnalysisList.size(); i++){ + setTableFonts(table.getRow(i+1).getCell(0), tollStationAnalysisList.get(i).get("facilityName").toString()); + setTableFonts(table.getRow(i+1).getCell(1), tollStationAnalysisList.get(i).get("facilityClose").toString()); + setTableFonts(table.getRow(i+1).getCell(2), tollStationAnalysisList.get(i).get("facilityRestriction").toString()); + setTableFonts(table.getRow(i+1).getCell(3), tollStationAnalysisList.get(i).get("facilityInterval").toString()); + } + + try { + + + // 复制Word模板 + InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("wordTemplate/chartTemplate.docx"); + + XWPFDocument copiedTemplate = new XWPFDocument(inputStream); + + //获取word中所有图表对象 + List charts = copiedTemplate.getCharts(); + XWPFChart chart = charts.get(0); + XSSFWorkbook workbook = chart.getWorkbook(); + XSSFSheet sheet = workbook.getSheetAt(0); + + //系列信息 + String[] singleBarSeriesNames = {"封闭","限行","间隔放行"}; + //分类信息 + for (int i = 0; i < tollStationAnalysisList.size(); i++){ + sheet.getRow(i+1).getCell(0).setCellValue(tollStationAnalysisList.get(i).get("facilityName").toString()); + sheet.getRow(i+1).getCell(1).setCellValue(Long.parseLong(tollStationAnalysisList.get(i).get("facilityClose").toString())); + sheet.getRow(i+1).getCell(2).setCellValue(Long.parseLong(tollStationAnalysisList.get(i).get("facilityRestriction").toString())); + sheet.getRow(i+1).getCell(3).setCellValue(Long.parseLong(tollStationAnalysisList.get(i).get("facilityInterval").toString())); + + } + PoiUtil.wordExportChar(chart, "交通管制情况", singleBarSeriesNames,sheet ); + + + // 追加到同一个Word文档中 + mergeChart(chart,doc); +// PoiUtil.mergeDoc(copiedTemplate, doc); + + + // 关闭复制的模板文档 + copiedTemplate.close(); + + } catch (Exception e){ + e.printStackTrace(); + } + + } + + //换行 + createLineBreak(doc); + + + List> trafficControlAnalysisList = dcTrafficIncidentsMapper.selectTrafficControlAnalysis(startDate,endDate); + if (trafficControlAnalysisList != null && trafficControlAnalysisList.size() > 0) { + + XWPFTable table = doc.createTable(7, 4); + //列宽自动分割 + CTTblWidth infoTableWidth = table.getCTTbl().addNewTblPr().addNewTblW(); + infoTableWidth.setType(STTblWidth.DXA); + infoTableWidth.setW(BigInteger.valueOf(9072)); + + setTableFonts(table.getRow(0).getCell(1), "封闭"); + setTableFonts(table.getRow(0).getCell(2), "限行"); + setTableFonts(table.getRow(0).getCell(3), "间隔放行"); + + //excel + for (int i = 0; i < trafficControlAnalysisList.size(); i++){ + setTableFonts(table.getRow(i+1).getCell(0), trafficControlAnalysisList.get(i).get("controlCauseName").toString()); + setTableFonts(table.getRow(i+1).getCell(1), trafficControlAnalysisList.get(i).get("facilityClose").toString()); + setTableFonts(table.getRow(i+1).getCell(2), trafficControlAnalysisList.get(i).get("facilityRestriction").toString()); + setTableFonts(table.getRow(i+1).getCell(3), trafficControlAnalysisList.get(i).get("facilityInterval").toString()); + } + + try { + + + // 复制Word模板 + InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("wordTemplate/chartTemplate.docx"); + XWPFDocument copiedTemplate = new XWPFDocument(inputStream); + + //获取word中所有图表对象 + List charts = copiedTemplate.getCharts(); + XWPFChart chart = charts.get(0); + XSSFWorkbook workbook = chart.getWorkbook(); + XSSFSheet sheet = workbook.getSheetAt(0); + //系列信息 + String[] singleBarSeriesNames = {"封闭","限行","间隔放行"}; + //分类信息 + for (int i = 0; i < trafficControlAnalysisList.size(); i++){ + sheet.getRow(i+1).getCell(0).setCellValue(trafficControlAnalysisList.get(i).get("controlCauseName").toString()); + sheet.getRow(i+1).getCell(1).setCellValue(Long.parseLong(trafficControlAnalysisList.get(i).get("facilityClose").toString())); + sheet.getRow(i+1).getCell(2).setCellValue(Long.parseLong(trafficControlAnalysisList.get(i).get("facilityRestriction").toString())); + sheet.getRow(i+1).getCell(3).setCellValue(Long.parseLong(trafficControlAnalysisList.get(i).get("facilityInterval").toString())); + } + for (int i = sheet.getLastRowNum(); i > trafficControlAnalysisList.size();i--){ + sheet.removeRow(sheet.getRow(i)); + } + + PoiUtil.wordExportChar(chart, "交通管制情况", singleBarSeriesNames,sheet ); + + // 追加到同一个Word文档中 + mergeChart(chart,doc); +// PoiUtil.mergeDoc(copiedTemplate, doc); + + + // 关闭复制的模板文档 + copiedTemplate.close(); + + } catch (Exception e){ + e.printStackTrace(); + } + + } + + //换行 + createLineBreak(doc); + + } + + + /** + * @Description 交通事故 + * + * @author liuwenge + * @date 2024/3/27 20:06 + * @param doc + * @return void + */ + public void trafficAccident(XWPFDocument doc) { + PoiUtil.createHeading2(doc, "交通事故"); + + + List> trafficAccidentList = dcTrafficIncidentsMapper.selectTrafficAccidentAnalysis(); + if (trafficAccidentList != null && trafficAccidentList.size() > 0) { + + XWPFTable table = doc.createTable(2, 4); + //列宽自动分割 + CTTblWidth infoTableWidth = table.getCTTbl().addNewTblPr().addNewTblW(); + infoTableWidth.setType(STTblWidth.DXA); + infoTableWidth.setW(BigInteger.valueOf(9072)); + + + //表格插入总数 + setTableFonts(table.getRow(0).getCell(0), "事故数量"); + Long allNum = 0L; + for (Map map : trafficAccidentList) { + allNum += Long.parseLong(map.get("num").toString()); + } + setTableFonts(table.getRow(1).getCell(0), allNum.toString()); + + //excel + for (int i = 0; i < trafficAccidentList.size(); i++){ + setTableFonts(table.getRow(0).getCell(i+1), trafficAccidentList.get(i).get("eventStateLabel").toString()); + setTableFonts(table.getRow(1).getCell(i+1), trafficAccidentList.get(i).get("num").toString()); + } + + try { + + + // 复制Word模板 + InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("wordTemplate/chartTemplate.docx"); + XWPFDocument copiedTemplate = new XWPFDocument(inputStream); + + //获取word中所有图表对象 + List charts = copiedTemplate.getCharts(); + XWPFChart chart = charts.get(1); + XSSFWorkbook workbook = chart.getWorkbook(); + XSSFSheet sheet = workbook.getSheetAt(0); + //系列信息 + String[] singleBarSeriesNames = {"数量"}; + //分类信息 + for (int i = 0; i < trafficAccidentList.size(); i++){ + sheet.getRow(i+1).getCell(0).setCellValue(trafficAccidentList.get(i).get("eventStateLabel").toString()); + sheet.getRow(i+1).getCell(1).setCellValue(Long.parseLong(trafficAccidentList.get(i).get("num").toString())); + + } + for (int i = sheet.getLastRowNum(); i > trafficAccidentList.size();i--){ + sheet.removeRow(sheet.getRow(i)); + } + PoiUtil.wordExportChar(chart, "交通事故", singleBarSeriesNames,sheet ); + + // 追加到同一个Word文档中 + mergeChart(chart,doc); +// PoiUtil.mergeDoc(copiedTemplate, doc); + // 关闭复制的模板文档 + copiedTemplate.close(); + + } catch (Exception e){ + e.printStackTrace(); + } + + } + + //换行 + createLineBreak(doc); + + + + } + + + /** + * @Description 出入口车流量 + * + * @author liuwenge + * @date 2024/3/28 9:14 + * @param doc + * @return void + */ + public void trafficFlow(XWPFDocument doc) { + PoiUtil.createHeading2(doc, "出入口车流量"); + + + List> trafficFlowList = new ArrayList<>(); + Map trafficFlow = new HashMap<>(); + trafficFlow.put("label","入口"); + trafficFlow.put("num","0"); + trafficFlowList.add(trafficFlow); + + trafficFlow = new HashMap<>(); + trafficFlow.put("label","出口"); + trafficFlow.put("num","0"); + trafficFlowList.add(trafficFlow); + + if (trafficFlowList != null && trafficFlowList.size() > 0) { + + XWPFTable table = doc.createTable(2, 3); + //列宽自动分割 + CTTblWidth infoTableWidth = table.getCTTbl().addNewTblPr().addNewTblW(); + infoTableWidth.setType(STTblWidth.DXA); + infoTableWidth.setW(BigInteger.valueOf(9072)); + + + //表格插入总数 + setTableFonts(table.getRow(0).getCell(0), "合计"); + Long allNum = 0L; + for (Map map : trafficFlowList) { + allNum += Long.parseLong(map.get("num").toString()); + } + setTableFonts(table.getRow(1).getCell(0), allNum.toString()); + + //excel + for (int i = 0; i < trafficFlowList.size(); i++){ + setTableFonts(table.getRow(0).getCell(i+1), trafficFlowList.get(i).get("label").toString()); + setTableFonts(table.getRow(1).getCell(i+1), trafficFlowList.get(i).get("num").toString()); + } + + try { + + + // 复制Word模板 + InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("wordTemplate/chartTemplate.docx"); + XWPFDocument copiedTemplate = new XWPFDocument(inputStream); + + //获取word中所有图表对象 + List charts = copiedTemplate.getCharts(); + XWPFChart chart = charts.get(1); + XSSFWorkbook workbook = chart.getWorkbook(); + XSSFSheet sheet = workbook.getSheetAt(0); + + //系列信息 + String[] singleBarSeriesNames = {"数量"}; + //分类信息 + for (int i = 0; i < trafficFlowList.size(); i++){ + sheet.getRow(i+1).getCell(0).setCellValue(trafficFlowList.get(i).get("label").toString()); + sheet.getRow(i+1).getCell(1).setCellValue(Long.parseLong(trafficFlowList.get(i).get("num").toString())); + + } + for (int i = sheet.getLastRowNum(); i > trafficFlowList.size();i--){ + sheet.removeRow(sheet.getRow(i)); + } + PoiUtil.wordExportChar(chart, "出入口车流量", singleBarSeriesNames,sheet ); + + // 追加到同一个Word文档中 + mergeChart(chart,doc); +// PoiUtil.mergeDoc(copiedTemplate, doc); + + // 关闭复制的模板文档 + copiedTemplate.close(); + + } catch (Exception e){ + e.printStackTrace(); + } + + } + + //换行 + createLineBreak(doc); + + + } + + + /** + * @Description 封闭原因统计 + * + * @author liuwenge + * @date 2024/3/28 11:29 + * @param doc + * @return void + */ + public void trafficControlReasons(XWPFDocument doc) { + PoiUtil.createHeading2(doc, "封闭原因统计"); + + SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + Calendar calendar = Calendar.getInstance(); + //将天至1 + calendar.set(Calendar.DAY_OF_MONTH, 1); + //将小时至0 + calendar.set(Calendar.HOUR_OF_DAY, 0); + //将分钟至0 + calendar.set(Calendar.MINUTE, 0); + //将秒至0 + calendar.set(Calendar.SECOND,0); + //将毫秒至0 + calendar.set(Calendar.MILLISECOND, 0); + //获得当前月第一天 + Date date = calendar.getTime(); + String startDate = dateFormat.format(date); + String endDate = dateFormat.format(new Date()); + + + //封闭 + List> closeList = dcTrafficIncidentsMapper.selectTrafficControlReasons("1",startDate,endDate); + //限行 + List> restrictionList = dcTrafficIncidentsMapper.selectTrafficControlReasons("2",startDate,endDate); + + + + if (closeList != null && closeList.size() > 0 && restrictionList != null && restrictionList.size() > 0 ) { + + XWPFTable table = doc.createTable(13, 3); + //列宽自动分割 + CTTblWidth infoTableWidth = table.getCTTbl().addNewTblPr().addNewTblW(); + infoTableWidth.setType(STTblWidth.DXA); + infoTableWidth.setW(BigInteger.valueOf(9072)); + + setTableFonts(table.getRow(0).getCell(1), "封闭"); + setTableFonts(table.getRow(0).getCell(2), "限行"); + + //excel + for (int i = 0; i < closeList.size(); i++){ + setTableFonts(table.getRow(i+1).getCell(0), closeList.get(i).get("causeTypeName").toString()); + setTableFonts(table.getRow(i+1).getCell(1), closeList.get(i).get("num").toString()); + setTableFonts(table.getRow(i+1).getCell(2), restrictionList.get(i).get("num").toString()); + } + + try { + + + // 复制Word模板 + InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("wordTemplate/chartTemplate.docx"); + XWPFDocument copiedTemplate = new XWPFDocument(inputStream); + + //获取word中所有图表对象 + List charts = copiedTemplate.getCharts(); + XWPFChart chart = charts.get(2); + XSSFWorkbook workbook = chart.getWorkbook(); + XSSFSheet sheet = workbook.getSheetAt(0); + + //系列信息 + String[] singleBarSeriesNames = {"封闭","限行"}; + //分类信息 + for (int i = 0; i < closeList.size(); i++){ + sheet.getRow(i+1).getCell(0).setCellValue(closeList.get(i).get("causeTypeName").toString()); + sheet.getRow(i+1).getCell(1).setCellValue(Long.parseLong(closeList.get(i).get("num").toString())); + sheet.getRow(i+1).getCell(2).setCellValue(Long.parseLong(restrictionList.get(i).get("num").toString())); + + } + PoiUtil.wordExportChar(chart, "封闭原因统计", singleBarSeriesNames,sheet ); + + + // 追加到同一个Word文档中 + mergeChart(chart,doc); +// PoiUtil.mergeDoc(copiedTemplate, doc); + + + // 关闭复制的模板文档 + copiedTemplate.close(); + + } catch (Exception e){ + e.printStackTrace(); + } + } + + //换行 + createLineBreak(doc); + } + + + /** + * @Description 封闭数量统计 + * + * @author liuwenge + * @date 2024/3/28 11:29 + * @param doc + * @return void + */ + public void closedQuantity(XWPFDocument doc) { + PoiUtil.createHeading2(doc, "封闭数量统计"); + + List> closedQuantityList = dcTrafficIncidentsMapper.selectClosedQuantity(); + + if (closedQuantityList != null && closedQuantityList.size() > 0) { + + XWPFTable table = doc.createTable(25, 2); + //列宽自动分割 + CTTblWidth infoTableWidth = table.getCTTbl().addNewTblPr().addNewTblW(); + infoTableWidth.setType(STTblWidth.DXA); + infoTableWidth.setW(BigInteger.valueOf(9072)); + + setTableFonts(table.getRow(0).getCell(0), "时间"); + setTableFonts(table.getRow(0).getCell(1), "数量"); + + //excel + for (int i = 0; i < closedQuantityList.size(); i++){ + setTableFonts(table.getRow(i+1).getCell(0), closedQuantityList.get(i).get("hours").toString()); + setTableFonts(table.getRow(i+1).getCell(1), closedQuantityList.get(i).get("num").toString()); + } + + try { + + + // 复制Word模板 + InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("wordTemplate/chartTemplate.docx"); + XWPFDocument copiedTemplate = new XWPFDocument(inputStream); + + //获取word中所有图表对象 + List charts = copiedTemplate.getCharts(); + XWPFChart chart = charts.get(3); + XSSFWorkbook workbook = chart.getWorkbook(); + XSSFSheet sheet = workbook.getSheetAt(0); + + //系列信息 + String[] singleBarSeriesNames = {"数量"}; + //分类信息 + for (int i = 0; i < closedQuantityList.size(); i++){ + sheet.getRow(i+1).getCell(0).setCellValue(closedQuantityList.get(i).get("hours").toString()); + sheet.getRow(i+1).getCell(1).setCellValue(Long.parseLong(closedQuantityList.get(i).get("num").toString())); + + } + PoiUtil.wordExportChar(chart, "封闭数量统计", singleBarSeriesNames,sheet ); + + + // 追加到同一个Word文档中 + mergeChart(chart,doc); +// PoiUtil.mergeDoc(copiedTemplate, doc); + + + // 关闭复制的模板文档 + copiedTemplate.close(); + + } catch (Exception e){ + e.printStackTrace(); + } + } + + //换行 + createLineBreak(doc); + } + + + /** + * @Description 事故类型统计 + * + * @author liuwenge + * @date 2024/3/28 11:29 + * @param doc + * @return void + */ + public void accidentType(XWPFDocument doc) { + PoiUtil.createHeading2(doc, "事故类型统计"); + + List> accidentTypeList = dcTrafficIncidentsMapper.selectAccidentTypeStatistics(); + + if (accidentTypeList != null && accidentTypeList.size() > 0) { + + XWPFTable table = doc.createTable(6, 2); + //列宽自动分割 + CTTblWidth infoTableWidth = table.getCTTbl().addNewTblPr().addNewTblW(); + infoTableWidth.setType(STTblWidth.DXA); + infoTableWidth.setW(BigInteger.valueOf(9072)); + + setTableFonts(table.getRow(0).getCell(0), "事故类型"); + setTableFonts(table.getRow(0).getCell(1), "数量"); + + //excel + for (int i = 0; i < accidentTypeList.size(); i++){ + setTableFonts(table.getRow(i+1).getCell(0), accidentTypeList.get(i).get("eventSubclassName").toString()); + setTableFonts(table.getRow(i+1).getCell(1), accidentTypeList.get(i).get("num").toString()); + } + + try { + + + // 复制Word模板 + InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("wordTemplate/chartTemplate2.docx"); + XWPFDocument copiedTemplate = new XWPFDocument(inputStream); + + //获取word中所有图表对象 + List charts = copiedTemplate.getCharts(); + XWPFChart chart = charts.get(0); + XSSFWorkbook workbook = chart.getWorkbook(); + XSSFSheet sheet = workbook.getSheetAt(0); + + //系列信息 + String[] singleBarSeriesNames = {"数量"}; + //分类信息 + for (int i = 0; i < accidentTypeList.size(); i++){ + sheet.getRow(i+1).getCell(0).setCellValue(accidentTypeList.get(i).get("eventSubclassName").toString()); + sheet.getRow(i+1).getCell(1).setCellValue(Long.parseLong(accidentTypeList.get(i).get("num").toString())); + + } + PoiUtil.wordExportChar(chart, "事故类型统计", singleBarSeriesNames,sheet ); + + + // 追加到同一个Word文档中 + mergeChart(chart,doc); +// PoiUtil.mergeDoc(copiedTemplate, doc); + + + // 关闭复制的模板文档 + copiedTemplate.close(); + + } catch (Exception e){ + e.printStackTrace(); + } + } + + //换行 + createLineBreak(doc); + + List> trafficAccidentsList = dcTrafficIncidentsMapper.selectTrafficAccidents(); + + if (trafficAccidentsList != null && trafficAccidentsList.size() > 0) { + + XWPFTable table = doc.createTable(25, 2); + //列宽自动分割 + CTTblWidth infoTableWidth = table.getCTTbl().addNewTblPr().addNewTblW(); + infoTableWidth.setType(STTblWidth.DXA); + infoTableWidth.setW(BigInteger.valueOf(9072)); + + setTableFonts(table.getRow(0).getCell(0), "时间"); + setTableFonts(table.getRow(0).getCell(1), "数量"); + + //excel + for (int i = 0; i < trafficAccidentsList.size(); i++){ + setTableFonts(table.getRow(i+1).getCell(0), trafficAccidentsList.get(i).get("hours").toString()); + setTableFonts(table.getRow(i+1).getCell(1), trafficAccidentsList.get(i).get("num").toString()); + } + + try { + + + // 复制Word模板 + InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("wordTemplate/chartTemplate.docx"); + XWPFDocument copiedTemplate = new XWPFDocument(inputStream); + + //获取word中所有图表对象 + List charts = copiedTemplate.getCharts(); + XWPFChart chart = charts.get(3); + XSSFWorkbook workbook = chart.getWorkbook(); + XSSFSheet sheet = workbook.getSheetAt(0); + + //系列信息 + String[] singleBarSeriesNames = {"数量"}; + //分类信息 + for (int i = 0; i < trafficAccidentsList.size(); i++){ + sheet.getRow(i+1).getCell(0).setCellValue(trafficAccidentsList.get(i).get("hours").toString()); + sheet.getRow(i+1).getCell(1).setCellValue(Long.parseLong(trafficAccidentsList.get(i).get("num").toString())); + + } + PoiUtil.wordExportChar(chart, "事故时间统计", singleBarSeriesNames,sheet ); + + + // 追加到同一个Word文档中 + mergeChart(chart,doc); +// PoiUtil.mergeDoc(copiedTemplate, doc); + + + // 关闭复制的模板文档 + copiedTemplate.close(); + + } catch (Exception e){ + e.printStackTrace(); + } + } + + //换行 + createLineBreak(doc); + } + + + + + /** + * @Description 拥堵地点 + * + * @author liuwenge + * @date 2024/3/28 16:56 + * @param doc + * @return void + */ + public void congestionLocation(XWPFDocument doc) { + PoiUtil.createHeading2(doc, "拥堵地点"); + + List> congestionLocationList = dcTrafficIncidentsMapper.selectCongestionLocation(); + + if (congestionLocationList != null && congestionLocationList.size() > 0) { + + XWPFTable table = doc.createTable(2, 5); + //列宽自动分割 + CTTblWidth infoTableWidth = table.getCTTbl().addNewTblPr().addNewTblW(); + infoTableWidth.setType(STTblWidth.DXA); + infoTableWidth.setW(BigInteger.valueOf(9072)); + + //表格插入总数 + setTableFonts(table.getRow(0).getCell(0), "合计"); + Long allNum = 0L; + for (Map map : congestionLocationList) { + allNum += Long.parseLong(map.get("num").toString()); + } + setTableFonts(table.getRow(1).getCell(0), allNum.toString()); + + //excel + for (int i = 0; i < congestionLocationList.size(); i++){ + setTableFonts(table.getRow(0).getCell(i+1), congestionLocationList.get(i).get("eventSubclassLabel").toString()); + setTableFonts(table.getRow(1).getCell(i+1), congestionLocationList.get(i).get("num").toString()); + } + + + + try { + + + // 复制Word模板 + InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("wordTemplate/chartTemplate.docx"); + XWPFDocument copiedTemplate = new XWPFDocument(inputStream); + + //获取word中所有图表对象 + List charts = copiedTemplate.getCharts(); + XWPFChart chart = charts.get(1); + XSSFWorkbook workbook = chart.getWorkbook(); + XSSFSheet sheet = workbook.getSheetAt(0); + + //系列信息 + String[] singleBarSeriesNames = {"数量"}; + //分类信息 + for (int i = 0; i < congestionLocationList.size(); i++){ + sheet.getRow(i+1).getCell(0).setCellValue(congestionLocationList.get(i).get("eventSubclassLabel").toString()); + sheet.getRow(i+1).getCell(1).setCellValue(Long.parseLong(congestionLocationList.get(i).get("num").toString())); + + } + for (int i = sheet.getLastRowNum(); i > congestionLocationList.size();i--){ + sheet.removeRow(sheet.getRow(i)); + } + PoiUtil.wordExportChar(chart, "拥堵地点", singleBarSeriesNames,sheet ); + + + // 追加到同一个Word文档中 + mergeChart(chart,doc); +// PoiUtil.mergeDoc(copiedTemplate, doc); + + + // 关闭复制的模板文档 + copiedTemplate.close(); + + } catch (Exception e){ + e.printStackTrace(); + } + } + + //换行 + createLineBreak(doc); + } + + /** + * @Description 拥堵时长 + * + * @author liuwenge + * @date 2024/3/28 17:27 + * @param doc + * @return void + */ + public void congestionDuration(XWPFDocument doc) { + PoiUtil.createHeading2(doc, "拥堵时长"); + + SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + Calendar calendar = Calendar.getInstance(); + //将天至1 + calendar.set(Calendar.DAY_OF_MONTH, 1); + //将小时至0 + calendar.set(Calendar.HOUR_OF_DAY, 0); + //将分钟至0 + calendar.set(Calendar.MINUTE, 0); + //将秒至0 + calendar.set(Calendar.SECOND,0); + //将毫秒至0 + calendar.set(Calendar.MILLISECOND, 0); + //获得当前月第一天 + Date date = calendar.getTime(); + String startDate = dateFormat.format(date); + String endDate = dateFormat.format(new Date()); + + List> congestionDurationList = dcTrafficIncidentsMapper.selectCongestionDuration(startDate,endDate); + + if (congestionDurationList != null && congestionDurationList.size() > 0) { + + XWPFTable table = doc.createTable(2, 1); + //列宽自动分割 + CTTblWidth infoTableWidth = table.getCTTbl().addNewTblPr().addNewTblW(); + infoTableWidth.setType(STTblWidth.DXA); + infoTableWidth.setW(BigInteger.valueOf(9072)); + + setTableFonts(table.getRow(0).getCell(0), "合计"); + setTableFonts(table.getRow(1).getCell(0), congestionDurationList.get(0).get("minuteTime").toString()); + } + + //换行 + createLineBreak(doc); + } + + /** + * @Description 拥堵长度 + * + * @author liuwenge + * @date 2024/3/28 19:31 + * @param doc + * @return void + */ + public void congestionMileage(XWPFDocument doc) { + PoiUtil.createHeading2(doc, "拥堵长度"); + + List> congestionMileageList = dcTrafficIncidentsMapper.selectCongestionMileage(); + + if (congestionMileageList != null && congestionMileageList.size() > 0) { + + XWPFTable table = doc.createTable(2, 6); + //列宽自动分割 + CTTblWidth infoTableWidth = table.getCTTbl().addNewTblPr().addNewTblW(); + infoTableWidth.setType(STTblWidth.DXA); + infoTableWidth.setW(BigInteger.valueOf(9072)); + + //表格插入总数 + setTableFonts(table.getRow(0).getCell(0), "合计"); + Long allNum = 0L; + for (Map map : congestionMileageList) { + allNum += Long.parseLong(map.get("num").toString()); + } + setTableFonts(table.getRow(1).getCell(0), allNum.toString()); + + //excel + for (int i = 0; i < congestionMileageList.size(); i++){ + setTableFonts(table.getRow(0).getCell(i+1), congestionMileageList.get(i).get("mileageType").toString()); + setTableFonts(table.getRow(1).getCell(i+1), congestionMileageList.get(i).get("num").toString()); + } + + + try { + + + // 复制Word模板 + InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("wordTemplate/chartTemplate.docx"); + XWPFDocument copiedTemplate = new XWPFDocument(inputStream); + + //获取word中所有图表对象 + List charts = copiedTemplate.getCharts(); + XWPFChart chart = charts.get(1); + XSSFWorkbook workbook = chart.getWorkbook(); + XSSFSheet sheet = workbook.getSheetAt(0); + + //系列信息 + String[] singleBarSeriesNames = {"数量"}; + //分类信息 + for (int i = 0; i < congestionMileageList.size(); i++){ + sheet.getRow(i+1).getCell(0).setCellValue(congestionMileageList.get(i).get("mileageType").toString()); + sheet.getRow(i+1).getCell(1).setCellValue(Long.parseLong(congestionMileageList.get(i).get("num").toString())); + + } + for (int i = sheet.getLastRowNum(); i > congestionMileageList.size();i--){ + sheet.removeRow(sheet.getRow(i)); + } + PoiUtil.wordExportChar(chart, "拥堵长度", singleBarSeriesNames,sheet ); + + + // 追加到同一个Word文档中 + mergeChart(chart,doc); +// PoiUtil.mergeDoc(copiedTemplate, doc); + + + // 关闭复制的模板文档 + copiedTemplate.close(); + + } catch (Exception e){ + e.printStackTrace(); + } + } + + //换行 + createLineBreak(doc); + } + +} diff --git a/zc-business/src/main/java/com/zc/business/mapper/DcTrafficIncidentsMapper.java b/zc-business/src/main/java/com/zc/business/mapper/DcTrafficIncidentsMapper.java index d01d8a09..dfaf12ee 100644 --- a/zc-business/src/main/java/com/zc/business/mapper/DcTrafficIncidentsMapper.java +++ b/zc-business/src/main/java/com/zc/business/mapper/DcTrafficIncidentsMapper.java @@ -236,4 +236,97 @@ public interface DcTrafficIncidentsMapper { */ List> selectEventTypeAnalysis(@Param("direction") String direction,@Param("type") String type,@Param("startTime") String startTime); + /** + * @Description 交通管制情况 + * + * @author liuwenge + * @date 2024/3/27 18:32 + * @param startTime + * @param endTime + * @return java.util.List> + */ + List> selectTrafficControlAnalysis(@Param("startTime") String startTime,@Param("endTime") String endTime); + + /** + * @Description 交通事故统计 + * + * @author liuwenge + * @date 2024/3/27 20:04 + * @param + * @return java.util.List> + */ + List> selectTrafficAccidentAnalysis(); + + /** + * @Description 封闭原因统计 + * + * @author liuwenge + * @date 2024/3/28 14:47 + * @param controlType + * @param startTime + * @param endTime + * @return java.util.List> + */ + List> selectTrafficControlReasons(@Param("controlType") String controlType,@Param("startTime") String startTime,@Param("endTime") String endTime); + + + /** + * @Description 封闭数量统计 + * + * @author liuwenge + * @date 2024/3/28 14:51 + * @param + * @return java.util.List> + */ + List> selectClosedQuantity(); + + /** + * @Description 事故类型统计--当月按类型 + * + * @author liuwenge + * @date 2024/3/28 15:19 + * @param + * @return java.util.List> + */ + List> selectAccidentTypeStatistics(); + + /** + * @Description 事故类型统计--当天按小时 + * + * @author liuwenge + * @date 2024/3/28 16:28 + * @param + * @return java.util.List> + */ + List> selectTrafficAccidents(); + + /** + * @Description 拥堵地点 + * + * @author liuwenge + * @date 2024/3/28 16:55 + * @param + * @return java.util.List> + */ + List> selectCongestionLocation(); + + /** + * @Description 拥堵时长 + * + * @author liuwenge + * @date 2024/3/28 17:23 + * @param + * @return java.util.List> + */ + List> selectCongestionDuration(@Param("startTime") String startTime,@Param("endTime") String endTime); + + /** + * @Description 拥堵长度 + * + * @author liuwenge + * @date 2024/3/28 19:30 + * @return java.util.List> + */ + List> selectCongestionMileage(); + } diff --git a/zc-business/src/main/java/com/zc/business/utils/PoiUtil.java b/zc-business/src/main/java/com/zc/business/utils/PoiUtil.java new file mode 100644 index 00000000..e799f2ef --- /dev/null +++ b/zc-business/src/main/java/com/zc/business/utils/PoiUtil.java @@ -0,0 +1,369 @@ +package com.zc.business.utils; + +import com.aliyuncs.utils.IOUtils; +import com.google.common.base.Strings; +import org.apache.poi.openxml4j.opc.OPCPackage; +import org.apache.poi.ss.util.CellRangeAddress; +import org.apache.poi.ss.util.CellReference; +import org.apache.poi.xddf.usermodel.chart.XDDFChartData; +import org.apache.poi.xddf.usermodel.chart.XDDFDataSource; +import org.apache.poi.xddf.usermodel.chart.XDDFDataSourcesFactory; +import org.apache.poi.xddf.usermodel.chart.XDDFNumericalDataSource; +import org.apache.poi.xssf.usermodel.XSSFSheet; +import org.apache.poi.xwpf.usermodel.*; +import org.apache.xmlbeans.XmlOptions; +import org.openxmlformats.schemas.drawingml.x2006.chart.CTPlotArea; +import org.openxmlformats.schemas.wordprocessingml.x2006.main.*; +import org.springframework.util.StringUtils; + +import java.io.*; +import java.math.BigInteger; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * @author : LCheng + * @date : 2020-12-10 10:03 + * description : poi工具 + */ +public class PoiUtil { + + public static int headingCount1 = 1; + public static int headingCount2 = 1; + + /** + * 根据word模板导出 针对图表(柱状图,折线图,饼图等)的处理 + * + * @param docChart 图表对象 + * @param title 图表标题 + * @param seriesNames 系列名称数组 + * @return {@link XWPFChart} + * @author LCheng + * @date 2020/12/10 11:08 + */ + public static XWPFChart wordExportChar(XWPFChart docChart, String title, String[] seriesNames, XSSFSheet sheet) { + //获取图表数据对象 + XDDFChartData chartData = docChart.getChartSeries().get(0); + + //word图表均对应一个内置的excel,用于保存图表对应的数据 + //excel中 第一列第二行开始的数据为分类信息 + //CellRangeAddress(1, categories.size(), 0, 0) 四个参数依次为 起始行 截止行 起始列 截止列。 + //根据分类信息的范围创建分类信息的数据源 + XDDFDataSource catDataSource = XDDFDataSourcesFactory.fromStringCellRange(sheet, new CellRangeAddress(1,sheet.getLastRowNum(),0,0)); + //更新数据 + for (int i = 0; i < seriesNames.length; i++) { + //excel中各系列对应的数据的范围 + //根据数据的范围创建值的数据源 + XDDFNumericalDataSource valDataSource = XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(1,sheet.getLastRowNum(),i+1,i+1)); + //获取图表系列的数据对象 + XDDFChartData.Series series = chartData.getSeries().get(i); + //替换系列数据对象中的分类和值 + series.replaceData(catDataSource, valDataSource); + //修改系列数据对象中的标题 + CellReference cellReference = docChart.setSheetTitle(seriesNames[i], 1); + series.setTitle(seriesNames[i], cellReference); + } + //更新图表数据对象 + docChart.plot(chartData); + //图表整体的标题 传空值则不替换标题 + if (!Strings.isNullOrEmpty(title)) { + docChart.setTitleText(title); + docChart.setTitleOverlay(false); + } + return docChart; + } + + /** + * 合并docx文件 + * @param srcDocxs 需要合并的目标docx文件 + * @param destDocx 合并后的docx输出文件 + */ + public static void mergeDoc(XWPFDocument srcDocxs, XWPFDocument destDocx) { + + + try { + + //获取目标文件的CTDocument1对象 + CTDocument1 ctDocument1 = srcDocxs.getDocument(); + //获取第一个目标文件的CTBody对象 + CTBody src1Body = ctDocument1.getBody(); + + + //获取目标文件中的图表 + List relations = srcDocxs.getCharts(); + //判断是否有图表,没有图表的话,追加到之前的目标文件后面 + if (relations.size() <= 0) { + CTBody src2Body = srcDocxs.getDocument().getBody(); + //获取目标文件中的图片 + List allPictures = srcDocxs.getAllPictures(); + // 记录图片合并前及合并后的ID + Map map = new HashMap(); + //遍历图片 + for (XWPFPictureData picture : allPictures) { + String before = srcDocxs.getRelationId(picture); + //将原文档中的图片加入到目标文档中 + String after = destDocx.addPictureData(picture.getData(), Document.PICTURE_TYPE_PNG); + map.put(before, after); + } + //将当前文件的内容追加到之前的目标文件中 + appendBody(src1Body, src2Body,map); + } + //遍历图表, + for (XWPFChart chart1 : relations) { + //是否是word中自带图表 + if (chart1 instanceof XWPFChart) { // 如果是图表元素 + XWPFChart chart = destDocx.createChart(5774310, 3076575); + CTPlotArea plotArea = chart1.getCTChart().getPlotArea(); + chart.getCTChart().setPlotArea(plotArea); + chart.getCTChart().setLegend(chart1.getCTChart().getLegend()); + } + } + //关闭流 + srcDocxs.close(); + + } catch (Exception e) { + e.printStackTrace(); + } + } + + + + /** + * 合并chart + * @param chart 需要合并的目标chart + * @param destDocx 合并后的docx输出文件 + */ + public static void mergeChart(XWPFChart chart, XWPFDocument destDocx) { + try { + + XWPFChart docxChart = destDocx.createChart(5774310, 3076575); + CTPlotArea plotArea = chart.getCTChart().getPlotArea(); + docxChart.getCTChart().setPlotArea(plotArea); + docxChart.getCTChart().setLegend(chart.getCTChart().getLegend()); + + } catch (Exception e) { + e.printStackTrace(); + } + } + + /** + * 插入换行符 + * @param destDocx 合并后的docx输出文件 + */ + public static void createLineBreak(XWPFDocument destDocx) { + try { + + XWPFParagraph paragraph = destDocx.createParagraph(); + XWPFRun run = paragraph.createRun(); + run.addBreak(); + + } catch (Exception e) { + e.printStackTrace(); + } + } + + + + /** + * 合并文档内容 + * + * @param src 目标文档 + * @param append 要合并的文档 + * @throws Exception + */ + private static void appendBody(CTBody src, CTBody append,Map map) throws Exception { + XmlOptions optionsOuter = new XmlOptions(); + optionsOuter.setSaveOuter(); + //获取目标文件的字符内容 + String srcString = src.xmlText(); + //获取目标文件字符的开头 + String prefix = srcString.substring(0, srcString.indexOf(">") + 1); + //获取目标文件字符的内容 + String mainPart = srcString.substring(srcString.indexOf(">") + 1, + srcString.lastIndexOf("<")); + //获取目标文件字符的结尾 + String sufix = srcString.substring(srcString.lastIndexOf("<")); + //获取需要追加的文件 + String appendString = append.xmlText(optionsOuter); + //获取需要追加的文件内容(除去头和尾) + String addPart = appendString.substring(appendString.indexOf(">") + 1, + appendString.lastIndexOf("<")); + if (map != null && !map.isEmpty()) { + //对xml字符串中图片ID进行替换 + for (Map.Entry set : map.entrySet()) { + addPart = addPart.replace(set.getKey(), set.getValue()); + } + } + //将获取到的文件内容合并成为新的CTBody + CTBody makeBody = CTBody.Factory.parse(prefix + mainPart + addPart + + sufix); + //将新的CTBody重新设置到目标文件中 + src.set(makeBody); + } + + public static XWPFParagraph createHeading(XWPFDocument doc, String title) { + //段落 + XWPFParagraph paragraph = doc.createParagraph(); + XWPFRun run = paragraph.createRun(); + run.setText(title); +// run.setColor("696969"); + run.setFontSize(18); + run.setBold(true);//标题加粗 + return paragraph; + } + + /** + * 创建标题1 + * + * @param doc + * @param title + */ + public static void createHeading1(XWPFDocument doc, String title) { + //段落 + XWPFParagraph paragraph = doc.createParagraph(); + XWPFRun run = paragraph.createRun(); + run.setText(title); +// run.setColor("696969"); + run.setFontSize(16); + run.setBold(true);//标题加粗 + paragraph.setStyle("Heading1"); + } + + /** + * 创建标题2 + * + * @param doc + * @param title + */ + public static void createHeading2(XWPFDocument doc, String title) { + XWPFParagraph paragraph = doc.createParagraph(); + XWPFRun run = paragraph.createRun(); + run.setText(title); + run.setFontSize(14); + run.setBold(true);//标题加粗 + paragraph.setStyle("Heading2"); + } + + public static void createTable(XWPFDocument doc) { + XWPFTable table = doc.createTable(3, 3); + //列宽自动分割 + CTTblWidth infoTableWidth = table.getCTTbl().addNewTblPr().addNewTblW(); + infoTableWidth.setType(STTblWidth.DXA); + infoTableWidth.setW(BigInteger.valueOf(9072)); + + setTableFonts(table.getRow(0).getCell(0), "编号"); + setTableFonts(table.getRow(0).getCell(1), "问题"); + setTableFonts(table.getRow(0).getCell(2), "应答"); + setTableFonts(table.getRow(1).getCell(0), "1"); + setTableFonts(table.getRow(1).getCell(1), "陈述日期"); + setTableFonts(table.getRow(1).getCell(2), "2017年02月17日"); + setTableFonts(table.getRow(2).getCell(0), "2"); + setTableFonts(table.getRow(2).getCell(1), "PICS序列号"); + setTableFonts(table.getRow(2).getCell(2), "121313132131"); + + } + + // word跨列合并单元格 + public static void mergeCellsHorizontal(XWPFTable table, int row, int fromCell, int toCell) { + for (int cellIndex = fromCell; cellIndex <= toCell; cellIndex++) { + XWPFTableCell cell = table.getRow(row).getCell(cellIndex); + if (cellIndex == fromCell) { + // The first merged cell is set with RESTART merge value + cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.RESTART); + } else { + // Cells which join (merge) the first one, are set with CONTINUE + cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.CONTINUE); + } + } + } + + // word跨行并单元格 + public static void mergeCellsVertically(XWPFTable table, int col, int fromRow, int toRow) { + for (int rowIndex = fromRow; rowIndex <= toRow; rowIndex++) { + XWPFTableCell cell = table.getRow(rowIndex).getCell(col); + if (rowIndex == fromRow) { + // The first merged cell is set with RESTART merge value + cell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.RESTART); + } else { + // Cells which join (merge) the first one, are set with CONTINUE + cell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.CONTINUE); + } + } + } + + /** + * 设置表格中字体 + * + * @param cell + * @param cellText + */ + public static void setTableFonts(XWPFTableCell cell, String cellText) { + CTP ctp = CTP.Factory.newInstance(); + XWPFParagraph p = new XWPFParagraph(ctp, cell); + p.setAlignment(ParagraphAlignment.CENTER); + XWPFRun run = p.createRun(); + run.setFontSize(8); + run.setText(cellText); + CTRPr rpr = run.getCTR().isSetRPr() ? run.getCTR().getRPr() : run.getCTR().addNewRPr(); + CTFonts fonts = rpr.isSetRFonts() ? rpr.getRFonts() : rpr.addNewRFonts(); + fonts.setAscii("仿宋"); + fonts.setEastAsia("仿宋"); + fonts.setHAnsi("仿宋"); + cell.setParagraph(p); + } + + /** + * 添加描述信息 + * + * @param doc + * @param description + */ + public static void addDescription(XWPFDocument doc, String description) { + if (StringUtils.isEmpty(description)) { + return; + } + XWPFParagraph title = doc.createParagraph(); + XWPFRun run = title.createRun(); + run.setText(description); + run.setBold(true); + title.setAlignment(ParagraphAlignment.CENTER); + } + + /** + * 创建目录 + * 创建并插入带超链接的目录 + * @param document + */ + public static void insertTOC2(XWPFDocument document) { + // 定义 TOC 字段属性 + CTSimpleField tocField = CTSimpleField.Factory.newInstance(); + tocField.setInstr("TOC \\h \\z \\t \"Heading1,Heading2\""); // 包含 Heading1 和 Heading2 样式的目录 + + // 创建包含 TOC 字段的段落 + XWPFParagraph tocPara = document.createParagraph(); + tocPara.getCTP().addNewFldSimple().set(tocField); + + // 更新文档字段以计算目录 + document.enforceUpdateFields(); + } + + /** + * 创建目录 + * 创建并插入带超链接的目录 + * @param document + */ + public static void insertTOC(XWPFDocument document) { + // 创建目录所在的段落 + XWPFParagraph tocPara = document.createParagraph(); + + + // 添加 TOC 域代码 + String tocFieldCode = "TOC \\o \"1-3\" \\h \\z \\u"; + CTSimpleField tocField = tocPara.getCTP().addNewFldSimple(); + tocField.setInstr(tocFieldCode); + tocField.setDirty(STOnOff.TRUE); + } +} + diff --git a/zc-business/src/main/resources/mapper/business/DcTrafficIncidentsMapper.xml b/zc-business/src/main/resources/mapper/business/DcTrafficIncidentsMapper.xml index 46746935..ce150537 100644 --- a/zc-business/src/main/resources/mapper/business/DcTrafficIncidentsMapper.xml +++ b/zc-business/src/main/resources/mapper/business/DcTrafficIncidentsMapper.xml @@ -398,4 +398,233 @@ group by event_subclass) t3 on t1.eventSubclass = t3.event_subclass + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/zc-business/src/main/resources/wordTemplate/chartTemplate.docx b/zc-business/src/main/resources/wordTemplate/chartTemplate.docx new file mode 100644 index 00000000..2f9a8f65 Binary files /dev/null and b/zc-business/src/main/resources/wordTemplate/chartTemplate.docx differ diff --git a/zc-business/src/main/resources/wordTemplate/chartTemplate2.docx b/zc-business/src/main/resources/wordTemplate/chartTemplate2.docx new file mode 100644 index 00000000..c0a398ea Binary files /dev/null and b/zc-business/src/main/resources/wordTemplate/chartTemplate2.docx differ diff --git a/zc-business/src/main/resources/wordTemplate/wordTemplate.docx b/zc-business/src/main/resources/wordTemplate/wordTemplate.docx new file mode 100644 index 00000000..5c48a30b Binary files /dev/null and b/zc-business/src/main/resources/wordTemplate/wordTemplate.docx differ