wangliang 4 år sedan
förälder
incheckning
1b4d78c407

+ 34 - 0
distributed-print-business/src/main/java/com/qmth/distributed/print/business/bean/dto/PdfDto.java

@@ -18,12 +18,18 @@ public class PdfDto {
     @ApiModelProperty(name = "页数")
     Integer pageCount = 0;
 
+    @ApiModelProperty(name = "实际页数")
+    Integer actualPageCount = 0;
+
     @ApiModelProperty(name = "页面大小")
     PageSizeEnum pageSize;
 
     @ApiModelProperty(name = "序号")
     Integer sequence = 0;
 
+    @ApiModelProperty(name = "tag")
+    boolean tag = true;
+
     public PdfDto() {
 
     }
@@ -46,6 +52,34 @@ public class PdfDto {
         this.sequence = sequence;
     }
 
+    public PdfDto(PageSizeEnum pageSize, Integer pageCount, boolean tag) {
+        this.pageSize = pageSize;
+        this.pageCount = pageCount;
+        this.tag = tag;
+    }
+
+    public PdfDto(PageSizeEnum pageSize, Integer pageCount, Integer actualPageCount) {
+        this.pageSize = pageSize;
+        this.pageCount = pageCount;
+        this.actualPageCount = actualPageCount;
+    }
+
+    public Integer getActualPageCount() {
+        return actualPageCount;
+    }
+
+    public void setActualPageCount(Integer actualPageCount) {
+        this.actualPageCount = actualPageCount;
+    }
+
+    public boolean isTag() {
+        return tag;
+    }
+
+    public void setTag(boolean tag) {
+        this.tag = tag;
+    }
+
     public Integer getSequence() {
         return sequence;
     }

+ 8 - 5
distributed-print-business/src/main/java/com/qmth/distributed/print/business/service/impl/BasicAttachmentServiceImpl.java

@@ -103,6 +103,7 @@ public class BasicAttachmentServiceImpl extends ServiceImpl<BasicAttachmentMappe
     @Transactional
     public BasicAttachment saveAttachmentHtml(String fileName, String htmlContent, Long userId, List<PdfDto> localFileList) throws IOException {
         BasicAttachment basicAttachment = null;
+        PdfDto pdfDto = null;
         try {
             byte[] bytes = htmlContent.getBytes();
             int size = bytes.length;
@@ -131,8 +132,8 @@ public class BasicAttachmentServiceImpl extends ServiceImpl<BasicAttachmentMappe
                 String destUrl = SystemConstant.PDF_TEMP_FILES_DIR + File.separator + pdfDirName;
                 HtmlToPdfUtil.convert(localHtmlFile.getPath(), destUrl, PageSizeEnum.A3);
                 File pdfFile = new File(destUrl);
-                int pageCount = PdfUtil.addPdfPage(pdfFile);
-                localFileList.add(new PdfDto(pdfFile.getPath(), PageSizeEnum.A3, pageCount));
+                pdfDto = PdfUtil.addPdfPage(pdfFile);
+                localFileList.add(new PdfDto(pdfFile.getPath(), PageSizeEnum.A3, pdfDto.getPageCount()));
                 fileMd5 = DigestUtils.md5Hex(new FileInputStream(pdfFile));
                 ossUtil.ossUpload(pdfDirName, pdfFile, BinaryUtil.toBase64String(HexUtils.decodeHex(fileMd5)));
                 localHtmlFile.delete();
@@ -155,6 +156,7 @@ public class BasicAttachmentServiceImpl extends ServiceImpl<BasicAttachmentMappe
                     UploadFileEnum.HTML, UploadFileEnum.PDF
             });
             basicAttachment = new BasicAttachment(jsonObject.toJSONString(), fileName, SystemConstant.HTML_PREFIX, new BigDecimal(size), fileMd5, userId);
+            basicAttachment.setPages(pdfDto.getActualPageCount());
             save(basicAttachment);
         } catch (Exception e) {
             log.error("请求出错", e);
@@ -239,9 +241,9 @@ public class BasicAttachmentServiceImpl extends ServiceImpl<BasicAttachmentMappe
             HtmlToPdfUtil.convert(htmlFile.getPath(), destUrl, PageSizeEnum.A4);
             File pdfFile = new File(destUrl);
             String fileMd5 = DigestUtils.md5Hex(new FileInputStream(pdfFile));
-            int pageCount = 0;
+            PdfDto pdfDto = null;
             if (oss) {//上传至oss
-                pageCount = PdfUtil.addPdfPage(pdfFile);
+                pdfDto = PdfUtil.addPdfPage(pdfFile);
                 ossUtil.ossUpload(pdfDirName, pdfFile, BinaryUtil.toBase64String(HexUtils.decodeHex(fileMd5)));
                 jsonObject.put(SystemConstant.PDF_PATH, pdfDirName);
                 if (Objects.nonNull(type) && Objects.equals(type, SystemConstant.FTL_PREFIX)) {
@@ -259,8 +261,9 @@ public class BasicAttachmentServiceImpl extends ServiceImpl<BasicAttachmentMappe
                 }
             }
             basicAttachment.setPath(jsonObject.toJSONString());
+            basicAttachment.setPages(pdfDto.getActualPageCount());
             for (int i = 0; i < printCount; i++) {
-                pdfList.add(new PdfDto(pdfFile.getPath(), PageSizeEnum.A4, pageCount, sequence));
+                pdfList.add(new PdfDto(pdfFile.getPath(), PageSizeEnum.A4, pdfDto.getPageCount(), sequence));
             }
             this.updateById(basicAttachment);
         } catch (Exception e) {

+ 7 - 11
distributed-print-business/src/main/java/com/qmth/distributed/print/business/templete/service/impl/TaskLogicServiceImpl.java

@@ -230,7 +230,8 @@ public class TaskLogicServiceImpl implements TaskLogicService {
                             examTaskDetail.setRelatePaperType(paperType);
 
                             //获取试卷pdf
-                            boolean tag = createPdfUtil.getPaperPdf(paperType, examTaskDetail, examPrintPlan.getBackupCount(), paperPdfTempList, backupPaperTempPdfList);
+                            PdfDto pdfDto = createPdfUtil.getPaperPdf(paperType, examTaskDetail, examPrintPlan.getBackupCount(), paperPdfTempList, backupPaperTempPdfList);
+                            examDetailCourse.setPaperPagesA3(pdfDto.getPageCount());
                             paperPdfList.addAll(paperPdfTempList);
                             backupPaperPdfList.addAll(backupPaperTempPdfList);
 
@@ -243,7 +244,9 @@ public class TaskLogicServiceImpl implements TaskLogicService {
                                 String studentContent = cardContent;
 
                                 for (int i = 1; i <= examPrintPlan.getBackupCount(); i++) {
-                                    basicAttachmentList.add(createPdfUtil.cardHtml(String.format("%02d", i), cardContent, paperType, examDetail, examDetailCourse, examCard, jsonArray, sysUser.getId(), cardPdfTempList));
+                                    BasicAttachment basicAttachment = createPdfUtil.cardHtml(String.format("%02d", i), cardContent, paperType, examDetail, examDetailCourse, examCard, jsonArray, sysUser.getId(), cardPdfTempList);
+                                    examDetailCourse.setCardPagesA3(basicAttachment.getPages());
+                                    basicAttachmentList.add(basicAttachment);
                                 }
                                 cardPdfList.addAll(cardPdfTempList);
 
@@ -265,7 +268,7 @@ public class TaskLogicServiceImpl implements TaskLogicService {
                                         if (Objects.nonNull(type) && Objects.equals(type.toUpperCase(), "SIGN")) {//签到表
                                             createPdfUtil.createSignBook(basicAttachment, basicSchool.getName(), examDetail, examDetailCourse, examStudentList, variablePdfList, (Integer) jsonObjectVariable.get("backupCount"));
                                         } else if (Objects.nonNull(type) && Objects.equals(type.toUpperCase(), "PACKAGE")) {//卷袋贴
-                                            createPdfUtil.createPaperPackage(tag, basicAttachment, basicSchool.getName(), examDetail, examDetailCourse, examStudentList, variablePdfList, (Integer) jsonObjectVariable.get("backupCount"));
+                                            createPdfUtil.createPaperPackage(pdfDto.isTag(), basicAttachment, basicSchool.getName(), examDetail, examDetailCourse, examStudentList, variablePdfList, (Integer) jsonObjectVariable.get("backupCount"));
                                         }
                                     }
                                 }
@@ -288,13 +291,6 @@ public class TaskLogicServiceImpl implements TaskLogicService {
                             examCardDetailService.saveOrUpdateBatch(examCardDetailList);
                         }
                         examTaskDetailService.saveOrUpdateBatch(examTaskDetailList);
-
-                        Integer paperPageA3 = 0;
-                        Integer cardPageA3 = 0;
-                        paperPageA3 = paperPageA3 + paperPdfTempList.stream().mapToInt(PdfDto::getPageCount).sum() + backupPaperTempPdfList.stream().mapToInt(PdfDto::getPageCount).sum();
-                        cardPageA3 = cardPageA3 + examStudentTempPdfList.stream().mapToInt(PdfDto::getPageCount).sum() + cardPdfTempList.stream().mapToInt(PdfDto::getPageCount).sum();
-                        examDetailCourse.setPaperPagesA3(paperPageA3);
-                        examDetailCourse.setCardPagesA3(cardPageA3);
                     }
                     detailCourseService.saveOrUpdateBatch(examDetailCourseList);
                     //合并pdf
@@ -447,7 +443,7 @@ public class TaskLogicServiceImpl implements TaskLogicService {
 
                 Cell cell = row.getCell(index);
                 String cellValue = String.valueOf(ExcelUtil.convert(cell));
-                if (cellValue == null || cellValue.length() < 1 || cellValue.equals("null")){
+                if (cellValue == null || cellValue.length() < 1 || cellValue.equals("null")) {
                     throw ExceptionResultEnum.ERROR.exception("excel中第[" + (r + 1) + "]行,第[" + (index + 1) + "]列,字段[" + name + "]必填");
                 }
 

+ 9 - 9
distributed-print-business/src/main/java/com/qmth/distributed/print/business/util/CreatePdfUtil.java

@@ -14,7 +14,6 @@ import com.qmth.distributed.print.business.enums.ExamDetailStatusEnum;
 import com.qmth.distributed.print.business.enums.PageSizeEnum;
 import com.qmth.distributed.print.business.enums.PrintMethodEnum;
 import com.qmth.distributed.print.business.service.BasicAttachmentService;
-import com.qmth.distributed.print.business.service.BasicExamRuleService;
 import com.qmth.distributed.print.business.service.CommonService;
 import com.qmth.distributed.print.business.service.ExamDetailService;
 import com.qmth.distributed.print.common.contant.SystemConstant;
@@ -74,9 +73,9 @@ public class CreatePdfUtil {
             String filePath = (String) jsonObject.get(SystemConstant.PATH);
             String url = SystemConstant.TEMP_FILES_DIR + File.separator + filePath;
             File localFile = ossUtil.ossDownload(filePath, url);
-            int pageCount = PdfUtil.addPdfPage(localFile);
+            PdfDto pdfDto = PdfUtil.addPdfPage(localFile);
             for (int i = 0; i < printCount; i++) {
-                ordinaryPdfList.add(new PdfDto(localFile.getPath(), PageSizeEnum.A4, pageCount));
+                ordinaryPdfList.add(new PdfDto(localFile.getPath(), PageSizeEnum.A4, pdfDto.getPageCount()));
             }
         } else {
             basicAttachmentService.saveAttachmentPdf(basicAttachment, ordinaryPdfList, printCount, 0);
@@ -297,8 +296,9 @@ public class CreatePdfUtil {
      * @return
      * @throws IOException
      */
-    public boolean getPaperPdf(String paperType, ExamTaskDetail examTaskDetail, Integer backupCount, List<PdfDto>... pdfList) throws IOException {
+    public PdfDto getPaperPdf(String paperType, ExamTaskDetail examTaskDetail, Integer backupCount, List<PdfDto>... pdfList) throws IOException {
         boolean tag = true;
+        int pages = 0;
         JSONArray jsonArrayPaper = JSONArray.parseArray(examTaskDetail.getPaperAttachmentIds());
         for (int i = 0; i < jsonArrayPaper.size(); i++) {
             JSONObject object = (JSONObject) jsonArrayPaper.get(i);
@@ -311,17 +311,17 @@ public class CreatePdfUtil {
                 String name = (String) object.get("name");
                 if (Objects.equals(name.toUpperCase(), paperType.toUpperCase())) {
                     File file = commonService.getFile(basicAttachment.getPath(), false);
-                    int pages = (int) object.get("pages");
+                    pages = (int) object.get("pages");
                     tag = pages > 2 ? true : false;
-                    int pageCount = PdfUtil.addPdfPage(file);
-                    pdfList[0].add(new PdfDto(file.getPath(), PageSizeEnum.A3, pageCount));
+                    PdfDto pdfDto = PdfUtil.addPdfPage(file);
+                    pdfList[0].add(new PdfDto(file.getPath(), PageSizeEnum.A3, pdfDto.getPageCount()));
                     for (int j = 1; j <= backupCount; j++) {
-                        pdfList[1].add(new PdfDto(file.getPath(), PageSizeEnum.A3, pageCount));
+                        pdfList[1].add(new PdfDto(file.getPath(), PageSizeEnum.A3, pdfDto.getPageCount()));
                     }
                 }
             }
         }
-        return tag;
+        return new PdfDto(PageSizeEnum.A3, pages, tag);
     }
 
     /**

+ 7 - 3
distributed-print-business/src/main/java/com/qmth/distributed/print/business/util/PdfUtil.java

@@ -5,6 +5,9 @@ import com.itextpdf.text.pdf.PdfCopy;
 import com.itextpdf.text.pdf.PdfImportedPage;
 import com.itextpdf.text.pdf.PdfReader;
 import com.itextpdf.text.pdf.PdfStamper;
+import com.qmth.distributed.print.business.bean.dto.PdfDto;
+import com.qmth.distributed.print.business.entity.BasicAttachment;
+import com.qmth.distributed.print.business.enums.PageSizeEnum;
 import com.qmth.distributed.print.business.enums.UploadFileEnum;
 import com.qmth.distributed.print.common.contant.SystemConstant;
 import org.apache.commons.io.IOUtils;
@@ -88,12 +91,13 @@ public class PdfUtil {
      * @return
      * @throws IOException
      */
-    public static int addPdfPage(File pdfFile) throws IOException {
+    public static PdfDto addPdfPage(File pdfFile) throws IOException {
         PdfReader reader = null;
         FileOutputStream fileOutputStream = null;
+        int pageCount = 0;
         try {
             reader = new PdfReader(pdfFile.getPath());
-            int pageCount = reader.getNumberOfPages();
+            pageCount = reader.getNumberOfPages();
             if (Math.abs(pageCount % 2) == 1) {//取偶
                 ByteArrayOutputStream baos = new ByteArrayOutputStream();
                 PdfStamper stamper = new PdfStamper(reader, baos);
@@ -113,7 +117,7 @@ public class PdfUtil {
                 fileOutputStream.close();
             }
         }
-        return Objects.nonNull(reader) ? reader.getNumberOfPages() : 0;
+        return new PdfDto(PageSizeEnum.A3, Objects.nonNull(reader) ? reader.getNumberOfPages() : 0, pageCount);
     }
 
     /**