|
@@ -0,0 +1,145 @@
|
|
|
+package com.rongwei.bscommon.sys.service.impl;
|
|
|
+
|
|
|
+import com.aspose.cells.PdfCompliance;
|
|
|
+import com.aspose.cells.PdfSaveOptions;
|
|
|
+import com.aspose.words.Document;
|
|
|
+import com.aspose.words.SaveOutputParameters;
|
|
|
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.rongwei.bscommon.sys.dao.QhseTargetResponsibilityDocumentDao;
|
|
|
+import com.rongwei.bscommon.sys.service.QhseTargetResponsibilityDocumentService;
|
|
|
+import com.rongwei.bscommon.sys.utils.WordMergeHandler;
|
|
|
+import com.rongwei.bscommon.sys.utils.WordUtils;
|
|
|
+import com.rongwei.bsentity.domain.QhseTargetResponsibilityDocumentDo;
|
|
|
+import com.rongwei.bsentity.domain.QhseTargetResponsibilityDocumentSignatureDo;
|
|
|
+import com.rongwei.rwcommon.base.exception.CustomException;
|
|
|
+import com.rongwei.rwcommon.utils.StringUtils;
|
|
|
+import com.rongwei.rwcommonentity.commonservers.domain.SysFileItemDo;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.io.OutputStream;
|
|
|
+import java.net.URLEncoder;
|
|
|
+import java.nio.file.Files;
|
|
|
+import java.nio.file.Paths;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+import static com.rongwei.bscommon.sys.utils.QHSEConstant.FILE_SEPARATOR;
|
|
|
+import static com.rongwei.bscommon.sys.utils.QHSEConstant.FileType.DOC;
|
|
|
+import static com.rongwei.bscommon.sys.utils.QHSEConstant.FileType.DOCX;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author libai
|
|
|
+ * @description 针对表【qhse_target_responsibility_document(目标责任书)】的数据库操作Service实现
|
|
|
+ * @createDate 2025-08-04 15:50:27
|
|
|
+ */
|
|
|
+@Service
|
|
|
+public class QhseTargetResponsibilityDocumentServiceImpl extends ServiceImpl<QhseTargetResponsibilityDocumentDao, QhseTargetResponsibilityDocumentDo>
|
|
|
+ implements QhseTargetResponsibilityDocumentService {
|
|
|
+ @Autowired
|
|
|
+ private QhseTargetResponsibilityDocumentSignatureServiceImpl qhseTargetResponsibilityDocumentSignatureService;
|
|
|
+ @Autowired
|
|
|
+ private FileFormatConversionServiceImpl fileFormatConversionService;
|
|
|
+
|
|
|
+
|
|
|
+ private static final Logger log = LoggerFactory.getLogger(QhseTargetResponsibilityDocumentServiceImpl.class);
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void lettersOfResponsibilitySignaturePreview(Map<String, String> mapData, HttpServletResponse response) {
|
|
|
+ log.debug("开始生成责任书签名后文件");
|
|
|
+ String mainId = mapData.getOrDefault("mainId", "");
|
|
|
+ String subTableId = mapData.getOrDefault("subTableId", "");
|
|
|
+ QhseTargetResponsibilityDocumentDo qhseTargetResponsibilityDocumentDo = this.getById(mainId);
|
|
|
+ if (qhseTargetResponsibilityDocumentDo == null) {
|
|
|
+ log.error("无法获取到责任书信息");
|
|
|
+ throw new CustomException("无法获取到责任书信息");
|
|
|
+ }
|
|
|
+ String responsibilityfiles = qhseTargetResponsibilityDocumentDo.getResponsibilityfiles();
|
|
|
+ if (StringUtils.isBlank(responsibilityfiles)) {
|
|
|
+ throw new CustomException("未上传责任书附件");
|
|
|
+ }
|
|
|
+ String fileId = responsibilityfiles.split(FILE_SEPARATOR)[1];
|
|
|
+
|
|
|
+ QhseTargetResponsibilityDocumentSignatureDo documentSignatureDo = qhseTargetResponsibilityDocumentSignatureService.getById(subTableId);
|
|
|
+ // 没有签名直接预览
|
|
|
+ if (documentSignatureDo == null || StringUtils.isBlank(documentSignatureDo.getSignature())) {
|
|
|
+ log.error("无法获取到签名信息");
|
|
|
+ fileFormatConversionService.fileFormatConversion(fileId, response);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ // 甲方或者乙方
|
|
|
+ String signaturesource = documentSignatureDo.getSignaturesource();
|
|
|
+ // 签名
|
|
|
+ String signatureInfo = documentSignatureDo.getSignature();
|
|
|
+ // Id
|
|
|
+ String signatureId = signatureInfo.split(FILE_SEPARATOR)[1];
|
|
|
+ SysFileItemDo tempDo = fileFormatConversionService.fileCheck(fileId);
|
|
|
+ if (tempDo == null) {
|
|
|
+ log.error("无法根据id:{}获取到责任书文件信息", fileId);
|
|
|
+ throw new RuntimeException("无法获取到责任书");
|
|
|
+ }
|
|
|
+ // 模板路径
|
|
|
+ String tempFullpath = tempDo.getFullpath();
|
|
|
+ if (StringUtils.isBlank(tempFullpath)) {
|
|
|
+ log.error("无法根据id:{}获取到责任书文件信息", fileId);
|
|
|
+ throw new RuntimeException("无法获取到责任书");
|
|
|
+ }
|
|
|
+ SysFileItemDo signatureDo = fileFormatConversionService.fileCheck(signatureId);
|
|
|
+ if (signatureDo == null) {
|
|
|
+ log.error("无法根据id:{}获取到签名信息", signatureId);
|
|
|
+ throw new RuntimeException("无法获取到签名");
|
|
|
+ }
|
|
|
+ // 乙方签名路径
|
|
|
+ String signaturePath = signatureDo.getFullpath();
|
|
|
+ if (StringUtils.isBlank(signaturePath)) {
|
|
|
+ log.error("无法根据id:{}获取到签名信息", signatureId);
|
|
|
+ throw new RuntimeException("无法获取到签名");
|
|
|
+ }
|
|
|
+
|
|
|
+ String filetype = tempDo.getFiletype();
|
|
|
+ if (!DOCX.equals(filetype) && !DOC.equals(filetype)) {
|
|
|
+ log.error("责任书格式:{}不正确", filetype);
|
|
|
+ throw new CustomException("责任书格式不正确");
|
|
|
+ }
|
|
|
+
|
|
|
+ try (InputStream templateStream = Files.newInputStream(Paths.get(tempFullpath));
|
|
|
+ OutputStream out = response.getOutputStream();) {
|
|
|
+ Document doc = new Document(templateStream);
|
|
|
+ // 设置邮件合并回调
|
|
|
+ doc.getMailMerge().setFieldMergingCallback(new WordMergeHandler());
|
|
|
+ HashMap<String, String> objectObjectHashMap = new HashMap<>();
|
|
|
+ objectObjectHashMap.put("YFQM", signaturePath);
|
|
|
+ objectObjectHashMap.put("ZRR", "责任人");
|
|
|
+ WordUtils.MailMergeFill(objectObjectHashMap, doc);
|
|
|
+
|
|
|
+ String fileName = null;
|
|
|
+ try {
|
|
|
+ fileName = URLEncoder.encode(tempDo.getFilename(), "utf-8");
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ response.setContentType("application/pdf");
|
|
|
+ response.setHeader("Content-Disposition", "inline; filename=" + fileName);
|
|
|
+ response.setHeader("X-Content-Length", String.valueOf(tempDo.getFilesize()));
|
|
|
+
|
|
|
+ // 设置 PDF 转换选项
|
|
|
+ PdfSaveOptions options = new PdfSaveOptions();
|
|
|
+ options.setCompliance(PdfCompliance.PDF_A_1_A); // 设置PDF/A-1a标准
|
|
|
+ // 3. 直接输出到响应流(避免中间文件)
|
|
|
+ SaveOutputParameters save = doc.save(out, com.aspose.words.SaveFormat.PDF);
|
|
|
+ // 4. 强制刷新缓冲区(确保数据完整传输)
|
|
|
+ out.flush();
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("excel转pdf失败- 原因: {} | 异常类型: {}", e.getMessage(), e.getClass().getSimpleName(), e);
|
|
|
+ throw new RuntimeException("获取签名责任书失败!请联系系统管理员");
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|