报表调整
This commit is contained in:
@@ -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<Object> 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);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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<AdPlan> 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<String, String> 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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user