wangliang 4 ani în urmă
părinte
comite
34fc76996d

+ 29 - 0
distributed-print-business/src/main/java/com/qmth/distributed/print/business/cache/CreatePdfCacheUtil.java

@@ -0,0 +1,29 @@
+package com.qmth.distributed.print.business.cache;
+
+import com.qmth.distributed.print.business.util.RedisUtil;
+import com.qmth.distributed.print.common.contant.SpringContextHolder;
+
+import java.util.Map;
+
+/**
+ * @Description: 生成pdf cache
+ * @Param:
+ * @return:
+ * @Author: wangliang
+ * @Date: 2021/4/26
+ */
+public class CreatePdfCacheUtil {
+    private static RedisUtil redisUtil = SpringContextHolder.getBean(RedisUtil.class);
+
+    public static void setCurrentPaperType(Long printPlanId, Map<String, Object> map) {
+        redisUtil.setForHash(RedisKeyHelper.printPlanIdCurrentPaperTypeKey(printPlanId), map);
+    }
+
+    public static Map getCurrentPaperType(Long printPlanId) {
+        return redisUtil.getHashEntries(RedisKeyHelper.printPlanIdCurrentPaperTypeKey(printPlanId));
+    }
+
+    public static void deleteCurrentPaperType(Long printPlanId) {
+        redisUtil.delete(RedisKeyHelper.printPlanIdCurrentPaperTypeKey(printPlanId));
+    }
+}

+ 17 - 0
distributed-print-business/src/main/java/com/qmth/distributed/print/business/cache/RedisKeyHelper.java

@@ -0,0 +1,17 @@
+package com.qmth.distributed.print.business.cache;
+
+/**
+ * @Description: redis cache helper
+ * @Param:
+ * @return:
+ * @Author: wangliang
+ * @Date: 2021/4/26
+ */
+public class RedisKeyHelper {
+
+    private static String printPlanIdCurrentPaperTypePrefix = "print_planId_current_paper_type:";
+
+    public static String printPlanIdCurrentPaperTypeKey(Long id) {
+        return printPlanIdCurrentPaperTypePrefix + id;
+    }
+}

+ 12 - 0
distributed-print-business/src/main/java/com/qmth/distributed/print/business/entity/ExamDetail.java

@@ -84,6 +84,18 @@ public class ExamDetail extends BaseEntity implements Serializable {
     @TableField(value = "attachment_id")
     private Long attachmentId;
 
+    @ApiModelProperty(value = "附件路径")
+    @TableField("attachment_path")
+    private String attachmentPath;
+
+    public String getAttachmentPath() {
+        return attachmentPath;
+    }
+
+    public void setAttachmentPath(String attachmentPath) {
+        this.attachmentPath = attachmentPath;
+    }
+
     public static long getSerialVersionUID() {
         return serialVersionUID;
     }

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

@@ -1,7 +1,6 @@
 package com.qmth.distributed.print.business.entity;
 
 import com.baomidou.mybatisplus.annotation.TableField;
-import com.baomidou.mybatisplus.annotation.TableId;
 import com.baomidou.mybatisplus.annotation.TableName;
 import com.fasterxml.jackson.databind.annotation.JsonSerialize;
 import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
@@ -45,17 +44,6 @@ 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;
     }

+ 3 - 2
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.ExamDetail;
 import com.qmth.distributed.print.business.entity.ExamDetailCourse;
 import com.qmth.distributed.print.business.enums.ClassifyEnum;
 import com.qmth.distributed.print.business.enums.UploadFileEnum;
