inspect-server/src/main/java/com/ruoyi/inspect/service/impl/InsReportServiceImpl.java
@@ -6,6 +6,7 @@
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;
import com.aspose.words.FontSettings;
import com.aspose.words.License;
import com.aspose.words.SaveFormat;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
@@ -17,6 +18,9 @@
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.config.Configure;
import com.deepoove.poi.data.PictureRenderData;
import com.deepoove.poi.data.PictureType;
import com.deepoove.poi.data.Pictures;
import com.itextpdf.text.BadElementException;
import com.itextpdf.text.DocumentException;
@@ -24,7 +28,9 @@
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import com.ruoyi.basic.mapper.IfsInventoryQuantityMapper;
import com.ruoyi.basic.mapper.SealConfigMapper;
import com.ruoyi.basic.pojo.IfsInventoryQuantity;
import com.ruoyi.basic.pojo.SealConfig;
import com.ruoyi.basic.vo.IfsInventoryQuantityVO;
import com.ruoyi.common.config.WechatProperty;
import com.ruoyi.common.constant.InsOrderTypeConstants;
@@ -44,6 +50,9 @@
import com.ruoyi.inspect.service.InsOrderService;
import com.ruoyi.inspect.service.InsReportService;
import com.ruoyi.inspect.service.RawMaterialOrderService;
import com.ruoyi.inspect.util.PreserveReportPicturePlaceholderHandler;
import com.ruoyi.inspect.util.ReportSealLayoutUtil;
import com.ruoyi.inspect.util.ReportSignaturePictureUtils;
import com.ruoyi.system.mapper.UserMapper;
import com.ruoyi.system.service.InformationNotificationService;
import lombok.extern.slf4j.Slf4j;
@@ -84,6 +93,15 @@
@Slf4j
public class InsReportServiceImpl extends ServiceImpl<InsReportMapper, InsReport>
        implements InsReportService {
    static {
        // Linux 服务器缺少中文字体,报告转 PDF 时中文会变成方块;将模板字体映射到 Noto CJK 兜底。
        FontSettings.addFontSubstitutes("宋体", "Noto Serif CJK SC");
        FontSettings.addFontSubstitutes("黑体", "Noto Sans CJK SC");
    }
    private static final int DETECTION_REPORT = 0;
    private static final int INSPECTION_REPORT = 1;
    @Resource
    private UserMapper userMapper;
    @Resource
@@ -103,6 +121,8 @@
    private InsOrderMapper insOrderMapper;
    @Resource
    private IfsInventoryQuantityMapper ifsInventoryQuantityMapper;
    @Resource
    private SealConfigMapper sealConfigMapper;
    @Resource
    private InsUnqualifiedHandlerMapper insUnqualifiedHandlerMapper;
    @Resource
@@ -207,30 +227,30 @@
        info.setViewStatus(false);
        info.setJumpPath(MenuJumpPathConstants.REPORT_PREPARATION);
        informationNotificationService.addInformationNotification(info);
        //系统生成报告地址
        String url = insReport.getUrl();
        //手动上传报告地址
        String urlS = insReport.getUrlS();
        String primaryReportUrl = getPrimaryReportUrl(insReport);
        // 判断是否是原材料  需要替换****成供应商
        IfsInventoryQuantity one = ifsInventoryQuantityMapper.selectOne(new LambdaQueryWrapper<IfsInventoryQuantity>()
                .eq(IfsInventoryQuantity::getId, order.getIfsInventoryId()));
        if (one != null) {
            if (isRawMater && order.getOrderType().equals(InsOrderTypeConstants.ENTER_THE_FACTORY)) {
                changeText(new HashMap<String, String>() {{
                    put("**********", one.getSupplierName());
                }}, (StrUtil.isBlank(urlS) ? url : urlS).replace("/word", wordUrl));
                for (String reportUrl : getApprovalReportUrls(insReport)) {
                    changeText(new HashMap<String, String>() {{
                        put("**********", one.getSupplierName());
                    }}, reportUrl.replace("/word", wordUrl));
                }
            }
        }
        wordInsertUrl(new HashMap<String, Object>() {{
            put("writeUrl", Pictures.ofLocal(imgUrl + "/" + signatureUrl).create());
        Map<String, Object> writeMarks = new HashMap<String, Object>() {{
            put("writeUrl", ReportSignaturePictureUtils.create(imgUrl, signatureUrl));
            put("writeDateUrl", Pictures.ofStream(DateImageUtil.createDateImage(null)).create());
            put("insUrl", Pictures.ofLocal(imgUrl + "/" + signatureUrl).create());
        }}, (StrUtil.isBlank(urlS) ? url : urlS).replace("/word", wordUrl));
            put("insUrl", ReportSignaturePictureUtils.create(imgUrl, signatureUrl));
        }};
        writeMarksToReportFiles(insReport, writeMarks);
        // 修改临时pdf
        String tempUrlPdf = wordToPdfTemp((StrUtil.isBlank(urlS) ? url : urlS).replace("/word", wordUrl));
        String tempUrlPdf = wordToPdfTemp(primaryReportUrl.replace("/word", wordUrl));
        insReport.setTempUrlPdf("/word/" + tempUrlPdf);
        insReportMapper.updateById(insReport);
@@ -301,6 +321,9 @@
            if (userId == null) {
                throw new ErrorException("缺少批准人");
            }
            if (Objects.equals(insReport.getExamineUserId(), userId)) {
                throw new ErrorException("审核人和批准人不能为同一人,请重新选择批准人");
            }
        }
        insReport.setRatifyUserId(userId);//批准人
        //获取审核人的签名地址
