deason 6 жил өмнө
parent
commit
a887a9c8b3

+ 5 - 0
examcloud-core-print-service/src/main/java/cn/com/qmth/examcloud/core/print/service/CoursePaperService.java

@@ -39,6 +39,11 @@ public interface CoursePaperService {
      */
     void syncCoursePaper(CoursePaper coursePaper);
 
+    /**
+     * 转换尚未转换过PDF的所有试卷
+     */
+    void convertCoursePapers();
+
     /**
      * (单个)分配待指定试卷
      *

+ 71 - 42
examcloud-core-print-service/src/main/java/cn/com/qmth/examcloud/core/print/service/impl/CoursePaperServiceImpl.java

@@ -74,6 +74,7 @@ public class CoursePaperServiceImpl implements CoursePaperService {
         SearchBuilder searches = new SearchBuilder();
         searches.eq("orgId", query.getOrgId());
         searches.eq("examId", query.getExamId());
+        searches.eq("converted", true);//只获取已转换的
         if (query.getCourseId() != null) {
             searches.eq("courseId", query.getCourseId());
         }
@@ -88,6 +89,7 @@ public class CoursePaperServiceImpl implements CoursePaperService {
         searches.eq("orgId", orgId);
         searches.eq("examId", examId);
         searches.eq("courseId", courseId);
+        searches.eq("converted", true);//只获取已转换的
         Specification<CoursePaper> spec = SpecUtils.buildSearchers(CoursePaper.class, searches.build());
         return coursePaperRepository.count(spec);
     }
@@ -115,62 +117,88 @@ public class CoursePaperServiceImpl implements CoursePaperService {
         final String paperHtmlUrl = QUESTION_URL_PREFIX.concat("/api/ecs_ques/paper/pdf/").concat(coursePaper.getPaperId());
         final String answerHtmlUrl = QUESTION_URL_PREFIX.concat("/api/ecs_ques/paper/answer/pdf/").concat(coursePaper.getPaperId());
 
-        //转换PDF文件
-        //final String rootDir = Constants.rootFileDir();
-        final String rootDir = systemProperty.getTempDir();
-        final String paperPdfPath = rootDir + "/" + FileUtils.randomUUID() + SUFFIX_PDF;
-        final String answerPdfPath = rootDir + "/" + FileUtils.randomUUID() + SUFFIX_PDF;
-        FileUtils.makeDirs(rootDir);
-        boolean paperResult = ElectronUtils.toPdf(systemProperty.getDir(), paperHtmlUrl, paperPdfPath);
-        boolean answerResult = ElectronUtils.toPdf(systemProperty.getDir(), answerHtmlUrl, answerPdfPath);
-        if (!paperResult || !answerResult) {
-            throw new StatusException(PRT_CODE_500, "转换PDF文件失败!");
-        }
-
-        //上传PDF至又拍云
-        String paperPdfUrl = upYunClient.upload(new File(paperPdfPath));
-        String answerPdfUrl = upYunClient.upload(new File(answerPdfPath));
-
-        //获取试卷PDF的页数
-        int paperP = 0;
-        try (FileInputStream fis = new FileInputStream(paperPdfPath);) {
-            PdfReader pdfReader = new PdfReader(fis);
-            paperP = pdfReader.getNumberOfPages();
-        } catch (IOException e) {
-            log.error(e.getMessage());
-        }
-        if (paperP == 0) {
-            throw new StatusException(PRT_CODE_500, "试卷页数不能为0!");
-        } else {
-            //试卷页数为奇数时加一
-            if (paperP % 2 != 0) {
-                paperP++;
-            }
-        }
-
         CoursePaper oldCoursePaper = coursePaperRepository.findByExamIdAndPaperId(coursePaper.getExamId(), coursePaper.getPaperId());
         if (oldCoursePaper != null) {
             //存在则修改
             oldCoursePaper.setPaperName(coursePaper.getPaperName());
             oldCoursePaper.setPaperHtmlUrl(paperHtmlUrl);
             oldCoursePaper.setAnswerHtmlUrl(answerHtmlUrl);
-            oldCoursePaper.setPaperPdfUrl(paperPdfUrl);
-            oldCoursePaper.setAnswerPdfUrl(answerPdfUrl);
-            oldCoursePaper.setPaperP(paperP);
+            oldCoursePaper.setConverted(false);//需要重新转换PDF
             coursePaperRepository.save(oldCoursePaper);
         } else {
             //否则新增
             coursePaper.setPaperHtmlUrl(paperHtmlUrl);
             coursePaper.setAnswerHtmlUrl(answerHtmlUrl);
-            coursePaper.setPaperPdfUrl(paperPdfUrl);
-            coursePaper.setAnswerPdfUrl(answerPdfUrl);
-            coursePaper.setPaperP(paperP);
+            coursePaper.setPaperPdfUrl("");
+            coursePaper.setAnswerPdfUrl("");
+            coursePaper.setPaperP(0);
+            coursePaper.setConverted(false);
             coursePaperRepository.save(coursePaper);
         }
 
         //保存或更新试卷试题结构
         examQuestionStructureService.savePaperQuestionStructure(coursePaper.getExamId(), coursePaper.getPaperId());
+    }
+
+    @Override
+    public void convertCoursePapers() {
+        //获取尚未转换过PDF的所有试卷
+        Specification<CoursePaper> spec = SearchBuilder.EQ("converted", false);
+        List<CoursePaper> coursePapers = coursePaperRepository.findAll(spec);
+        log.debug("待转换PDF的试卷数:" + coursePapers.size());
+
+        for (CoursePaper coursePaper : coursePapers) {
+            //将试卷和答案页面转换为PDF文件
+            //final String rootDir = Constants.rootFileDir();
+            final String rootDir = systemProperty.getTempDir();
+            final String paperPdfPath = rootDir + "/" + FileUtils.randomUUID() + SUFFIX_PDF;
+            final String answerPdfPath = rootDir + "/" + FileUtils.randomUUID() + SUFFIX_PDF;
+            FileUtils.makeDirs(rootDir);
+
+            boolean paperResult = ElectronUtils.toPdf(systemProperty.getDir(), coursePaper.getPaperHtmlUrl(), paperPdfPath);
+            if (!paperResult) {
+                log.warn(String.format("试卷页面转换PDF文件失败!paperId = %s", coursePaper.getPaperId()));
+                continue;
+            }
+
+            boolean answerResult = ElectronUtils.toPdf(systemProperty.getDir(), coursePaper.getAnswerHtmlUrl(), answerPdfPath);
+            if (!answerResult) {
+                log.warn(String.format("答案页面转换PDF文件失败!paperId = %s", coursePaper.getPaperId()));
+                continue;
+            }
+
+            //上传PDF至又拍云
+            String paperPdfUrl = upYunClient.upload(new File(paperPdfPath));
+            String answerPdfUrl = upYunClient.upload(new File(answerPdfPath));
+
+            //获取试卷PDF的页数
+            int paperP = 0;
+            try (FileInputStream fis = new FileInputStream(paperPdfPath);) {
+                PdfReader pdfReader = new PdfReader(fis);
+                paperP = pdfReader.getNumberOfPages();
+            } catch (IOException e) {
+                log.error(e.getMessage());
+            }
+
+            log.debug(String.format("试卷实际页数为%s!paperId = %s", paperP, coursePaper.getPaperId()));
+
+            //试卷页数为奇数时加一
+            if (paperP % 2 != 0) {
+                paperP++;
+            }
+
+            coursePaper.setPaperPdfUrl(paperPdfUrl);
+            coursePaper.setAnswerPdfUrl(answerPdfUrl);
+            coursePaper.setPaperP(paperP);
+            coursePaper.setConverted(true);
+            coursePaperRepository.save(coursePaper);
+
+            //转换成功后,依据特定情况分配默认试卷
+            this.allotDefaultCoursePaper(coursePaper);
+        }
+    }
 
+    private void allotDefaultCoursePaper(CoursePaper coursePaper) {
         //如果当前考试课程"只有一种试卷类型"且"未分配试卷",则默认分配一次该试卷,已分配过则跳过分配
         SearchBuilder searches = new SearchBuilder()
                 .eq("orgId", coursePaper.getOrgId())
@@ -250,8 +278,8 @@ public class CoursePaperServiceImpl implements CoursePaperService {
         //反馈信息
         StringBuilder errors = new StringBuilder();
 
-        //获取考试课程下的试卷列表
-        List<CoursePaper> coursePapers = this.getCoursePapers(orgId, examId);
+        //获取考试课程下的已转换PDF的试卷列表
+        List<CoursePaper> coursePapers = this.getCoursePapers(orgId, examId, true);
 
         //按课程封装课程试卷列表 Map<courseId, List<CoursePaper>>
         Map<Long, List<CoursePaper>> paperMaps = new HashMap<>();
@@ -311,10 +339,11 @@ public class CoursePaperServiceImpl implements CoursePaperService {
         }
     }
 
-    private List<CoursePaper> getCoursePapers(Long orgId, Long examId) {
+    private List<CoursePaper> getCoursePapers(Long orgId, Long examId, boolean converted) {
         SearchBuilder searches = new SearchBuilder();
         searches.eq("orgId", orgId);
         searches.eq("examId", examId);
+        searches.eq("converted", converted);
         Specification<CoursePaper> spec = SpecUtils.buildSearchers(CoursePaper.class, searches.build());
         return coursePaperRepository.findAll(spec);
     }

+ 1 - 0
examcloud-core-print-starter/src/test/java/cn/com/qmth/examcloud/core/print/test/CoursePaperServiceTest.java

@@ -65,6 +65,7 @@ public class CoursePaperServiceTest extends BaseTest {
         coursePaper.setPaperName("测试试卷");
         coursePaperService.syncCoursePaper(coursePaper);
 
+//        coursePaperService.convertCoursePapers();
 //        ExamQuestionStructureInfo info = statisticService.findStructureByPaperId(coursePaper.getExamId(), coursePaper.getPaperId());
 //        System.out.println(jsonMapper.toJson(info));
     }