@@ -80,7 +81,7 @@ public interface BasicAttachmentService extends IService<BasicAttachment> {
      * 保存附件
      *
      * @param classifyEnum
-     * @param examDetailCourse
+     * @param examDetail
      * @param basicAttachment
      * @param pdfList
      * @param printCount
@@ -88,7 +89,7 @@ public interface BasicAttachmentService extends IService<BasicAttachment> {
      * @return
      * @throws IOException
      */
-    public BasicAttachment saveAttachmentPdf(ClassifyEnum classifyEnum, ExamDetailCourse examDetailCourse, BasicAttachment basicAttachment, List<PdfDto> pdfList, Integer printCount, Integer sequence) throws IOException, DocumentException;
+    public BasicAttachment saveAttachmentPdf(ClassifyEnum classifyEnum, ExamDetail examDetail, BasicAttachment basicAttachment, List<PdfDto> pdfList, Integer printCount, Integer sequence) throws IOException, DocumentException;
 
     /**
      * 删除附件

+ 23 - 8
distributed-print-business/src/main/java/com/qmth/distributed/print/business/service/impl/BasicAttachmentServiceImpl.java

@@ -10,7 +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.ExamDetail;
 import com.qmth.distributed.print.business.entity.SysUser;
 import com.qmth.distributed.print.business.enums.ClassifyEnum;
 import com.qmth.distributed.print.business.enums.PageSizeEnum;
@@ -131,11 +131,23 @@ public class BasicAttachmentServiceImpl extends ServiceImpl<BasicAttachmentMappe
                 jsonObject.put(SystemConstant.PATH, dirName);
                 String url = SystemConstant.TEMP_FILES_DIR + File.separator + dirName;
                 File localHtmlFile = ossUtil.ossDownload(dirName, url);
-                String pdfDirName = dirName.replaceAll(SystemConstant.HTML_PREFIX, SystemConstant.PDF_PREFIX).replaceAll(UploadFileEnum.HTML.name().toLowerCase(), UploadFileEnum.PDF.name().toLowerCase());
+                StringJoiner pdfStringJoiner = new StringJoiner("");
+                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;
 //                destUrl = destUrl.replaceAll("\\\\","/");
                 HtmlToPdfUtil.convert(localHtmlFile.getPath(), destUrl, PageSizeEnum.A3);
                 File pdfFile = new File(destUrl);
+                if (!pdfFile.exists()) {
+                    pdfFile.getParentFile().mkdirs();
+                    pdfFile.createNewFile();
+                }
+
                 pdfDto = PdfUtil.addPdfPage(pdfFile);
                 localFileList.add(new PdfDto(pdfFile.getPath(), PageSizeEnum.A3, pdfDto.getPageCount()));
                 fileMd5 = DigestUtils.md5Hex(new FileInputStream(pdfFile));
@@ -242,7 +254,7 @@ public class BasicAttachmentServiceImpl extends ServiceImpl<BasicAttachmentMappe
      * 保存附件
      *
      * @param classifyEnum
-     * @param examDetailCourse
+     * @param examDetail
      * @param basicAttachment
      * @param pdfList
      * @param printCount
@@ -252,7 +264,7 @@ public class BasicAttachmentServiceImpl extends ServiceImpl<BasicAttachmentMappe
      */
     @Override
     @Transactional
-    public BasicAttachment saveAttachmentPdf(ClassifyEnum classifyEnum, ExamDetailCourse examDetailCourse, BasicAttachment basicAttachment, List<PdfDto> pdfList, Integer printCount, Integer sequence) throws IOException, DocumentException {
+    public BasicAttachment saveAttachmentPdf(ClassifyEnum classifyEnum, ExamDetail examDetail, BasicAttachment basicAttachment, List<PdfDto> pdfList, Integer printCount, Integer sequence) throws IOException, DocumentException {
         try {
             boolean oss = dictionaryConfig.sysDomain().isOss();
             JSONObject jsonObject = JSONObject.parseObject(basicAttachment.getPath());
@@ -289,20 +301,23 @@ public class BasicAttachmentServiceImpl extends ServiceImpl<BasicAttachmentMappe
             }
 
             HtmlToPdfUtil.convert(htmlFile.getPath(), destUrl, PageSizeEnum.A4);
-            String fileMd5 = DigestUtils.md5Hex(new FileInputStream(pdfFile));
+            String htmlFileMd5 = DigestUtils.md5Hex(new FileInputStream(htmlFile));
+            String pdfFileMd5 = DigestUtils.md5Hex(new FileInputStream(pdfFile));
             PdfDto pdfDto = null;
             if (oss) {//上传至oss
                 pdfDto = PdfUtil.addPdfPage(pdfFile);
-                ossUtil.ossUpload(pdfDirName, pdfFile, BinaryUtil.toBase64String(HexUtils.decodeHex(fileMd5)));
+                ossUtil.ossUpload(pdfDirName, pdfFile, BinaryUtil.toBase64String(HexUtils.decodeHex(pdfFileMd5)));
                 htmlFile.delete();
             }
-            JSONObject attachmentPath = JSONObject.parseObject(examDetailCourse.getAttachmentPath());
+            JSONObject attachmentPath = JSONObject.parseObject(examDetail.getAttachmentPath());
             attachmentPath = Objects.isNull(attachmentPath) ? new JSONObject() : attachmentPath;
             JSONArray jsonArray = (JSONArray) attachmentPath.get(SystemConstant.PATH);
             jsonArray = Objects.isNull(jsonArray) ? new JSONArray() : jsonArray;
             JSONObject object = new JSONObject();
             object.put("printType", classifyEnum.name());
             object.put(SystemConstant.HTML_PATH, filePath);
+            object.put("htmlMd5", htmlFileMd5);
+            object.put("pdfMd5", pdfFileMd5);
             object.put(SystemConstant.PDF_PATH, pdfDirName);
             object.put(SystemConstant.TYPE, SystemConstant.OSS);
             object.put(SystemConstant.UPLOAD_TYPE, new UploadFileEnum[]{
@@ -311,7 +326,7 @@ public class BasicAttachmentServiceImpl extends ServiceImpl<BasicAttachmentMappe
             });
             jsonArray.add(object);
             attachmentPath.put(SystemConstant.PATH, jsonArray);
-            examDetailCourse.setAttachmentPath(attachmentPath.toJSONString());
+            examDetail.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));

+ 5 - 1
distributed-print-business/src/main/java/com/qmth/distributed/print/business/service/impl/ExamDetailServiceImpl.java

@@ -12,6 +12,7 @@ import com.qmth.distributed.print.business.bean.params.SerialNumberParams;
 import com.qmth.distributed.print.business.bean.result.ExaminationDetailResult;
 import com.qmth.distributed.print.business.bean.result.ExaminationResult;
 import com.qmth.distributed.print.business.bean.result.SummarizedDataResult;
+import com.qmth.distributed.print.business.cache.CreatePdfCacheUtil;
 import com.qmth.distributed.print.business.entity.*;
 import com.qmth.distributed.print.business.enums.ExamDetailStatusEnum;
 import com.qmth.distributed.print.business.enums.PrintPlanStatusEnum;
@@ -20,6 +21,7 @@ import com.qmth.distributed.print.business.mapper.ExamDetailMapper;
 import com.qmth.distributed.print.business.service.*;
 import com.qmth.distributed.print.business.templete.execute.AsyncCreatePdfTempleteService;
 import com.qmth.distributed.print.business.util.ConvertUtil;
+import com.qmth.distributed.print.business.util.CreatePdfUtil;
 import com.qmth.distributed.print.business.util.ServletUtil;
 import com.qmth.distributed.print.common.contant.SystemConstant;
 import com.qmth.distributed.print.common.enums.ExceptionResultEnum;
@@ -339,7 +341,7 @@ public class ExamDetailServiceImpl extends ServiceImpl<ExamDetailMapper, ExamDet
         QueryWrapper<ExamDetail> queryWrapper = new QueryWrapper<>();
         queryWrapper.lambda().eq(ExamDetail::getPrintPlanId, detail.getPrintPlanId()).notIn(ExamDetail::getStatus, ExamDetailStatusEnum.NEW, ExamDetailStatusEnum.READY);
         List<ExamDetail> examDetails = this.list(queryWrapper);
-        if(examDetails.isEmpty()){
+        if (examDetails.isEmpty()) {
             UpdateWrapper<ExamPrintPlan> printPlanUpdateWrapper = new UpdateWrapper<>();
             printPlanUpdateWrapper.lambda().set(ExamPrintPlan::getStatus, PrintPlanStatusEnum.READY).eq(ExamPrintPlan::getId, detail.getPrintPlanId());
             examPrintPlanService.update(printPlanUpdateWrapper);
@@ -677,6 +679,8 @@ public class ExamDetailServiceImpl extends ServiceImpl<ExamDetailMapper, ExamDet
         }
         // 删除exam_student 表数据
         examStudentService.remove(new QueryWrapper<ExamStudent>().lambda().in(ExamStudent::getId, examStudentIds));
+
+        CreatePdfCacheUtil.deleteCurrentPaperType(printPlanId);
     }
 
     private List<ExtendFieldsDto> getExtendFieldsByFields(List<FieldsDto> fieldsDtoList) {

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

@@ -47,7 +47,6 @@ import java.io.*;
 import java.lang.reflect.Field;
 import java.time.LocalDateTime;
 import java.util.*;
-import java.util.concurrent.ConcurrentHashMap;
 import java.util.stream.Collectors;
 
 /**
@@ -115,8 +114,6 @@ public class TaskLogicServiceImpl implements TaskLogicService {
     @Resource
     BasicExamRuleService basicExamRuleService;
 
-    private static volatile ConcurrentHashMap<Integer, String> paperTypeMap = null;
-
     /**
      * 创建pdf逻辑
      *
@@ -168,7 +165,6 @@ public class TaskLogicServiceImpl implements TaskLogicService {
             }
             attachmentIds = Objects.isNull(attachmentIds) ? attachmentIds = new HashSet<>() : attachmentIds;
             ftlList = Objects.isNull(ftlList) ? ftlList = new HashSet<>() : ftlList;
-            paperTypeMap = Objects.isNull(paperTypeMap) ? paperTypeMap = new ConcurrentHashMap<>() : paperTypeMap;
             for (ExamDetail examDetail : examDetailList) {
                 tbTask.setObjName(examDetail.getExamRoom() + "-" + examDetail.getExamPlace());
                 //查询examDetailCourse
@@ -196,7 +192,7 @@ public class TaskLogicServiceImpl implements TaskLogicService {
                                 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"));
+                                    createPdfUtil.createCheckIn(examDetail, basicAttachment, ordinaryPdfList, (Integer) jsonObjectOrdinary.get("backupCount"));
                                 }
                             }
                         }
@@ -763,8 +759,4 @@ public class TaskLogicServiceImpl implements TaskLogicService {
         map.put("count", count);
         return map;
     }
-
-    public static ConcurrentHashMap<Integer, String> getPaperTypeMap() {
-        return paperTypeMap;
-    }
 }

+ 63 - 35
distributed-print-business/src/main/java/com/qmth/distributed/print/business/util/CreatePdfUtil.java

@@ -8,12 +8,12 @@ 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.cache.CreatePdfCacheUtil;
 import com.qmth.distributed.print.business.entity.*;
 import com.qmth.distributed.print.business.enums.*;
 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.business.templete.service.impl.TaskLogicServiceImpl;
 import com.qmth.distributed.print.common.contant.SystemConstant;
 import com.qmth.distributed.print.common.enums.ExceptionResultEnum;
 import org.apache.commons.codec.digest.DigestUtils;
@@ -26,7 +26,6 @@ import java.io.File;
 import java.io.FileInputStream;
 import java.io.IOException;
 import java.util.*;
-import java.util.concurrent.ConcurrentMap;
 import java.util.stream.Collectors;
 
 /**
@@ -55,15 +54,18 @@ public class CreatePdfUtil {
     @Resource
     ExamDetailService detailService;
 
+    @Resource
+    RedisUtil redisUtil;
+
     /**
      * 创建登记表
      *
-     * @param examDetailCourse
+     * @param examDetail
      * @param basicAttachment
      * @param ordinaryPdfList
      * @param printCount
      */
-    public void createCheckIn(ExamDetailCourse examDetailCourse, BasicAttachment basicAttachment, List<PdfDto> ordinaryPdfList, Integer printCount) throws IOException, DocumentException {
+    public void createCheckIn(ExamDetail examDetail, BasicAttachment basicAttachment, List<PdfDto> ordinaryPdfList, Integer printCount) throws IOException, DocumentException {
         if (Objects.isNull(basicAttachment)) {
             throw ExceptionResultEnum.ERROR.exception("找不到附件");
         }
@@ -78,7 +80,7 @@ public class CreatePdfUtil {
                 ordinaryPdfList.add(new PdfDto(localFile.getPath(), PageSizeEnum.A4, pdfDto.getPageCount()));
             }
         } else {
-            basicAttachmentService.saveAttachmentPdf(ClassifyEnum.CHECK_IN, examDetailCourse, basicAttachment, ordinaryPdfList, printCount, 0);
+            basicAttachmentService.saveAttachmentPdf(ClassifyEnum.CHECK_IN, examDetail, basicAttachment, ordinaryPdfList, printCount, 0);
         }
     }
 
@@ -154,7 +156,7 @@ 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("examDetail", v -> examDetail);
         htmlMap.computeIfAbsent("printType", v -> ClassifyEnum.PACKAGE);
         freemarkerUtil.createPaperPackage(htmlMap);
     }
@@ -252,7 +254,7 @@ 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("examDetail", v -> examDetail);
         htmlMap.computeIfAbsent("printType", v -> ClassifyEnum.SIGN);
         freemarkerUtil.createSignBook(htmlMap);
     }
@@ -341,43 +343,69 @@ public class CreatePdfUtil {
      * @return
      */
     public String getPaperType(ExamPrintPlan examPrintPlan, ExamTaskDetail examTaskDetail, ExamDetail examDetail, ExamDetailCourse examDetailCourse) {
-        ConcurrentMap<Integer, String> paperTypeMap = TaskLogicServiceImpl.getPaperTypeMap();
+        Map<String, Object> paperTypeMap = CreatePdfCacheUtil.getCurrentPaperType(examPrintPlan.getId());
+        paperTypeMap = Objects.isNull(paperTypeMap) ? paperTypeMap = new HashMap<>() : paperTypeMap;
         String key = examDetail.getSchoolId()
                 + Math.abs(examDetail.getExamStartTime() + examDetail.getExamEndTime())
                 + examDetail.getExamPlace()
                 + examDetailCourse.getCourseCode()
                 + examDetailCourse.getPaperNumber();
         String paperType = null;
-        if (paperTypeMap.containsKey(key.hashCode())) {
-            paperType = paperTypeMap.get(key.hashCode());
-        } else {
-            //抽取卷型
-            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(",");
+        boolean lock = true;
+        for (int i = 0; i < SystemConstant.MAX_RETRY_COUNT; i++) {
+            lock = redisUtil.lock(key, SystemConstant.REDIS_CACHE_TIME_OUT);
+            if (lock) {
+                try {
+                    if (paperTypeMap.containsKey(key)) {
+                        paperType = (String) paperTypeMap.get(key);
+                    } else {
+                        //抽取卷型
+                        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);
+                        paperType = paperTypes[paperRandom];
+                        String finalPaperType = paperType;
+                        paperTypeMap.computeIfAbsent(key, v -> finalPaperType);
+                        CreatePdfCacheUtil.setCurrentPaperType(examPrintPlan.getId(), paperTypeMap);
+                    }
+                    break;
+                } catch (Exception e) {
+                    log.error("请求出错", e);
+                } finally {
+                    redisUtil.releaseLock(key);
                 }
-            } else {
-                if (Objects.isNull(exposedPaperType) && Objects.isNull(unexposedPaperType)) {
-                    throw ExceptionResultEnum.ERROR.exception("当前没有未曝光的卷型");
-                }
-                if (Objects.nonNull(unexposedPaperType)) {
-                    paperTypes = unexposedPaperType.split(",");
-                } else {
-                    paperTypes = exposedPaperType.split(",");
+            } else {//未获取到锁,等待500毫秒继续获取
+                try {
+                    Thread.sleep(500);
+                } catch (InterruptedException e) {
+                    e.printStackTrace();
                 }
             }
-            int paperRandom = new Random().nextInt(paperTypes.length);
-            paperType = paperTypes[paperRandom];
-            String finalPaperType = paperType;
-            paperTypeMap.computeIfAbsent(key.hashCode(), v -> finalPaperType);
+        }
+        log.info("getPaperType key:{},paperType:{},lock:{},examDetailId:{},examDetailCourseId:{}", key, paperType, lock, examDetail.getId(), examDetailCourse.getId());
+        if (!lock) {
+            log.info("getPaperType 未获取到锁,key:{}", key);
+            throw ExceptionResultEnum.ERROR.exception("未获取到试卷类型");
         }
         return paperType;
     }

+ 17 - 15
distributed-print-business/src/main/java/com/qmth/distributed/print/business/util/FreemarkerUtil.java

@@ -4,7 +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.entity.ExamDetail;
 import com.qmth.distributed.print.business.enums.ClassifyEnum;
 import com.qmth.distributed.print.business.enums.UploadFileEnum;
 import com.qmth.distributed.print.business.service.BasicAttachmentService;
@@ -20,6 +20,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;
@@ -81,7 +82,7 @@ public class FreemarkerUtil {
         Writer out = null;
         try {
             BasicAttachment basicAttachment = (BasicAttachment) dataMap.get("basicAttachment");
-            ExamDetailCourse examDetailCourse = (ExamDetailCourse) dataMap.get("examDetailCourse");
+            ExamDetail examDetail = (ExamDetail) dataMap.get("examDetail");
             ClassifyEnum classifyEnum = (ClassifyEnum) dataMap.get("printType");
             JSONObject jsonObject = JSONObject.parseObject(basicAttachment.getPath());
             String path = (String) jsonObject.get(SystemConstant.PATH);
@@ -94,12 +95,17 @@ public class FreemarkerUtil {
             // step1 加载模版文件
             Template template = configuration.getTemplate(ftlName + SystemConstant.FTL_PREFIX);
             // step2 生成数据
-            StringJoiner localStringJoiner = new StringJoiner("");
-            localStringJoiner.add(SystemConstant.TEMP_FILES_DIR + File.separator + ftlPath.replaceAll(UploadFileEnum.FILE.getTitle(), UploadFileEnum.HTML.getTitle()).replaceAll(UploadFileEnum.UPLOAD.getTitle(), UploadFileEnum.HTML.getTitle()))
-                    .add(File.separator)
-                    .add(ftlName)
-                    .add(SystemConstant.HTML_PREFIX);
-            File htmlFile = new File(localStringJoiner.toString());
+            boolean oss = dictionaryConfig.sysDomain().isOss();
+            LocalDateTime nowTime = LocalDateTime.now();
+            StringJoiner stringJoiner = new StringJoiner("");
+            stringJoiner.add(SystemConstant.TEMP_FILES_DIR).add(File.separator);
+            StringJoiner htmlStringJoiner = new StringJoiner("");
+            htmlStringJoiner.add(UploadFileEnum.HTML.getTitle()).add(File.separator);
+            htmlStringJoiner.add(String.valueOf(nowTime.getYear())).add(File.separator)
+                    .add(String.format("%02d", nowTime.getMonthValue())).add(File.separator)
+                    .add(String.format("%02d", nowTime.getDayOfMonth()));
+            htmlStringJoiner.add(File.separator).add(SystemConstant.getUuid()).add(SystemConstant.HTML_PREFIX);
+            File htmlFile = new File(stringJoiner.toString() + htmlStringJoiner.toString());
             if (!htmlFile.getParentFile().exists()) {
                 htmlFile.getParentFile().mkdirs();
                 htmlFile.createNewFile();
@@ -108,19 +114,15 @@ public class FreemarkerUtil {
             // step3 输出文件
             template.process(dataMap, out);
             out.flush();
-            boolean oss = dictionaryConfig.sysDomain().isOss();
             if (oss) {
-                StringJoiner stringJoiner = new StringJoiner("");
-                stringJoiner.add(ftlPath.replaceAll(UploadFileEnum.FILE.getTitle(), UploadFileEnum.HTML.getTitle()).replaceAll(UploadFileEnum.UPLOAD.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());
+                ossUtil.ossUpload(htmlStringJoiner.toString(), htmlFile, DigestUtils.md5Hex(new FileInputStream(htmlFile)));
+                jsonObject.put(SystemConstant.HTML_PATH, htmlStringJoiner.toString());
                 jsonObject.put(SystemConstant.UPLOAD_TYPE, new UploadFileEnum[]{
                         UploadFileEnum.FILE,
                         UploadFileEnum.HTML
                 });
                 basicAttachment.setPath(jsonObject.toJSONString());
-                basicAttachmentService.saveAttachmentPdf(classifyEnum, examDetailCourse, basicAttachment, (List<PdfDto>) dataMap.get("variablePdfList"), (Integer) dataMap.get("printCount"), (Integer) dataMap.get("sequence"));
+                basicAttachmentService.saveAttachmentPdf(classifyEnum, examDetail, basicAttachment, (List<PdfDto>) dataMap.get("variablePdfList"), (Integer) dataMap.get("printCount"), (Integer) dataMap.get("sequence"));
             }
             htmlFile.delete();
         } catch (Exception e) {

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

@@ -93,6 +93,12 @@ public class SystemConstant {
     public static final int DEFAULT_SESSION_EXPIRE = 1;//过期时间1天
     public static final long REDIS_DEFAULT_EXPIRE_TIME = 24 * 60L * 60L;//过期时间24小时
 
+    /**
+     * redis lock
+     */
+    public static final int MAX_RETRY_COUNT = 30;
+    public static final long REDIS_CACHE_TIME_OUT = 60L;
+
     /**
      * aes相关
      */