deason 6 years ago
parent
commit
9059a6f3dc

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

@@ -255,8 +255,12 @@ public class CoursePaperServiceImpl implements CoursePaperService {
         Check.isFalse(req.required(), "至少选择一种导出内容!");
 
         SearchBuilder searches = new SearchBuilder();
-        searches.in("id", req.getIds());
         searches.eq("paperStatus", PaperStatus.已有.getIndex());
+        if (req.getIds().size() == 1) {
+            searches.eq("id", req.getIds().get(0));
+        } else {
+            searches.in("id", req.getIds());
+        }
         Specification<CourseStatistic> spec = SpecUtils.buildSearchers(CourseStatistic.class, searches.build());
 
         List<CourseStatistic> statistics = courseStatisticRepository.findAll(spec);
@@ -271,9 +275,9 @@ public class CoursePaperServiceImpl implements CoursePaperService {
         Check.isFalse(req.required(), "至少选择一种导出内容!");
 
         SearchBuilder searches = new SearchBuilder();
+        searches.eq("paperStatus", PaperStatus.已有.getIndex());
         searches.eq("orgId", req.getOrgId());
         searches.eq("examId", req.getExamId());
-        searches.eq("paperStatus", PaperStatus.已有.getIndex());
         Specification<CourseStatistic> spec = SpecUtils.buildSearchers(CourseStatistic.class, searches.build());
 
         List<CourseStatistic> statistics = courseStatisticRepository.findAll(spec);
@@ -286,10 +290,12 @@ public class CoursePaperServiceImpl implements CoursePaperService {
             return null;
         }
 
-        //封装待导出的试卷文件列表
+        //封装待导出的试卷或答案文件信息列表
         List<ExportFileInfo> exportFiles = new ArrayList<>();
+
         //封装试卷结构-客观题数据
         List<ObjectiveQuestionStructure> allObjectives = new ArrayList<>();
+
         //封装试卷结构-主观题数据
         List<SubjectiveQuestionStructure> allSubjectives = new ArrayList<>();
 
@@ -322,17 +328,32 @@ public class CoursePaperServiceImpl implements CoursePaperService {
                 List<SubjectiveQuestionStructure> subjectives = questionStructureService.getSubjectiveQuestionStructureList(paper.getExamId(), paper.getPaperId());
                 allSubjectives.addAll(subjectives);
             }
+
             exportFiles.add(info);
         }
 
+        //文件存放目录
+        final String targetDir = Constants.rootFileDir() + "/" + FileUtils.randomUUID();
+        FileUtils.makeDirs(targetDir);
+
+        //优先处理试卷或答案文件(需要网络下载)
+        if (needPaper || needAnswer) {
+            this.doPaperAndAnswerFiles(exportFiles, targetDir);
+        }
+
+        //处理试卷结构文件
+        if (needStruct) {
+            this.doStructureFiles(allObjectives, allSubjectives, targetDir);
+        }
+
+        log.debug("All files finished.");
+
         //打包所有文件
-        return packageFiles(exportFiles, allObjectives, allSubjectives, needStruct);
+        return packageFiles(targetDir);
     }
 
-    private File packageFiles(List<ExportFileInfo> exportFiles, List<ObjectiveQuestionStructure> allObjectives, List<SubjectiveQuestionStructure> allSubjectives, boolean needStruct) {
-        //文件存放目录
-        final String rootDir = Constants.rootFileDir();
-        final String targetDir = rootDir + "/" + FileUtils.randomUUID();
+    private void doPaperAndAnswerFiles(List<ExportFileInfo> exportFiles, final String targetDir) {
+        //试卷或答案存放目录
         final String paperDir = targetDir + "/" + PAPER_DIR;
         final String answerDir = targetDir + "/" + ANSWER_DIR;
         FileUtils.makeDirs(paperDir);
@@ -345,16 +366,17 @@ public class CoursePaperServiceImpl implements CoursePaperService {
             this.loadFile(answerDir, info.getAnswerWord());
             this.loadFile(answerDir, info.getAnswerPdf());
         }
+    }
 
-        //生成试卷结构文件
-        if (needStruct) {
-            final String objectiveFile = targetDir + "/" + OBJECTIVE_EXCEL_NAME;
-            ExcelUtils.exportExcel(ObjectiveQuestionStructure.class, allObjectives, new File(objectiveFile), OBJECTIVE_TITLE);
+    private void doStructureFiles(List<ObjectiveQuestionStructure> allObjectives, List<SubjectiveQuestionStructure> allSubjectives, final String targetDir) {
+        final String objectiveFile = targetDir + "/" + OBJECTIVE_EXCEL_NAME;
+        ExcelUtils.exportExcel(ObjectiveQuestionStructure.class, allObjectives, new File(objectiveFile), OBJECTIVE_TITLE);
 
-            final String subjectiveFile = targetDir + "/" + SUBJECTIVE_EXCEL_NAME;
-            ExcelUtils.exportExcel(SubjectiveQuestionStructure.class, allSubjectives, new File(subjectiveFile), SUBJECTIVE_TITLE);
-        }
+        final String subjectiveFile = targetDir + "/" + SUBJECTIVE_EXCEL_NAME;
+        ExcelUtils.exportExcel(SubjectiveQuestionStructure.class, allSubjectives, new File(subjectiveFile), SUBJECTIVE_TITLE);
+    }
 
+    private File packageFiles(final String targetDir) {
         //压缩打包所有文件
         File target = new File(targetDir);
         File zipFile = new File(targetDir + SUFFIX_ZIP);

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

@@ -10,6 +10,8 @@ package cn.com.qmth.examcloud.core.print.test;
 import cn.com.qmth.examcloud.core.print.entity.CoursePaper;
 import cn.com.qmth.examcloud.core.print.service.CoursePaperService;
 import cn.com.qmth.examcloud.core.print.service.bean.coursepaper.ExportAllReq;
+import cn.com.qmth.examcloud.core.print.service.bean.coursepaper.ExportBatchReq;
+import com.google.common.collect.Lists;
 import org.junit.Test;
 import org.springframework.beans.factory.annotation.Autowired;
 
@@ -66,7 +68,22 @@ public class CoursePaperServiceTest extends BaseTest {
         req.setNeedAnswer(true);
         req.setNeedStruct(true);
         File file = coursePaperService.exportAllCoursePaper(req);
-        System.out.println(file.getPath());
+        if (file != null) {
+            System.out.println(file.getPath());
+        }
+    }
+
+    @Test
+    public void exportBatchCoursePaperTest() throws Exception {
+        ExportBatchReq req = new ExportBatchReq();
+        req.setIds(Lists.newArrayList(27L, 28L, 29L));
+        req.setNeedPaper(true);
+        req.setNeedAnswer(true);
+        req.setNeedStruct(true);
+        File file = coursePaperService.exportBatchCoursePaper(req);
+        if (file != null) {
+            System.out.println(file.getPath());
+        }
     }
 
 }