From ac0a9dd7770ce0e8f71d2a3be01f8c8db5a003f7 Mon Sep 17 00:00:00 2001 From: caozehui <2427765068@qq.com> Date: Thu, 20 Mar 2025 13:44:16 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BE=AE=E8=B0=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../report/controller/ReportController.java | 2 +- .../report/service/IPqReportService.java | 2 +- .../service/impl/PqReportServiceImpl.java | 98 ++++++++++++--- system/pom.xml | 20 ++-- .../service/impl/SysLogAuditServiceImpl.java | 112 +++++++++--------- 5 files changed, 148 insertions(+), 86 deletions(-) diff --git a/detection/src/main/java/com/njcn/gather/report/controller/ReportController.java b/detection/src/main/java/com/njcn/gather/report/controller/ReportController.java index 73445c44..cd341ec5 100644 --- a/detection/src/main/java/com/njcn/gather/report/controller/ReportController.java +++ b/detection/src/main/java/com/njcn/gather/report/controller/ReportController.java @@ -83,7 +83,7 @@ public class ReportController extends BaseController { public HttpResult getById(@RequestParam("id") String id) { String methodDescribe = getMethodDescribe("getById"); LogUtil.njcnDebug(log, "{},查询参数为:{}", methodDescribe, id); - return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, pqReportService.getById(id), methodDescribe); + return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, pqReportService.getByReportId(id), methodDescribe); } @OperateInfo(operateType = OperateType.ADD) diff --git a/detection/src/main/java/com/njcn/gather/report/service/IPqReportService.java b/detection/src/main/java/com/njcn/gather/report/service/IPqReportService.java index 16b090ef..f12bf6ad 100644 --- a/detection/src/main/java/com/njcn/gather/report/service/IPqReportService.java +++ b/detection/src/main/java/com/njcn/gather/report/service/IPqReportService.java @@ -30,7 +30,7 @@ public interface IPqReportService extends IService { * @param id * @return */ - PqReportVO getById(String id); + PqReportVO getByReportId(String id); /** * 新增报告 diff --git a/detection/src/main/java/com/njcn/gather/report/service/impl/PqReportServiceImpl.java b/detection/src/main/java/com/njcn/gather/report/service/impl/PqReportServiceImpl.java index 04d01899..a86b8753 100644 --- a/detection/src/main/java/com/njcn/gather/report/service/impl/PqReportServiceImpl.java +++ b/detection/src/main/java/com/njcn/gather/report/service/impl/PqReportServiceImpl.java @@ -51,6 +51,7 @@ import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Value; import org.springframework.core.io.ClassPathResource; +import org.springframework.core.io.FileSystemResource; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.multipart.MultipartFile; @@ -131,7 +132,7 @@ public class PqReportServiceImpl extends ServiceImpl i } @Override - public PqReportVO getById(String id) { + public PqReportVO getByReportId(String id) { PqReport pqReport = this.baseMapper.selectById(id); PqReportVO pqReportVO = new PqReportVO(); BeanUtils.copyProperties(pqReport, pqReportVO); @@ -304,6 +305,62 @@ public class PqReportServiceImpl extends ServiceImpl i @Override public void generateReport(DevReportParam devReportParam) { + AdPlan plan = adPlanService.getById(devReportParam.getPlanId()); + if (ObjectUtil.isNotNull(plan.getReportTemplateId())) { + this.generateReportByPlan(plan, devReportParam); + } else { + // 根据设备类型找到报告模板 + PqDevVO pqDevVO = iPqDevService.getPqDevById(devReportParam.getDevId()); + if (Objects.isNull(pqDevVO)) { + throw new BusinessException("请检查装置是否存在!"); + } + // 获取设备型号 + DevType devType = devTypeService.getById(pqDevVO.getDevType()); + if (Objects.isNull(devType)) { + throw new BusinessException("设备类型缺失,请联系管理员!"); + } + DictData reportName = devTypeService.getReportName(pqDevVO.getDevType()); + if (Objects.isNull(reportName)) { + throw new BusinessException("报告模板缺失,请联系管理员!"); + } + // 读取模板文件 + ClassPathResource resource = new ClassPathResource("/model/" + reportName.getCode() + ".docx"); + try (InputStream inputStream = resource.getInputStream()) { + // 加载Word文档 + XWPFDocument baseModelDocument = new XWPFDocument(inputStream); + // 处理基础模版中的信息 + dealBaseModel(baseModelDocument, pqDevVO, devType); + // 处理数据页中的信息 + dealDataModel(baseModelDocument, devReportParam, pqDevVO); + // 处理需要输出的目录地址 基础路径+设备类型+装置编号.docx + // 最终文件输出的路径 + String dirPath = reportPath.concat(File.separator).concat(devType.getName()); + ensureDirectoryExists(dirPath); // 确保目录存在 + FileOutputStream out = new FileOutputStream(dirPath.concat(File.separator).concat(pqDevVO.getCreateId()).concat(".docx")); + // 4. 保存新的Word文档 + try { + baseModelDocument.write(out); + } catch (IOException e) { + throw new BusinessException("生成报告文件失败"); + } + out.close(); + + System.out.println("报告生成成功!"); + this.updateDevAndPlanState(devReportParam.getDevId(), devReportParam.getPlanId()); + } catch (IOException e) { + log.error("生成报告文件失败", e); + throw new RuntimeException(e); + } + } + } + + /** + * 根据计划绑定的报告模板生成报告 + * + * @param plan + * @param devReportParam + */ + private void generateReportByPlan(AdPlan plan, DevReportParam devReportParam) { // 根据设备类型找到报告模板 PqDevVO pqDevVO = iPqDevService.getPqDevById(devReportParam.getDevId()); if (Objects.isNull(pqDevVO)) { @@ -314,12 +371,12 @@ public class PqReportServiceImpl extends ServiceImpl i if (Objects.isNull(devType)) { throw new BusinessException("设备类型缺失,请联系管理员!"); } - DictData reportName = devTypeService.getReportName(pqDevVO.getDevType()); - if (Objects.isNull(reportName)) { + PqReport report = this.lambdaQuery().eq(PqReport::getId, plan.getAssociateReport()).eq(PqReport::getState, DataStateEnum.ENABLE.getCode()).one(); + if (Objects.isNull(report)) { throw new BusinessException("报告模板缺失,请联系管理员!"); } - // 读取模板文件 - ClassPathResource resource = new ClassPathResource("/model/" + reportName.getCode() + ".docx"); + + FileSystemResource resource = new FileSystemResource(report.getBasePath()); try (InputStream inputStream = resource.getInputStream()) { // 加载Word文档 XWPFDocument baseModelDocument = new XWPFDocument(inputStream); @@ -342,25 +399,30 @@ public class PqReportServiceImpl extends ServiceImpl i System.out.println("报告生成成功!"); - // 将改设备的报告生成状态调整为已生成 - iPqDevService.updatePqDevReportState(devReportParam.getDevId(), DevReportStateEnum.GENERATED.getValue()); - - // 判断该计划下是否所有设备报告已生成,如果已生成则将计划的报告状态给为已生成 - int count = iPqDevService.countUnReportDev(devReportParam.getPlanId()); - LambdaUpdateWrapper updateWrapper = new LambdaUpdateWrapper<>(); - updateWrapper.eq(AdPlan::getId, devReportParam.getPlanId()); - if (count == 0) { - updateWrapper.set(AdPlan::getReportState, PlanReportStateEnum.REPORT_STATE_ALL_GENERATED.getValue()); - } else { - updateWrapper.set(AdPlan::getReportState, PlanReportStateEnum.REPORT_STATE_PARTIALLY_GENERATED.getValue()); - } - adPlanService.update(updateWrapper); + this.updateDevAndPlanState(devReportParam.getDevId(), devReportParam.getPlanId()); } catch (IOException e) { log.error("生成报告文件失败", e); throw new RuntimeException(e); } } + private void updateDevAndPlanState(String devId, String planId) { + // 将改设备的报告生成状态调整为已生成 + iPqDevService.updatePqDevReportState(devId, DevReportStateEnum.GENERATED.getValue()); + + // 判断该计划下是否所有设备报告已生成,如果已生成则将计划的报告状态给为已生成 + int count = iPqDevService.countUnReportDev(planId); + LambdaUpdateWrapper updateWrapper = new LambdaUpdateWrapper<>(); + updateWrapper.eq(AdPlan::getId, planId); + if (count == 0) { + updateWrapper.set(AdPlan::getReportState, PlanReportStateEnum.REPORT_STATE_ALL_GENERATED.getValue()); + } else { + updateWrapper.set(AdPlan::getReportState, PlanReportStateEnum.REPORT_STATE_PARTIALLY_GENERATED.getValue()); + } + adPlanService.update(updateWrapper); + } + + @Override public void downloadReport(DevReportParam devReportParam, HttpServletResponse response) { PqDevVO pqDevVO = iPqDevService.getPqDevById(devReportParam.getDevId()); diff --git a/system/pom.xml b/system/pom.xml index 16b4739b..e51d540d 100644 --- a/system/pom.xml +++ b/system/pom.xml @@ -39,16 +39,16 @@ 1.2.83 - - org.apache.poi - poi-ooxml - 5.2.3 - - - org.apache.poi - poi-ooxml-full - 5.2.3 - + + + + + + + + + + diff --git a/system/src/main/java/com/njcn/gather/system/log/service/impl/SysLogAuditServiceImpl.java b/system/src/main/java/com/njcn/gather/system/log/service/impl/SysLogAuditServiceImpl.java index 7737b2ab..0bd204c8 100644 --- a/system/src/main/java/com/njcn/gather/system/log/service/impl/SysLogAuditServiceImpl.java +++ b/system/src/main/java/com/njcn/gather/system/log/service/impl/SysLogAuditServiceImpl.java @@ -165,7 +165,7 @@ public class SysLogAuditServiceImpl extends ServiceImpl categoryList, List sheetDataList) { - int row1 = 0; - int row2 = 8; - int col1 = 0; - int col2 = 30; - // 设置图表列宽 - for (int i = 0; i < row2; i++) { - sheet.setColumnWidth(i, 6000); - } - //y轴显示数据 - String[] headArray = categoryList.stream().collect(Collectors.toList()).toArray(new String[]{}); - - // Create a chart - XSSFDrawing drawing = sheet.createDrawingPatriarch(); - ClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, col1, row1, row2, col2); - XSSFChart chart = drawing.createChart(anchor); - //标题是否覆盖图表 - chart.setTitleOverlay(false); - //设置图表标题 - chart.setTitleText("分析结果"); - // 创建图表系列 - XDDFChartLegend legend = chart.getOrAddLegend(); - legend.setPosition(LegendPosition.TOP); - - //动态数据 - //x轴数据 - XDDFDataSource xData = XDDFDataSourcesFactory.fromArray(headArray); - //饼图数据 - XDDFPieChartData data = (XDDFPieChartData) chart.createData(ChartTypes.PIE, null, null); - data.setVaryColors(true); - - Double[] yArray = sheetDataList.stream().collect(Collectors.toList()).toArray(new Double[]{}); - //y轴数据 - XDDFNumericalDataSource yData = XDDFDataSourcesFactory.fromArray(yArray); - XDDFChartData.Series series = data.addSeries(xData, yData); - - //series.setTitle("title", null); - series.setShowLeaderLines(true); - // 隐藏图例标识、系列名称、分类名称和数值 - XDDFPieChartData.Series s = (XDDFPieChartData.Series) series; - CTPieSer ctPieSer = s.getCTPieSer(); - showCateName(ctPieSer, false); - showVal(ctPieSer, false); - showLegendKey(ctPieSer, false); - showSerName(ctPieSer, false); - - chart.plot(data); - } +// /** +// * 创建饼图 +// * +// * @param sheet +// * @param categoryList +// * @param sheetDataList +// */ +// private void createPieChart(XSSFSheet sheet, List categoryList, List sheetDataList) { +// int row1 = 0; +// int row2 = 8; +// int col1 = 0; +// int col2 = 30; +// // 设置图表列宽 +// for (int i = 0; i < row2; i++) { +// sheet.setColumnWidth(i, 6000); +// } +// //y轴显示数据 +// String[] headArray = categoryList.stream().collect(Collectors.toList()).toArray(new String[]{}); +// +// // Create a chart +// XSSFDrawing drawing = sheet.createDrawingPatriarch(); +// ClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, col1, row1, row2, col2); +// XSSFChart chart = drawing.createChart(anchor); +// //标题是否覆盖图表 +// chart.setTitleOverlay(false); +// //设置图表标题 +// chart.setTitleText("分析结果"); +// // 创建图表系列 +// XDDFChartLegend legend = chart.getOrAddLegend(); +// legend.setPosition(LegendPosition.TOP); +// +// //动态数据 +// //x轴数据 +// XDDFDataSource xData = XDDFDataSourcesFactory.fromArray(headArray); +// //饼图数据 +// XDDFPieChartData data = (XDDFPieChartData) chart.createData(ChartTypes.PIE, null, null); +// data.setVaryColors(true); +// +// Double[] yArray = sheetDataList.stream().collect(Collectors.toList()).toArray(new Double[]{}); +// //y轴数据 +// XDDFNumericalDataSource yData = XDDFDataSourcesFactory.fromArray(yArray); +// XDDFChartData.Series series = data.addSeries(xData, yData); +// +// //series.setTitle("title", null); +// series.setShowLeaderLines(true); +// // 隐藏图例标识、系列名称、分类名称和数值 +// XDDFPieChartData.Series s = (XDDFPieChartData.Series) series; +// CTPieSer ctPieSer = s.getCTPieSer(); +// showCateName(ctPieSer, false); +// showVal(ctPieSer, false); +// showLegendKey(ctPieSer, false); +// showSerName(ctPieSer, false); +// +// chart.plot(data); +// } // 控制值系列名称是否显示 private void showSerName(CTPieSer series, boolean val) {