| | |
| | | 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; |
| | |
| | | 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; |
| | |
| | | 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; |
| | |
| | | private InsOrderMapper insOrderMapper; |
| | | @Resource |
| | | private IfsInventoryQuantityMapper ifsInventoryQuantityMapper; |
| | | @Resource |
| | | private SealConfigMapper sealConfigMapper; |
| | | @Resource |
| | | private InsUnqualifiedHandlerMapper insUnqualifiedHandlerMapper; |
| | | @Resource |
| | |
| | | //批准 |
| | | @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("审核人和批准人不能为同一人,请重新选择批准人"); |
| | |
| | | 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 + "找不到报告专用章"); |
| | | |
| | | 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<String, Object>() {{ |
| | | put("ratifyUrl", ReportSignaturePictureUtils.create(imgUrl, signatureUrl)); |
| | | put("ratifyDateUrl", Pictures.ofStream(DateImageUtil.createDateImage(null)).create()); |
| | | put("seal1", Pictures.ofLocal(imgUrl + "/" + sealUrl).create()); |
| | | put("seal2", Pictures.ofLocal(imgUrl + "/" + sealUrl).create()); |
| | | }}; |
| | | writeMarksToReportFiles(insReport, ratifyMarks); |
| | | 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(primaryReportUrl.replace(".docx", ".pdf")); |
| | |
| | | insOrderMapper.updateById(insOrder); |
| | | |
| | | for (String reportUrl : getApprovalReportUrls(insReport)) { |
| | | wordToPdf(reportUrl.replace("/word", wordUrl), sealUrl, |
| | | wordToPdf(reportUrl.replace("/word", wordUrl), |
| | | null, |
| | | seamSeal == null ? null : seamSeal.getAddress(), |
| | | isRawMater && order.getOrderType().equals(InsOrderTypeConstants.ENTER_THE_FACTORY)); |
| | | } |
| | | |
| | |
| | | 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) { |
| | | 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 { |
| | |
| | | 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)); |
| | | } |
| | | } |
| | | |
| | |
| | | } |
| | | |
| | | @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 { |
| | | //凭证 不然切换后有水印 |
| | |
| | | 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 { |
| | |
| | | } |
| | | } |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | /** |
| | |
| | | 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()); |
| | | |
| | | } |
| | |
| | | } |
| | | |
| | | /** |
| | | * 盖骑缝章 |
| | | * 按参考报告盖 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 |
| | |
| | | @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() |