晏有为
2024-05-23 10de56ca16261e6a4eec49dfd32f0a6b232f7baf
inspect-server/src/main/java/com/yuanchu/mom/service/impl/InsReportServiceImpl.java
@@ -1,31 +1,34 @@
package com.yuanchu.mom.service.impl;
import com.aspose.words.License;
import com.aspose.words.SaveFormat;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.data.Pictures;
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.yuanchu.mom.common.GetLook;
import com.yuanchu.mom.common.PrintChina;
import com.yuanchu.mom.dto.ReportPageDto;
import com.yuanchu.mom.exception.ErrorException;
import com.yuanchu.mom.mapper.InsOrderMapper;
import com.yuanchu.mom.mapper.InsReportMapper;
import com.yuanchu.mom.mapper.UserMapper;
import com.yuanchu.mom.pojo.InsOrder;
import com.yuanchu.mom.pojo.InsReport;
import com.yuanchu.mom.service.InsReportService;
import com.yuanchu.mom.mapper.InsReportMapper;
import com.yuanchu.mom.utils.QueryWrappers;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.time.LocalDateTime;
@@ -71,18 +74,6 @@
    }
    @Override
    public String wordToHtml(String path) {
        try(ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
            Document document = new Document();
            document.loadFromFile(path.replace("/word", wordUrl));
            document.saveToFile(outputStream, FileFormat.Html);
            return outputStream.toString();
        } catch (Exception e) {
            throw new ErrorException("转换失败");
        }
    }
    @Override
    public int inReport(String url, Integer id) {
        InsReport insReport = new InsReport();
        insReport.setId(id);
@@ -104,7 +95,12 @@
        insReport.setWriteUserId(getLook.selectPowerByMethodAndUserId(null).get("userId"));//提交人
        insReport.setWriteTime(LocalDateTime.now());//提交时间
        //获取提交人的签名地址
        String signatureUrl = userMapper.selectById(insReport.getWriteUserId()).getSignatureUrl();
        String signatureUrl;
        try {
            signatureUrl = userMapper.selectById(insReport.getWriteUserId()).getSignatureUrl();
        }catch (Exception e){
            throw new ErrorException("找不到编制人的签名");
        }
        //系统生成报告地址
        String url = insReport.getUrl();
        //手动上传报告地址
@@ -131,7 +127,12 @@
            return insReportMapper.updateById(insReport);
        }
        //获取审核人的签名地址
        String signatureUrl = userMapper.selectById(insReport.getExamineUserId()).getSignatureUrl();
        String signatureUrl;
        try {
            signatureUrl = userMapper.selectById(insReport.getExamineUserId()).getSignatureUrl();
        }catch (Exception e){
            throw new ErrorException("找不到审核人的签名");
        }
        //系统生成报告地址
        String url = insReport.getUrl();
        //手动上传报告地址
@@ -158,14 +159,29 @@
            insReport.setState(0);//提交状态改为待提交
            return insReportMapper.updateById(insReport);
        }
        //获取审核人的签名地址
        String signatureUrl = userMapper.selectById(insReport.getRatifyUserId()).getSignatureUrl();
        //获取批准人的签名地址
        String signatureUrl;
        try {
            signatureUrl = userMapper.selectById(insReport.getRatifyUserId()).getSignatureUrl();
        }catch (Exception e){
            throw new ErrorException("找不到批准人的签名");
        }
        String sealUrl;
        try {
            String laboratory = insOrderMapper.selectById(insReport.getInsOrderId()).getLaboratory();
            sealUrl = insReportMapper.getLaboratoryByName(laboratory);
        }catch (Exception e){
            throw new ErrorException("找不到报告专用章");
        }
        if(sealUrl==null) throw new ErrorException("找不到报告专用章");
        //系统生成报告地址
        String url = insReport.getUrl();
        //手动上传报告地址
        String urlS = insReport.getUrlS();
        wordInsertUrl(new HashMap<String, Object>(){{
            put("ratifyUrl", Pictures.ofLocal(imgUrl+"/"+signatureUrl).create());
            put("seal1", Pictures.ofLocal(imgUrl+"/"+sealUrl).create());
            put("seal2", Pictures.ofLocal(imgUrl+"/"+sealUrl).create());
        }}, (urlS==null?url:urlS).replace("/word", wordUrl));
        wordToPdf((urlS == null ? url : urlS).replace("/word", wordUrl));
        InsOrder insOrder = new InsOrder();
@@ -189,11 +205,8 @@
    @Override
    public void wordToPdf(String path) {
        CompletableFuture.supplyAsync(() -> {
            try(ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
                Document document = new Document();
                document.loadFromFile(path);
                document.saveToFile(path.replace(".docx", ".pdf"), FileFormat.PDF);
                System.out.println(path.replace(".docx", ".pdf"));
            try {
                wordToPdf(path, path.replace(".docx", ".pdf"));
                return null;
            } catch (Exception e) {
                throw new ErrorException("转换失败");
@@ -204,6 +217,38 @@
            return null;
        });
    }
    public String wordToPdf(String wordPath,String pdfPath) {
        FileOutputStream os = null;
        try {
            //凭证 不然切换后有水印
//            InputStream inputStream = this.getClass().getResourceAsStream("/lib/license.xml");
            InputStream is = new ClassPathResource("/lib/license.xml").getInputStream();
            License license = new License();
            license.setLicense(is);
            if (!license.getIsLicensed()) {
                System.out.println("License验证不通过...");
                return null;
            }
            //生成一个空的PDF文件
            File file = new File(pdfPath);
            os = new FileOutputStream(file);
            //要转换的word文件
            com.aspose.words.Document doc = new com.aspose.words.Document(wordPath);
            doc.save(os, SaveFormat.PDF);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }
}