wangliang 4 年之前
父节点
当前提交
4b8555ab60

+ 10 - 0
distributed-print-business/src/main/java/com/qmth/distributed/print/business/service/BasicAttachmentService.java

@@ -51,6 +51,16 @@ public interface BasicAttachmentService extends IService<BasicAttachment> {
      */
     public BasicAttachment saveAttachmentHtml(String fileName, String htmlContent, Long userId, List<String> localFileList) throws IOException;
 
+    /**
+     * 保存附件
+     *
+     * @param dirName
+     * @param userId
+     * @return
+     * @throws IOException
+     */
+    public BasicAttachment saveAttachmentPdf(String dirName, Long userId) throws IOException;
+
     /**
      * 删除附件
      *

+ 41 - 7
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 cn.hutool.core.lang.UUID;
 import com.alibaba.fastjson.JSONObject;
 import com.aliyun.oss.common.utils.BinaryUtil;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
@@ -21,7 +20,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.lang3.StringUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -31,7 +29,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;
@@ -119,8 +119,6 @@ public class BasicAttachmentServiceImpl extends ServiceImpl<BasicAttachmentMappe
             if (oss) {//上传至oss
                 String dirName = stringJoiner.toString().replaceAll("\\\\", "/");
                 ossUtil.ossUpload(dirName, htmlContent);
-                byte[] data = ossUtil.ossDownload(dirName);
-                fileMd5 = DigestUtils.md5Hex(new ByteArrayInputStream(data));
                 jsonObject.put(SystemConstant.TYPE, SystemConstant.OSS);
                 jsonObject.put(SystemConstant.PATH, dirName);
                 String url = SystemConstant.TEMP_FILES_DIR + File.separator + dirName;
@@ -130,7 +128,8 @@ public class BasicAttachmentServiceImpl extends ServiceImpl<BasicAttachmentMappe
                 HtmlToPdfUtil.convert(localHtmlFile.getPath(), destUrl, PageSizeEnum.A3);
                 File pdf = new File(destUrl);
                 localFileList.add(pdf.getPath());
-                ossUtil.ossUpload(pdfDirName, pdf, BinaryUtil.toBase64String(HexUtils.decodeHex(DigestUtils.md5Hex(new FileInputStream(pdf)))));
+                fileMd5 = DigestUtils.md5Hex(new FileInputStream(pdf));
+                ossUtil.ossUpload(pdfDirName, pdf, BinaryUtil.toBase64String(HexUtils.decodeHex(fileMd5)));
                 localHtmlFile.delete();
                 jsonObject.put(SystemConstant.PDF_PATH, pdfDirName);
             } else {//上传至服务器
@@ -147,7 +146,9 @@ public class BasicAttachmentServiceImpl extends ServiceImpl<BasicAttachmentMappe
                 HtmlToPdfUtil.convert(finalFile.getPath(), destUrl, PageSizeEnum.A3);
                 jsonObject.put(SystemConstant.PDF_PATH, destUrl);
             }
-            jsonObject.put(SystemConstant.UPLOAD_TYPE, UploadFileEnum.HTML);
+            jsonObject.put(SystemConstant.UPLOAD_TYPE, new UploadFileEnum[]{
+                    UploadFileEnum.HTML, UploadFileEnum.PDF
+            });
             basicAttachment = new BasicAttachment(jsonObject.toJSONString(), fileName, SystemConstant.HTML_PREFIX, new BigDecimal(size), fileMd5, userId);
             save(basicAttachment);
         } catch (Exception e) {
@@ -162,6 +163,39 @@ public class BasicAttachmentServiceImpl extends ServiceImpl<BasicAttachmentMappe
         return basicAttachment;
     }
 
+    /**
+     * 保存附件
+     *
+     * @param dirName
+     * @param userId
+     * @return
+     * @throws IOException
+     */
+    @Override
+    @Transactional
+    public BasicAttachment saveAttachmentPdf(String dirName, Long userId) throws IOException {
+        BasicAttachment basicAttachment = null;
+        try {
+            StringJoiner stringJoiner = new StringJoiner("").add(SystemConstant.PDF_TEMP_FILES_DIR).add(File.separator);
+            File localPdfFile = new File(stringJoiner.toString() + File.separator + dirName);
+            JSONObject jsonPdf = new JSONObject();
+            jsonPdf.put(SystemConstant.TYPE, SystemConstant.OSS);
+            jsonPdf.put(SystemConstant.PATH, dirName);
+            jsonPdf.put(SystemConstant.UPLOAD_TYPE, UploadFileEnum.PDF);
+            basicAttachment = new BasicAttachment(jsonPdf.toJSONString(), localPdfFile.getName(), SystemConstant.PDF_PREFIX, new BigDecimal(localPdfFile.length()), DigestUtils.md5Hex(new FileInputStream(localPdfFile)), userId);
+            save(basicAttachment);
+        } catch (Exception e) {
+            log.error("请求出错", e);
+            deleteAttachment(basicAttachment);
+            if (e instanceof ApiException) {
+                ResultUtil.error((ApiException) e, e.getMessage());
+            } else {
+                ResultUtil.error(e.getMessage());
+            }
+        }
+        return basicAttachment;
+    }
+
     /**
      * 删除附件
      *

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

@@ -9,7 +9,6 @@ import com.qmth.boot.api.exception.ApiException;
 import com.qmth.distributed.print.business.bean.dto.excel.UserImportDto;
 import com.qmth.distributed.print.business.entity.*;
 import com.qmth.distributed.print.business.enums.ExamStatusEnum;
-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.GoogleBarCodeUtil;
@@ -33,7 +32,6 @@ import javax.annotation.Resource;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.IOException;
-import java.math.BigDecimal;
 import java.util.*;
 import java.util.stream.Collectors;
 
@@ -179,11 +177,10 @@ public class TaskLogicServiceImpl implements TaskLogicService {
             examDetailCourseQueryWrapper.lambda().eq(ExamDetailCourse::getExamDetailId, examDetail.getId());
             List<ExamDetailCourse> examDetailCourseList = detailCourseService.list(examDetailCourseQueryWrapper);
 
-            StringJoiner stringJoiner = new StringJoiner("").add(SystemConstant.PDF_TEMP_FILES_DIR).add(File.separator);
             for (ExamDetailCourse examDetailCourse : examDetailCourseList) {
                 List<String> paperPdfList = new ArrayList<>();
                 List<String> cardPdfList = new ArrayList<>();
-                List<String> studentPdfList = new ArrayList<>();
+                List<String> examStudentPdfList = new ArrayList<>();
                 //查询试卷
                 QueryWrapper<ExamTask> examTaskQueryWrapper = new QueryWrapper<>();
                 examTaskQueryWrapper.lambda().eq(ExamTask::getSchoolId, sysUser.getSchoolId())
@@ -248,23 +245,7 @@ public class TaskLogicServiceImpl implements TaskLogicService {
 
                         for (String paperType : paperTypes) {
                             for (Integer integer = 1; integer <= examPrintPlan.getBackupCount(); integer++) {
-                                //通用题卡
-                                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, sysUser.getId(), cardPdfList);
-                                JSONObject object = new JSONObject();
-                                object.put("name", paperType);
-                                object.put("examTaskDetailId", examTaskDetail.getId());
-                                object.put("attachmentId", cardAttachment.getId());
-                                jsonArray.add(object);
-                                basicAttachmentList.add(cardAttachment);
+                                basicAttachmentList.add(cardHtml(cardContent, paperType, examDetailCourse, examCard, examTaskDetail, jsonArray, sysUser.getId(), cardPdfList));
                             }
 
                             //查询考生
@@ -274,33 +255,7 @@ public class TaskLogicServiceImpl implements TaskLogicService {
                             List<ExamStudent> examStudentList = examStudentService.list(examStudentQueryWrapper);
 
                             for (ExamStudent t : examStudentList) {
-                                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, sysUser.getId(), studentPdfList);
-                                t.setAttachmentId(examStudentAttachment.getId());
-                                basicAttachmentList.add(examStudentAttachment);
+                                basicAttachmentList.add(examStudentHtml(attachmentIds, studentContent, t, paperType, examCard, sysUser.getId(), examStudentPdfList));
                             }
                             examStudentService.saveOrUpdateBatch(examStudentList);
                         }
@@ -308,37 +263,13 @@ public class TaskLogicServiceImpl implements TaskLogicService {
                         examCardDetail.setAttachmentId(jsonObject.toJSONString());
                     }
                     examCardDetailService.saveOrUpdateBatch(examCardDetailList);
-                    List<String> mergePdfList = new ArrayList<>();
-                    mergePdfList.addAll(paperPdfList);
-                    mergePdfList.addAll(cardPdfList);
-                    mergePdfList.addAll(studentPdfList);
-                    String dirName = PdfUtil.mergePdf(mergePdfList.toArray(new String[mergePdfList.size()]), null);
-                    File localPdfFile = new File(stringJoiner.toString() + File.separator + dirName);
-                    JSONObject jsonPdf = new JSONObject();
-                    jsonPdf.put(SystemConstant.TYPE, SystemConstant.OSS);
-                    jsonPdf.put(SystemConstant.PATH, dirName);
-                    jsonPdf.put(SystemConstant.UPLOAD_TYPE, UploadFileEnum.PDF);
-                    BasicAttachment basicAttachment = new BasicAttachment(jsonPdf.toJSONString(), localPdfFile.getName(), SystemConstant.PDF_PREFIX, new BigDecimal(localPdfFile.length()), DigestUtils.md5Hex(new FileInputStream(localPdfFile)), sysUser.getId());
-                    basicAttachmentService.save(basicAttachment);
-                    tbTask.setImportFileName(basicAttachment.getName());
-                    tbTask.setImportFilePath(basicAttachment.getPath());
-                    examDetail.setAttachmentId(basicAttachment.getId());
-                    detailService.saveOrUpdate(examDetail);
-                    basicAttachmentList.add(basicAttachment);
-                    ossUtil.ossUpload(dirName, localPdfFile, BinaryUtil.toBase64String(HexUtils.decodeHex(DigestUtils.md5Hex(new FileInputStream(localPdfFile)))));
-                    localPdfFile.delete();
-                    for (String s : mergePdfList) {
-                        new File(s).delete();
-                    }
+
+                    //合并pdf
+                    basicAttachmentList.add(mergePdf(tbTask, examDetail, sysUser.getId(), paperPdfList, cardPdfList, examStudentPdfList));
                 }
             }
             //最后一步删除附件
-            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);
-            }
+            deleteAttachment(attachmentIds);
         } catch (Exception e) {
             log.error("请求出错", e);
             basicAttachmentService.batchDeleteAttachment(basicAttachmentList);
@@ -350,4 +281,126 @@ public class TaskLogicServiceImpl implements TaskLogicService {
         }
         return map;
     }
+
+    /**
+     * 合并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<>();
+        mergePdfList.addAll(list[0]);
+        mergePdfList.addAll(list[1]);
+        mergePdfList.addAll(list[2]);
+        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());
+        detailService.saveOrUpdate(examDetail);
+        ossUtil.ossUpload(dirName, localPdfFile, BinaryUtil.toBase64String(HexUtils.decodeHex(DigestUtils.md5Hex(new FileInputStream(localPdfFile)))));
+        localPdfFile.delete();
+        for (String s : mergePdfList) {
+            new File(s).delete();
+        }
+        return basicAttachment;
+    }
+
+    /**
+     * 删除附件
+     *
+     * @param attachmentIds
+     */
+    public void deleteAttachment(Set<Long> attachmentIds) {
+        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);
+        }
+    }
+
+    /**
+     * 考生题卡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 examTaskDetail
+     * @param jsonArray
+     * @param userId
+     * @param cardPdfList
+     * @return
+     * @throws IOException
+     */
+    public BasicAttachment cardHtml(String cardContent, String paperType, ExamDetailCourse examDetailCourse, ExamCard examCard, ExamTaskDetail examTaskDetail, 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("examTaskDetailId", examTaskDetail.getId());
+        object.put("attachmentId", cardAttachment.getId());
+        jsonArray.add(object);
+        return cardAttachment;
+    }
 }