wangliang 4 vuotta sitten
vanhempi
commit
e5d8c872ae

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

@@ -66,10 +66,11 @@ public interface BasicAttachmentService extends IService<BasicAttachment> {
      *
      * @param basicAttachment
      * @param variablePdfList
+     * @param printCount
      * @return
      * @throws IOException
      */
-    public BasicAttachment saveAttachmentPdf(BasicAttachment basicAttachment, List<String> variablePdfList) throws IOException;
+    public BasicAttachment saveAttachmentPdf(BasicAttachment basicAttachment, List<String> variablePdfList, Integer printCount) throws IOException;
 
     /**
      * 删除附件

+ 30 - 13
distributed-print-business/src/main/java/com/qmth/distributed/print/business/service/impl/BasicAttachmentServiceImpl.java

@@ -1,6 +1,5 @@
 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;
@@ -128,10 +127,14 @@ public class BasicAttachmentServiceImpl extends ServiceImpl<BasicAttachmentMappe
                 String pdfDirName = dirName.replaceAll(SystemConstant.HTML_PREFIX, SystemConstant.PDF_PREFIX).replaceAll(UploadFileEnum.HTML.name().toLowerCase(), UploadFileEnum.PDF.name().toLowerCase());
                 String destUrl = SystemConstant.PDF_TEMP_FILES_DIR + File.separator + pdfDirName;
                 HtmlToPdfUtil.convert(localHtmlFile.getPath(), destUrl, PageSizeEnum.A3);
-                File pdf = new File(destUrl);
-                localFileList.add(pdf.getPath());
-                fileMd5 = DigestUtils.md5Hex(new FileInputStream(pdf));
-                ossUtil.ossUpload(pdfDirName, pdf, BinaryUtil.toBase64String(HexUtils.decodeHex(fileMd5)));
+                File pdfFile = new File(destUrl);
+                if (!pdfFile.getParentFile().exists()) {
+                    pdfFile.getParentFile().mkdirs();
+                    pdfFile.createNewFile();
+                }
+                localFileList.add(pdfFile.getPath());
+                fileMd5 = DigestUtils.md5Hex(new FileInputStream(pdfFile));
+                ossUtil.ossUpload(pdfDirName, pdfFile, BinaryUtil.toBase64String(HexUtils.decodeHex(fileMd5)));
                 localHtmlFile.delete();
                 jsonObject.put(SystemConstant.PDF_PATH, pdfDirName);
             } else {//上传至服务器
@@ -180,6 +183,10 @@ public class BasicAttachmentServiceImpl extends ServiceImpl<BasicAttachmentMappe
         try {
             StringJoiner stringJoiner = new StringJoiner("").add(SystemConstant.PDF_TEMP_FILES_DIR).add(File.separator);
             File localPdfFile = new File(stringJoiner.toString() + File.separator + dirName);
+            if (!localPdfFile.getParentFile().exists()) {
+                localPdfFile.getParentFile().mkdirs();
+                localPdfFile.createNewFile();
+            }
             JSONObject jsonPdf = new JSONObject();
             jsonPdf.put(SystemConstant.TYPE, SystemConstant.OSS);
             jsonPdf.put(SystemConstant.PATH, dirName);
@@ -202,12 +209,14 @@ public class BasicAttachmentServiceImpl extends ServiceImpl<BasicAttachmentMappe
      * 保存附件
      *
      * @param basicAttachment
+     * @param variablePdfList
+     * @param printCount
      * @return
      * @throws IOException
      */
     @Override
     @Transactional
-    public BasicAttachment saveAttachmentPdf(BasicAttachment basicAttachment, List<String> variablePdfList) throws IOException {
+    public BasicAttachment saveAttachmentPdf(BasicAttachment basicAttachment, List<String> variablePdfList, Integer printCount) throws IOException {
         boolean oss = dictionaryConfig.sysDomain().isOss();
         JSONObject jsonObject = JSONObject.parseObject(basicAttachment.getPath());
         String filePath = (String) jsonObject.get(SystemConstant.HTML_PATH);
@@ -216,17 +225,25 @@ public class BasicAttachmentServiceImpl extends ServiceImpl<BasicAttachmentMappe
         String pdfDirName = filePath.replaceAll(SystemConstant.HTML_PREFIX, SystemConstant.PDF_PREFIX).replaceAll(UploadFileEnum.HTML.name().toLowerCase(), UploadFileEnum.PDF.name().toLowerCase());
         String destUrl = SystemConstant.PDF_TEMP_FILES_DIR + File.separator + pdfDirName;
         HtmlToPdfUtil.convert(htmlFile.getPath(), destUrl, PageSizeEnum.A4);
-        File pdf = new File(destUrl);
-        String fileMd5 = DigestUtils.md5Hex(new FileInputStream(pdf));
+        File pdfFile = new File(destUrl);
+        if (!pdfFile.getParentFile().exists()) {
+            pdfFile.getParentFile().mkdirs();
+            pdfFile.createNewFile();
+        }
+        String fileMd5 = DigestUtils.md5Hex(new FileInputStream(pdfFile));
         if (oss) {//上传至oss
-            ossUtil.ossUpload(pdfDirName, pdf, BinaryUtil.toBase64String(HexUtils.decodeHex(fileMd5)));
+            ossUtil.ossUpload(pdfDirName, pdfFile, BinaryUtil.toBase64String(HexUtils.decodeHex(fileMd5)));
             jsonObject.put(SystemConstant.PDF_PATH, pdfDirName);
-            JSONArray jsonArray = (JSONArray) jsonObject.get(SystemConstant.UPLOAD_TYPE);
-            jsonArray.add(UploadFileEnum.PDF);
-            jsonObject.put(SystemConstant.UPLOAD_TYPE, jsonArray);
+            jsonObject.put(SystemConstant.UPLOAD_TYPE, new UploadFileEnum[]{
+                    UploadFileEnum.FILE,
+                    UploadFileEnum.HTML,
+                    UploadFileEnum.PDF
+            });
         }
         basicAttachment.setPath(jsonObject.toJSONString());
-        variablePdfList.add(pdf.getPath());
+        for (int i = 0; i < printCount; i++) {
+            variablePdfList.add(pdfFile.getPath());
+        }
         this.updateById(basicAttachment);
         return basicAttachment;
     }

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

@@ -169,6 +169,7 @@ public class TaskLogicServiceImpl implements TaskLogicService {
             //这里为保存附件html的逻辑
             //查询题卡详情是否有附件id,有的话则把以前的附件删除
             Set<Long> attachmentIds = null;
+            Set<File> ftlList = null;
 
             //查询printPlan
             ExamPrintPlan examPrintPlan = examPrintPlanService.getById(tbTask.getPrintPlanId());
@@ -187,6 +188,7 @@ public class TaskLogicServiceImpl implements TaskLogicService {
                 throw ExceptionResultEnum.ERROR.exception("考务计划为空");
             }
             attachmentIds = Objects.isNull(attachmentIds) ? attachmentIds = new HashSet<>() : attachmentIds;
+            ftlList = Objects.isNull(ftlList) ? ftlList = new HashSet<>() : ftlList;
             for (ExamDetail examDetail : examDetailList) {
                 if (Objects.nonNull(examDetail.getAttachmentId())) {
                     attachmentIds.add(examDetail.getAttachmentId());
@@ -264,7 +266,6 @@ public class TaskLogicServiceImpl implements TaskLogicService {
                                     .eq(ExamStudent::getExamDetailCourseId, examTaskDetail.getId());
                             List<ExamStudent> examStudentList = examStudentService.list(examStudentQueryWrapper);
 
-                            Integer signVariableBackupCount = null, packagVariableBackupCount = null;
                             if (Objects.nonNull(examPrintPlan.getVariableContent())) {
                                 //获取变量印品
                                 JSONArray jsonArrayVariable = JSONArray.parseArray(examPrintPlan.getVariableContent());
@@ -273,12 +274,11 @@ public class TaskLogicServiceImpl implements TaskLogicService {
                                     String type = (String) jsonObjectVariable.get("type");
                                     Long attachmentId = (Long) jsonObjectVariable.get("attachmentId");
                                     BasicAttachment basicAttachment = basicAttachmentService.getById(attachmentId);
-                                    commonService.getFile(basicAttachment.getPath(), false);
+                                    ftlList.add(commonService.getFile(basicAttachment.getPath(), false));
                                     if (Objects.nonNull(type) && Objects.equals(type.toUpperCase(), "SIGN")) {//签到表
-                                        signVariableBackupCount = (Integer) jsonObjectVariable.get("backupCount");
-                                        createSignBook(basicAttachment, basicSchool.getName(), examDetailCourse, examStudentList, variablePdfList);
+                                        createSignBook(basicAttachment, basicSchool.getName(), examDetailCourse, examStudentList, variablePdfList, (Integer) jsonObjectVariable.get("backupCount"));
                                     } else if (Objects.nonNull(type) && Objects.equals(type.toUpperCase(), "PACKAG")) {//卷袋贴
-                                        packagVariableBackupCount = (Integer) jsonObjectVariable.get("backupCount");
+                                        createPaperPackage(basicAttachment, basicSchool.getName(), examDetailCourse, examStudentList, variablePdfList, (Integer) jsonObjectVariable.get("backupCount"));
                                     }
                                 }
                             }
@@ -298,7 +298,7 @@ public class TaskLogicServiceImpl implements TaskLogicService {
                 basicAttachmentList.add(mergePdf(tbTask, examDetail, sysUser.getId(), variablePdfList, ordinaryPdfList, paperPdfList, examStudentPdfList, backupPaperPdfList, cardPdfList));
             }
             //最后一步删除附件
-            deleteAttachment(attachmentIds);
+            deleteAttachment(attachmentIds, ftlList);
         } catch (Exception e) {
             log.error("请求出错", e);
             basicAttachmentService.batchDeleteAttachment(basicAttachmentList);
@@ -366,12 +366,12 @@ public class TaskLogicServiceImpl implements TaskLogicService {
         JSONObject jsonObject = new JSONObject();
         stringJoiner.add(File.separator).add(SystemConstant.getUuid()).add(".").add(SystemConstant.XLSX);
 
-        String dirName = stringJoiner.toString().replaceAll("\\\\","/");
+        String dirName = stringJoiner.toString().replaceAll("\\\\", "/");
         ossUtil.ossUpload(dirName, in, DigestUtils.md5Hex(in));
         jsonObject.put(SystemConstant.TYPE, SystemConstant.OSS);
         jsonObject.put(SystemConstant.PATH, dirName);
         jsonObject.put(SystemConstant.UPLOAD_TYPE, UploadFileEnum.FILE.getTitle());
-        map.put("path",jsonObject.toString());
+        map.put("path", jsonObject.toString());
         return map;
     }
 
@@ -527,20 +527,61 @@ public class TaskLogicServiceImpl implements TaskLogicService {
     }
 
     /**
-     * 创建签到表
+     * 生成卷袋贴
      *
      * @param basicAttachment
      * @param schoolName
      * @param examDetailCourse
      * @param examStudentList
      * @param variablePdfList
+     * @param printCount
      * @throws IOException
      */
-    private void createSignBook(BasicAttachment basicAttachment, String schoolName, ExamDetailCourse examDetailCourse, List<ExamStudent> examStudentList, List<String> variablePdfList) 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");
@@ -600,6 +641,7 @@ public class TaskLogicServiceImpl implements TaskLogicService {
         }
         htmlMap.computeIfAbsent("basicAttachment", v -> basicAttachment);
         htmlMap.computeIfAbsent("variablePdfList", v -> variablePdfList);
+        htmlMap.computeIfAbsent("printCount", v -> printCount);
         freemarkerUtil.createSignBook(htmlMap);
     }
 
@@ -735,14 +777,20 @@ public class TaskLogicServiceImpl implements TaskLogicService {
      * 删除附件
      *
      * @param attachmentIds
+     * @param ftlList
      */
-    public void deleteAttachment(Set<Long> attachmentIds) {
+    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();
+            }
+        }
     }
 
     /**

+ 7 - 8
distributed-print-business/src/main/java/com/qmth/distributed/print/business/util/FreemarkerUtil.java

@@ -17,7 +17,6 @@ import org.springframework.util.ResourceUtils;
 
 import javax.annotation.Resource;
 import java.io.*;
-import java.time.LocalDateTime;
 import java.util.List;
 import java.util.Map;
 import java.util.Objects;
@@ -96,27 +95,27 @@ public class FreemarkerUtil {
                     .add(ftlName)
                     .add(SystemConstant.HTML_PREFIX);
             File htmlFile = new File(localStringJoiner.toString());
+            if (!htmlFile.getParentFile().exists()) {
+                htmlFile.getParentFile().mkdirs();
+                htmlFile.createNewFile();
+            }
             out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(htmlFile)));
             // step3 输出文件
             template.process(dataMap, out);
             out.flush();
             boolean oss = dictionaryConfig.sysDomain().isOss();
             if (oss) {
-                LocalDateTime nowTime = LocalDateTime.now();
                 StringJoiner stringJoiner = new StringJoiner("");
-                stringJoiner.add(UploadFileEnum.HTML.getTitle()).add(File.separator)
-                        .add(String.valueOf(nowTime.getYear())).add(File.separator)
-                        .add(String.format("%02d", nowTime.getMonthValue())).add(File.separator)
-                        .add(String.format("%02d", nowTime.getDayOfMonth()))
+                stringJoiner.add(ftlPath.replaceAll(UploadFileEnum.FILE.getTitle(), UploadFileEnum.HTML.getTitle()))
                         .add(File.separator).add(ftlName).add(SystemConstant.HTML_PREFIX);
                 ossUtil.ossUpload(stringJoiner.toString(), htmlFile, DigestUtils.md5Hex(new FileInputStream(htmlFile)));
                 jsonObject.put(SystemConstant.HTML_PATH, stringJoiner.toString());
                 jsonObject.put(SystemConstant.UPLOAD_TYPE, new UploadFileEnum[]{
-                        UploadFileEnum.valueOf((String) jsonObject.get(SystemConstant.UPLOAD_TYPE)),
+                        UploadFileEnum.FILE,
                         UploadFileEnum.HTML
                 });
                 basicAttachment.setPath(jsonObject.toJSONString());
-                basicAttachmentService.saveAttachmentPdf(basicAttachment, (List<String>) dataMap.get("variablePdfList"));
+                basicAttachmentService.saveAttachmentPdf(basicAttachment, (List<String>) dataMap.get("variablePdfList"), (Integer) dataMap.get("printCount"));
             }
             log.info("{}文件创建成功", htmlFile.toString());
             htmlFile.delete();