@@ -328,17 +351,15 @@
        info.setViewStatus(false);
        info.setJumpPath(MenuJumpPathConstants.REPORT_PREPARATION);
        informationNotificationService.addInformationNotification(info);
        //系统生成报告地址
        String url = insReport.getUrl();
        //手动上传报告地址
        String urlS = insReport.getUrlS();
        wordInsertUrl(new HashMap<String, Object>() {{
            put("examineUrl", Pictures.ofLocal(imgUrl + "/" + signatureUrl).create());
        String primaryReportUrl = getPrimaryReportUrl(insReport);
        Map<String, Object> examineMarks = new HashMap<String, Object>() {{
            put("examineUrl", ReportSignaturePictureUtils.create(imgUrl, signatureUrl));
            put("examineDateUrl", Pictures.ofStream(DateImageUtil.createDateImage(null)).create());
        }}, (StrUtil.isBlank(urlS) ? url : urlS).replace("/word", wordUrl));
        }};
        writeMarksToReportFiles(insReport, examineMarks);
        // 修改临时pdf
        String tempUrlPdf = wordToPdfTemp((StrUtil.isBlank(urlS) ? url : urlS).replace("/word", wordUrl));
        String tempUrlPdf = wordToPdfTemp(primaryReportUrl.replace("/word", wordUrl));
        insReport.setTempUrlPdf("/word/" + tempUrlPdf);
        // 发送企业微信通知(通知批准人审批)
