wangliang 4 سال پیش
والد
کامیت
d8ff113427

+ 16 - 1
distributed-print-business/src/main/java/com/qmth/distributed/print/business/bean/dto/PdfDto.java

@@ -16,11 +16,26 @@ public class PdfDto {
     String path;
 
     @ApiModelProperty(name = "页数")
-    Integer pageCount;
+    Integer pageCount = 0;
 
     @ApiModelProperty(name = "页面大小")
     PageSizeEnum pageSize;
 
+    public PdfDto() {
+
+    }
+
+    public PdfDto(String path, PageSizeEnum pageSize) {
+        this.path = path;
+        this.pageSize = pageSize;
+    }
+
+    public PdfDto(String path, PageSizeEnum pageSize, Integer pageCount) {
+        this.path = path;
+        this.pageSize = pageSize;
+        this.pageCount = pageCount;
+    }
+
     public String getPath() {
         return path;
     }

+ 3 - 2
distributed-print-business/src/main/java/com/qmth/distributed/print/business/service/BasicAttachmentService.java

@@ -2,6 +2,7 @@ package com.qmth.distributed.print.business.service;
 
 import com.baomidou.mybatisplus.extension.service.IService;
 import com.itextpdf.text.DocumentException;
+import com.qmth.distributed.print.business.bean.dto.PdfDto;
 import com.qmth.distributed.print.business.entity.BasicAttachment;
 import com.qmth.distributed.print.business.enums.UploadFileEnum;
 import org.springframework.web.multipart.MultipartFile;
@@ -50,7 +51,7 @@ public interface BasicAttachmentService extends IService<BasicAttachment> {
      * @return
      * @throws IOException
      */
-    public BasicAttachment saveAttachmentHtml(String fileName, String htmlContent, Long userId, List<String> localFileList) throws IOException;
+    public BasicAttachment saveAttachmentHtml(String fileName, String htmlContent, Long userId, List<PdfDto> localFileList) throws IOException;
 
     /**
      * 保存附件
@@ -71,7 +72,7 @@ public interface BasicAttachmentService extends IService<BasicAttachment> {
      * @return
      * @throws IOException
      */
-    public BasicAttachment saveAttachmentPdf(BasicAttachment basicAttachment, List<String> pdfList, Integer printCount) throws IOException, DocumentException;
+    public BasicAttachment saveAttachmentPdf(BasicAttachment basicAttachment, List<PdfDto> pdfList, Integer printCount) throws IOException, DocumentException;
 
     /**
      * 删除附件

+ 11 - 27
distributed-print-business/src/main/java/com/qmth/distributed/print/business/service/impl/BasicAttachmentServiceImpl.java

@@ -5,8 +5,8 @@ import com.aliyun.oss.common.utils.BinaryUtil;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.itextpdf.text.DocumentException;
 import com.itextpdf.text.pdf.PdfReader;
-import com.itextpdf.text.pdf.PdfStamper;
 import com.qmth.boot.api.exception.ApiException;
+import com.qmth.distributed.print.business.bean.dto.PdfDto;
 import com.qmth.distributed.print.business.config.DictionaryConfig;
 import com.qmth.distributed.print.business.entity.BasicAttachment;
 import com.qmth.distributed.print.business.entity.SysUser;
@@ -24,7 +24,6 @@ import com.qmth.distributed.print.common.util.HexUtils;
 import com.qmth.distributed.print.common.util.ResultUtil;
 import org.apache.commons.codec.digest.DigestUtils;
 import org.apache.commons.io.FileUtils;
-import org.apache.commons.io.IOUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -34,7 +33,9 @@ import org.springframework.util.FileCopyUtils;
 import org.springframework.web.multipart.MultipartFile;
 
 import javax.annotation.Resource;
-import java.io.*;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
 import java.math.BigDecimal;
 import java.time.LocalDateTime;
 import java.util.List;
@@ -100,7 +101,7 @@ public class BasicAttachmentServiceImpl extends ServiceImpl<BasicAttachmentMappe
      */
     @Override
     @Transactional
-    public BasicAttachment saveAttachmentHtml(String fileName, String htmlContent, Long userId, List<String> localFileList) throws IOException {
+    public BasicAttachment saveAttachmentHtml(String fileName, String htmlContent, Long userId, List<PdfDto> localFileList) throws IOException {
         BasicAttachment basicAttachment = null;
         try {
             byte[] bytes = htmlContent.getBytes();
@@ -134,7 +135,8 @@ public class BasicAttachmentServiceImpl extends ServiceImpl<BasicAttachmentMappe
                     pdfFile.getParentFile().mkdirs();
                     pdfFile.createNewFile();
                 }
-                localFileList.add(pdfFile.getPath());
+                int pageCount = PdfUtil.addPdfPage(pdfFile);
+                localFileList.add(new PdfDto(pdfFile.getPath(), PageSizeEnum.A3, pageCount));
                 fileMd5 = DigestUtils.md5Hex(new FileInputStream(pdfFile));
                 ossUtil.ossUpload(pdfDirName, pdfFile, BinaryUtil.toBase64String(HexUtils.decodeHex(fileMd5)));
                 localHtmlFile.delete();
@@ -218,7 +220,7 @@ public class BasicAttachmentServiceImpl extends ServiceImpl<BasicAttachmentMappe
      */
     @Override
     @Transactional
-    public BasicAttachment saveAttachmentPdf(BasicAttachment basicAttachment, List<String> pdfList, Integer printCount) throws IOException, DocumentException {
+    public BasicAttachment saveAttachmentPdf(BasicAttachment basicAttachment, List<PdfDto> pdfList, Integer printCount) throws IOException, DocumentException {
         try {
             boolean oss = dictionaryConfig.sysDomain().isOss();
             JSONObject jsonObject = JSONObject.parseObject(basicAttachment.getPath());
@@ -244,9 +246,9 @@ public class BasicAttachmentServiceImpl extends ServiceImpl<BasicAttachmentMappe
                 pdfFile.createNewFile();
             }
             String fileMd5 = DigestUtils.md5Hex(new FileInputStream(pdfFile));
+            int pageCount = 0;
             if (oss) {//上传至oss
-                int pageCount = PdfUtil.addPdfPage(pdfFile);
-
+                pageCount = 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)) {
@@ -265,7 +267,7 @@ public class BasicAttachmentServiceImpl extends ServiceImpl<BasicAttachmentMappe
             }
             basicAttachment.setPath(jsonObject.toJSONString());
             for (int i = 0; i < printCount; i++) {
-                pdfList.add(pdfFile.getPath());
+                pdfList.add(new PdfDto(pdfFile.getPath(), PageSizeEnum.A4, pageCount));
             }
             this.updateById(basicAttachment);
         } catch (Exception e) {
@@ -279,24 +281,6 @@ public class BasicAttachmentServiceImpl extends ServiceImpl<BasicAttachmentMappe
         return basicAttachment;
     }
 
-    public static void main(String[] args) throws IOException, DocumentException {
-        File file = new File("/Users/king/Downloads/669ad9917f814bcabdd35201073885be.pdf");
-        PdfReader reader = new PdfReader(file.getPath());
-        int pageCount = reader.getNumberOfPages();
-        System.out.println("pageCount:" + pageCount);
-//
-        if (Math.abs(pageCount % 2) == 1) {//取偶
-            ByteArrayOutputStream baos = new ByteArrayOutputStream();
-            PdfStamper stamper = new PdfStamper(reader, baos);
-            stamper.insertPage(pageCount + 1, reader.getPageSizeWithRotation(1));
-            stamper.close();
-            FileOutputStream fileOutputStream = new FileOutputStream(file);
-            IOUtils.write(baos.toByteArray(), fileOutputStream);
-        }
-        pageCount = reader.getNumberOfPages();
-        System.out.println("pageCount:" + pageCount);
-    }
-
     /**
      * 删除附件
      *

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

@@ -5,14 +5,15 @@ import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.google.gson.Gson;
-import com.itextpdf.text.DocumentException;
 import com.qmth.boot.api.exception.ApiException;
-import com.qmth.distributed.print.business.bean.dto.ExamStudentDto;
 import com.qmth.distributed.print.business.bean.dto.FieldsDto;
+import com.qmth.distributed.print.business.bean.dto.PdfDto;
 import com.qmth.distributed.print.business.bean.dto.excel.UserImportDto;
 import com.qmth.distributed.print.business.bean.result.ExaminationResult;
 import com.qmth.distributed.print.business.entity.*;
-import com.qmth.distributed.print.business.enums.*;
+import com.qmth.distributed.print.business.enums.ExamStatusEnum;
+import com.qmth.distributed.print.business.enums.PrintPlanStatusEnum;
+import com.qmth.distributed.print.business.enums.UploadFileEnum;
 import com.qmth.distributed.print.business.service.*;
 import com.qmth.distributed.print.business.templete.service.TaskLogicService;
 import com.qmth.distributed.print.business.util.*;
@@ -21,7 +22,6 @@ import com.qmth.distributed.print.common.contant.SystemConstant;
 import com.qmth.distributed.print.common.enums.ExceptionResultEnum;
 import com.qmth.distributed.print.common.util.Base64Util;
 import com.qmth.distributed.print.common.util.ResultUtil;
-import org.apache.commons.codec.digest.DigestUtils;
 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
 import org.apache.poi.ss.usermodel.CellType;
 import org.apache.poi.ss.usermodel.Row;
@@ -98,10 +98,10 @@ public class TaskLogicServiceImpl implements TaskLogicService {
     CacheService cacheService;
 
     @Resource
-    FreemarkerUtil freemarkerUtil;
+    BasicCourseService basicCourseService;
 
     @Resource
-    BasicCourseService basicCourseService;
+    CreatePdfUtil createPdfUtil;
 
     /**
      * 执行导入用户逻辑
@@ -201,12 +201,12 @@ public class TaskLogicServiceImpl implements TaskLogicService {
                 examDetailCourseQueryWrapper.lambda().eq(ExamDetailCourse::getExamDetailId, examDetail.getId());
                 List<ExamDetailCourse> examDetailCourseList = detailCourseService.list(examDetailCourseQueryWrapper);
 
-                List<String> variablePdfList = new ArrayList<>();//变量印品(签到表、卷袋贴)
-                List<String> ordinaryPdfList = new ArrayList<>();//普通印品(登记表)
-                List<String> paperPdfList = new ArrayList<>();//所有试卷
-                List<String> examStudentPdfList = new ArrayList<>();//所有题卡
-                List<String> backupPaperPdfList = new ArrayList<>();//备份试卷
-                List<String> cardPdfList = new ArrayList<>();//备份题卡
+                List<PdfDto> variablePdfList = new ArrayList<>();//变量印品(签到表、卷袋贴)
+                List<PdfDto> ordinaryPdfList = new ArrayList<>();//普通印品(登记表)
+                List<PdfDto> paperPdfList = new ArrayList<>();//所有试卷
+                List<PdfDto> examStudentPdfList = new ArrayList<>();//所有题卡
+                List<PdfDto> backupPaperPdfList = new ArrayList<>();//备份试卷
+                List<PdfDto> cardPdfList = new ArrayList<>();//备份题卡
 
                 if (Objects.nonNull(examPrintPlan.getOrdinaryContent())) {
                     //获取普通印品
@@ -215,15 +215,15 @@ public class TaskLogicServiceImpl implements TaskLogicService {
                         JSONObject jsonObjectOrdinary = jsonArrayOrdinary.getJSONObject(i);
                         Long attachmentId = (Long) jsonObjectOrdinary.get("attachmentId");
                         BasicAttachment basicAttachment = basicAttachmentService.getById(attachmentId);
-                        createCheckIn(basicAttachment, ordinaryPdfList, (Integer) jsonObjectOrdinary.get("backupCount"));
+                        createPdfUtil.createCheckIn(basicAttachment, ordinaryPdfList, (Integer) jsonObjectOrdinary.get("backupCount"));
                     }
                 }
 
                 for (ExamDetailCourse examDetailCourse : examDetailCourseList) {
-                    List<String> paperPdfTempList = new ArrayList<>();//所有试卷
-                    List<String> examStudentTempPdfList = new ArrayList<>();//所有题卡
-                    List<String> backupPaperTempPdfList = new ArrayList<>();//备份试卷
-                    List<String> cardPdfTempList = new ArrayList<>();//备份题卡
+                    List<PdfDto> paperPdfTempList = new ArrayList<>();//所有试卷
+                    List<PdfDto> examStudentTempPdfList = new ArrayList<>();//所有题卡
+                    List<PdfDto> backupPaperTempPdfList = new ArrayList<>();//备份试卷
+                    List<PdfDto> cardPdfTempList = new ArrayList<>();//备份题卡
 
                     //查询试卷
                     QueryWrapper<ExamTask> examTaskQueryWrapper = new QueryWrapper<>();
@@ -256,23 +256,23 @@ public class TaskLogicServiceImpl implements TaskLogicService {
                         List<ExamCardDetail> examCardDetailList = examCardDetailService.list(examCardDetailQueryWrapper);
 
                         //抽取卷型
-                        String paperType = getPaperType(examPrintPlan, examTaskDetail);
+                        String paperType = createPdfUtil.getPaperType(examPrintPlan, examTaskDetail);
 
                         //获取试卷pdf
-                        getPaperPdf(examTaskDetail, examPrintPlan.getBackupCount(), paperPdfTempList, backupPaperTempPdfList);
+                        createPdfUtil.getPaperPdf(examTaskDetail, examPrintPlan.getBackupCount(), paperPdfTempList, backupPaperTempPdfList);
                         paperPdfList.addAll(paperPdfTempList);
                         backupPaperPdfList.addAll(backupPaperTempPdfList);
 
                         basicAttachmentList = Objects.isNull(basicAttachmentList) ? basicAttachmentList = new ArrayList<>() : basicAttachmentList;
                         for (ExamCardDetail examCardDetail : examCardDetailList) {
-                            getCardAttachmentId(examCardDetail, attachmentIds);
+                            createPdfUtil.getCardAttachmentId(examCardDetail, attachmentIds);
 
                             //把模板页面上的 ${} 替换成实际内容
-                            String cardContent = replaceHtmlTemplete(examCardDetail);
+                            String cardContent = createPdfUtil.replaceHtmlTemplete(examCardDetail);
                             String studentContent = cardContent;
 
                             for (int i = 1; i <= examPrintPlan.getBackupCount(); i++) {
-                                basicAttachmentList.add(cardHtml(cardContent, paperType, examDetailCourse, examCard, jsonArray, sysUser.getId(), cardPdfTempList));
+                                basicAttachmentList.add(createPdfUtil.cardHtml(cardContent, paperType, examDetailCourse, examCard, jsonArray, sysUser.getId(), cardPdfTempList));
                             }
                             cardPdfList.addAll(cardPdfTempList);
 
@@ -292,15 +292,15 @@ public class TaskLogicServiceImpl implements TaskLogicService {
                                     BasicAttachment basicAttachment = basicAttachmentService.getById(attachmentId);
                                     ftlList.add(commonService.getFile(basicAttachment.getPath(), false));
                                     if (Objects.nonNull(type) && Objects.equals(type.toUpperCase(), "SIGN")) {//签到表
-                                        createSignBook(basicAttachment, basicSchool.getName(), examDetailCourse, examStudentList, variablePdfList, (Integer) jsonObjectVariable.get("backupCount"));
+                                        createPdfUtil.createSignBook(basicAttachment, basicSchool.getName(), examDetailCourse, examStudentList, variablePdfList, (Integer) jsonObjectVariable.get("backupCount"));
                                     } else if (Objects.nonNull(type) && Objects.equals(type.toUpperCase(), "PACKAG")) {//卷袋贴
-                                        createPaperPackage(basicAttachment, basicSchool.getName(), examDetailCourse, examStudentList, variablePdfList, (Integer) jsonObjectVariable.get("backupCount"));
+                                        createPdfUtil.createPaperPackage(basicAttachment, basicSchool.getName(), examDetailCourse, examStudentList, variablePdfList, (Integer) jsonObjectVariable.get("backupCount"));
                                     }
                                 }
                             }
 
                             for (ExamStudent t : examStudentList) {
-                                basicAttachmentList.add(examStudentHtml(attachmentIds, studentContent, t, paperType, examCard, sysUser.getId(), examStudentTempPdfList));
+                                basicAttachmentList.add(createPdfUtil.examStudentHtml(attachmentIds, studentContent, t, paperType, examCard, sysUser.getId(), examStudentTempPdfList));
                             }
                             examStudentPdfList.addAll(examStudentTempPdfList);
                             examStudentService.saveOrUpdateBatch(examStudentList);
@@ -313,18 +313,18 @@ public class TaskLogicServiceImpl implements TaskLogicService {
 
                     Integer paperPageA3 = 0;
                     Integer cardPageA3 = 0;
-                    paperPageA3 = paperPageA3 + paperPdfTempList.size() + backupPaperTempPdfList.size();
-                    cardPageA3 = cardPageA3 + examStudentTempPdfList.size() + cardPdfTempList.size();
+                    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
-                basicAttachmentList.add(mergePdf(tbTask, examDetail, sysUser.getId(), variablePdfList, ordinaryPdfList, paperPdfList, examStudentPdfList, backupPaperPdfList, cardPdfList));
+                basicAttachmentList.add(createPdfUtil.mergePdf(tbTask, examDetail, sysUser.getId(), variablePdfList, ordinaryPdfList, paperPdfList, examStudentPdfList, backupPaperPdfList, cardPdfList));
             }
             map.computeIfAbsent("size", v -> examDetailList.size());
             //最后一步删除附件
-            deleteAttachment(attachmentIds, ftlList);
+            createPdfUtil.deleteAttachment(attachmentIds, ftlList);
         } catch (Exception e) {
             log.error("请求出错", e);
             basicAttachmentService.batchDeleteAttachment(basicAttachmentList);
@@ -415,7 +415,7 @@ public class TaskLogicServiceImpl implements TaskLogicService {
         }
 
         PrintPlanStatusEnum printPlanStatus = examPrintPlan.getStatus();
-        if (PrintPlanStatusEnum.NEW != printPlanStatus && PrintPlanStatusEnum.READY != printPlanStatus){
+        if (PrintPlanStatusEnum.NEW != printPlanStatus && PrintPlanStatusEnum.READY != printPlanStatus) {
             throw ExceptionResultEnum.ERROR.exception(printPlanStatus + "状态下的印刷计划不允许导入");
         }
 
@@ -529,10 +529,10 @@ public class TaskLogicServiceImpl implements TaskLogicService {
                 }
             }
             // 校验课程信息
-            basicCourseService.verifyCourseInfo(schoolId,courseCode,courseName,userId);
+            basicCourseService.verifyCourseInfo(schoolId, courseCode, courseName, userId);
 
             // 解析时间
-            Map<String,Object> timeMap = ConvertUtil.analyzeStartAndEndTime(examDate,examTime);
+            Map<String, Object> timeMap = ConvertUtil.analyzeStartAndEndTime(examDate, examTime);
             String examStartTime = String.valueOf(timeMap.get("startTime"));
             String examEndTime = String.valueOf(timeMap.get("endTime"));
 
@@ -572,386 +572,11 @@ public class TaskLogicServiceImpl implements TaskLogicService {
             return Stream.of(tmp);
         }).distinct().collect(Collectors.toList());
         for (Map<String, Object> stringObjectMap : checkList) {
-            Long checkSchoolId =  SystemConstant.convertIdToLong(String.valueOf(stringObjectMap.get("schoolId")));
-            String checkCourseCode =  String.valueOf(stringObjectMap.get("courseCode"));
-            String checkPaperNumber =  String.valueOf(stringObjectMap.get("paperNumber"));
-            commonService.checkData(checkSchoolId,checkCourseCode,checkPaperNumber);
+            Long checkSchoolId = SystemConstant.convertIdToLong(String.valueOf(stringObjectMap.get("schoolId")));
+            String checkCourseCode = String.valueOf(stringObjectMap.get("courseCode"));
+            String checkPaperNumber = String.valueOf(stringObjectMap.get("paperNumber"));
+            commonService.checkData(checkSchoolId, checkCourseCode, checkPaperNumber);
         }
         return null;
     }
-
-    /**
-     * 创建登记表
-     *
-     * @param basicAttachment
-     * @param ordinaryPdfList
-     * @param printCount
-     */
-    private void createCheckIn(BasicAttachment basicAttachment, List<String> ordinaryPdfList, Integer printCount) throws IOException, DocumentException {
-        String type = basicAttachment.getType();
-        JSONObject jsonObject = JSONObject.parseObject(basicAttachment.getPath());
-        if (Objects.nonNull(type) && Objects.equals(type, SystemConstant.PDF_PREFIX)) {
-            String filePath = (String) jsonObject.get(SystemConstant.PATH);
-            String url = SystemConstant.TEMP_FILES_DIR + File.separator + filePath;
-            File localFile = ossUtil.ossDownload(filePath, url);
-            for (int i = 0; i < printCount; i++) {
-                ordinaryPdfList.add(localFile.getPath());
-            }
-        } else {
-            basicAttachmentService.saveAttachmentPdf(basicAttachment, ordinaryPdfList, printCount);
-        }
-    }
-
-    /**
-     * 生成卷袋贴
-     *
-     * @param basicAttachment
-     * @param schoolName
-     * @param examDetailCourse
-     * @param examStudentList
-     * @param variablePdfList
-     * @param printCount
-     * @throws IOException
-     */
-    private void createPaperPackage(BasicAttachment basicAttachment, String schoolName, ExamDetailCourse examDetailCourse, List<ExamStudent> examStudentList, List<String> variablePdfList, Integer printCount) throws IOException {
-        Map<String, Object> htmlMap = new HashMap<>();
-        htmlMap.put("examDetailCourseId", examDetailCourse.getId());
-        htmlMap.put("schoolName", schoolName);
-        htmlMap.put("examName", "测试exam1");
-        htmlMap.put("courseName", examDetailCourse.getCourseName());
-        htmlMap.put("courseCode", examDetailCourse.getCourseCode());
-        htmlMap.put("examSite", "测试examSite1");
-        htmlMap.put("examRoom", "测试examRoom1");
-        htmlMap.put("startTime", "2021-04-13 00:00:00");
-        htmlMap.put("endTime", "2021-04-14 00:00:00");
-        htmlMap.put("paperCode", examDetailCourse.getPaperNumber());
-        htmlMap.put("paperCodeImg", GoogleBarCodeUtil.createBarCode(examDetailCourse.getPaperNumber(), false));
-
-        htmlMap.put("startCollege", "测试startCollege1");
-        htmlMap.put("className", "测试className1");
-        htmlMap.put("proctorCollege", "测试proctorCollege1");
-        htmlMap.put("examManager", "测试examManager1");
-
-        htmlMap.put("minSite", "测试minSite1");
-        htmlMap.put("maxSite", "测试maxSite1");
-        htmlMap.put("studentCount", examStudentList.size());
-
-        htmlMap.put("tag", "测试tag1");
-
-        htmlMap.computeIfAbsent("basicAttachment", v -> basicAttachment);
-        htmlMap.computeIfAbsent("variablePdfList", v -> variablePdfList);
-        htmlMap.computeIfAbsent("printCount", v -> printCount);
-        freemarkerUtil.createPaperPackage(htmlMap);
-    }
-
-    /**
-     * 创建签到表
-     *
-     * @param basicAttachment
-     * @param schoolName
-     * @param examDetailCourse
-     * @param examStudentList
-     * @param variablePdfList
-     * @param printCount
-     */
-    private void createSignBook(BasicAttachment basicAttachment, String schoolName, ExamDetailCourse examDetailCourse, List<ExamStudent> examStudentList, List<String> variablePdfList, Integer printCount) throws IOException {
-        Map<String, Object> htmlMap = new HashMap<>();
-        htmlMap.put("examDetailCourseId", examDetailCourse.getId());
-        htmlMap.put("schoolName", schoolName);
-        htmlMap.put("courseName", examDetailCourse.getCourseName());
-        htmlMap.put("courseCode", examDetailCourse.getCourseCode());
-        htmlMap.put("examName", "测试exam1");
-        htmlMap.put("examRoom", "测试examRoom1");
-        htmlMap.put("classTeacher", "测试老师1");
-        htmlMap.put("startTime", "2021-04-13 00:00:00");
-        htmlMap.put("endTime", "2021-04-14 00:00:00");
-        htmlMap.put("paperCode", examDetailCourse.getPaperNumber());
-        htmlMap.put("paperCodeImg", GoogleBarCodeUtil.createBarCode(examDetailCourse.getPaperNumber(), false));
-
-        int totalCount = examStudentList.size();
-        if (totalCount > 0) {
-            List<Map> subList = new ArrayList<>();
-            int pageCount = totalCount % 80 == 0 ? totalCount / 80 : totalCount / 80 + 1;
-            for (int i = 0; i < pageCount; i++) {
-                Map subMap = new HashMap();
-                subMap.put("index", i + 1);
-                int studentCount;
-                List<ExamStudent> subStudents;
-                if (pageCount == 1) {
-                    studentCount = totalCount;
-                    subStudents = examStudentList;
-                } else if (pageCount > 1 && i == pageCount - 1) {
-                    studentCount = totalCount - (pageCount - 1) * 80;
-                    subStudents = examStudentList.subList(80 * i, examStudentList.size());
-                } else {
-                    studentCount = 80;
-                    subStudents = examStudentList.subList(80 * i, 80 * (i + 1));
-                }
-                subMap.put("studentCount", studentCount);
-                List<ExamStudentDto> examStudentList1 = new ArrayList<>();
-                List<ExamStudentDto> examStudentList2 = new ArrayList<>();
-
-                Gson gson = new Gson();
-                int mod = subStudents.size() % 2;
-                int htmlTableCount = mod == 0 ? subStudents.size() / 2 : subStudents.size() / 2 + 1;
-                for (int j = 0; j < htmlTableCount; j++) {
-                    ExamStudentDto examStudentDto = gson.fromJson(gson.toJson(subStudents.get(j)), ExamStudentDto.class);
-                    examStudentDto.setIndex(j + 1);
-                    examStudentList1.add(examStudentDto);
-                }
-                for (int j = htmlTableCount; j < subStudents.size(); j++) {
-                    ExamStudentDto examStudentDto = gson.fromJson(gson.toJson(subStudents.get(j)), ExamStudentDto.class);
-                    examStudentDto.setIndex(j + 1);
-                    examStudentList2.add(examStudentDto);
-                }
-                if (examStudentList1.size() > examStudentList2.size()) {
-                    for (int j = subStudents.size() - htmlTableCount; j < examStudentList1.size(); j++) {
-                        examStudentList2.add(new ExamStudentDto());
-                    }
-                }
-                subMap.put("studentList1", examStudentList1);
-                subMap.put("studentList2", examStudentList2);
-                subList.add(subMap);
-            }
-            htmlMap.put("subList", subList);
-        }
-        htmlMap.computeIfAbsent("basicAttachment", v -> basicAttachment);
-        htmlMap.computeIfAbsent("variablePdfList", v -> variablePdfList);
-        htmlMap.computeIfAbsent("printCount", v -> printCount);
-        freemarkerUtil.createSignBook(htmlMap);
-    }
-
-
-    /**
-     * 替换html通用模版
-     *
-     * @param examCardDetail
-     * @return
-     */
-    public String replaceHtmlTemplete(ExamCardDetail examCardDetail) {
-        String content = examCardDetail.getHtmlContent();
-        log.info("contentTemp:{}", content);
-        content = content.replaceAll("<img src=\"data:image/png;base64,\\$\\{examNumber\\}\">", "");
-        content = content.replaceAll("\\$\\{examNumberStr\\}", "");
-        return content;
-    }
-
-    /**
-     * 获取题卡attachmentId
-     *
-     * @param examCardDetail
-     * @param attachmentIds
-     */
-    public void getCardAttachmentId(ExamCardDetail examCardDetail, Set<Long> attachmentIds) {
-        if (Objects.nonNull(examCardDetail.getAttachmentId())) {
-            JSONObject jsonObjectCard = JSONObject.parseObject(examCardDetail.getAttachmentId());
-            JSONArray jsonArrayCard = (JSONArray) jsonObjectCard.get("card");
-            for (int i = 0; i < jsonArrayCard.size(); i++) {
-                JSONObject object = (JSONObject) jsonArrayCard.get(i);
-                if (Objects.nonNull(object.get("attachmentId"))) {
-                    attachmentIds.add(Long.parseLong(String.valueOf(object.get("attachmentId"))));
-                }
-            }
-        }
-    }
-
-    /**
-     * 获取试卷pdf
-     *
-     * @param examTaskDetail
-     * @param backupCount
-     * @param pdfList
-     * @return
-     * @throws IOException
-     */
-    public void getPaperPdf(ExamTaskDetail examTaskDetail, Integer backupCount, List<String>... pdfList) throws IOException {
-        JSONObject jsonObjectPaper = JSONObject.parseObject(examTaskDetail.getPaperAttachmentIds());
-        JSONArray jsonArrayPaper = jsonObjectPaper.getJSONArray("paper");
-        for (int i = 0; i < jsonArrayPaper.size(); i++) {
-            JSONObject object = (JSONObject) jsonArrayPaper.get(i);
-            if (Objects.nonNull(object.get("attachmentId"))) {
-                Long attachmentId = Long.parseLong(String.valueOf(object.get("attachmentId")));
-                BasicAttachment basicAttachment = basicAttachmentService.getById(attachmentId);
-                String filePath = commonService.getFile(basicAttachment.getPath(), false).getPath();
-                pdfList[0].add(filePath);
-                for (int j = 1; j <= backupCount; j++) {
-                    pdfList[1].add(filePath);
-                }
-            }
-        }
-    }
-
-    /**
-     * 抽取试卷
-     *
-     * @param examPrintPlan
-     * @param examTaskDetail
-     * @return
-     */
-    public String getPaperType(ExamPrintPlan examPrintPlan, ExamTaskDetail examTaskDetail) {
-        //抽取卷型
-        DrawRuleEnum drawRule = Objects.nonNull(examPrintPlan.getDrawRule()) ? examPrintPlan.getDrawRule() : DrawRuleEnum.ONE;
-        //未曝光卷型
-        String unexposedPaperType = examTaskDetail.getUnexposedPaperType();
-        //已曝光卷型
-        String exposedPaperType = examTaskDetail.getExposedPaperType();
-        String[] paperTypes = null;
-        if (drawRule == DrawRuleEnum.ONE) {
-            if (Objects.isNull(unexposedPaperType)) {
-                throw ExceptionResultEnum.ERROR.exception("当前没有未曝光的卷型");
-            } else {
-                paperTypes = unexposedPaperType.split("/");
-            }
-        } else {
-            if (Objects.isNull(exposedPaperType) && Objects.isNull(unexposedPaperType)) {
-                throw ExceptionResultEnum.ERROR.exception("当前没有未曝光的卷型");
-            }
-            if (Objects.nonNull(unexposedPaperType)) {
-                paperTypes = unexposedPaperType.split("/");
-            } else {
-                paperTypes = exposedPaperType.split("/");
-            }
-        }
-        int paperRandom = new Random().nextInt(paperTypes.length);
-        String paperType = paperTypes[paperRandom];
-        examTaskDetail.setPaperType(paperType);
-        return paperType;
-    }
-
-    /**
-     * 合并pdf
-     *
-     * @param tbTask
-     * @param examDetail
-     * @param userId
-     * @param list
-     * @return
-     * @throws IOException
-     */
-    public BasicAttachment mergePdf(TBTask tbTask, ExamDetail examDetail, Long userId, List<String>... list) throws IOException {
-        StringJoiner stringJoiner = new StringJoiner("").add(SystemConstant.PDF_TEMP_FILES_DIR).add(File.separator);
-        List<String> mergePdfList = new ArrayList<>();
-        int pageA3Count = 0;
-        int pageA4Count = 0;
-        for (int i = 0; i < list.length; i++) {
-            if (i < 2) {
-                pageA4Count = pageA4Count + list[i].size();
-            } else {
-                pageA3Count = pageA3Count + list[i].size();
-            }
-            mergePdfList.addAll(list[i]);
-        }
-        String dirName = PdfUtil.mergePdf(mergePdfList.toArray(new String[mergePdfList.size()]), null);
-        File localPdfFile = new File(stringJoiner.toString() + File.separator + dirName);
-        BasicAttachment basicAttachment = basicAttachmentService.saveAttachmentPdf(dirName, userId);
-        tbTask.setImportFileName(basicAttachment.getName());
-        tbTask.setImportFilePath(basicAttachment.getPath());
-        examDetail.setAttachmentId(basicAttachment.getId());
-        examDetail.setPagesA3(pageA3Count);
-        examDetail.setPagesA4(pageA4Count);
-        examDetail.setStatus(ExamDetailStatusEnum.READY);
-        detailService.saveOrUpdate(examDetail);
-        ossUtil.ossUpload(dirName, localPdfFile, DigestUtils.md5Hex(new FileInputStream(localPdfFile)));
-        localPdfFile.delete();
-        for (String s : mergePdfList) {
-            new File(s).delete();
-        }
-        return basicAttachment;
-    }
-
-    /**
-     * 删除附件
-     *
-     * @param attachmentIds
-     * @param ftlList
-     */
-    public void deleteAttachment(Set<Long> attachmentIds, Set<File> ftlList) {
-        if (Objects.nonNull(attachmentIds) && attachmentIds.size() > 0) {
-            QueryWrapper<BasicAttachment> basicAttachmentQueryWrapper = new QueryWrapper<>();
-            basicAttachmentQueryWrapper.lambda().in(BasicAttachment::getId, attachmentIds);
-            basicAttachmentService.batchDeleteAttachment(basicAttachmentService.list(basicAttachmentQueryWrapper));
-            basicAttachmentService.removeByIds(attachmentIds);
-        }
-        if (Objects.nonNull(ftlList) && ftlList.size() > 0) {
-            for (File file : ftlList) {
-                file.delete();
-            }
-        }
-    }
-
-    /**
-     * 考生题卡html
-     *
-     * @param attachmentIds
-     * @param studentContent
-     * @param t
-     * @param paperType
-     * @param examCard
-     * @param userId
-     * @param examStudentPdfList
-     * @return
-     * @throws IOException
-     */
-    public BasicAttachment examStudentHtml(Set<Long> attachmentIds, String studentContent, ExamStudent t, String paperType, ExamCard examCard, Long userId, List<String> examStudentPdfList) throws IOException {
-        if (Objects.nonNull(t.getAttachmentId())) {
-            attachmentIds.add(t.getAttachmentId());
-        }
-        String studentHtml = studentContent;
-        if (Objects.nonNull(t.getExtendFields())) {
-            JSONArray jsonObjectExtend = (JSONArray) JSONArray.parse(t.getExtendFields());//扩展字段
-            if (Objects.nonNull(jsonObjectExtend)) {
-                for (int i = 0; i < jsonObjectExtend.size(); i++) {
-                    JSONObject object = (JSONObject) jsonObjectExtend.get(i);
-                    studentHtml = studentHtml.replaceAll("\\$\\{" + object.get("code") + "\\}", String.valueOf(object.get("value")));
-                }
-            }
-        }
-        //生成学生考号条码并将图片转成base64
-        studentHtml = studentHtml.replaceAll("\\$\\{examNumber\\}", GoogleBarCodeUtil.createBarCode(t.getTicketNumber(), false));
-        studentHtml = studentHtml.replaceAll("\\$\\{examNumberStr\\}", t.getTicketNumber());
-        //随机生成学生试卷条码并将图片转成base64
-        studentHtml = studentHtml.replaceAll("\\$\\{paperType\\}", GoogleBarCodeUtil.createBarCode(SystemConstant.convertPaperType(paperType), false));
-        studentHtml = studentHtml.replaceAll("\\$\\{paperTypeName\\}", paperType);
-        studentHtml = studentHtml.replaceAll("\\$\\{studentCode\\}", t.getStudentCode());
-        studentHtml = studentHtml.replaceAll("\\$\\{name\\}", t.getStudentName());
-        studentHtml = studentHtml.replaceAll("\\$\\{courseName\\}", examCard.getCourseName());
-
-        //学生题卡
-        BasicAttachment examStudentAttachment = basicAttachmentService.saveAttachmentHtml(examCard.getSchoolId() + "|" + examCard.getCourseCode() + "|" + t.getTicketNumber(), studentHtml, userId, examStudentPdfList);
-        t.setAttachmentId(examStudentAttachment.getId());
-        return examStudentAttachment;
-    }
-
-    /**
-     * 通用题卡html
-     *
-     * @param cardContent
-     * @param paperType
-     * @param examDetailCourse
-     * @param examCard
-     * @param jsonArray
-     * @param userId
-     * @param cardPdfList
-     * @return
-     * @throws IOException
-     */
-    public BasicAttachment cardHtml(String cardContent, String paperType, ExamDetailCourse examDetailCourse, ExamCard examCard, JSONArray jsonArray, Long userId, List<String> cardPdfList) throws IOException {
-        //通用题卡
-        String cardTemp = cardContent;
-        cardTemp = cardTemp.replaceAll("\\$\\{paperTypeName\\}", paperType);
-        //随机生成试卷条码并将图片转成base64
-        cardTemp = cardTemp.replaceAll("\\$\\{paperType\\}", GoogleBarCodeUtil.createBarCode(SystemConstant.convertPaperType(paperType), false));
-        //通用题卡生成卷袋贴条码
-        String paperCode = examDetailCourse.getPaperNumber();
-        String paperCodeImg = GoogleBarCodeUtil.createBarCode(paperCode, false);
-        String paperCodeDiv = "<div class=\"page-box page-box-0\"><div class=\"package-number\" style=\"position: absolute;width: 200px;height: 40px;top: 80px;right: 35px;transform: rotate(-90deg);transform-origin: center right;text-align: center;z-index: 99;\"><img src=\"data:image/png;base64," + paperCodeImg + "\" style=\"display: block; height: 28px; width: 100%\" /><p style=\"line-height: 1; font-size: 12px; margin: 0;\">" + paperCode + "</p></div>";
-        cardTemp = cardTemp.replaceAll("<div class=\"page-box page-box-0\">", paperCodeDiv);
-        BasicAttachment cardAttachment = basicAttachmentService.saveAttachmentHtml(examCard.getSchoolId() + "|" + examCard.getCourseCode(), cardTemp, userId, cardPdfList);
-        JSONObject object = new JSONObject();
-        object.put("name", paperType);
-        object.put("examDetailCourseId", examDetailCourse.getId());
-        object.put("attachmentId", cardAttachment.getId());
-        jsonArray.add(object);
-        return cardAttachment;
-    }
 }

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

@@ -0,0 +1,433 @@
+package com.qmth.distributed.print.business.util;
+
+import com.alibaba.fastjson.JSONArray;
+import com.alibaba.fastjson.JSONObject;
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.google.gson.Gson;
+import com.itextpdf.text.DocumentException;
+import com.qmth.distributed.print.business.bean.dto.ExamStudentDto;
+import com.qmth.distributed.print.business.bean.dto.PdfDto;
+import com.qmth.distributed.print.business.entity.*;
+import com.qmth.distributed.print.business.enums.DrawRuleEnum;
+import com.qmth.distributed.print.business.enums.ExamDetailStatusEnum;
+import com.qmth.distributed.print.business.enums.PageSizeEnum;
+import com.qmth.distributed.print.business.service.BasicAttachmentService;
+import com.qmth.distributed.print.business.service.CommonService;
+import com.qmth.distributed.print.business.service.ExamDetailService;
+import com.qmth.distributed.print.common.contant.SystemConstant;
+import com.qmth.distributed.print.common.enums.ExceptionResultEnum;
+import org.apache.commons.codec.digest.DigestUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.stereotype.Component;
+
+import javax.annotation.Resource;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.util.*;
+import java.util.stream.Collectors;
+
+/**
+ * @Description: 创建pdf util
+ * @Param:
+ * @return:
+ * @Author: wangliang
+ * @Date: 2021/4/14
+ */
+@Component
+public class CreatePdfUtil {
+    private final static Logger log = LoggerFactory.getLogger(CreatePdfUtil.class);
+
+    @Resource
+    OssUtil ossUtil;
+
+    @Resource
+    BasicAttachmentService basicAttachmentService;
+
+    @Resource
+    FreemarkerUtil freemarkerUtil;
+
+    @Resource
+    CommonService commonService;
+
+    @Resource
+    ExamDetailService detailService;
+
+    /**
+     * 创建登记表
+     *
+     * @param basicAttachment
+     * @param ordinaryPdfList
+     * @param printCount
+     */
+    public void createCheckIn(BasicAttachment basicAttachment, List<PdfDto> ordinaryPdfList, Integer printCount) throws IOException, DocumentException {
+        String type = basicAttachment.getType();
+        JSONObject jsonObject = JSONObject.parseObject(basicAttachment.getPath());
+        if (Objects.nonNull(type) && Objects.equals(type, SystemConstant.PDF_PREFIX)) {
+            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);
+            for (int i = 0; i < printCount; i++) {
+                ordinaryPdfList.add(new PdfDto(localFile.getPath(), PageSizeEnum.A4, pageCount));
+            }
+        } else {
+            basicAttachmentService.saveAttachmentPdf(basicAttachment, ordinaryPdfList, printCount);
+        }
+    }
+
+    /**
+     * 生成卷袋贴
+     *
+     * @param basicAttachment
+     * @param schoolName
+     * @param examDetailCourse
+     * @param examStudentList
+     * @param variablePdfList
+     * @param printCount
+     * @throws IOException
+     */
+    public void createPaperPackage(BasicAttachment basicAttachment, String schoolName, ExamDetailCourse examDetailCourse, List<ExamStudent> examStudentList, List<PdfDto> variablePdfList, Integer printCount) throws IOException {
+        Map<String, Object> htmlMap = new HashMap<>();
+        htmlMap.put("examDetailCourseId", examDetailCourse.getId());
+        htmlMap.put("schoolName", schoolName);
+        htmlMap.put("examName", "测试exam1");
+        htmlMap.put("courseName", examDetailCourse.getCourseName());
+        htmlMap.put("courseCode", examDetailCourse.getCourseCode());
+        htmlMap.put("examSite", "测试examSite1");
+        htmlMap.put("examRoom", "测试examRoom1");
+        htmlMap.put("startTime", "2021-04-13 00:00:00");
+        htmlMap.put("endTime", "2021-04-14 00:00:00");
+        htmlMap.put("paperCode", examDetailCourse.getPaperNumber());
+        htmlMap.put("paperCodeImg", GoogleBarCodeUtil.createBarCode(examDetailCourse.getPaperNumber(), false));
+
+        htmlMap.put("startCollege", "测试startCollege1");
+        htmlMap.put("className", "测试className1");
+        htmlMap.put("proctorCollege", "测试proctorCollege1");
+        htmlMap.put("examManager", "测试examManager1");
+
+        htmlMap.put("minSite", "测试minSite1");
+        htmlMap.put("maxSite", "测试maxSite1");
+        htmlMap.put("studentCount", examStudentList.size());
+
+        htmlMap.put("tag", "测试tag1");
+
+        htmlMap.computeIfAbsent("basicAttachment", v -> basicAttachment);
+        htmlMap.computeIfAbsent("variablePdfList", v -> variablePdfList);
+        htmlMap.computeIfAbsent("printCount", v -> printCount);
+        freemarkerUtil.createPaperPackage(htmlMap);
+    }
+
+    /**
+     * 创建签到表
+     *
+     * @param basicAttachment
+     * @param schoolName
+     * @param examDetailCourse
+     * @param examStudentList
+     * @param variablePdfList
+     * @param printCount
+     */
+    public void createSignBook(BasicAttachment basicAttachment, String schoolName, ExamDetailCourse examDetailCourse, List<ExamStudent> examStudentList, List<PdfDto> variablePdfList, Integer printCount) throws IOException {
+        Map<String, Object> htmlMap = new HashMap<>();
+        htmlMap.put("examDetailCourseId", examDetailCourse.getId());
+        htmlMap.put("schoolName", schoolName);
+        htmlMap.put("courseName", examDetailCourse.getCourseName());
+        htmlMap.put("courseCode", examDetailCourse.getCourseCode());
+        htmlMap.put("examName", "测试exam1");
+        htmlMap.put("examRoom", "测试examRoom1");
+        htmlMap.put("classTeacher", "测试老师1");
+        htmlMap.put("startTime", "2021-04-13 00:00:00");
+        htmlMap.put("endTime", "2021-04-14 00:00:00");
+        htmlMap.put("paperCode", examDetailCourse.getPaperNumber());
+        htmlMap.put("paperCodeImg", GoogleBarCodeUtil.createBarCode(examDetailCourse.getPaperNumber(), false));
+
+        int totalCount = examStudentList.size();
+        if (totalCount > 0) {
+            List<Map> subList = new ArrayList<>();
+            int pageCount = totalCount % 80 == 0 ? totalCount / 80 : totalCount / 80 + 1;
+            for (int i = 0; i < pageCount; i++) {
+                Map subMap = new HashMap();
+                subMap.put("index", i + 1);
+                int studentCount;
+                List<ExamStudent> subStudents;
+                if (pageCount == 1) {
+                    studentCount = totalCount;
+                    subStudents = examStudentList;
+                } else if (pageCount > 1 && i == pageCount - 1) {
+                    studentCount = totalCount - (pageCount - 1) * 80;
+                    subStudents = examStudentList.subList(80 * i, examStudentList.size());
+                } else {
+                    studentCount = 80;
+                    subStudents = examStudentList.subList(80 * i, 80 * (i + 1));
+                }
+                subMap.put("studentCount", studentCount);
+                List<ExamStudentDto> examStudentList1 = new ArrayList<>();
+                List<ExamStudentDto> examStudentList2 = new ArrayList<>();
+
+                Gson gson = new Gson();
+                int mod = subStudents.size() % 2;
+                int htmlTableCount = mod == 0 ? subStudents.size() / 2 : subStudents.size() / 2 + 1;
+                for (int j = 0; j < htmlTableCount; j++) {
+                    ExamStudentDto examStudentDto = gson.fromJson(gson.toJson(subStudents.get(j)), ExamStudentDto.class);
+                    examStudentDto.setIndex(j + 1);
+                    examStudentList1.add(examStudentDto);
+                }
+                for (int j = htmlTableCount; j < subStudents.size(); j++) {
+                    ExamStudentDto examStudentDto = gson.fromJson(gson.toJson(subStudents.get(j)), ExamStudentDto.class);
+                    examStudentDto.setIndex(j + 1);
+                    examStudentList2.add(examStudentDto);
+                }
+                if (examStudentList1.size() > examStudentList2.size()) {
+                    for (int j = subStudents.size() - htmlTableCount; j < examStudentList1.size(); j++) {
+                        examStudentList2.add(new ExamStudentDto());
+                    }
+                }
+                subMap.put("studentList1", examStudentList1);
+                subMap.put("studentList2", examStudentList2);
+                subList.add(subMap);
+            }
+            htmlMap.put("subList", subList);
+        }
+        htmlMap.computeIfAbsent("basicAttachment", v -> basicAttachment);
+        htmlMap.computeIfAbsent("variablePdfList", v -> variablePdfList);
+        htmlMap.computeIfAbsent("printCount", v -> printCount);
+        freemarkerUtil.createSignBook(htmlMap);
+    }
+
+    /**
+     * 替换html通用模版
+     *
+     * @param examCardDetail
+     * @return
+     */
+    public String replaceHtmlTemplete(ExamCardDetail examCardDetail) {
+        String content = examCardDetail.getHtmlContent();
+        log.info("contentTemp:{}", content);
+        content = content.replaceAll("<img src=\"data:image/png;base64,\\$\\{examNumber\\}\">", "");
+        content = content.replaceAll("\\$\\{examNumberStr\\}", "");
+        return content;
+    }
+
+    /**
+     * 获取题卡attachmentId
+     *
+     * @param examCardDetail
+     * @param attachmentIds
+     */
+    public void getCardAttachmentId(ExamCardDetail examCardDetail, Set<Long> attachmentIds) {
+        if (Objects.nonNull(examCardDetail.getAttachmentId())) {
+            JSONObject jsonObjectCard = JSONObject.parseObject(examCardDetail.getAttachmentId());
+            JSONArray jsonArrayCard = (JSONArray) jsonObjectCard.get("card");
+            for (int i = 0; i < jsonArrayCard.size(); i++) {
+                JSONObject object = (JSONObject) jsonArrayCard.get(i);
+                if (Objects.nonNull(object.get("attachmentId"))) {
+                    attachmentIds.add(Long.parseLong(String.valueOf(object.get("attachmentId"))));
+                }
+            }
+        }
+    }
+
+    /**
+     * 获取试卷pdf
+     *
+     * @param examTaskDetail
+     * @param backupCount
+     * @param pdfList
+     * @return
+     * @throws IOException
+     */
+    public void getPaperPdf(ExamTaskDetail examTaskDetail, Integer backupCount, List<PdfDto>... pdfList) throws IOException {
+        JSONObject jsonObjectPaper = JSONObject.parseObject(examTaskDetail.getPaperAttachmentIds());
+        JSONArray jsonArrayPaper = jsonObjectPaper.getJSONArray("paper");
+        for (int i = 0; i < jsonArrayPaper.size(); i++) {
+            JSONObject object = (JSONObject) jsonArrayPaper.get(i);
+            if (Objects.nonNull(object.get("attachmentId"))) {
+                Long attachmentId = Long.parseLong(String.valueOf(object.get("attachmentId")));
+                BasicAttachment basicAttachment = basicAttachmentService.getById(attachmentId);
+                File file = commonService.getFile(basicAttachment.getPath(), false);
+                int pageCount = PdfUtil.addPdfPage(file);
+                pdfList[0].add(new PdfDto(file.getPath(), PageSizeEnum.A3, pageCount));
+                for (int j = 1; j <= backupCount; j++) {
+                    pdfList[1].add(new PdfDto(file.getPath(), PageSizeEnum.A3, pageCount));
+                }
+            }
+        }
+    }
+
+    /**
+     * 抽取试卷
+     *
+     * @param examPrintPlan
+     * @param examTaskDetail
+     * @return
+     */
+    public String getPaperType(ExamPrintPlan examPrintPlan, ExamTaskDetail examTaskDetail) {
+        //抽取卷型
+        DrawRuleEnum drawRule = Objects.nonNull(examPrintPlan.getDrawRule()) ? examPrintPlan.getDrawRule() : DrawRuleEnum.ONE;
+        //未曝光卷型
+        String unexposedPaperType = examTaskDetail.getUnexposedPaperType();
+        //已曝光卷型
+        String exposedPaperType = examTaskDetail.getExposedPaperType();
+        String[] paperTypes = null;
+        if (drawRule == DrawRuleEnum.ONE) {
+            if (Objects.isNull(unexposedPaperType)) {
+                throw ExceptionResultEnum.ERROR.exception("当前没有未曝光的卷型");
+            } else {
+                paperTypes = unexposedPaperType.split("/");
+            }
+        } else {
+            if (Objects.isNull(exposedPaperType) && Objects.isNull(unexposedPaperType)) {
+                throw ExceptionResultEnum.ERROR.exception("当前没有未曝光的卷型");
+            }
+            if (Objects.nonNull(unexposedPaperType)) {
+                paperTypes = unexposedPaperType.split("/");
+            } else {
+                paperTypes = exposedPaperType.split("/");
+            }
+        }
+        int paperRandom = new Random().nextInt(paperTypes.length);
+        String paperType = paperTypes[paperRandom];
+        examTaskDetail.setPaperType(paperType);
+        return paperType;
+    }
+
+    /**
+     * 合并pdf
+     *
+     * @param tbTask
+     * @param examDetail
+     * @param userId
+     * @param list
+     * @return
+     * @throws IOException
+     */
+    public BasicAttachment mergePdf(TBTask tbTask, ExamDetail examDetail, Long userId, List<PdfDto>... list) throws IOException {
+        StringJoiner stringJoiner = new StringJoiner("").add(SystemConstant.PDF_TEMP_FILES_DIR).add(File.separator);
+        List<PdfDto> mergePdfList = new ArrayList<>();
+        int pageA3Count = 0;
+        int pageA4Count = 0;
+        for (int i = 0; i < list.length; i++) {
+            if (list[i].get(0).getPageSize() == PageSizeEnum.A4) {
+                pageA4Count = pageA4Count + list[i].stream().mapToInt(PdfDto::getPageCount).sum();
+            } else {
+                pageA3Count = pageA3Count + list[i].stream().mapToInt(PdfDto::getPageCount).sum();
+            }
+            mergePdfList.addAll(list[i]);
+        }
+        List<String> pathList = mergePdfList.stream().map(PdfDto::getPath).collect(Collectors.toList());
+        String dirName = PdfUtil.mergePdf(pathList.toArray(new String[mergePdfList.size()]), null);
+        File localPdfFile = new File(stringJoiner.toString() + File.separator + dirName);
+        BasicAttachment basicAttachment = basicAttachmentService.saveAttachmentPdf(dirName, userId);
+        tbTask.setImportFileName(basicAttachment.getName());
+        tbTask.setImportFilePath(basicAttachment.getPath());
+        examDetail.setAttachmentId(basicAttachment.getId());
+        examDetail.setPagesA3(pageA3Count);
+        examDetail.setPagesA4(pageA4Count);
+        examDetail.setStatus(ExamDetailStatusEnum.READY);
+        detailService.saveOrUpdate(examDetail);
+        ossUtil.ossUpload(dirName, localPdfFile, DigestUtils.md5Hex(new FileInputStream(localPdfFile)));
+        localPdfFile.delete();
+        for (PdfDto pdfDto : mergePdfList) {
+            new File(pdfDto.getPath()).delete();
+        }
+        return basicAttachment;
+    }
+
+    /**
+     * 删除附件
+     *
+     * @param attachmentIds
+     * @param ftlList
+     */
+    public void deleteAttachment(Set<Long> attachmentIds, Set<File> ftlList) {
+        if (Objects.nonNull(attachmentIds) && attachmentIds.size() > 0) {
+            QueryWrapper<BasicAttachment> basicAttachmentQueryWrapper = new QueryWrapper<>();
+            basicAttachmentQueryWrapper.lambda().in(BasicAttachment::getId, attachmentIds);
+            basicAttachmentService.batchDeleteAttachment(basicAttachmentService.list(basicAttachmentQueryWrapper));
+            basicAttachmentService.removeByIds(attachmentIds);
+        }
+        if (Objects.nonNull(ftlList) && ftlList.size() > 0) {
+            for (File file : ftlList) {
+                file.delete();
+            }
+        }
+    }
+
+    /**
+     * 考生题卡html
+     *
+     * @param attachmentIds
+     * @param studentContent
+     * @param t
+     * @param paperType
+     * @param examCard
+     * @param userId
+     * @param examStudentPdfList
+     * @return
+     * @throws IOException
+     */
+    public BasicAttachment examStudentHtml(Set<Long> attachmentIds, String studentContent, ExamStudent t, String paperType, ExamCard examCard, Long userId, List<PdfDto> examStudentPdfList) throws IOException {
+        if (Objects.nonNull(t.getAttachmentId())) {
+            attachmentIds.add(t.getAttachmentId());
+        }
+        String studentHtml = studentContent;
+        if (Objects.nonNull(t.getExtendFields())) {
+            JSONArray jsonObjectExtend = (JSONArray) JSONArray.parse(t.getExtendFields());//扩展字段
+            if (Objects.nonNull(jsonObjectExtend)) {
+                for (int i = 0; i < jsonObjectExtend.size(); i++) {
+                    JSONObject object = (JSONObject) jsonObjectExtend.get(i);
+                    studentHtml = studentHtml.replaceAll("\\$\\{" + object.get("code") + "\\}", String.valueOf(object.get("value")));
+                }
+            }
+        }
+        //生成学生考号条码并将图片转成base64
+        studentHtml = studentHtml.replaceAll("\\$\\{examNumber\\}", GoogleBarCodeUtil.createBarCode(t.getTicketNumber(), false));
+        studentHtml = studentHtml.replaceAll("\\$\\{examNumberStr\\}", t.getTicketNumber());
+        //随机生成学生试卷条码并将图片转成base64
+        studentHtml = studentHtml.replaceAll("\\$\\{paperType\\}", GoogleBarCodeUtil.createBarCode(SystemConstant.convertPaperType(paperType), false));
+        studentHtml = studentHtml.replaceAll("\\$\\{paperTypeName\\}", paperType);
+        studentHtml = studentHtml.replaceAll("\\$\\{studentCode\\}", t.getStudentCode());
+        studentHtml = studentHtml.replaceAll("\\$\\{name\\}", t.getStudentName());
+        studentHtml = studentHtml.replaceAll("\\$\\{courseName\\}", examCard.getCourseName());
+
+        //学生题卡
+        BasicAttachment examStudentAttachment = basicAttachmentService.saveAttachmentHtml(examCard.getSchoolId() + "|" + examCard.getCourseCode() + "|" + t.getTicketNumber(), studentHtml, userId, examStudentPdfList);
+        t.setAttachmentId(examStudentAttachment.getId());
+        return examStudentAttachment;
+    }
+
+    /**
+     * 通用题卡html
+     *
+     * @param cardContent
+     * @param paperType
+     * @param examDetailCourse
+     * @param examCard
+     * @param jsonArray
+     * @param userId
+     * @param cardPdfList
+     * @return
+     * @throws IOException
+     */
+    public BasicAttachment cardHtml(String cardContent, String paperType, ExamDetailCourse examDetailCourse, ExamCard examCard, JSONArray jsonArray, Long userId, List<PdfDto> cardPdfList) throws IOException {
+        //通用题卡
+        String cardTemp = cardContent;
+        cardTemp = cardTemp.replaceAll("\\$\\{paperTypeName\\}", paperType);
+        //随机生成试卷条码并将图片转成base64
+        cardTemp = cardTemp.replaceAll("\\$\\{paperType\\}", GoogleBarCodeUtil.createBarCode(SystemConstant.convertPaperType(paperType), false));
+        //通用题卡生成卷袋贴条码
+        String paperCode = examDetailCourse.getPaperNumber();
+        String paperCodeImg = GoogleBarCodeUtil.createBarCode(paperCode, false);
+        String paperCodeDiv = "<div class=\"page-box page-box-0\"><div class=\"package-number\" style=\"position: absolute;width: 200px;height: 40px;top: 80px;right: 35px;transform: rotate(-90deg);transform-origin: center right;text-align: center;z-index: 99;\"><img src=\"data:image/png;base64," + paperCodeImg + "\" style=\"display: block; height: 28px; width: 100%\" /><p style=\"line-height: 1; font-size: 12px; margin: 0;\">" + paperCode + "</p></div>";
+        cardTemp = cardTemp.replaceAll("<div class=\"page-box page-box-0\">", paperCodeDiv);
+        BasicAttachment cardAttachment = basicAttachmentService.saveAttachmentHtml(examCard.getSchoolId() + "|" + examCard.getCourseCode(), cardTemp, userId, cardPdfList);
+        JSONObject object = new JSONObject();
+        object.put("name", paperType);
+        object.put("examDetailCourseId", examDetailCourse.getId());
+        object.put("attachmentId", cardAttachment.getId());
+        jsonArray.add(object);
+        return cardAttachment;
+    }
+}

+ 2 - 1
distributed-print-business/src/main/java/com/qmth/distributed/print/business/util/FreemarkerUtil.java

@@ -1,6 +1,7 @@
 package com.qmth.distributed.print.business.util;
 
 import com.alibaba.fastjson.JSONObject;
+import com.qmth.distributed.print.business.bean.dto.PdfDto;
 import com.qmth.distributed.print.business.config.DictionaryConfig;
 import com.qmth.distributed.print.business.entity.BasicAttachment;
 import com.qmth.distributed.print.business.enums.UploadFileEnum;
@@ -115,7 +116,7 @@ public class FreemarkerUtil {
                         UploadFileEnum.HTML
                 });
                 basicAttachment.setPath(jsonObject.toJSONString());
-                basicAttachmentService.saveAttachmentPdf(basicAttachment, (List<String>) dataMap.get("variablePdfList"), (Integer) dataMap.get("printCount"));
+                basicAttachmentService.saveAttachmentPdf(basicAttachment, (List<PdfDto>) dataMap.get("variablePdfList"), (Integer) dataMap.get("printCount"));
             }
             log.info("{}文件创建成功", htmlFile.toString());
             htmlFile.delete();