Browse Source

更改上传html、pdf路径

wangliang 4 năm trước cách đây
mục cha
commit
634dfe8e56

+ 11 - 0
distributed-print-business/src/main/java/com/qmth/distributed/print/business/entity/ExamDetailCourse.java

@@ -45,6 +45,17 @@ public class ExamDetailCourse extends BaseEntity implements Serializable {
     @TableField("card_pages_a3")
     private Integer cardPagesA3;
 
+    @TableField("attachment_path")
+    private String attachmentPath;
+
+    public String getAttachmentPath() {
+        return attachmentPath;
+    }
+
+    public void setAttachmentPath(String attachmentPath) {
+        this.attachmentPath = attachmentPath;
+    }
+
     public Long getSchoolId() {
         return schoolId;
     }

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

@@ -4,6 +4,7 @@ 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.entity.ExamDetailCourse;
 import com.qmth.distributed.print.business.enums.UploadFileEnum;
 import org.springframework.web.multipart.MultipartFile;
 
@@ -56,8 +57,9 @@ public interface BasicAttachmentService extends IService<BasicAttachment> {
 
     /**
      * 保存html文件
-     * @param rootPath 文件路径
-     * @param fileName 文件名
+     *
+     * @param rootPath    文件路径
+     * @param fileName    文件名
      * @param htmlContent 文件内容
      * @return
      */
@@ -76,6 +78,8 @@ public interface BasicAttachmentService extends IService<BasicAttachment> {
     /**
      * 保存附件
      *
+     * @param printType
+     * @param examDetailCourse
      * @param basicAttachment
      * @param pdfList
      * @param printCount
@@ -83,7 +87,7 @@ public interface BasicAttachmentService extends IService<BasicAttachment> {
      * @return
      * @throws IOException
      */
-    public BasicAttachment saveAttachmentPdf(BasicAttachment basicAttachment, List<PdfDto> pdfList, Integer printCount, Integer sequence) throws IOException, DocumentException;
+    public BasicAttachment saveAttachmentPdf(String printType, ExamDetailCourse examDetailCourse, BasicAttachment basicAttachment, List<PdfDto> pdfList, Integer printCount, Integer sequence) throws IOException, DocumentException;
 
     /**
      * 删除附件

+ 36 - 19
distributed-print-business/src/main/java/com/qmth/distributed/print/business/service/impl/BasicAttachmentServiceImpl.java

@@ -1,5 +1,6 @@
 package com.qmth.distributed.print.business.service.impl;
 
+import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
 import com.aliyun.oss.common.utils.BinaryUtil;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
@@ -9,6 +10,7 @@ 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.ExamDetailCourse;
 import com.qmth.distributed.print.business.entity.SysUser;
 import com.qmth.distributed.print.business.enums.PageSizeEnum;
 import com.qmth.distributed.print.business.enums.UploadFileEnum;
@@ -238,6 +240,8 @@ public class BasicAttachmentServiceImpl extends ServiceImpl<BasicAttachmentMappe
     /**
      * 保存附件
      *
+     * @param printType
+     * @param examDetailCourse
      * @param basicAttachment
      * @param pdfList
      * @param printCount
@@ -247,7 +251,7 @@ public class BasicAttachmentServiceImpl extends ServiceImpl<BasicAttachmentMappe
      */
     @Override
     @Transactional
-    public BasicAttachment saveAttachmentPdf(BasicAttachment basicAttachment, List<PdfDto> pdfList, Integer printCount, Integer sequence) throws IOException, DocumentException {
+    public BasicAttachment saveAttachmentPdf(String printType, ExamDetailCourse examDetailCourse, BasicAttachment basicAttachment, List<PdfDto> pdfList, Integer printCount, Integer sequence) throws IOException, DocumentException {
         try {
             boolean oss = dictionaryConfig.sysDomain().isOss();
             JSONObject jsonObject = JSONObject.parseObject(basicAttachment.getPath());
@@ -264,36 +268,49 @@ public class BasicAttachmentServiceImpl extends ServiceImpl<BasicAttachmentMappe
                 url = SystemConstant.TEMP_FILES_DIR + File.separator + filePath;
                 htmlFile = ossUtil.ossDownload(filePath, url);
             }
-            String pdfDirName = filePath.replaceAll(SystemConstant.HTML_PREFIX, SystemConstant.PDF_PREFIX).replaceAll(UploadFileEnum.HTML.name().toLowerCase(), UploadFileEnum.PDF.name().toLowerCase());
+            LocalDateTime nowTime = LocalDateTime.now();
+            StringJoiner pdfStringJoiner = new StringJoiner("");
+            if (!oss) {
+                pdfStringJoiner.add(SystemConstant.TEMP_FILES_DIR).add(File.separator);
+            }
+            pdfStringJoiner.add(UploadFileEnum.PDF.getTitle()).add(File.separator);
+            pdfStringJoiner.add(String.valueOf(nowTime.getYear())).add(File.separator)
+                    .add(String.format("%02d", nowTime.getMonthValue())).add(File.separator)
+                    .add(String.format("%02d", nowTime.getDayOfMonth()));
+            pdfStringJoiner.add(File.separator).add(SystemConstant.getUuid()).add(SystemConstant.PDF_PREFIX);
+
+            String pdfDirName = pdfStringJoiner.toString();
             String destUrl = SystemConstant.PDF_TEMP_FILES_DIR + File.separator + pdfDirName;
-            HtmlToPdfUtil.convert(htmlFile.getPath(), destUrl, PageSizeEnum.A4);
             File pdfFile = new File(destUrl);
+            if (!pdfFile.exists()) {
+                pdfFile.getParentFile().mkdirs();
+                pdfFile.createNewFile();
+            }
+
+            HtmlToPdfUtil.convert(htmlFile.getPath(), destUrl, PageSizeEnum.A4);
             String fileMd5 = DigestUtils.md5Hex(new FileInputStream(pdfFile));
             PdfDto pdfDto = null;
             if (oss) {//上传至oss
                 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)) {
-                    jsonObject.put(SystemConstant.UPLOAD_TYPE, new UploadFileEnum[]{
-                            UploadFileEnum.FILE,
-                            UploadFileEnum.HTML,
-                            UploadFileEnum.PDF
-                    });
-                } else {
-                    jsonObject.put(SystemConstant.UPLOAD_TYPE, new UploadFileEnum[]{
-                            UploadFileEnum.FILE,
-                            UploadFileEnum.PDF
-                    });
-                    htmlFile.delete();
-                }
+                htmlFile.delete();
             }
-            basicAttachment.setPath(jsonObject.toJSONString());
+            JSONObject attachmentPath = JSONObject.parseObject(examDetailCourse.getAttachmentPath());
+            attachmentPath = Objects.isNull(attachmentPath) ? new JSONObject() : attachmentPath;
+            JSONArray jsonArray = (JSONArray) attachmentPath.get("path");
+            jsonArray = Objects.isNull(jsonArray) ? new JSONArray() : jsonArray;
+            JSONObject object = new JSONObject();
+            object.put(SystemConstant.TYPE, printType);
+            object.put(SystemConstant.HTML_PATH, filePath);
+            object.put(SystemConstant.PDF_PATH, pdfDirName);
+            object.put(SystemConstant.UPLOAD_TYPE, oss);
+            jsonArray.add(object);
+            attachmentPath.put("path", jsonArray);
+            examDetailCourse.setAttachmentPath(attachmentPath.toJSONString());
             basicAttachment.setPages(pdfDto.getActualPageCount());
             for (int i = 0; i < printCount; i++) {
                 pdfList.add(new PdfDto(pdfFile.getPath(), PageSizeEnum.A4, pdfDto.getPageCount(), sequence));
             }
-            this.updateById(basicAttachment);
         } catch (Exception e) {
             log.error("请求出错", e);
             if (e instanceof ApiException) {

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

@@ -182,20 +182,20 @@ public class TaskLogicServiceImpl implements TaskLogicService {
                     List<PdfDto> backupPaperPdfList = new ArrayList<>();//备份试卷
                     List<PdfDto> cardPdfList = new ArrayList<>();//备份题卡
 
-                    if (Objects.nonNull(examPrintPlan.getOrdinaryContent())) {
-                        //获取普通印品
-                        JSONArray jsonArrayOrdinary = JSONArray.parseArray(examPrintPlan.getOrdinaryContent());
-                        for (int i = 0; i < jsonArrayOrdinary.size(); i++) {
-                            JSONObject jsonObjectOrdinary = jsonArrayOrdinary.getJSONObject(i);
-                            if (Objects.nonNull(jsonObjectOrdinary.get("attachmentId"))) {
-                                Long attachmentId = Long.parseLong((String) jsonObjectOrdinary.get("attachmentId"));
-                                BasicAttachment basicAttachment = basicAttachmentService.getById(attachmentId);
-                                createPdfUtil.createCheckIn(basicAttachment, ordinaryPdfList, (Integer) jsonObjectOrdinary.get("backupCount"));
+                    for (ExamDetailCourse examDetailCourse : examDetailCourseList) {
+                        if (Objects.nonNull(examPrintPlan.getOrdinaryContent())) {
+                            //获取普通印品
+                            JSONArray jsonArrayOrdinary = JSONArray.parseArray(examPrintPlan.getOrdinaryContent());
+                            for (int i = 0; i < jsonArrayOrdinary.size(); i++) {
+                                JSONObject jsonObjectOrdinary = jsonArrayOrdinary.getJSONObject(i);
+                                if (Objects.nonNull(jsonObjectOrdinary.get("attachmentId"))) {
+                                    Long attachmentId = Long.parseLong((String) jsonObjectOrdinary.get("attachmentId"));
+                                    BasicAttachment basicAttachment = basicAttachmentService.getById(attachmentId);
+                                    createPdfUtil.createCheckIn(examDetailCourse, basicAttachment, ordinaryPdfList, (Integer) jsonObjectOrdinary.get("backupCount"));
+                                }
                             }
                         }
-                    }
 
-                    for (ExamDetailCourse examDetailCourse : examDetailCourseList) {
                         List<PdfDto> paperPdfTempList = new ArrayList<>();//所有试卷
                         List<PdfDto> examStudentTempPdfList = new ArrayList<>();//所有题卡
                         List<PdfDto> backupPaperTempPdfList = new ArrayList<>();//备份试卷

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

@@ -59,11 +59,12 @@ public class CreatePdfUtil {
     /**
      * 创建登记表
      *
+     * @param examDetailCourse
      * @param basicAttachment
      * @param ordinaryPdfList
      * @param printCount
      */
-    public void createCheckIn(BasicAttachment basicAttachment, List<PdfDto> ordinaryPdfList, Integer printCount) throws IOException, DocumentException {
+    public void createCheckIn(ExamDetailCourse examDetailCourse, BasicAttachment basicAttachment, List<PdfDto> ordinaryPdfList, Integer printCount) throws IOException, DocumentException {
         if (Objects.isNull(basicAttachment)) {
             throw ExceptionResultEnum.ERROR.exception("找不到附件");
         }
@@ -78,7 +79,7 @@ public class CreatePdfUtil {
                 ordinaryPdfList.add(new PdfDto(localFile.getPath(), PageSizeEnum.A4, pdfDto.getPageCount()));
             }
         } else {
-            basicAttachmentService.saveAttachmentPdf(basicAttachment, ordinaryPdfList, printCount, 0);
+            basicAttachmentService.saveAttachmentPdf("checkIn", examDetailCourse, basicAttachment, ordinaryPdfList, printCount, 0);
         }
     }
 
@@ -154,6 +155,8 @@ public class CreatePdfUtil {
         htmlMap.computeIfAbsent("variablePdfList", v -> variablePdfList);
         htmlMap.computeIfAbsent("printCount", v -> printCount);
         htmlMap.computeIfAbsent("sequence", v -> 1);
+        htmlMap.computeIfAbsent("examDetailCourse", v -> examDetailCourse);
+        htmlMap.computeIfAbsent("printType", v -> "paperPackage");
         freemarkerUtil.createPaperPackage(htmlMap);
     }
 
@@ -250,6 +253,8 @@ public class CreatePdfUtil {
         htmlMap.computeIfAbsent("variablePdfList", v -> variablePdfList);
         htmlMap.computeIfAbsent("printCount", v -> printCount);
         htmlMap.computeIfAbsent("sequence", v -> 2);
+        htmlMap.computeIfAbsent("examDetailCourse", v -> examDetailCourse);
+        htmlMap.computeIfAbsent("printType", v -> "signBook");
         freemarkerUtil.createSignBook(htmlMap);
     }
 
@@ -373,11 +378,13 @@ public class CreatePdfUtil {
     public BasicAttachment mergePdf(BasicExamRule basicExamRule, TBTask tbTask, ExamDetail examDetail, Long userId, Long schoolId, List<PdfDto>... list) throws IOException {
         StringJoiner stringJoiner = new StringJoiner("").add(SystemConstant.PDF_TEMP_FILES_DIR).add(File.separator);
         List<PdfDto> mergePdfList = new ArrayList<>();
+        List<PdfDto> mergePdfA4List = new ArrayList<>();
         int pageA3Count = 0;
         int pageA4Count = 0;
         for (int i = 0; i < list.length; i++) {
             if (list[i].size() > 0 && list[i].get(0).getPageSize() == PageSizeEnum.A4) {
                 pageA4Count = pageA4Count + list[i].stream().mapToInt(PdfDto::getPageCount).sum();
+                mergePdfA4List.addAll(list[i]);
             } else if (list[i].size() > 0 && list[i].get(0).getPageSize() == PageSizeEnum.A3) {
                 pageA3Count = pageA3Count + list[i].stream().mapToInt(PdfDto::getPageCount).sum();
                 mergePdfList.addAll(list[i]);
@@ -401,6 +408,9 @@ public class CreatePdfUtil {
         for (PdfDto pdfDto : mergePdfList) {
             new File(pdfDto.getPath()).delete();
         }
+        for (PdfDto pdfDto : mergePdfA4List) {
+            new File(pdfDto.getPath()).delete();
+        }
         return basicAttachment;
     }
 

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

@@ -4,6 +4,7 @@ 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.entity.ExamDetailCourse;
 import com.qmth.distributed.print.business.enums.UploadFileEnum;
 import com.qmth.distributed.print.business.service.BasicAttachmentService;
 import com.qmth.distributed.print.common.contant.SystemConstant;
@@ -79,6 +80,8 @@ public class FreemarkerUtil {
         Writer out = null;
         try {
             BasicAttachment basicAttachment = (BasicAttachment) dataMap.get("basicAttachment");
+            ExamDetailCourse examDetailCourse = (ExamDetailCourse) dataMap.get("examDetailCourse");
+            String printType = (String) dataMap.get("printType");
             JSONObject jsonObject = JSONObject.parseObject(basicAttachment.getPath());
             String path = (String) jsonObject.get(SystemConstant.PATH);
             String ftlPath = path.substring(0, path.lastIndexOf("/"));
@@ -116,7 +119,7 @@ public class FreemarkerUtil {
                         UploadFileEnum.HTML
                 });
                 basicAttachment.setPath(jsonObject.toJSONString());
-                basicAttachmentService.saveAttachmentPdf(basicAttachment, (List<PdfDto>) dataMap.get("variablePdfList"), (Integer) dataMap.get("printCount"), (Integer) dataMap.get("sequence"));
+                basicAttachmentService.saveAttachmentPdf(printType, examDetailCourse, basicAttachment, (List<PdfDto>) dataMap.get("variablePdfList"), (Integer) dataMap.get("printCount"), (Integer) dataMap.get("sequence"));
             }
             htmlFile.delete();
         } catch (Exception e) {