@@ -383,8 +404,12 @@
    //批准
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int ratifyReport(Integer id, Integer isRatify, String ratifyTell) {
    public int ratifyReport(Integer id, Integer isRatify, String ratifyTell, String sealIds,
                            Integer maSealId, Integer reportSealId, Integer seamSealId) {
        InsReport insReport = insReportMapper.selectById(id);
        if (Objects.equals(insReport.getExamineUserId(), SecurityUtils.getUserId().intValue())) {
            throw new ErrorException("审核人和批准人不能为同一人,请重新选择批准人");
        }
        insReport.setIsRatify(isRatify);
        if (ObjectUtils.isNotEmpty(ratifyTell)) {
            insReport.setRatifyTell(ratifyTell);
@@ -454,45 +479,79 @@
        InsOrder order = insOrderMapper.selectById(insOrderId);
        boolean isRawMater = order.getTypeSource() != null && order.getTypeSource().equals(1);
        //获取场所的报告专用章
        String sealUrl;
        String laboratory = insOrderMapper.selectById(insReport.getInsOrderId()).getLaboratory();
        try {
            String type = "";
            if (isRawMater && order.getOrderType().equals(InsOrderTypeConstants.ENTER_THE_FACTORY)) {
                type = "进厂报告";
            } else {
                type = "委托报告";
            }
            sealUrl = insReportMapper.getLaboratoryByName(laboratory, type);
        } catch (Exception e) {
            throw new ErrorException(laboratory + "找不到报告专用章");
        // 新版按用途选择:MA章、报告章(模板 seal1/seal2)、PDF骑缝章。
        // 兼容旧前端:只传 sealIds 时,第一枚章继续作为报告章和骑缝章,MA章留空。
        SealConfig maSeal;
        SealConfig reportSeal;
        SealConfig seamSeal;
        boolean positionMode = maSealId != null || reportSealId != null || seamSealId != null;
        if (positionMode) {
            maSeal = resolveSeal(maSealId);
            reportSeal = resolveSeal(reportSealId);
            seamSeal = resolveSeal(seamSealId);
        } else {
            SealConfig legacySeal = resolveSeals(sealIds).get(0);
            maSeal = null;
            reportSeal = legacySeal;
            seamSeal = legacySeal;
        }
        if (StringUtils.isBlank(sealUrl)) {
            throw new ErrorException(laboratory + "找不到报告专用章");
        }
        //系统生成报告地址
        String url = insReport.getUrl();
        //手动上传报告地址
        String urlS = insReport.getUrlS();
        String finalUrl = (StrUtil.isBlank(urlS) ? url : urlS).replace("/word", wordUrl);
        wordInsertUrl(new HashMap<String, Object>() {{
            put("ratifyUrl", Pictures.ofLocal(imgUrl + "/" + signatureUrl).create());
            put("ratifyDateUrl", Pictures.ofStream(DateImageUtil.createDateImage(null)).create());
            put("seal1", Pictures.ofLocal(imgUrl + "/" + sealUrl).create());
            put("seal2", Pictures.ofLocal(imgUrl + "/" + sealUrl).create());
        }}, finalUrl);
        LinkedHashMap<Integer, SealConfig> chosenById = new LinkedHashMap<>();
        Arrays.asList(maSeal, reportSeal, seamSeal).stream()
                .filter(Objects::nonNull)
                .forEach(seal -> chosenById.put(seal.getId(), seal));
        List<SealConfig> chosenSeals = new ArrayList<>(chosenById.values());
        if (chosenSeals.isEmpty()) {
            throw new ErrorException("请至少选择一种报告印章");
        }
        insReport.setSealIds(chosenSeals.stream().map(s -> String.valueOf(s.getId())).collect(Collectors.joining(",")));
        insReport.setSealNames(chosenSeals.stream().map(SealConfig::getName).collect(Collectors.joining(",")));
        insReport.setMaSealId(maSeal == null ? null : maSeal.getId());
        insReport.setMaSealName(maSeal == null ? null : maSeal.getName());
        insReport.setReportSealId(reportSeal == null ? null : reportSeal.getId());
        insReport.setReportSealName(reportSeal == null ? null : reportSeal.getName());
        insReport.setSeamSealId(seamSeal == null ? null : seamSeal.getId());
        insReport.setSeamSealName(seamSeal == null ? null : seamSeal.getName());
        String primaryReportUrl = getPrimaryReportUrl(insReport);
        String finalUrl = primaryReportUrl.replace("/word", wordUrl);
        Map<String, Object> ratifyMarks = new HashMap<>();
        ratifyMarks.put("ratifyUrl", ReportSignaturePictureUtils.create(imgUrl, signatureUrl));
        ratifyMarks.put("ratifyDateUrl", Pictures.ofStream(DateImageUtil.createDateImage(null)).create());
        if (reportSeal != null) {
            ratifyMarks.put("seal1", createSealPicture(reportSeal.getAddress()));
            ratifyMarks.put("seal2", createSealPicture(reportSeal.getAddress()));
        }
        writeApprovalMarksToReportFiles(insReport, ratifyMarks);
        // 印章必须使用浮动定位,否则行内图片会撑高段落/表格并改变原报告分页。
        for (String reportUrl : getApprovalReportUrls(insReport)) {
            try {
                ReportSealLayoutUtil.apply(
                        reportUrl.replace("/word", wordUrl),
                        reportSeal == null ? null : Paths.get(imgUrl, reportSeal.getAddress()).toString(),
                        maSeal == null ? null : Paths.get(imgUrl, maSeal.getAddress()).toString());
            } catch (Exception e) {
                log.error("报告印章浮动定位失败: {}", reportUrl, e);
                throw new ErrorException("报告盖章失败,请检查印章图片和报告文件");
            }
        }
        // 修改临时pdf
        insReport.setTempUrlPdf((StrUtil.isBlank(urlS) ? url : urlS).replace(".docx", ".pdf"));
        insReport.setTempUrlPdf(primaryReportUrl.replace(".docx", ".pdf"));
        InsOrder insOrder = new InsOrder();
        insOrder.setId(insOrderId);
        insOrder.setState(4);
        insOrderMapper.updateById(insOrder);
        wordToPdf(finalUrl, sealUrl, isRawMater && order.getOrderType().equals(InsOrderTypeConstants.ENTER_THE_FACTORY));
        for (String reportUrl : getApprovalReportUrls(insReport)) {
            wordToPdf(reportUrl.replace("/word", wordUrl),
                    null,
                    seamSeal == null ? null : seamSeal.getAddress(),
                    isRawMater && order.getOrderType().equals(InsOrderTypeConstants.ENTER_THE_FACTORY));
        }
        // 判断是否为原材料
        if (isRawMater) {
@@ -521,15 +580,132 @@
        return insReportMapper.updateById(insReport);
    }
    /**
     * 解析批准时选择的印章(逗号分隔的印章id)。
     * 未传或全部无效时回退到第一个启用印章(默认“1号章”)。
     */
    private List<SealConfig> resolveSeals(String sealIds) {
        List<SealConfig> chosen = new ArrayList<>();
        if (StringUtils.isNotBlank(sealIds)) {
            List<Integer> ids = new ArrayList<>();
            for (String s : sealIds.split(",")) {
                if (StringUtils.isNotBlank(s)) {
                    ids.add(Integer.valueOf(s.trim()));
                }
            }
            if (!ids.isEmpty()) {
                Map<Integer, SealConfig> byId = sealConfigMapper.selectBatchIds(ids).stream()
                        .collect(Collectors.toMap(SealConfig::getId, x -> x));
                for (Integer sid : ids) {
                    SealConfig seal = byId.get(sid);
                    if (seal != null && Objects.equals(seal.getStatus(), 1)
                            && StringUtils.isNotBlank(seal.getAddress())) {
                        chosen.add(seal);
                    }
                }
            }
        }
        if (chosen.isEmpty()) {
            List<SealConfig> enabled = sealConfigMapper.selectList(Wrappers.<SealConfig>lambdaQuery()
                    .eq(SealConfig::getStatus, 1).orderByAsc(SealConfig::getCode));
            if (!enabled.isEmpty()) {
                chosen.add(enabled.get(0));
            }
        }
        if (chosen.isEmpty()) {
            throw new ErrorException("未找到可用印章,请先在印章管理中维护并启用印章");
        }
        return chosen;
    }
    private SealConfig resolveSeal(Integer sealId) {
        if (sealId == null) {
            return null;
        }
        SealConfig seal = sealConfigMapper.selectById(sealId);
        if (seal == null) {
            throw new ErrorException("所选印章不存在,请重新选择");
        }
        if (!Objects.equals(seal.getStatus(), 1)) {
            throw new ErrorException("印章“" + seal.getName() + "”已停用,请重新选择");
        }
        if (StringUtils.isBlank(seal.getAddress())) {
            throw new ErrorException("印章“" + seal.getName() + "”未上传图片,请重新选择");
        }
        return seal;
    }
    /**
     * 按图片内容识别印章格式,兼容历史数据中扩展名被保存为“_png”等非标准文件名的情况。
     */
    private PictureRenderData createSealPicture(String sealUrl) {
        Path sealPath = Paths.get(imgUrl, sealUrl);
        if (!Files.isRegularFile(sealPath)) {
            throw new ErrorException("印章图片文件不存在,请重新上传印章图片");
        }
        try {
            byte[] imageBytes = Files.readAllBytes(sealPath);
            PictureType pictureType = PictureType.suggestFileType(imageBytes);
            return Pictures.ofBytes(imageBytes, pictureType).create();
        } catch (Exception e) {
            log.error("读取印章图片失败: {}", sealPath, e);
            throw new ErrorException("印章图片无法识别,请重新上传 jpg 或 png 图片");
        }
    }
    @Override
    public int wordInsertUrl(Map<String, Object> map, String url) {
        XWPFTemplate template = XWPFTemplate.compile(url).render(map);
        Configure configure = Configure.builder()
                .setValidErrorHandler(new PreserveReportPicturePlaceholderHandler())
                .build();
        XWPFTemplate template = XWPFTemplate.compile(url, configure).render(map);
        try {
            template.writeAndClose(Files.newOutputStream(Paths.get(url)));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return 1;
    }
    private int wordInsertApprovalMarks(Map<String, Object> map, String url) {
        Configure configure = Configure.builder()
                .setValidErrorHandler(new PreserveReportPicturePlaceholderHandler(
                        new HashSet<>(Arrays.asList("seal1", "seal2"))))
                .build();
        XWPFTemplate template = XWPFTemplate.compile(url, configure).render(map);
        try {
            template.writeAndClose(Files.newOutputStream(Paths.get(url)));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return 1;
    }
    private String getPrimaryReportUrl(InsReport report) {
        String reportUrl = StringUtils.isNotBlank(report.getUrlS()) ? report.getUrlS() : report.getUrl();
        if (StringUtils.isBlank(reportUrl)) {
            throw new ErrorException("检测报告文件不存在");
        }
        return reportUrl;
    }
    private List<String> getApprovalReportUrls(InsReport report) {
        return Arrays.asList(getPrimaryReportUrl(report), report.getInspectionUrl()).stream()
                .filter(StringUtils::isNotBlank)
                .distinct()
                .collect(Collectors.toList());
    }
    private void writeMarksToReportFiles(InsReport report, Map<String, Object> marks) {
        for (String reportUrl : getApprovalReportUrls(report)) {
            wordInsertUrl(marks, reportUrl.replace("/word", wordUrl));
        }
    }
    private void writeApprovalMarksToReportFiles(InsReport report, Map<String, Object> marks) {
        for (String reportUrl : getApprovalReportUrls(report)) {
            wordInsertApprovalMarks(marks, reportUrl.replace("/word", wordUrl));
        }
    }
@@ -569,6 +745,53 @@
            e.printStackTrace();
        }
        return "/word/zip/output.zip";
    }
    @Override
    public void download(Integer id, Integer reportType, Integer createOrderUser, HttpServletResponse response) {
        if (id == null) {
            throw new ErrorException("报告不能为空");
        }
        reportType = reportType == null ? DETECTION_REPORT : reportType;
        if (!Objects.equals(reportType, DETECTION_REPORT) && !Objects.equals(reportType, INSPECTION_REPORT)) {
            throw new ErrorException("报告类型不正确");
        }
        InsReport report = insReportMapper.selectDownloadableReport(id, createOrderUser);
        if (report == null) {
            throw new ErrorException("报告不存在、尚未生成或无权下载");
        }
        String reportUrl = Objects.equals(reportType, INSPECTION_REPORT)
                ? report.getInspectionUrl() : getPrimaryReportUrl(report);
        if (StringUtils.isBlank(reportUrl) || !reportUrl.replace('\\', '/').startsWith("/word/")) {
            throw new ErrorException((Objects.equals(reportType, INSPECTION_REPORT) ? "检验报告" : "检测报告")
                    + "文件不存在,请联系管理员");
        }
        Path root = Paths.get(wordUrl).toAbsolutePath().normalize();
        Path file = root.resolve(reportUrl.replace('\\', '/').substring("/word/".length())).normalize();
        if (!file.startsWith(root) || !Files.isRegularFile(file)) {
            throw new ErrorException("报告文件不存在,请联系管理员");
        }
        try {
            String filename = file.getFileName().toString();
            String encodedName = URLEncoder.encode(filename, "UTF-8").replace("+", "%20");
            response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
            response.setHeader("Content-Disposition", "attachment; filename*=UTF-8''" + encodedName);
            response.setHeader("Access-Control-Expose-Headers", "Content-Disposition,download-filename");
            response.setHeader("download-filename", encodedName);
            response.setContentLengthLong(Files.size(file));
            try (InputStream inputStream = Files.newInputStream(file);
                 OutputStream outputStream = response.getOutputStream()) {
                byte[] buffer = new byte[8192];
                int length;
                while ((length = inputStream.read(buffer)) != -1) {
                    outputStream.write(buffer, 0, length);
                }
                outputStream.flush();
            }
        } catch (IOException e) {
            log.error("下载报告失败,reportId={}", id, e);
            throw new ErrorException("报告下载失败");
        }
    }
    //批量上传
@@ -686,16 +909,17 @@
    }
    @Override
    public void wordToPdf(String path, String sealUrl, boolean isRawMater) {
    public void wordToPdf(String path, String maSealUrl, String seamSealUrl, boolean isRawMater) {
        try {
            wordToPdf(path, path.replace(".docx", ".pdf"), sealUrl, isRawMater);
            wordToPdf(path, path.replace(".docx", ".pdf"), maSealUrl, seamSealUrl, isRawMater);
        } catch (Exception e) {
            throw new ErrorException("转换失败");
        }
    }
    public String wordToPdf(String wordPath, String pdfPath, String sealUrl, boolean isRawMater) {
    public String wordToPdf(String wordPath, String pdfPath, String maSealUrl,
                            String seamSealUrl, boolean isRawMater) {
        FileOutputStream os = null;
        try {
            //凭证 不然切换后有水印
@@ -706,27 +930,31 @@
                log.info("License验证不通过...");
                return null;
            }
            //生成一个空的PDF文件
            File file;
            //判断是否是进厂报告
            if (!isRawMater) {
                file = new File(pdfPath.replace(".pdf", "-1.pdf"));
            } else {
                file = new File(pdfPath.replace(".pdf", ".pdf"));
            }
            String effectiveSeamSealUrl = isRawMater ? null : seamSealUrl;
            boolean addPdfMarks = StringUtils.isNotBlank(maSealUrl)
                    || StringUtils.isNotBlank(effectiveSeamSealUrl);
            String sourcePdfPath = addPdfMarks ? pdfPath.replace(".pdf", "-stamp-source.pdf") : pdfPath;
            File file = new File(sourcePdfPath);
            os = new FileOutputStream(file);
            //要转换的word文件
            com.aspose.words.Document doc = new com.aspose.words.Document(wordPath);
            doc.save(os, SaveFormat.PDF);
            os.close();
            os = null;
            //添加骑缝章
            if (!isRawMater) {
                stamperCheckMarkPDF(pdfPath.replace(".pdf", "-1.pdf"), pdfPath, imgUrl + "/" + sealUrl);
            // MA章固定在第1页左上角;骑缝章按页切片固定在右侧中部。
            if (addPdfMarks) {
                stamperReportMarksPDF(
                        sourcePdfPath,
                        pdfPath,
                        StringUtils.isBlank(maSealUrl) ? null : Paths.get(imgUrl, maSealUrl).toString(),
                        StringUtils.isBlank(effectiveSeamSealUrl)
                                ? null : Paths.get(imgUrl, effectiveSeamSealUrl).toString());
            }
            return new File(pdfPath).getName();
        } catch (Exception e) {
            e.printStackTrace();
            log.error("生成盖章PDF失败: {}", wordPath, e);
            throw new ErrorException("报告转PDF或盖章失败");
        } finally {
            if (os != null) {
                try {
@@ -736,7 +964,6 @@
                }
            }
        }
        return null;
    }
    /**
@@ -761,7 +988,8 @@
                subImg = img.getSubimage(i * sw, 0, sw, h);
            }
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            ImageIO.write(subImg, Path.substring(Path.lastIndexOf('.') + 1), out);
            // 统一编码为 PNG,兼容历史“_png”等没有标准扩展名的图片文件。
            ImageIO.write(subImg, "png", out);
            nImage[i] = com.itextpdf.text.Image.getInstance(out.toByteArray());
        }
@@ -769,33 +997,46 @@
    }
    /**
     * 盖骑缝章
     * 按参考报告盖 PDF 标志章:MA章位于第1页左上角,骑缝章位于每页右侧中部。
     *
     * @param infilePath  原PDF路径
     * @param outFilePath 输出PDF路径
     */
    public static void stamperCheckMarkPDF(String infilePath, String outFilePath, String picPath) throws IOException, DocumentException {
    public static void stamperReportMarksPDF(String infilePath, String outFilePath,
                                             String maPicPath, String seamPicPath)
            throws IOException, DocumentException {
        PdfReader reader = new PdfReader(infilePath);//选择需要印章的pdf
        PdfStamper stamp = new PdfStamper(reader, new FileOutputStream(outFilePath));//加完印章后的pdf
        com.itextpdf.text.Rectangle pageSize = reader.getPageSize(1);//获得第一页
        float height = pageSize.getHeight();
        float width = pageSize.getWidth();
        int nums = reader.getNumberOfPages();
        com.itextpdf.text.Image[] nImage = slicingImages(picPath, nums);//生成骑缝章切割图片
        boolean hasMaSeal = maPicPath != null && !maPicPath.trim().isEmpty();
        boolean hasSeamSeal = seamPicPath != null && !seamPicPath.trim().isEmpty();
        com.itextpdf.text.Image[] seamImages = hasSeamSeal ? slicingImages(seamPicPath, nums) : null;
        for (int n = 1; n <= nums; n++) {
            com.itextpdf.text.Rectangle pageSize = reader.getPageSize(n);
            float height = pageSize.getHeight();
            float width = pageSize.getWidth();
            PdfContentByte over = stamp.getOverContent(n);//设置在第几页打印印章
            com.itextpdf.text.Image img = nImage[n - 1];//选择图片
            float newHeight = 100f;
            float newWidth = img.getWidth() / (img.getHeight() / 100);
            img.scaleAbsolute(newWidth, newHeight);//控制图片大小
            img.setAbsolutePosition(width - newWidth, height / 2 - newHeight / 2);//控制图片位置
            over.addImage(img);
            if (n == 1 && hasMaSeal) {
                com.itextpdf.text.Image maImage = com.itextpdf.text.Image.getInstance(maPicPath);
                maImage.scaleToFit(100f, 90f);
                maImage.setAbsolutePosition(30f, height - maImage.getScaledHeight() - 30f);
                over.addImage(maImage);
            }
            if (hasSeamSeal) {
                com.itextpdf.text.Image seamImage = seamImages[n - 1];
                float newHeight = 100f;
                float newWidth = seamImage.getWidth() / (seamImage.getHeight() / newHeight);
                seamImage.scaleAbsolute(newWidth, newHeight);
                seamImage.setAbsolutePosition(width - newWidth, height / 2 - newHeight / 2);
                over.addImage(seamImage);
            }
        }
        stamp.close();
        reader.close();
    }
    @Override
@@ -929,20 +1170,22 @@
    @Transactional(rollbackFor = Exception.class)
    public boolean sendBackTask(Integer id) {
        InsReport report = insReportMapper.selectById(id);
        // 根据订单查询试验室
        String laboratory = insOrderMapper.selectLaboratoryByOrderId(report.getInsOrderId());
        if (report == null || report.getInsOrderId() == null) {
            throw new ErrorException("报告不存在或未关联检验单");
        }
        // 修改订单状态
        // 报告由订单下各试验室共同形成;退回时全部恢复为待复核,避免只随机退回一个试验室。
        insOrderStateMapper.update(null, Wrappers.<InsOrderState>lambdaUpdate()
                .eq(InsOrderState::getInsOrderId, report.getInsOrderId())
                .eq(InsOrderState::getLaboratory, laboratory)
                .set(InsOrderState::getInsState, 4));
        Integer insSampleId = insSampleUserMapper.selectOne(Wrappers.<InsSampleUser>lambdaQuery()
        InsSampleUser sampleUser = insSampleUserMapper.selectOne(Wrappers.<InsSampleUser>lambdaQuery()
                .eq(InsSampleUser::getInsSampleId, report.getInsOrderId())
                .orderByDesc(InsSampleUser::getId)
                .last("limit 1")).getId();
        insSampleUserMapper.deleteById(insSampleId);
                .last("limit 1"));
        if (sampleUser != null) {
            insSampleUserMapper.deleteById(sampleUser.getId());
        }
        // 修改报告行为未显示
        insReportMapper.update(null, Wrappers.<InsReport>lambdaUpdate()