This commit is contained in:
caozehui
2025-03-20 13:44:16 +08:00
parent f294cb315f
commit ac0a9dd777
5 changed files with 148 additions and 86 deletions

View File

@@ -83,7 +83,7 @@ public class ReportController extends BaseController {
public HttpResult<PqReportVO> 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)

View File

@@ -30,7 +30,7 @@ public interface IPqReportService extends IService<PqReport> {
* @param id
* @return
*/
PqReportVO getById(String id);
PqReportVO getByReportId(String id);
/**
* 新增报告

View File

@@ -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<PqReportMapper, PqReport> 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<PqReportMapper, PqReport> 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<PqReportMapper, PqReport> 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<PqReportMapper, PqReport> i
System.out.println("报告生成成功!");
// 将改设备的报告生成状态调整为已生成
iPqDevService.updatePqDevReportState(devReportParam.getDevId(), DevReportStateEnum.GENERATED.getValue());
// 判断该计划下是否所有设备报告已生成,如果已生成则将计划的报告状态给为已生成
int count = iPqDevService.countUnReportDev(devReportParam.getPlanId());
LambdaUpdateWrapper<AdPlan> 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<AdPlan> 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());