Browse Source

3.3.1 生成pdf优化

xiaofei 1 year ago
parent
commit
df65c4d840

+ 39 - 15
distributed-print-business/src/main/java/com/qmth/distributed/print/business/bean/dto/CreatePdfDto.java

@@ -1,5 +1,9 @@
 package com.qmth.distributed.print.business.bean.dto;
 
+import com.qmth.distributed.print.business.entity.ExamDetail;
+import com.qmth.distributed.print.business.entity.ExamDetailCourse;
+import com.qmth.distributed.print.business.entity.ExamPrintPlan;
+import com.qmth.teachcloud.common.entity.BasicAttachment;
 import io.swagger.annotations.ApiModelProperty;
 
 import java.io.File;
@@ -11,16 +15,20 @@ import java.util.List;
  */
 public class CreatePdfDto {
 
-    @ApiModelProperty(name = "所有试卷")
+    @ApiModelProperty(name = "所有试卷(试卷+备用试卷)")
     private List<PdfDto> paperPdfList = new ArrayList<>();
-    @ApiModelProperty(name = "所有题卡")
+    @ApiModelProperty(name = "所有题卡(题卡+备用题卡)")
     private List<PdfDto> cardPdfList = new ArrayList<>();
-    @ApiModelProperty(name = "备份试卷")
-    private List<PdfDto> backupPaperPdfList = new ArrayList<>();
-    @ApiModelProperty(name = "备份题卡")
-    private List<PdfDto> backupCardPdfList = new ArrayList<>();
-    @ApiModelProperty(name = "临时文件")
+    @ApiModelProperty(name = "考场对象")
+    private ExamDetail examDetail;
+    @ApiModelProperty(name = "计划对象")
+    private ExamPrintPlan examPrintPlan;
+    @ApiModelProperty(name = "考场课程对象")
+    private List<ExamDetailCourse> examDetailCourseList;
+    @ApiModelProperty(name = "需要删除的临时文件")
     private List<File> fileTempList = new ArrayList<>();
+    @ApiModelProperty(name = "需要删除的附件数据")
+    private List<BasicAttachment> attachmentList = new ArrayList<>();
 
     public List<PdfDto> getPaperPdfList() {
         return paperPdfList;
@@ -38,20 +46,28 @@ public class CreatePdfDto {
         this.cardPdfList = cardPdfList;
     }
 
-    public List<PdfDto> getBackupPaperPdfList() {
-        return backupPaperPdfList;
+    public ExamDetail getExamDetail() {
+        return examDetail;
     }
 
-    public void setBackupPaperPdfList(List<PdfDto> backupPaperPdfList) {
-        this.backupPaperPdfList = backupPaperPdfList;
+    public void setExamDetail(ExamDetail examDetail) {
+        this.examDetail = examDetail;
     }
 
-    public List<PdfDto> getBackupCardPdfList() {
-        return backupCardPdfList;
+    public ExamPrintPlan getExamPrintPlan() {
+        return examPrintPlan;
     }
 
-    public void setBackupCardPdfList(List<PdfDto> backupCardPdfList) {
-        this.backupCardPdfList = backupCardPdfList;
+    public void setExamPrintPlan(ExamPrintPlan examPrintPlan) {
+        this.examPrintPlan = examPrintPlan;
+    }
+
+    public List<ExamDetailCourse> getExamDetailCourseList() {
+        return examDetailCourseList;
+    }
+
+    public void setExamDetailCourseList(List<ExamDetailCourse> examDetailCourseList) {
+        this.examDetailCourseList = examDetailCourseList;
     }
 
     public List<File> getFileTempList() {
@@ -61,4 +77,12 @@ public class CreatePdfDto {
     public void setFileTempList(List<File> fileTempList) {
         this.fileTempList = fileTempList;
     }
+
+    public List<BasicAttachment> getAttachmentList() {
+        return attachmentList;
+    }
+
+    public void setAttachmentList(List<BasicAttachment> attachmentList) {
+        this.attachmentList = attachmentList;
+    }
 }

+ 26 - 0
distributed-print-business/src/main/java/com/qmth/distributed/print/business/entity/ExamTaskDetail.java

@@ -1,15 +1,21 @@
 package com.qmth.distributed.print.business.entity;
 
+import com.alibaba.fastjson.JSON;
 import com.baomidou.mybatisplus.annotation.FieldStrategy;
 import com.baomidou.mybatisplus.annotation.TableField;
 import com.baomidou.mybatisplus.annotation.TableName;
 import com.fasterxml.jackson.databind.annotation.JsonSerialize;
 import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
 import com.qmth.teachcloud.common.base.BaseEntity;
+import com.qmth.teachcloud.common.bean.vo.PaperInfoVo;
 import io.swagger.annotations.ApiModelProperty;
+import org.apache.commons.lang3.StringUtils;
 
 import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.List;
+import java.util.stream.Collectors;
 
 /**
  * <p>
@@ -94,6 +100,10 @@ public class ExamTaskDetail extends BaseEntity implements Serializable {
     @TableField(exist = false)
     private List<Long> approveUserIds;
 
+    @ApiModelProperty(value = "卷型解析内容")
+    @TableField(exist = false)
+    List<PaperInfoVo> paperInfoVoList;
+
     public static long getSerialVersionUID() {
         return serialVersionUID;
     }
@@ -193,4 +203,20 @@ public class ExamTaskDetail extends BaseEntity implements Serializable {
     public void setOperateType(String operateType) {
         this.operateType = operateType;
     }
+
+    public List<PaperInfoVo> getPaperInfoVoList(String paperTypes) {
+        List<PaperInfoVo> paperInfoVos = new ArrayList<>();
+        if(StringUtils.isNotBlank(paperAttachmentIds)){
+            if (StringUtils.isNotBlank(paperAttachmentIds)) {
+                paperInfoVos = JSON.parseArray(paperAttachmentIds, PaperInfoVo.class);
+                if (StringUtils.isBlank(paperTypes)) {
+                    return paperInfoVos;
+                } else {
+                    List<String> paperTypeList = Arrays.asList(paperTypes.split(","));
+                    paperInfoVos = paperInfoVos.stream().filter(m -> paperTypeList.contains(m.getName())).collect(Collectors.toList());
+                }
+            }
+        }
+        return paperInfoVos;
+    }
 }

+ 2 - 2
distributed-print-business/src/main/java/com/qmth/distributed/print/business/service/impl/PrintCommonServiceImpl.java

@@ -653,7 +653,7 @@ public class PrintCommonServiceImpl implements PrintCommonService {
     @Transactional
     public BasicAttachment saveAttachmentPdfFromHtml(String fileName, File file, Long userId, List<PdfDto> localFileList) {
         BasicAttachment basicAttachment = null;
-        File  pdfFileTemp = null;
+        File pdfFileTemp = null;
         try {
 
             pdfFileTemp = SystemConstant.getFileTempVar(SystemConstant.PDF_PREFIX);
@@ -665,7 +665,7 @@ public class PrintCommonServiceImpl implements PrintCommonService {
             FilePathVo filePathVo = fileUploadService.uploadFile(pdfFileTemp, UploadFileEnum.PDF, pdfDirName);
 
             PdfDto pdfDto = PdfUtil.addPdfPage(pdfFileTemp);
-            if(localFileList != null) {
+            if (localFileList != null) {
                 localFileList.add(new PdfDto(pdfFileTemp.getPath(), PageSizeEnum.A3, pdfDto.getPageCount()));
             }
             basicAttachment = new BasicAttachment(JSON.toJSONString(filePathVo), fileName, SystemConstant.PDF_PREFIX, new BigDecimal(file.length()), filePathVo.getMd5(), userId);

+ 151 - 161
distributed-print-business/src/main/java/com/qmth/distributed/print/business/templete/service/impl/PdfTaskLogicServiceImpl.java

@@ -1,5 +1,6 @@
 package com.qmth.distributed.print.business.templete.service.impl;
 
+import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
@@ -14,22 +15,21 @@ import com.qmth.distributed.print.business.enums.ExamStatusEnum;
 import com.qmth.distributed.print.business.enums.PrintPlanStatusEnum;
 import com.qmth.distributed.print.business.service.*;
 import com.qmth.distributed.print.business.templete.service.PdfTaskLogicService;
+import com.qmth.distributed.print.business.util.PdfUtil;
 import com.qmth.distributed.print.business.util.pdf.CreatePdfNewUtil;
 import com.qmth.teachcloud.common.base.BaseEntity;
+import com.qmth.teachcloud.common.bean.vo.FilePathVo;
 import com.qmth.teachcloud.common.bean.vo.PaperInfoVo;
 import com.qmth.teachcloud.common.contant.SystemConstant;
 import com.qmth.teachcloud.common.entity.BasicAttachment;
 import com.qmth.teachcloud.common.entity.BasicExam;
-import com.qmth.teachcloud.common.entity.BasicPrintConfig;
 import com.qmth.teachcloud.common.entity.BasicSchool;
-import com.qmth.teachcloud.common.enums.CreatePdfTypeEnum;
-import com.qmth.teachcloud.common.enums.ExamCategoryEnum;
-import com.qmth.teachcloud.common.enums.ExceptionResultEnum;
-import com.qmth.teachcloud.common.enums.FlowStatusEnum;
+import com.qmth.teachcloud.common.enums.*;
 import com.qmth.teachcloud.common.service.BasicAttachmentService;
 import com.qmth.teachcloud.common.service.BasicStudentService;
 import com.qmth.teachcloud.common.service.CommonCacheService;
-import com.qmth.teachcloud.common.util.ExamTaskUtil;
+import com.qmth.teachcloud.common.service.FileUploadService;
+import com.qmth.teachcloud.common.util.FileUtil;
 import org.apache.commons.collections4.CollectionUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.slf4j.Logger;
@@ -39,6 +39,8 @@ import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
 import javax.annotation.Resource;
+import java.io.File;
+import java.math.BigDecimal;
 import java.util.*;
 import java.util.concurrent.atomic.AtomicInteger;
 import java.util.stream.Collectors;
@@ -84,6 +86,10 @@ public class PdfTaskLogicServiceImpl implements PdfTaskLogicService {
     BasicExamService basicExamService;
     @Resource
     ExamTaskAssignPaperTypeService examTaskAssignPaperTypeService;
+    @Resource
+    private PrintCommonService printCommonService;
+    @Resource
+    private FileUploadService fileUploadService;
 
     /**
      * 创建A4文件
@@ -140,32 +146,111 @@ public class PdfTaskLogicServiceImpl implements PdfTaskLogicService {
 
     @Override
     public void executeCreatePdfLogic(TBTaskPdf tbTaskPdf) {
+        CreatePdfDto createPdfDto = new CreatePdfDto();
         try {
             // 更新状态(考场状态、印刷计划状态)
             updatePdfDataStatus(tbTaskPdf);
+
+            ExamDetail examDetail = examDetailService.getById(tbTaskPdf.getId());
+            createPdfDto.setExamDetail(examDetail);
+            ExamPrintPlan examPrintPlan = examPrintPlanService.getById(examDetail.getPrintPlanId());
+            createPdfDto.setExamPrintPlan(examPrintPlan);
             List<ExamDetailCourse> examDetailCourseList = examDetailCourseService.listByExamDetailId(tbTaskPdf.getId());
+            createPdfDto.setExamDetailCourseList(examDetailCourseList);
+
             // 抽取卷型,更新考场、考生对应卷型值
-            updateAssignPaperType(tbTaskPdf, examDetailCourseList);
+            updateAssignPaperType(tbTaskPdf, createPdfDto);
             // 查询生成pdf需要文件
-            CreatePdfDto createPdfDto = new CreatePdfDto();
             assemblePdfFile(tbTaskPdf, createPdfDto);
             // 生成pdf文件并合并
             //合并(试卷+备用试卷)
-            String dirNamePaper = createPdfNewUtil.mergeA3Pdf(createPdfDto.getPaperPdfList(), createPdfDto.getBackupPaperPdfList());
+            String dirNamePaper = createPdfNewUtil.mergeA3Pdf(createPdfDto.getPaperPdfList());
             //合并A3(题卡+备用题卡)
-            String dirNameCardA3 = createPdfNewUtil.mergeA3Pdf(createPdfDto.getCardPdfList(), createPdfDto.getBackupCardPdfList());
-            // 生成A4文件
-            createA4File(tbTaskPdf, examDetailCourseList);
+            String dirNameCardA3 = createPdfNewUtil.mergeA3Pdf(createPdfDto.getCardPdfList());
             // 更新文件路径、更新考场状态、印刷计划状态
             createPdfNewUtil.mergePdfSaveDb(dirNamePaper, dirNameCardA3, tbTaskPdf);
+            // 生成A4文件
+            createA4File(tbTaskPdf, examDetailCourseList);
         } catch (Exception e) {
             throw new RuntimeException(e);
+        } finally {
+            if(CollectionUtils.isNotEmpty(createPdfDto.getFileTempList())){
+                for (File file : createPdfDto.getFileTempList()) {
+                    FileUtil.deleteFile(file);
+                }
+            }
+        }
+    }
+
+    private void updatePdfDataStatus(TBTaskPdf tbTaskPdf) {
+        //查询printPlan
+        ExamPrintPlan examPrintPlan = examPrintPlanService.getById(tbTaskPdf.getPrintPlanId());
+        if (Objects.isNull(examPrintPlan)) {
+            throw ExceptionResultEnum.EXAM_PRINT_IS_NULL.exception();
+        }
+        UpdateWrapper<ExamDetail> examDetailQueryWrapper = new UpdateWrapper<>();
+        examDetailQueryWrapper.lambda().set(ExamDetail::getStatus, ExamDetailStatusEnum.CREATING)
+                .eq(ExamDetail::getId, tbTaskPdf.getId());
+        examDetailService.update(examDetailQueryWrapper);
+
+        //所有考场都撤回,印刷任务状态改为就绪
+        QueryWrapper<ExamDetail> queryWrapper = new QueryWrapper<>();
+        queryWrapper.lambda().eq(ExamDetail::getPrintPlanId, tbTaskPdf.getPrintPlanId())
+                .notIn(ExamDetail::getStatus, ExamDetailStatusEnum.NEW, ExamDetailStatusEnum.READY);
+        List<ExamDetail> examDetails = examDetailService.list(queryWrapper);
+        if (CollectionUtils.isEmpty(examDetails)) {
+            examPrintPlanService.updateStatusById(tbTaskPdf.getPrintPlanId(), PrintPlanStatusEnum.READY);
+        }
+    }
+
+    @Transactional
+    public void updateAssignPaperType(TBTaskPdf tbTaskPdf, CreatePdfDto createPdfDto) {
+        for (ExamDetailCourse examDetailCourse : createPdfDto.getExamDetailCourseList()) {
+            ExamTaskAssignPaperType examTaskAssignPaperType = examTaskAssignPaperTypeService.extractPaperType(tbTaskPdf, examDetailCourse);
+            List<ExamStudent> examStudentList = examStudentService.listByExamDetailCourseId(examDetailCourse.getId());
+            // 考生实际关联试卷类型
+            List<String> relatePaperTypes = new ArrayList<>();
+            List<String> paperTypes = Arrays.stream(examTaskAssignPaperType.getPaperType().split(",")).sorted(Comparator.comparing(String::valueOf)).collect(Collectors.toList());
+            if (CollectionUtils.isNotEmpty(examStudentList)) {
+                AtomicInteger atomicInteger = new AtomicInteger(0);
+                for (ExamStudent examStudent : examStudentList) {
+                    int i1 = atomicInteger.getAndIncrement();
+                    int mod = i1 % paperTypes.size();
+                    examStudent.setPaperType(paperTypes.get(mod));
+                    if (!relatePaperTypes.contains(examStudent.getPaperType())) {
+                        relatePaperTypes.add(examStudent.getPaperType());
+                    }
+                }
+            } else if (examDetailCourse.getTotalSubjects() != null) {
+                if (examDetailCourse.getTotalSubjects().intValue() - paperTypes.size() >= 0) {
+                    relatePaperTypes.addAll(paperTypes);
+                } else {
+                    for (int i = 0; i < examDetailCourse.getTotalSubjects().intValue(); i++) {
+                        relatePaperTypes.add(paperTypes.get(i));
+                    }
+                }
+            } else {
+                relatePaperTypes.addAll(paperTypes);
+            }
+
+            // 更新卷型数据
+            if (!CollectionUtils.isEqualCollection(paperTypes, relatePaperTypes)) {
+                examTaskAssignPaperType.setPaperType(String.join(",", relatePaperTypes));
+                examTaskAssignPaperTypeService.updateByMultiId(examTaskAssignPaperType);
+            }
+
+            examDetailCourse.setPaperType(String.join(",", relatePaperTypes));
+            examDetailCourseService.updateById(examDetailCourse);
+
+            examStudentService.updateBatchById(examStudentList);
         }
     }
 
     private void assemblePdfFile(TBTaskPdf tbTaskPdf, CreatePdfDto createPdfDto) throws Exception {
-        List<ExamDetailCourse> examDetailCourseList = examDetailCourseService.listByExamDetailId(tbTaskPdf.getId());
-        for (ExamDetailCourse examDetailCourse : examDetailCourseList) {
+        ExamDetail examDetail = createPdfDto.getExamDetail();
+        ExamPrintPlan examPrintPlan = createPdfDto.getExamPrintPlan();
+
+        for (ExamDetailCourse examDetailCourse : createPdfDto.getExamDetailCourseList()) {
             String[] paperTypes = examDetailCourse.getPaperType().split(",");
 
             List<PdfDto> studentPaperPdfList = new ArrayList<>();//所有试卷
@@ -188,57 +273,25 @@ public class PdfTaskLogicServiceImpl implements PdfTaskLogicService {
             }
             ExamTaskDetail examTaskDetail = examTaskDetailService.getByExamTaskId(examTask.getId());
 
-            Map<String, ExamCard> examCardMap = new HashMap<>();
-            List<PaperInfoVo> paperInfoVoList = ExamTaskUtil.parsePaperAttachmentPath(examTaskDetail.getPaperAttachmentIds());
-            ExamCard examCard;
-            for (PaperInfoVo paperInfoVo : paperInfoVoList) {
-                Long cardId = Long.valueOf(paperInfoVo.getCardId());
-                if (Objects.nonNull(cardId)) {
-                    examCard = examCardService.getById(cardId);
-                    if (Objects.isNull(examCard)) {
-                        throw ExceptionResultEnum.EXAM_CARD_IS_NULL.exception();
-                    }
-                    examCardMap.put(paperInfoVo.getName(), examCard);
-                }
-            }
-
-            //查询题卡规则
-            BasicCardRule basicCardRule = basicCardRuleService.getById(examTask.getCardRuleId());
-
             List<ExamStudent> examStudentList = examStudentService.listByExamDetailCourseId(examDetailCourse.getId());
 
             CreatePdfTypeEnum createPdfType = tbTaskPdf.getCreateType();
-
-            ExamPrintPlan examPrintPlan = examPrintPlanService.getById(tbTaskPdf.getPrintPlanId());
             String printContent = examPrintPlan.getPrintContent();
             if (StringUtils.isBlank(printContent)) {
-                continue;
+                throw ExceptionResultEnum.ERROR.exception("印刷计划未设置打印内容,[试卷、题卡]至少选择一项");
             }
-
             // 计算备份数量,默认最小为1份。
-            ExamDetail examDetail = examDetailService.getById(tbTaskPdf.getId());
             int backupCount = SystemConstant.calcBackupCount(examDetail.getBackupCount(), examDetail.getTotalSubjects(), 1);
 
             // 试卷数据组装
             if ((CreatePdfTypeEnum.ALL.equals(createPdfType) || CreatePdfTypeEnum.PAPER.equals(createPdfType)) && printContent.contains("PAPER")) {
-                List<PaperPdfDto> paperPdfDto = createPdfNewUtil.getPaperPdfFile(examDetailCourse.getPaperType(), examTaskDetail, createPdfDto.getFileTempList());
-
-                //获取试卷pdf
-                PdfDto pdfDto = createPdfNewUtil.getPaperPdf(paperPdfDto, backupCount, backupPaperPdfList);
-                if (Objects.nonNull(pdfDto)) {
-                    examDetailCourse.setPaperPagesA3(pdfDto.getPageCount());
-                } else {
-                    examDetailCourse.setPaperPagesA3(examDetailCourse.getPaperPagesA3());
-                }
-                // 备用试卷
-                createPdfDto.getBackupPaperPdfList().addAll(backupPaperPdfList);
+                List<PaperPdfDto> paperPdfDtoList = createPdfNewUtil.getPaperPdfFile(examDetailCourse.getPaperType(), examTaskDetail.getPaperInfoVoList(null), createPdfDto);
 
-                if (examStudentList != null && examStudentList.size() > 0) {
+                // 生成考生试卷
+                if (CollectionUtils.isNotEmpty(examStudentList)) {
                     for (ExamStudent t : examStudentList) {
-                        if (Objects.nonNull(pdfDto)) {
-                            String[] waterMarkNames = {t.getStudentName(), t.getTicketNumber()};
-                            createPdfNewUtil.getExamStudentPaperPdf(t.getPaperType(), paperPdfDto, studentPaperPdfList, waterMarkNames);
-                        }
+                        String[] waterMarkNames = {t.getStudentName(), t.getTicketNumber()};
+                        createPdfNewUtil.getExamStudentPaperPdf(t.getPaperType(), paperPdfDtoList, createPdfDto, waterMarkNames);
                     }
                 } else if (examDetail.getTotalSubjects() != null) {
                     AtomicInteger atomicInteger = new AtomicInteger(0);
@@ -246,56 +299,41 @@ public class PdfTaskLogicServiceImpl implements PdfTaskLogicService {
                     while (i < examDetail.getTotalSubjects()) {
                         int seq = atomicInteger.getAndIncrement();
                         int mod = seq % paperTypes.length;
-                        if (Objects.nonNull(pdfDto)) {
-                            createPdfNewUtil.getExamStudentPaperPdf(paperTypes[mod], paperPdfDto, studentPaperPdfList, null);
-                        }
+                        createPdfNewUtil.getExamStudentPaperPdf(paperTypes[mod], paperPdfDtoList, createPdfDto, null);
                         i++;
                     }
                 } else {
-                    throw ExceptionResultEnum.ERROR.exception("数据错误:未找到考生或者印刷数量");
+                    throw ExceptionResultEnum.ERROR.exception("考试数据有误,未找到考生或者印刷数量");
+                }
+
+                // 获取备份试卷
+                PdfDto pdfDto = createPdfNewUtil.getBackupPaperPdfList(paperPdfDtoList, backupCount, createPdfDto);
+                if (Objects.nonNull(pdfDto)) {
+                    examDetailCourse.setPaperPagesA3(pdfDto.getPageCount());
                 }
-                // 考生试卷
-                createPdfDto.getPaperPdfList().addAll(studentPaperPdfList);
             }
 
             // 题卡数据组装
             if ((CreatePdfTypeEnum.ALL.equals(createPdfType) || CreatePdfTypeEnum.CARD_A3.equals(createPdfType)) && printContent.contains("CARD")) {
-                Map<String, ExamCard> examCardDetailMap = new HashMap<>();
-                Map<String, String> cardContentMap = new HashMap<>();
-                JSONObject jsonObject = new JSONObject();
-                JSONArray jsonArray = new JSONArray();
-                JSONArray stuJsonArray = new JSONArray();
-                for (String s : paperTypes) {
-                    examCard = examCardMap.get(s);
-                    Optional.ofNullable(examCard).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("卷型" + s + "题卡不存在"));
-
-                    //把模板页面上的 ${} 替换成空
-                    String cardContent = createPdfNewUtil.resetHtmlTemplateBar(examCard.getHtmlContent());
-
-                    for (int i = 1; i <= backupCount; i++) {
-                        String packageCode = examDetail.getPackageCode() + String.format(SystemConstant.DATE_TIME_FORMAT, i);
-                        BasicAttachment basicAttachment = createPdfNewUtil.cardHtml(packageCode, cardContent, s, tbTaskPdf.getCreateId(), backupCardPdfList, basicCardRule);
-                        examDetailCourse.setCardPagesA3(basicAttachment.getPages());
-
-                        JSONObject object = new JSONObject();
-                        object.put("name", s);
-                        object.put("attachmentId", basicAttachment.getId());
-                        object.put("packageCode", packageCode);
-                        jsonArray.add(object);
+                //查询题卡规则
+                BasicCardRule basicCardRule = basicCardRuleService.getById(examTask.getCardRuleId());
+                Map<String, ExamCard> examCardMap = new HashMap<>();
+                ExamCard examCard;
+                for (PaperInfoVo paperInfoVo : examTaskDetail.getPaperInfoVoList(null)) {
+                    if (StringUtils.isNotBlank(paperInfoVo.getCardId())) {
+                        examCard = examCardService.getById(paperInfoVo.getCardId());
+                        if (Objects.isNull(examCard)) {
+                            throw ExceptionResultEnum.EXAM_CARD_IS_NULL.exception();
+                        }
+                        examCardMap.put(paperInfoVo.getName(), examCard);
                     }
-                    examCardDetailMap.put(s, examCard);
-                    cardContentMap.put(s, cardContent);
                 }
-                jsonObject.put("card", jsonArray);
-                examDetailCourse.setAttachmentId(jsonObject.toJSONString());
-
-                // 备用题卡
-                createPdfDto.getBackupCardPdfList().addAll(backupCardPdfList);
 
+                JSONArray stuJsonArray = new JSONArray();
                 if (examStudentList != null && examStudentList.size() > 0) {
                     for (ExamStudent t : examStudentList) {
                         // 用带条码的模板
-                        createPdfNewUtil.examStudentHtml(examCardDetailMap.get(t.getPaperType()).getHtmlContent(), t, t.getPaperType(), examDetail, tbTaskPdf.getCreateId(), studentCardPdfList, basicCardRule);
+                        createPdfNewUtil.examStudentHtml(examCardMap.get(t.getPaperType()).getHtmlContent(), t, t.getPaperType(), examDetail, tbTaskPdf.getCreateId(), studentCardPdfList, basicCardRule);
                     }
                     examStudentService.updateBatchById(examStudentList);
                 } else if (examDetailCourse.getTotalSubjects() != null) {
@@ -306,7 +344,7 @@ public class PdfTaskLogicServiceImpl implements PdfTaskLogicService {
                         int seq = atomicInteger.getAndIncrement();
                         int mod = seq % examTaskDetail.getDrawCount();
                         String tempPaperType = paperTypes[mod];
-                        BasicAttachment basicAttachment = createPdfNewUtil.examStudentHtml(cardContentMap.get(tempPaperType), null, tempPaperType, examDetail, tbTaskPdf.getCreateId(), studentCardPdfList, basicCardRule);
+                        BasicAttachment basicAttachment = createPdfNewUtil.examStudentHtml(examCardMap.get(tempPaperType).getHtmlContent(), null, tempPaperType, examDetail, tbTaskPdf.getCreateId(), studentCardPdfList, basicCardRule);
 
                         if (!stringBasicAttachmentMap.containsKey(tempPaperType)) {
                             stringBasicAttachmentMap.put(tempPaperType, basicAttachment);
@@ -326,84 +364,36 @@ public class PdfTaskLogicServiceImpl implements PdfTaskLogicService {
                 }
                 // 题卡
                 createPdfDto.getCardPdfList().addAll(studentCardPdfList);
-            }
-        }
-    }
 
-    @Transactional
-    public void updateAssignPaperType(TBTaskPdf tbTaskPdf, List<ExamDetailCourse> examDetailCourseList) {
+                JSONObject jsonObject = new JSONObject();
+                JSONArray jsonArray = new JSONArray();
+                for (String s : paperTypes) {
+                    examCard = examCardMap.get(s);
+                    for (int i = 1; i <= backupCount; i++) {
+                        String backupPackageCode = examDetail.getPackageCode() + String.format(SystemConstant.DATE_TIME_FORMAT, i);
+                        String replaceBackupCardHtmlContent = createPdfNewUtil.replaceBackupCardHtmlParam(examCard.getHtmlContent(), s, basicCardRule, backupPackageCode);
+                        File pdfFile = PdfUtil.htmlToPdf(replaceBackupCardHtmlContent);
+                        PdfDto pdfDto = PdfUtil.addPdfPage(pdfFile);
+                        FilePathVo filePathVo = fileUploadService.uploadFile(pdfFile, UploadFileEnum.PDF, pdfFile.getName());
+                        BasicAttachment basicAttachment = new BasicAttachment(JSON.toJSONString(filePathVo), pdfFile.getName(), SystemConstant.PDF_PREFIX, new BigDecimal(pdfFile.length()), filePathVo.getMd5(), tbTaskPdf.getCreateId());
+                        basicAttachmentService.save(basicAttachment);
 
-        for (ExamDetailCourse examDetailCourse : examDetailCourseList) {
-            ExamTaskAssignPaperType examTaskAssignPaperType = examTaskAssignPaperTypeService.extractPaperType(tbTaskPdf, examDetailCourse);
-            List<ExamStudent> examStudentList = examStudentService.listByExamDetailCourseId(examDetailCourse.getId());
-            // 考生实际关联试卷类型
-            List<String> relatePaperTypes = new ArrayList<>();
-            List<String> paperTypes = Arrays.stream(examTaskAssignPaperType.getPaperType().split(",")).sorted(Comparator.comparing(String::valueOf)).collect(Collectors.toList());
-            if (CollectionUtils.isNotEmpty(examStudentList)) {
-                AtomicInteger atomicInteger = new AtomicInteger(0);
-                for (ExamStudent examStudent : examStudentList) {
-                    int i1 = atomicInteger.getAndIncrement();
-                    int mod = i1 % paperTypes.size();
-                    examStudent.setPaperType(paperTypes.get(mod));
-                    if (!relatePaperTypes.contains(examStudent.getPaperType())) {
-                        relatePaperTypes.add(examStudent.getPaperType());
-                    }
-                }
-            } else if (examDetailCourse.getTotalSubjects() != null) {
-                if (examDetailCourse.getTotalSubjects().intValue() - paperTypes.size() >= 0) {
-                    relatePaperTypes.addAll(paperTypes);
-                } else {
-                    for (int i = 0; i < examDetailCourse.getTotalSubjects().intValue(); i++) {
-                        relatePaperTypes.add(paperTypes.get(i));
+                        examDetailCourse.setCardPagesA3(pdfDto.getActualPageCount());
+
+                        // 备用题卡
+                        createPdfDto.getCardPdfList().add(new PdfDto(pdfFile.getPath(), PageSizeEnum.A3, pdfDto.getPageCount()));
+
+                        JSONObject object = new JSONObject();
+                        object.put("name", s);
+                        object.put("attachmentId", basicAttachment.getId());
+                        object.put("packageCode", backupPackageCode);
+                        jsonArray.add(object);
                     }
                 }
-            } else {
-                relatePaperTypes.addAll(paperTypes);
-            }
-
-            // 更新卷型数据
-            if (!CollectionUtils.isEqualCollection(paperTypes, relatePaperTypes)) {
-                examTaskAssignPaperType.setPaperType(String.join(",", relatePaperTypes));
-                examTaskAssignPaperTypeService.updateByMultiId(examTaskAssignPaperType);
+                jsonObject.put("card", jsonArray);
+                examDetailCourse.setAttachmentId(jsonObject.toJSONString());
             }
-
-            examDetailCourse.setPaperType(String.join(",", relatePaperTypes));
-            examDetailCourseService.updateById(examDetailCourse);
-
-            examStudentService.updateBatchById(examStudentList);
-        }
-    }
-
-    private void updatePdfDataStatus(TBTaskPdf tbTaskPdf) {
-        //查询printPlan
-        ExamPrintPlan examPrintPlan = examPrintPlanService.getById(tbTaskPdf.getPrintPlanId());
-        if (Objects.isNull(examPrintPlan)) {
-            throw ExceptionResultEnum.EXAM_PRINT_IS_NULL.exception();
-        }
-        UpdateWrapper<ExamDetail> examDetailQueryWrapper = new UpdateWrapper<>();
-        examDetailQueryWrapper.lambda().set(ExamDetail::getStatus, ExamDetailStatusEnum.CREATING)
-                .eq(ExamDetail::getId, tbTaskPdf.getId());
-        examDetailService.update(examDetailQueryWrapper);
-
-        //所有考场都撤回,印刷任务状态改为就绪
-        QueryWrapper<ExamDetail> queryWrapper = new QueryWrapper<>();
-        queryWrapper.lambda().eq(ExamDetail::getPrintPlanId, tbTaskPdf.getPrintPlanId())
-                .notIn(ExamDetail::getStatus, ExamDetailStatusEnum.NEW, ExamDetailStatusEnum.READY);
-        List<ExamDetail> examDetails = examDetailService.list(queryWrapper);
-        if (CollectionUtils.isEmpty(examDetails)) {
-            examPrintPlanService.updateStatusById(tbTaskPdf.getPrintPlanId(), PrintPlanStatusEnum.READY);
         }
     }
 
-    /**
-     * 更新考试计划
-     *
-     * @param basicPrintConfig
-     * @param examPrintPlan
-     */
-    @Transactional
-    public void updateExamPrintPlan(BasicPrintConfig basicPrintConfig, ExamPrintPlan examPrintPlan) {
-
-    }
-
 }

+ 30 - 3
distributed-print-business/src/main/java/com/qmth/distributed/print/business/util/PdfUtil.java

@@ -6,6 +6,7 @@ import com.itextpdf.text.DocumentException;
 import com.itextpdf.text.Element;
 import com.itextpdf.text.Rectangle;
 import com.itextpdf.text.pdf.*;
+import com.qmth.boot.api.exception.ApiException;
 import com.qmth.boot.tools.models.ByteArray;
 import com.qmth.distributed.print.business.bean.dto.PdfDto;
 import com.qmth.teachcloud.common.config.DictionaryConfig;
@@ -17,13 +18,17 @@ import com.qmth.teachcloud.common.enums.PageSizeEnum;
 import com.qmth.teachcloud.common.enums.UploadFileEnum;
 import com.qmth.teachcloud.common.service.CommonCacheService;
 import com.qmth.teachcloud.common.util.FileUtil;
+import com.qmth.teachcloud.common.util.HtmlToPdfUtil;
+import com.qmth.teachcloud.common.util.ResultUtil;
 import org.apache.commons.io.IOUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.springframework.util.FileCopyUtils;
 
 import javax.swing.*;
 import java.awt.*;
 import java.io.*;
+import java.nio.charset.StandardCharsets;
 import java.util.List;
 import java.util.*;
 
@@ -135,11 +140,9 @@ public class PdfUtil {
     }
 
     /**
-     * 新增pdf page
+     * 新增空白pdf(奇数页补空白面后变成偶数页)
      *
      * @param pdfFile
-     * @return
-     * @throws IOException
      */
     public static PdfDto addPdfPage(File pdfFile) throws IOException {
         PdfReader reader = null;
@@ -467,4 +470,28 @@ public class PdfUtil {
         }
         return null;
     }
+
+    /**
+     * html转pdf文件
+     *
+     * @param htmlContent html内容
+     */
+    public static File htmlToPdf(String htmlContent) {
+        File htmlFileTemp = null;
+        try {
+            htmlFileTemp = SystemConstant.getFileTempVar(SystemConstant.HTML_PREFIX);
+            FileCopyUtils.copy(htmlContent.getBytes(StandardCharsets.UTF_8), htmlFileTemp);
+
+            File pdfFileTemp = SystemConstant.getFileTempVar(SystemConstant.PDF_PREFIX);
+            // html转pdf
+            HtmlToPdfUtil.convert(htmlFileTemp.getPath(), pdfFileTemp.getPath(), PageSizeEnum.A3);
+            return pdfFileTemp;
+        } catch (Exception e) {
+            throw ExceptionResultEnum.ERROR.exception("html文件转pdf文件失败");
+        } finally {
+            if (htmlFileTemp != null) {
+                FileUtil.deleteFile(htmlFileTemp);
+            }
+        }
+    }
 }

+ 76 - 35
distributed-print-business/src/main/java/com/qmth/distributed/print/business/util/pdf/CreatePdfNewUtil.java

@@ -81,12 +81,13 @@ public class CreatePdfNewUtil {
 
     /**
      * 创建登记表
+     *
      * @param examDetail
      * @param basicAttachment
      * @param printCount
      * @throws Exception
      */
-    public void createCheckIn(ExamDetail examDetail, BasicAttachment basicAttachment,Integer printCount) throws Exception {
+    public void createCheckIn(ExamDetail examDetail, BasicAttachment basicAttachment, Integer printCount) throws Exception {
         Optional.ofNullable(basicAttachment).orElseThrow(() -> ExceptionResultEnum.ATTACHMENT_IS_NULL.exception());
 
         String type = basicAttachment.getType();
@@ -113,6 +114,7 @@ public class CreatePdfNewUtil {
 
     /**
      * 生成卷袋贴
+     *
      * @param templateId
      * @param schoolName
      * @param examDetail
@@ -547,8 +549,7 @@ public class CreatePdfNewUtil {
      * @param content 题卡详细内容
      */
     public String resetHtmlTemplateBar(String content) {
-        content = content.replaceAll("<img src=\"data:image/png;base64,\\$\\{examNumber\\}\">", "");
-        content = content.replaceAll("\\$\\{examNumberStr\\}", "");
+
         return content;
     }
 
@@ -624,10 +625,10 @@ public class CreatePdfNewUtil {
      *
      * @param stuPaperType   考生卷型
      * @param paperPdfDto    抽取的试卷文件信息
-     * @param pdfList        考生试卷集合
+     * @param createPdfDto   生成pdf
      * @param waterMarkNames 水印内容
      */
-    public PdfDto getExamStudentPaperPdf(String stuPaperType, List<PaperPdfDto> paperPdfDto, List<PdfDto> pdfList, String[] waterMarkNames) throws IOException {
+    public PdfDto getExamStudentPaperPdf(String stuPaperType, List<PaperPdfDto> paperPdfDto, CreatePdfDto createPdfDto, String[] waterMarkNames) throws IOException {
         Set<Integer> pagesList = new HashSet<>();
         if (!CollectionUtils.isEmpty(paperPdfDto)) {
             for (PaperPdfDto dto : paperPdfDto) {
@@ -636,7 +637,7 @@ public class CreatePdfNewUtil {
                     pagesList.add(pages);
                     PdfDto pdfDto = PdfUtil.addPdfPage(dto.getFile());
                     File file = paperPdfDto.size() > 1 ? PdfUtil.addWaterMark(dto.getFile(), waterMarkNames, 0.5f, 12, 0) : dto.getFile();
-                    pdfList.add(new PdfDto(file.getPath(), dto.getPageSize(), pdfDto.getPageCount()));
+                    createPdfDto.getPaperPdfList().add(new PdfDto(file.getPath(), dto.getPageSize(), pdfDto.getPageCount()));
                 }
             }
             int pageCount = pagesList.stream().mapToInt(Integer::intValue).sum();
@@ -647,13 +648,13 @@ public class CreatePdfNewUtil {
     }
 
     /**
-     * 获取试卷pdf
+     * 获取备份试卷pdf
      *
-     * @param paperPdfDto 抽取的试卷文件信息
-     * @param backupCount 备份数量
-     * @param pdfList     备份试卷集合
+     * @param paperPdfDto  抽取的试卷文件信息
+     * @param backupCount  备份数量
+     * @param createPdfDto 生成pdf对象
      */
-    public PdfDto getPaperPdf(List<PaperPdfDto> paperPdfDto, Integer backupCount, List<PdfDto> pdfList) throws
+    public PdfDto getBackupPaperPdfList(List<PaperPdfDto> paperPdfDto, Integer backupCount, CreatePdfDto createPdfDto) throws
             IOException {
         Set<Integer> pagesList = new HashSet<>();
         boolean tag = false;
@@ -664,7 +665,7 @@ public class CreatePdfNewUtil {
                 tag = pages > 2;
                 PdfDto pdfDto = PdfUtil.addPdfPage(dto.getFile());
                 for (int j = 1; j <= backupCount; j++) {
-                    pdfList.add(new PdfDto(dto.getFile().getPath(), dto.getPageSize(), pdfDto.getPageCount()));
+                    createPdfDto.getPaperPdfList().add(new PdfDto(dto.getFile().getPath(), dto.getPageSize(), pdfDto.getPageCount()));
                 }
             }
             int pageCount = pagesList.stream().mapToInt(Integer::intValue).sum();
@@ -677,35 +678,31 @@ public class CreatePdfNewUtil {
     /**
      * 获取考试试卷
      *
-     * @param paperType      抽取的试卷型(多个用,分隔)
-     * @param examTaskDetail 命题任务上传试卷信息
-     * @param fileTempList
+     * @param paperType       抽取的试卷型(多个用,分隔)
+     * @param paperInfoVoList 命题任务上传试卷信息
      */
-    public List<PaperPdfDto> getPaperPdfFile(String paperType, ExamTaskDetail examTaskDetail, List<File> fileTempList) throws Exception {
+    public List<PaperPdfDto> getPaperPdfFile(String paperType, List<PaperInfoVo> paperInfoVoList, CreatePdfDto createPdfDto) throws Exception {
         String[] paperTypes = paperType.split(",");
         List<PaperPdfDto> paperPdfDtoList = new ArrayList<>();
         PaperPdfDto paperPdfDto;
-        List<PaperInfoVo> paperInfoVoList = ExamTaskUtil.parsePaperAttachmentPath(examTaskDetail.getPaperAttachmentIds());
         for (PaperInfoVo paperInfoVo : paperInfoVoList) {
-            String attachmentId = paperInfoVo.getAttachmentId();
-            if (StringUtils.isNotBlank(attachmentId)) {
-                BasicAttachment basicAttachment = basicAttachmentService.getById(attachmentId);
-                Optional.ofNullable(basicAttachment).orElseThrow(() -> ExceptionResultEnum.ATTACHMENT_IS_NULL.exception("文件路径不存在"));
+            BasicAttachment basicAttachment = basicAttachmentService.getById(paperInfoVo.getAttachmentId());
+            Optional.ofNullable(basicAttachment).orElseThrow(() -> ExceptionResultEnum.ATTACHMENT_IS_NULL.exception("命题任务中上传试卷文件路径不存在"));
 
-                // 卷型
-                String name = paperInfoVo.getName();
-                OriginalVo original = paperInfoVo.getOriginal();
-                // 返回抽中的试卷
-                for (String type : paperTypes) {
-                    if (Objects.equals(name.toUpperCase(), type.toUpperCase())) {
-                        File file = teachcloudCommonService.getFile(basicAttachment.getPath(), false, fileTempList);
-                        PageSizeEnum pageSizeEnum = PageSizeEnum.valueOf(original.getPageSize());
-                        Optional.ofNullable(pageSizeEnum).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("未设置pdf格式"));
-
-                        paperPdfDto = new PaperPdfDto(type, file, original.getPages(), pageSizeEnum);
-                        paperPdfDtoList.add(paperPdfDto);
-                        break;
-                    }
+            // 卷型
+            String name = paperInfoVo.getName();
+            OriginalVo original = paperInfoVo.getOriginal();
+            // 返回抽中的试卷
+            for (String type : paperTypes) {
+                if (StringUtils.equalsIgnoreCase(name, type)) {
+                    File file = SystemConstant.getFileTempVar(SystemConstant.PDF_PREFIX);
+                    createPdfDto.getFileTempList().add(file);
+                    fileUploadService.downloadFile(basicAttachment, file.getPath());
+                    PageSizeEnum pageSizeEnum = PageSizeEnum.valueOf(original.getPageSize());
+                    Optional.ofNullable(pageSizeEnum).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("命题任务中上传试卷文件未设置pdf格式"));
+                    paperPdfDto = new PaperPdfDto(type, file, original.getPages(), pageSizeEnum);
+                    paperPdfDtoList.add(paperPdfDto);
+                    break;
                 }
             }
         }
@@ -943,6 +940,7 @@ public class CreatePdfNewUtil {
 
     /**
      * 通用题卡html
+     *
      * @param packageCode
      * @param cardContent
      * @param paperType
@@ -992,6 +990,48 @@ public class CreatePdfNewUtil {
         return printCommonService.saveAttachmentPdfFromHtml(packageCode + paperType, cardTemp, userId, cardPdfList);
     }
 
+    /**
+     * 通用题卡html
+     *
+     * @param htmlContent       html内容
+     * @param paperType         卷型
+     * @param basicCardRule     题卡规则
+     * @param backupPackageCode 备用题卡条码号
+     */
+    public String replaceBackupCardHtmlParam(String htmlContent, String paperType, BasicCardRule basicCardRule, String backupPackageCode) throws
+            IOException {
+        // 替换条码参数
+        htmlContent = htmlContent.replaceAll("<img src=\"data:image/png;base64,\\$\\{examNumber\\}\">", "");
+        htmlContent = htmlContent.replaceAll("\\$\\{examNumberStr\\}", "");
+
+        //替换卷型参数
+        htmlContent = htmlContent.replaceAll("\\$\\{paperTypeName\\}", paperType);
+        //替换卷型条码(base64)
+        if (Objects.nonNull(paperType)) {
+            htmlContent = htmlContent.replaceAll("\\$\\{paperType\\}", GoogleBarCodeUtil.createBarCode(SystemConstant.convertPaperType(paperType), false));
+        }
+        // 替换考生变量参数
+        if (Objects.nonNull(basicCardRule) && Objects.nonNull(basicCardRule.getExtendFields())) {
+            JSONArray jsonObjectExtend = (JSONArray) JSONArray.parse(basicCardRule.getExtendFields());//扩展字段
+            if (Objects.nonNull(jsonObjectExtend)) {
+                int i = 0;
+                while (i < jsonObjectExtend.size()) {
+                    JSONObject object = (JSONObject) jsonObjectExtend.get(i);
+                    htmlContent = htmlContent.replaceAll("\\$\\{" + object.get("code") + "\\}", "");
+                    i++;
+                }
+            }
+        }
+
+        if (StringUtils.isNotBlank(backupPackageCode)) {
+            //通用题卡生成卷袋贴条码
+            String packageCodeImg = GoogleBarCodeUtil.createBarCode(backupPackageCode, false);
+            htmlContent = htmlContent.replaceAll("\\$\\{packageCodeDom\\}", "<img src='" + "data:image/png;base64," + packageCodeImg + "'><p>" + backupPackageCode + "</p>");
+
+        }
+        return htmlContent;
+    }
+
     public String getPaperType(Long printPlanId, Long examId, String paperNumber) {
         ExamPrintPlan examPrintPlan = examPrintPlanService.getById(printPlanId);
         //抽取卷型
@@ -1036,4 +1076,5 @@ public class CreatePdfNewUtil {
         }
         return paperType;
     }
+
 }

+ 3 - 3
distributed-print/install/mysql/upgrade/3.3.1.sql

@@ -152,11 +152,11 @@ CREATE TABLE `t_c_final_score` (
                                       `update_id` bigint DEFAULT NULL COMMENT '更新人id',
                                       `update_time` bigint DEFAULT NULL COMMENT '更新时间',
                                       PRIMARY KEY (`id`),
-                                      UNIQUE KEY `t_c_score_end_exam_unique` (`exam_id`,`course_code`,`course_name`,`paper_number`,`name`,`exam_number`)
+                                      UNIQUE KEY `t_c_score_end_exam_unique` (`exam_id`,`course_code`,`course_name`,`paper_number`,`name`,`student_code`)
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='期末考试成绩表';
 
 DROP TABLE IF EXISTS `t_c_usual_score`;
-CREATE TABLE `t_c_usual_score (
+CREATE TABLE `t_c_usual_score` (
                                     `id` bigint NOT NULL COMMENT '主键',
                                     `exam_id` bigint NOT NULL COMMENT '考试id',
                                     `course_code` varchar(100) COLLATE utf8mb4_general_ci NOT NULL COMMENT '课程编码',
@@ -171,7 +171,7 @@ CREATE TABLE `t_c_usual_score (
                                     `update_id` bigint DEFAULT NULL COMMENT '更新人id',
                                     `update_time` bigint DEFAULT NULL COMMENT '更新时间',
                                     PRIMARY KEY (`id`),
-                                    UNIQUE KEY `t_c_score_normal_unique` (`exam_id`,`course_code`,`course_name`,`paper_number`,`name`,`exam_number`)
+                                    UNIQUE KEY `t_c_score_normal_unique` (`exam_id`,`course_code`,`course_name`,`paper_number`,`name`,`student_code`)
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='平常作业成绩表';
 
 DROP TABLE IF EXISTS `t_r_basic_info`;

+ 0 - 3
teachcloud-common/src/main/java/com/qmth/teachcloud/common/service/FileUploadService.java

@@ -9,14 +9,11 @@ import java.io.File;
 public interface FileUploadService {
 
     FilePathVo uploadFile(File pdfFile, UploadFileEnum uploadFileEnum, String fileName);
-
     File downloadFile(String path, String pathName) throws Exception;
     File downloadFile(String type, UploadFileEnum uploadFileEnum, String path, String pathName) throws Exception;
     File downloadFile(Long attachmentId, String pathName) throws Exception;
     File downloadFile(BasicAttachment attachment, String pathName) throws Exception;
-
     String filePreview(String path);
-
     String filePreview(Long attachmentId);
 
 }

+ 3 - 10
teachcloud-common/src/main/java/com/qmth/teachcloud/common/service/impl/FileUploadServiceImpl.java

@@ -91,7 +91,7 @@ public class FileUploadServiceImpl implements FileUploadService {
     @Override
     public File downloadFile(String path, String pathName) throws Exception {
         if (StringUtils.isBlank(path)) {
-            return null;
+            throw ExceptionResultEnum.ERROR.exception("附件文件路径不存在");
         }
         File file = new File(pathName);
         if (!file.exists()) {
@@ -161,14 +161,7 @@ public class FileUploadServiceImpl implements FileUploadService {
      */
     @Override
     public File downloadFile(Long attachmentId, String pathName) throws Exception {
-        if (attachmentId == null) {
-            return null;
-        }
-        BasicAttachment basicAttachment = basicAttachmentService.getById(attachmentId);
-        if (basicAttachment == null || StringUtils.isBlank(basicAttachment.getPath())) {
-            return null;
-        }
-        return downloadFile(basicAttachment.getPath(), pathName);
+        return downloadFile(basicAttachmentService.getById(attachmentId), pathName);
     }
 
     /**
@@ -182,7 +175,7 @@ public class FileUploadServiceImpl implements FileUploadService {
     @Override
     public File downloadFile(BasicAttachment attachment, String pathName) throws Exception {
         if (attachment == null || StringUtils.isBlank(attachment.getPath())) {
-            return null;
+            throw ExceptionResultEnum.ERROR.exception("附件文件保存数据不存在");
         }
         return downloadFile(attachment.getPath(), pathName);
     }