wangliang 4 years ago
parent
commit
c53c7dcce1

+ 8 - 4
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;
@@ -209,19 +210,22 @@ public class BasicAttachmentServiceImpl extends ServiceImpl<BasicAttachmentMappe
     public BasicAttachment saveAttachmentPdf(BasicAttachment basicAttachment, List<String> variablePdfList) throws IOException {
         boolean oss = dictionaryConfig.sysDomain().isOss();
         JSONObject jsonObject = JSONObject.parseObject(basicAttachment.getPath());
-        String filePath = (String) jsonObject.get(SystemConstant.PATH);
+        String filePath = (String) jsonObject.get(SystemConstant.HTML_PATH);
         String url = SystemConstant.TEMP_FILES_DIR + File.separator + filePath;
-        File localHtmlFile = ossUtil.ossDownload(filePath, url);
+        File htmlFile = new File(url);
         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(localHtmlFile.getPath(), destUrl, PageSizeEnum.A4);
+        HtmlToPdfUtil.convert(htmlFile.getPath(), destUrl, PageSizeEnum.A4);
         File pdf = new File(destUrl);
         String fileMd5 = DigestUtils.md5Hex(new FileInputStream(pdf));
         if (oss) {//上传至oss
             ossUtil.ossUpload(pdfDirName, pdf, BinaryUtil.toBase64String(HexUtils.decodeHex(fileMd5)));
-            localHtmlFile.delete();
             jsonObject.put(SystemConstant.PDF_PATH, pdfDirName);
+            JSONArray jsonArray = (JSONArray) jsonObject.get(SystemConstant.UPLOAD_TYPE);
+            jsonArray.add(UploadFileEnum.PDF);
+            jsonObject.put(SystemConstant.UPLOAD_TYPE, jsonArray);
         }
+        basicAttachment.setPath(jsonObject.toJSONString());
         variablePdfList.add(pdf.getPath());
         this.updateById(basicAttachment);
         return basicAttachment;

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

@@ -272,7 +272,6 @@ public class TaskLogicServiceImpl implements TaskLogicService {
                                     Long attachmentId = (Long) jsonObjectVariable.get("attachmentId");
                                     BasicAttachment basicAttachment = basicAttachmentService.getById(attachmentId);
                                     commonService.getFile(basicAttachment.getPath(), false);
-//                                    log.info("htmlContent:{}", htmlContent);
                                     if (Objects.nonNull(type) && Objects.equals(type.toUpperCase(), "SIGN")) {//签到表
                                         signVariableBackupCount = (Integer) jsonObjectVariable.get("backupCount");
                                         createSignBook(basicAttachment, basicSchool.getName(), examDetailCourse, examStudentList, variablePdfList);
@@ -580,12 +579,8 @@ public class TaskLogicServiceImpl implements TaskLogicService {
             }
             htmlMap.put("subList", subList);
         }
-        JSONObject jsonObject = JSONObject.parseObject(basicAttachment.getPath());
-        String path = (String) jsonObject.get("path");
-        String ftlPath = path.substring(0, path.lastIndexOf("/"));
-        String ftlName = path.substring(path.lastIndexOf("/") + 1, path.lastIndexOf("."));
-        htmlMap.computeIfAbsent("ftlPath", v -> ftlPath);
-        htmlMap.computeIfAbsent("ftlName", v -> ftlName);
+        htmlMap.computeIfAbsent("basicAttachment", v -> basicAttachment);
+        htmlMap.computeIfAbsent("variablePdfList", v -> variablePdfList);
         freemarkerUtil.createSignBook(htmlMap);
     }
 

+ 25 - 5
distributed-print-business/src/main/java/com/qmth/distributed/print/business/util/FreemarkerUtil.java

@@ -1,7 +1,10 @@
 package com.qmth.distributed.print.business.util;
 
+import com.alibaba.fastjson.JSONObject;
 import com.qmth.distributed.print.business.config.DictionaryConfig;
+import com.qmth.distributed.print.business.entity.BasicAttachment;
 import com.qmth.distributed.print.business.enums.UploadFileEnum;
+import com.qmth.distributed.print.business.service.BasicAttachmentService;
 import com.qmth.distributed.print.common.contant.SystemConstant;
 import com.qmth.distributed.print.common.enums.ExceptionResultEnum;
 import freemarker.template.Configuration;
@@ -15,6 +18,7 @@ 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;
 import java.util.StringJoiner;
@@ -36,6 +40,9 @@ public class FreemarkerUtil {
     @Resource
     OssUtil ossUtil;
 
+    @Resource
+    BasicAttachmentService basicAttachmentService;
+
     /**
      * 单独生成签到表
      *
@@ -71,8 +78,12 @@ public class FreemarkerUtil {
     private void createSignBookAndPaperPackageHtml(Map<String, Object> dataMap) throws IOException {
         Writer out = null;
         try {
-            String ftlPath = (String) dataMap.get("ftlPath");
-            String ftlName = (String) dataMap.get("ftlName");
+            BasicAttachment basicAttachment = (BasicAttachment) dataMap.get("basicAttachment");
+            JSONObject jsonObject = JSONObject.parseObject(basicAttachment.getPath());
+            String path = (String) jsonObject.get(SystemConstant.PATH);
+            String ftlPath = path.substring(0, path.lastIndexOf("/"));
+            String ftlName = path.substring(path.lastIndexOf("/") + 1, path.lastIndexOf("."));
+
             Configuration configuration = new Configuration(Configuration.VERSION_2_3_29);
             File templates = ResourceUtils.getFile(SystemConstant.TEMP_FILES_DIR + File.separator + ftlPath);
             configuration.setDirectoryForTemplateLoading(templates);
@@ -80,7 +91,7 @@ public class FreemarkerUtil {
             Template template = configuration.getTemplate(ftlName + SystemConstant.FTL_PREFIX);
             // step2 生成数据
             StringJoiner localStringJoiner = new StringJoiner("");
-            localStringJoiner.add(SystemConstant.TEMP_FILES_DIR + File.separator + ftlPath)
+            localStringJoiner.add(SystemConstant.TEMP_FILES_DIR + File.separator + ftlPath.replaceAll(UploadFileEnum.FILE.getTitle(), UploadFileEnum.HTML.getTitle()))
                     .add(File.separator)
                     .add(ftlName)
                     .add(SystemConstant.HTML_PREFIX);
@@ -93,13 +104,22 @@ public class FreemarkerUtil {
             if (oss) {
                 LocalDateTime nowTime = LocalDateTime.now();
                 StringJoiner stringJoiner = new StringJoiner("");
-                stringJoiner.add(UploadFileEnum.HTML.name()).add(File.separator)
+                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()));
+                        .add(String.format("%02d", nowTime.getDayOfMonth()))
+                        .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.HTML
+                });
+                basicAttachment.setPath(jsonObject.toJSONString());
+                basicAttachmentService.saveAttachmentPdf(basicAttachment, (List<String>) dataMap.get("variablePdfList"));
             }
             log.info("{}文件创建成功", htmlFile.toString());
+            htmlFile.delete();
         } catch (Exception e) {
             log.error("请求出错", e);
         } finally {

+ 1 - 0
distributed-print-common/src/main/java/com/qmth/distributed/print/common/contant/SystemConstant.java

@@ -42,6 +42,7 @@ public class SystemConstant {
     public static final String UPDATE_TIME = "updateTime";
     public static final String PATH = "path";
     public static final String PDF_PATH = "pdfPath";
+    public static final String HTML_PATH = "htmlPath";
     public static final String TYPE = "type";
     public static final String LOCAL = "local";
     public static final String OSS = "oss";