From 4f657669adb879c4eeabfea9a67f92bc0bc76198 Mon Sep 17 00:00:00 2001 From: hongawen <83944980@qq.com> Date: Wed, 15 Jan 2025 18:28:06 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8A=A5=E8=A1=A8=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../report/controller/ReportController.java | 24 +++- .../gather/report/service/IReportService.java | 4 +- .../service/impl/ReportServiceImpl.java | 132 +++++++++++++----- device/pom.xml | 6 + .../device/device/mapper/PqDevMapper.java | 2 + .../device/mapper/mapping/PqDevMapper.xml | 4 + .../device/service/impl/PqDevServiceImpl.java | 3 + entrance/src/main/resources/application.yml | 4 + 8 files changed, 139 insertions(+), 40 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 fe7ac1a7..9e33f27e 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 @@ -1,10 +1,13 @@ package com.njcn.gather.report.controller; import com.njcn.common.pojo.annotation.OperateInfo; +import com.njcn.common.pojo.enums.response.CommonResponseEnum; +import com.njcn.common.pojo.response.HttpResult; import com.njcn.common.utils.LogUtil; import com.njcn.gather.report.pojo.DevReportParam; import com.njcn.gather.report.service.IReportService; import com.njcn.web.controller.BaseController; +import com.njcn.web.utils.HttpResultUtil; import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiOperation; @@ -38,11 +41,26 @@ public class ReportController extends BaseController { @PostMapping("/generateReport") @ApiOperation("生成测试报告") @ApiImplicitParam(name = "param", value = "查询参数", required = true) - public void generateReport(@RequestBody DevReportParam devReportParam, HttpServletResponse response) { - String methodDescribe = getMethodDescribe("list"); + public HttpResult generateReport(@RequestBody DevReportParam devReportParam) { + String methodDescribe = getMethodDescribe("generateReport"); LogUtil.njcnDebug(log, "{},终端参数为:{}", methodDescribe, devReportParam); - reportService.generateReport(devReportParam,response); + reportService.generateReport(devReportParam); + return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe); + } + + /** + * 下载报告 + */ + @OperateInfo + @PostMapping("/downloadReport") + @ApiOperation("下载测试报告") + @ApiImplicitParam(name = "param", value = "查询参数", required = true) + public void downloadReport(@RequestBody DevReportParam devReportParam, HttpServletResponse response) { + String methodDescribe = getMethodDescribe("downloadReport"); + LogUtil.njcnDebug(log, "{},终端参数为:{}", methodDescribe, devReportParam); + reportService.downloadReport(devReportParam,response); } + } diff --git a/detection/src/main/java/com/njcn/gather/report/service/IReportService.java b/detection/src/main/java/com/njcn/gather/report/service/IReportService.java index 233ca495..f8515a45 100644 --- a/detection/src/main/java/com/njcn/gather/report/service/IReportService.java +++ b/detection/src/main/java/com/njcn/gather/report/service/IReportService.java @@ -10,5 +10,7 @@ import javax.servlet.http.HttpServletResponse; * @data 2025/1/9 20:59 */ public interface IReportService { - void generateReport(DevReportParam devReportParam, HttpServletResponse response); + void generateReport(DevReportParam devReportParam); + + void downloadReport(DevReportParam devReportParam, HttpServletResponse response); } diff --git a/detection/src/main/java/com/njcn/gather/report/service/impl/ReportServiceImpl.java b/detection/src/main/java/com/njcn/gather/report/service/impl/ReportServiceImpl.java index adf48c02..a576dd45 100644 --- a/detection/src/main/java/com/njcn/gather/report/service/impl/ReportServiceImpl.java +++ b/detection/src/main/java/com/njcn/gather/report/service/impl/ReportServiceImpl.java @@ -12,7 +12,6 @@ import com.njcn.gather.detection.pojo.vo.DetectionData; import com.njcn.gather.device.device.pojo.vo.PqDevVO; import com.njcn.gather.device.device.service.IPqDevService; import com.njcn.gather.device.pojo.enums.DevReportStateEnum; -import com.njcn.gather.device.script.pojo.po.PqScript; import com.njcn.gather.device.script.pojo.po.PqScriptCheckData; import com.njcn.gather.device.script.pojo.po.PqScriptDtls; import com.njcn.gather.device.script.service.IPqScriptCheckDataService; @@ -29,14 +28,12 @@ import com.njcn.gather.storage.pojo.po.AdHarmonicResult; import com.njcn.gather.storage.pojo.po.AdNonHarmonicResult; import com.njcn.gather.storage.service.AdHarmonicService; import com.njcn.gather.storage.service.AdNonHarmonicService; -import com.njcn.gather.system.dictionary.pojo.po.DictData; import com.njcn.gather.system.dictionary.service.IDictDataService; -import io.swagger.models.auth.In; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.apache.poi.xwpf.usermodel.XWPFDocument; +import org.springframework.beans.factory.annotation.Value; import org.springframework.core.io.ClassPathResource; -import org.springframework.core.io.Resource; import org.springframework.stereotype.Service; import javax.servlet.http.HttpServletResponse; @@ -55,6 +52,12 @@ import java.util.*; @RequiredArgsConstructor public class ReportServiceImpl implements IReportService { + @Value("${report.template:D:\\template\\}") + private String templatePath; + + @Value("${report.reportDir:D:\\report}") + private String reportPath; + private final IPqDevService iPqDevService; private final IDictDataService dictDataService; @@ -73,40 +76,44 @@ public class ReportServiceImpl implements IReportService { @Override - public void generateReport(DevReportParam devReportParam, HttpServletResponse response) { + public void generateReport(DevReportParam devReportParam) { // 读取模板文件 - Resource baseModel = new ClassPathResource("model/BaseModel.docx"); - try (FileInputStream baseModelFis = new FileInputStream(baseModel.getFile())) { + ClassPathResource resource = new ClassPathResource("/model/BaseModel.docx"); + try (InputStream inputStream = resource.getInputStream()) { // 加载Word文档 - XWPFDocument baseModelDocument = new XWPFDocument(baseModelFis); - + XWPFDocument baseModelDocument = new XWPFDocument(inputStream); + 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("设备类型丢失,请联系管理员!"); + } // 处理基础模版中的信息 - dealBaseModel(baseModelDocument, devReportParam); + dealBaseModel(baseModelDocument, pqDevVO); // 处理数据页中的信息 - dealDataModel(baseModelDocument, devReportParam); -// //最终文件输出的路径 -// FileOutputStream out = new FileOutputStream("C:\\Users\\hongawen\\Desktop\\testModel\\BaseDataModel" + DateUtil.format(new Date(), DatePattern.CHINESE_DATE_TIME_PATTERN) + ".docx"); -// // 4. 保存新的Word文档 -// try { -// baseModelDocument.write(out); -// } catch (IOException e) { -// throw new BusinessException("生成报告文件失败"); -// } -// out.close(); + 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(); - // 设置响应头 - response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document"); - String fileName = "调试报告" + DateUtil.format(new Date(), DatePattern.CHINESE_DATE_TIME_PATTERN) + ".docx"; - response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\""); - // 保存新的Word文档到响应输出流 - baseModelDocument.write(response.getOutputStream()); - response.getOutputStream().flush(); System.out.println("报告生成成功!"); // 将改设备的报告生成状态调整为已生成 iPqDevService.updatePqDevReportState(devReportParam.getDevId(), 1); - // 判断该计划下是否所有设备报告已生成,如果已生成则将计划状态改为已完成 + // 判断该计划下是否所有设备报告已生成,如果已生成则将计划的报告状态给为已生成 int count = iPqDevService.countUnReportDev(devReportParam.getPlanId()); if (count == 0) { LambdaUpdateWrapper updateWrapper = new LambdaUpdateWrapper<>(); @@ -116,21 +123,63 @@ public class ReportServiceImpl implements IReportService { } } catch (Exception e) { + log.error("生成报告文件失败", e); throw new RuntimeException(e); } } + @Override + public void downloadReport(DevReportParam devReportParam, HttpServletResponse response) { + 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("设备类型丢失,请联系管理员!"); + } + // 找到该文件的路径 + String filePath = reportPath.concat(File.separator).concat(devType.getName()).concat(File.separator).concat(pqDevVO.getCreateId()).concat(".docx"); + File reportFile = new File(filePath); + if (!reportFile.exists()) { + // 如果文件不存在,则将改设备的报告生成状态调整为未生成 + iPqDevService.updatePqDevReportState(devReportParam.getDevId(), 0); + throw new BusinessException("报告文件不存在,请重新生成报告!"); + } else { + try (FileInputStream fis = new FileInputStream(reportFile); + OutputStream os = response.getOutputStream()) { + + // 设置响应头 + response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document"); + String fileName = pqDevVO.getCreateId() + ".docx"; + response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\""); + + // 将文件内容写入响应输出流 + byte[] buffer = new byte[1024]; + int bytesRead; + while ((bytesRead = fis.read(buffer)) != -1) { + os.write(buffer, 0, bytesRead); + } + // 刷新输出流 + os.flush(); + } catch (IOException e) { + e.printStackTrace(); + response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); + } + } + + } + /** * 处理基础模版中的信息,非数据页报告 * * @param baseModelDocument 模板文件 - * @param devReportParam 被检设备参数 */ - private void dealBaseModel(XWPFDocument baseModelDocument, DevReportParam devReportParam) { + private void dealBaseModel(XWPFDocument baseModelDocument, PqDevVO pqDevVO) { // 首先获取非数据页中需要的信息 Map baseModelMap = new HashMap<>(16); - PqDevVO pqDevVO = iPqDevService.getPqDevById(devReportParam.getDevId()); // 获取设备型号 DevType devType = devTypeService.getById(pqDevVO.getDevType()); baseModelMap.put("${devType}", devType.getName()); @@ -170,15 +219,13 @@ public class ReportServiceImpl implements IReportService { * @param baseModelDocument 非数据页的内容 * @param devReportParam 查询参数 */ - private void dealDataModel(XWPFDocument baseModelDocument, DevReportParam devReportParam) throws IOException { - PqDevVO pqDevVO = iPqDevService.getPqDevById(devReportParam.getDevId()); + private void dealDataModel(XWPFDocument baseModelDocument, DevReportParam devReportParam, PqDevVO pqDevVO) throws IOException { AdPlan adPlan = adPlanService.getById(devReportParam.getPlanId()); String scriptId = adPlan.getScriptId(); Integer devChns = pqDevVO.getDevChns(); for (int i = 1; i <= devChns; i++) { - Resource dataModel = new ClassPathResource("model/BaseDataModel.docx"); - FileInputStream dataModelFis = new FileInputStream(dataModel.getFile()); - XWPFDocument dataModelDocumentTemp = new XWPFDocument(dataModelFis); + ClassPathResource resource = new ClassPathResource("/model/BaseDataModel.docx"); + XWPFDocument dataModelDocumentTemp = new XWPFDocument(resource.getInputStream()); SingleNonHarmParam singleNonHarmParam = new SingleNonHarmParam(); singleNonHarmParam.setPlanCode(adPlan.getCode()); @@ -492,4 +539,17 @@ public class ReportServiceImpl implements IReportService { } + private void ensureDirectoryExists(String directoryPath) { + File directory = new File(directoryPath); + if (!directory.exists()) { + boolean created = directory.mkdirs(); + if (!created) { + throw new BusinessException("目录创建失败: " + directoryPath); + } + } else { + System.out.println("目录已存在: " + directoryPath); + } + } + + } diff --git a/device/pom.xml b/device/pom.xml index eeaf245c..3fc98b80 100644 --- a/device/pom.xml +++ b/device/pom.xml @@ -34,6 +34,12 @@ 1.0.0 compile + + com.njcn.gather + detection + 1.0.0 + compile + com.alibaba fastjson diff --git a/device/src/main/java/com/njcn/gather/device/device/mapper/PqDevMapper.java b/device/src/main/java/com/njcn/gather/device/device/mapper/PqDevMapper.java index 5b912b1e..762381dc 100644 --- a/device/src/main/java/com/njcn/gather/device/device/mapper/PqDevMapper.java +++ b/device/src/main/java/com/njcn/gather/device/device/mapper/PqDevMapper.java @@ -23,5 +23,7 @@ public interface PqDevMapper extends MPJBaseMapper { List selectDevInfo(@Param("devIds") List devIds); void finishPlan(@Param("planId")String planId); + + void updateReportState(@Param("id")String id); } diff --git a/device/src/main/java/com/njcn/gather/device/device/mapper/mapping/PqDevMapper.xml b/device/src/main/java/com/njcn/gather/device/device/mapper/mapping/PqDevMapper.xml index 8f271994..a08c1253 100644 --- a/device/src/main/java/com/njcn/gather/device/device/mapper/mapping/PqDevMapper.xml +++ b/device/src/main/java/com/njcn/gather/device/device/mapper/mapping/PqDevMapper.xml @@ -52,5 +52,9 @@ update ad_plan set Test_State = 2 where id = #{planId} + + + update pq_dev set Report_State = 1 where id = #{id} + diff --git a/device/src/main/java/com/njcn/gather/device/device/service/impl/PqDevServiceImpl.java b/device/src/main/java/com/njcn/gather/device/device/service/impl/PqDevServiceImpl.java index e5e12199..5f85fff5 100644 --- a/device/src/main/java/com/njcn/gather/device/device/service/impl/PqDevServiceImpl.java +++ b/device/src/main/java/com/njcn/gather/device/device/service/impl/PqDevServiceImpl.java @@ -501,6 +501,9 @@ public class PqDevServiceImpl extends ServiceImpl implements if (result.containsKey(pqDev.getId())) { Integer checkState; if (pqDev.getReCheckNum() >= config.getMaxTime()) { + // 装置报告生成 todo... + this.baseMapper.updateReportState(pqDev.getId()); + // 装置归档 checkState = CheckStateEnum.DOCUMENTED.getValue(); }else{ checkState = CheckStateEnum.CHECKED.getValue(); diff --git a/entrance/src/main/resources/application.yml b/entrance/src/main/resources/application.yml index 7d08e660..f0ed6e27 100644 --- a/entrance/src/main/resources/application.yml +++ b/entrance/src/main/resources/application.yml @@ -74,3 +74,7 @@ Flicker: log: homeDir: D:\logs commonLevel: info +report: + template: D:\\template + reportDir: D:\\report +