Controller
设置报表的生成后保存的路径,进行抛出异常处理。
@GetMapping("download/{eventType}/{eventId}")
public String download(@PathVariable("eventType") Integer eventType, @PathVariable("eventId") Long eventId, HttpServletRequest request, HttpServletResponse response) {
String pdfPath = "src/main/resources/baobiao/xunzhen/";//设置报表生成后,保存路径
String chartPath = "src/main/resources/baobiao/pic/";//设置图片生成后,保存路径
EventCommonInfo xunzhenEventVo = xunzhenEventVoDao.getXunzhenEventVoById(eventId);
XunzhenEvent xunzhenEvent = xunzhenEventDao.getById(eventId);
String warehouseAndSegmentName = xunzhenEventVo.getWarehouseName() + xunzhenEventVo.getSegmentName() + "仓";//获得仓库仓间名
Date start = xunzhenEventVo.getStartTime();
Date end = xunzhenEventVo.getEndTime();
String startStr = DateUtils.parseDateToString(start, DateUtils.DATE_FORMAT_COMPACTFULL);
String endStr = DateUtils.parseDateToString(end, DateUtils.DATE_FORMAT_COMPACTFULL);
String fileName = warehouseAndSegmentName + "_" + startStr + "_" + endStr + ".PDF";// 文件名
String chartENVName = warehouseAndSegmentName + "_" + startStr + "_" + endStr +"ENV"+".jpg";// 图片名
String chartSanqiName = warehouseAndSegmentName + "_" + startStr + "_" + endStr +"SANQI"+".jpg";// 图片名
//设置文件路径
File file = new File(pdfPath + fileName);
if (!file.exists()) {
//文件不存在就生成
generateXunzhenPdf(eventId,pdfPath,fileName,chartPath,chartENVName,chartSanqiName,xunzhenEventVo,xunzhenEvent);
}
response.setContentType("application/force-download");// 设置强制下载不打开
response.addHeader("Content-Disposition", "attachment;fileName=" + fileName);// 设置文件名
byte[] buffer = new byte[1024];
FileInputStream fis = null;
BufferedInputStream bis = null;
try {
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
OutputStream os = response.getOutputStream();
int i = bis.read(buffer);
while (i != -1) {
os.write(buffer, 0, i);
i = bis.read(buffer);
}
return "success";
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return "failed";
}
具体业务处理
查寻相关数据封装到数据类中
public Tuple2 generateXunzhenPdf(Long eventId, String pdfPath, String fileName, String chartPath, String chartENVName,String chartSanqiName, EventCommonInfo xunzhenEventVo, XunzhenEvent xunzhenEvent) {
XunzhenBaobiaoInfo xunzhenBaobiaoInfo = new XunzhenBaobiaoInfo();
String warehouseAndSegmentName = xunzhenEventVo.getWarehouseName() + xunzhenEventVo.getSegmentName() + "仓";
xunzhenBaobiaoInfo.setWarehouseAndSegmentName(warehouseAndSegmentName);
Date start = xunzhenEventVo.getStartTime();//得到相关时间
Date sanqi = xunzhenEventVo.getSanqiTime();
Date end = xunzhenEventVo.getEndTime();
String startStr = DateUtils.parseDateToString(start, DateUtils.DATE_FORMAT_FULL);//将时间转为标准时间格式字符串
String endStr = DateUtils.parseDateToString(end, DateUtils.DATE_FORMAT_FULL);
String timeRange = startStr + "_" + endStr;//填充相关信息
String segmentInfo = String.valueOf(xunzhenEventVo.getHeight()) + "x" +
String.valueOf(xunzhenEventVo.getWidth()) + "x" +
String.valueOf(xunzhenEventVo.getLength());
xunzhenBaobiaoInfo.setXunzhenTimeRange(timeRange);
xunzhenBaobiaoInfo.setSegmentInfo(segmentInfo);
xunzhenBaobiaoInfo.setChargeName(xunzhenEventVo.getUserName());
xunzhenBaobiaoInfo.setWeight(String.valueOf(xunzhenEventVo.getWeight()));
String hzsType = "";
if (xunzhenEventVo.getHzsType() == 1) {
hzsType = "56%";
} else if (xunzhenEventVo.getHzsType() == 2) {
hzsType = "85%";
}
xunzhenBaobiaoInfo.setHzsType(hzsType);
xunzhenBaobiaoInfo.setSanqiTime(xunzhenEvent.getSanqiTime().toString());
// AccumulateXunzhenStat accumulateXunzhenStat = conditionService.computeXunzhenResult(xunzhenEventVo.getSegmentId(), new Timestamp(start.getTime()), new Timestamp(end.getTime()));
xunzhenBaobiaoInfo.setSanqiTime(String.valueOf(xunzhenEventVo.getSanqiTime()));
xunzhenBaobiaoInfo.setHint(String.valueOf(xunzhenEventVo.getHint()));
xunzhenBaobiaoInfo.setFilePath(pdfPath);
xunzhenBaobiaoInfo.setFileName(fileName);
//生成图表1
generateXunzhenChart(xunzhenEventVo.getSegmentId(), warehouseAndSegmentName, chartPath, chartENVName, start, end);
//生成图表2
generateSanqiChart(xunzhenEventVo.getSegmentId(), warehouseAndSegmentName, chartPath, chartSanqiName, sanqi, end);
xunzhenBaobiaoInfo.setChart1CompletePathname(chartPath + chartENVName);
xunzhenBaobiaoInfo.setChart2CompletePathname(chartPath + chartSanqiName);
GeneratePdfService.generateXunzhenBaogao(xunzhenBaobiaoInfo);//生成报表
return Tuple2.with(pdfPath, fileName);
}
生成图表先获得具体时间,再将时间转换为LocalDateTime类型,后取得时分秒作为x轴坐标,数据值作为y值,封装到时间序列中,再封装到时间序列集合,再封装到XYDataset类型中,用于图表生成
private void generateSanqiChart(Long segmentId, String title, String chartPath, String chartName, Date sanqi, Date end) {
Instant instantsanqi = sanqi.toInstant();
Instant instantend = end.toInstant();
ZoneId zoneId = ZoneId.systemDefault();
LocalDateTime sanqiTime = instantsanqi.atZone(zoneId).toLocalDateTime();
LocalDateTime endTime = instantend.atZone(zoneId).toLocalDateTime();
Tuple5 kuneiT5 = chartService.getTHMAxisDataById(segmentId, sanqiTime, endTime, "hour", 1l, EventType.XUNZHEN.getNumber(), DeviceEnum.KUNEI.getNumber());
List<String> xAxis1 = (List<String>) kuneiT5.first;
List<Float> kuneiHzsppm = (List<Float>) kuneiT5.fourth;
TimeSeriesCollection kuneiHzsppmDataset = new TimeSeriesCollection();
TimeSeries kuneiHzsppmTimeSeries = new TimeSeries("hzppm");
//填充数据
for (int i = 0; i < xAxis1.size(); i++) {
DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime localDateTime = LocalDateTime.parse(xAxis1.get(i),df);
//LocalDateTime localDateTime = xAxis1.get(i);
int minute = localDateTime.getMinute();
int hour = localDateTime.getHour();
int day = localDateTime.getDayOfMonth();
int month = localDateTime.getMonth().getValue();
int year = localDateTime.getYear();
Minute x = new Minute(minute,hour,day,month,year);
Float y = kuneiHzsppm.get(i)!=null?kuneiHzsppm.get(i):null;
kuneiHzsppmTimeSeries.add(x,y);
}
kuneiHzsppmDataset.addSeries(kuneiHzsppmTimeSeries);
generateChartService.setKuneiHzsppm(kuneiHzsppmDataset);
generateChartService.createSanqiLineChart(title,chartPath,chartName);
}
}
生成图表
private void generateSanqiChart(Long segmentId, String title, String chartPath, String chartName, Date sanqi, Date end) {
Instant instantsanqi = sanqi.toInstant();
Instant instantend = end.toInstant();
ZoneId zoneId = ZoneId.systemDefault();
LocalDateTime sanqiTime = instantsanqi.atZone(zoneId).toLocalDateTime();
LocalDateTime endTime = instantend.atZone(zoneId).toLocalDateTime();
Tuple5 kuneiT5 = chartService.getTHMAxisDataById(segmentId, sanqiTime, endTime, "hour", 1l, EventType.XUNZHEN.getNumber(), DeviceEnum.KUNEI.getNumber());
List<String> xAxis1 = (List<String>) kuneiT5.first;
List<Float> kuneiHzsppm = (List<Float>) kuneiT5.fourth;
TimeSeriesCollection kuneiHzsppmDataset = new TimeSeriesCollection();
TimeSeries kuneiHzsppmTimeSeries = new TimeSeries("hzppm");
//填充数据
for (int i = 0; i < xAxis1.size(); i++) {
DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime localDateTime = LocalDateTime.parse(xAxis1.get(i),df);
//LocalDateTime localDateTime = xAxis1.get(i);
int minute = localDateTime.getMinute();
int hour = localDateTime.getHour();
int day = localDateTime.getDayOfMonth();
int month = localDateTime.getMonth().getValue();
int year = localDateTime.getYear();
Minute x = new Minute(minute,hour,day,month,year);
Float y = kuneiHzsppm.get(i)!=null?kuneiHzsppm.get(i):null;
kuneiHzsppmTimeSeries.add(x,y);
}
kuneiHzsppmDataset.addSeries(kuneiHzsppmTimeSeries);
generateChartService.setKuneiHzsppm(kuneiHzsppmDataset);
generateChartService.createSanqiLineChart(title,chartPath,chartName);
}
}
生成表格
cell = new PdfPCell(new Phrase("熏蒸作业报告",titleFont));//设置表格里面的给个小格子
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setBorderWidth(0);
cell.setPaddingBottom(20);
cell.setColspan(4);//占据八列
cell.setRowspan(2);
table.addCell(cell);
cell = new PdfPCell(middleCell("熏蒸仓库基础信息",font));
cell.setBackgroundColor(BaseColor.GRAY);
cell.setColspan(4);//占据八列
cell.setRowspan(2);
table.addCell(cell);
//向表格填充数据
table.addCell(new Paragraph("熏蒸仓库",font_2));
table.addCell(new Paragraph(xunzhenBaobiaoInfo.getWarehouseAndSegmentName(),font_2));
table.addCell(new Paragraph("熏蒸时间",font_2));
table.addCell(new Paragraph(xunzhenBaobiaoInfo.getXunzhenTimeRange(),font_2));
table.addCell(new Paragraph("隔断类型",font_2));
table.addCell(new Paragraph(xunzhenBaobiaoInfo.getSegmentInfo(),font_2));
table.addCell(new Paragraph("作业负责人",font_2));
table.addCell(new Paragraph(xunzhenBaobiaoInfo.getChargeName(),font_2));
cell = new PdfPCell(new Phrase("熏蒸作业信息",font));
cell.setColspan(4);//占据八列
cell.setBackgroundColor(BaseColor.GRAY);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
table.addCell(cell);
table.addCell(new Paragraph("投药量",font_2));
table.addCell(new Paragraph(xunzhenBaobiaoInfo.getWeight(),font_2));
table.addCell(new Paragraph("投药类型",font_2));
table.addCell(new Paragraph(xunzhenBaobiaoInfo.getHzsType(),font_2));
table.addCell(new Paragraph("浓度达到300ppm的时间",font_2));
table.addCell(new Paragraph(xunzhenBaobiaoInfo.getReach300ppmTimePoint(),font_2));
table.addCell(new Paragraph("浓度持续超过300ppm时间",font_2));
table.addCell(new Paragraph(xunzhenBaobiaoInfo.getRaach300ppmTimeDuration(),font_2));
table.addCell(middleCell("浓度最大值",font_2));
table.addCell(new Paragraph(xunzhenBaobiaoInfo.getMaxPPM(),font_2));
table.addCell(new Paragraph("浓度平均值(A)",font_2));
table.addCell(new Paragraph(xunzhenBaobiaoInfo.getAveragePPM(),font_2));
table.addCell(new Paragraph("A/B-1",font_2));
cell = new PdfPCell(new Paragraph(xunzhenBaobiaoInfo.getHint(),font_2));
cell.setColspan(3);
table.addCell(cell);
table.addCell(middleCell("散气时间",font_2));
table.addCell(new Paragraph(xunzhenBaobiaoInfo.getSanqiTime(),font_2));
table.addCell(new Paragraph("浓度降至0.3ppm时间",font_2));
table.addCell(new Paragraph(xunzhenBaobiaoInfo.getPpmReachSafeTime(),font_2));
cell = new PdfPCell(new Phrase("温湿度及磷化氢浓度监测图",font));
cell.setColspan(4);//占据八列
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setBackgroundColor(BaseColor.GRAY);
table.addCell(cell);
byte[] bt1 = FileUtils.readFileToByteArray(new File(xunzhenBaobiaoInfo.getChart1CompletePathname()));
Image image1 = Image.getInstance(bt1);//将图表传入
image1.setAlignment(Image.MIDDLE);
cell = new PdfPCell(image1);
image1.scalePercent(getPercent(image1.getHeight(),image1.getWidth())+3);
cell.setColspan(4);//占据八列
cell.setPaddingTop(20);
table.addCell(cell);//将图表传入表格
cell = new PdfPCell(new Phrase("散气监测图",font));
cell.setColspan(4);//占据八列
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setBackgroundColor(BaseColor.GRAY);
table.addCell(cell);
byte[] bt2 = FileUtils.readFileToByteArray(new File(xunzhenBaobiaoInfo.getChart2CompletePathname()));
Image image2 = Image.getInstance(bt2);
image2.setAlignment(Image.MIDDLE);
cell = new PdfPCell(image2);
image2.scalePercent(getPercent(image2.getHeight(),image2.getWidth())+3);
cell.setColspan(4);//占据八列
cell.setPaddingTop(20);
table.addCell(cell);