deason 6 سال پیش
والد
کامیت
f4a492be77

+ 12 - 5
examcloud-core-print-common/src/main/java/cn/com/qmth/examcloud/core/print/common/utils/ExcelUtils.java

@@ -28,16 +28,23 @@ import java.util.List;
 public class ExcelUtils {
     private static Logger log = LoggerFactory.getLogger(ExcelUtils.class);
 
-    public static <T> void toExcelFile(Class<T> clazz, List<T> list, File excelFile) {
-        toExcelFile(clazz, list, excelFile, null);
+    public static <T> void exportExcel(Class<T> clazz, List<T> records, File excelFile) {
+        exportExcel(clazz, records, excelFile, null);
     }
 
-    public static <T> void toExcelFile(Class<T> clazz, List<T> list, File excelFile, String title) {
+    public static <T> void exportExcel(Class<T> clazz, List<T> records, File excelFile, String title) {
         if (excelFile == null) {
-            log.warn("[Param] excelFile is null.");
+            log.warn("[ExportExcel] excelFile is null.");
             return;
         }
 
+        if (records == null) {
+            log.warn("[ExportExcel] records is null.");
+            return;
+        }
+
+        log.debug("[ExportExcel] records size is " + records.size());
+
         ExportParams params = new ExportParams();
         params.setStyle(ExcelStyle.class);
         params.setType(ExcelType.XSSF);
@@ -48,7 +55,7 @@ public class ExcelUtils {
         }
 
         try (FileOutputStream fos = new FileOutputStream(excelFile);
-             Workbook workbook = ExcelExportUtil.exportExcel(params, clazz, list);) {
+             Workbook workbook = ExcelExportUtil.exportExcel(params, clazz, records);) {
 
             workbook.write(fos);
         } catch (FileNotFoundException e) {

+ 6 - 30
examcloud-core-print-service/src/main/java/cn/com/qmth/examcloud/core/print/service/bean/coursepaper/ExportBatchReq.java

@@ -20,17 +20,9 @@ import java.util.List;
 public class ExportBatchReq implements JsonSerializable {
     private static final long serialVersionUID = 1L;
     /**
-     * 学校机构ID
+     * ID列表
      */
-    private Long orgId;
-    /**
-     * 考试ID
-     */
-    private Long examId;
-    /**
-     * 试卷ID列表
-     */
-    private List<String> paperIds;
+    private List<Long> ids;
     /**
      * 是否需要导出试卷
      */
@@ -58,28 +50,12 @@ public class ExportBatchReq implements JsonSerializable {
         return false;
     }
 
-    public Long getOrgId() {
-        return orgId;
-    }
-
-    public void setOrgId(Long orgId) {
-        this.orgId = orgId;
-    }
-
-    public Long getExamId() {
-        return examId;
-    }
-
-    public void setExamId(Long examId) {
-        this.examId = examId;
-    }
-
-    public List<String> getPaperIds() {
-        return paperIds;
+    public List<Long> getIds() {
+        return ids;
     }
 
-    public void setPaperIds(List<String> paperIds) {
-        this.paperIds = paperIds;
+    public void setIds(List<Long> ids) {
+        this.ids = ids;
     }
 
     public Boolean getNeedPaper() {

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

@@ -233,24 +233,34 @@ public class CoursePaperServiceImpl implements CoursePaperService {
 
     @Override
     public CoursePaperTotalInfo getPaperTotalByOrgIdAndExamId(Long orgId, Long examId) {
+        Check.isNull(orgId, "学校ID不能为空!");
+        Check.isNull(examId, "考试ID不能为空!");
         //todo
+
         return null;
     }
 
     @Override
     public void checkPaperStructure(Long examId, String paperId) {
+        Check.isNull(examId, "考试ID不能为空!");
+        Check.isEmpty(paperId, "试卷ID不能为空!");
         //todo
+
     }
 
     @Override
     public File exportBatchCoursePaper(ExportBatchReq req) {
         Check.isNull(req, "参数不能为空!");
-        Check.isNull(req.getOrgId(), "学校ID不能为空!");
-        Check.isNull(req.getExamId(), "考试ID不能为空!");
-        Check.isNull(req.required(), "至少选择一种导出内容!");
-        Check.isEmpty(req.getPaperIds(), "至少选择一门课程!");
-        //todo
-        return null;
+        Check.isEmpty(req.getIds(), "至少选择一门考试课程!");
+        Check.isFalse(req.required(), "至少选择一种导出内容!");
+
+        SearchBuilder searches = new SearchBuilder();
+        searches.in("id", req.getIds());
+        searches.eq("paperStatus", PaperStatus.已有.getIndex());
+        Specification<CourseStatistic> spec = SpecUtils.buildSearchers(CourseStatistic.class, searches.build());
+
+        List<CourseStatistic> statistics = courseStatisticRepository.findAll(spec);
+        return this.doExport(statistics, req.getNeedPaper(), req.getNeedAnswer(), req.getNeedStruct());
     }
 
     @Override
@@ -258,16 +268,21 @@ public class CoursePaperServiceImpl implements CoursePaperService {
         Check.isNull(req, "参数不能为空!");
         Check.isNull(req.getOrgId(), "学校ID不能为空!");
         Check.isNull(req.getExamId(), "考试ID不能为空!");
-        Check.isNull(req.required(), "至少选择一种导出内容!");
+        Check.isFalse(req.required(), "至少选择一种导出内容!");
 
         SearchBuilder searches = new SearchBuilder();
         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);
+        return this.doExport(statistics, req.getNeedPaper(), req.getNeedAnswer(), req.getNeedStruct());
+    }
+
+    private File doExport(List<CourseStatistic> statistics, boolean needPaper, boolean needAnswer, boolean needStruct) {
         if (statistics == null || statistics.isEmpty()) {
-            log.warn(String.format("No paper to export! orgId = %s examId = %s", req.getOrgId(), req.getExamId()));
+            log.warn("No paper to export!");
             return null;
         }
 
@@ -288,30 +303,30 @@ public class CoursePaperServiceImpl implements CoursePaperService {
             final String title = String.format("%s_%s_%s_", paper.getPaperName(), paper.getCourseCode(), statistic.getPaperType());
             ExportFileInfo info = new ExportFileInfo();
 
-            if (req.getNeedPaper()) {
+            if (needPaper) {
                 info.setPaperWord(new Pair<>(title + PAPER_DOC_NAME, paper.getPaperWordUrl()));
                 info.setPaperPdf(new Pair<>(title + PAPER_PDF_NAME, paper.getPaperPdfUrl()));
             }
 
-            if (req.getNeedAnswer()) {
+            if (needAnswer) {
                 info.setAnswerWord(new Pair<>(title + ANSWER_DOC_NAME, paper.getAnswerWordUrl()));
                 info.setAnswerPdf(new Pair<>(title + ANSWER_PDF_NAME, paper.getAnswerPdfUrl()));
             }
 
-            if (req.getNeedStruct()) {
+            if (needStruct) {
                 //客观题结构
-                List<ObjectiveQuestionStructure> objectives = questionStructureService.getObjectiveQuestionStructureList(req.getExamId(), paper.getPaperId());
+                List<ObjectiveQuestionStructure> objectives = questionStructureService.getObjectiveQuestionStructureList(paper.getExamId(), paper.getPaperId());
                 allObjectives.addAll(objectives);
 
                 //主观题结构
-                List<SubjectiveQuestionStructure> subjectives = questionStructureService.getSubjectiveQuestionStructureList(req.getExamId(), paper.getPaperId());
+                List<SubjectiveQuestionStructure> subjectives = questionStructureService.getSubjectiveQuestionStructureList(paper.getExamId(), paper.getPaperId());
                 allSubjectives.addAll(subjectives);
             }
             exportFiles.add(info);
         }
 
         //打包所有文件
-        return packageFiles(exportFiles, allObjectives, allSubjectives, req.getNeedStruct());
+        return packageFiles(exportFiles, allObjectives, allSubjectives, needStruct);
     }
 
     private File packageFiles(List<ExportFileInfo> exportFiles, List<ObjectiveQuestionStructure> allObjectives, List<SubjectiveQuestionStructure> allSubjectives, boolean needStruct) {
@@ -334,10 +349,10 @@ public class CoursePaperServiceImpl implements CoursePaperService {
         //生成试卷结构文件
         if (needStruct) {
             final String objectiveFile = targetDir + "/" + OBJECTIVE_EXCEL_NAME;
-            ExcelUtils.toExcelFile(ObjectiveQuestionStructure.class, allObjectives, new File(objectiveFile), OBJECTIVE_TITLE);
+            ExcelUtils.exportExcel(ObjectiveQuestionStructure.class, allObjectives, new File(objectiveFile), OBJECTIVE_TITLE);
 
             final String subjectiveFile = targetDir + "/" + SUBJECTIVE_EXCEL_NAME;
-            ExcelUtils.toExcelFile(SubjectiveQuestionStructure.class, allSubjectives, new File(subjectiveFile), SUBJECTIVE_TITLE);
+            ExcelUtils.exportExcel(SubjectiveQuestionStructure.class, allSubjectives, new File(subjectiveFile), SUBJECTIVE_TITLE);
         }
 
         //压缩打包所有文件

+ 23 - 11
examcloud-core-print-service/src/main/java/cn/com/qmth/examcloud/core/print/service/impl/QuestionStructureServiceImpl.java

@@ -7,6 +7,8 @@
 
 package cn.com.qmth.examcloud.core.print.service.impl;
 
+import cn.com.qmth.examcloud.core.print.common.jpa.SearchBuilder;
+import cn.com.qmth.examcloud.core.print.common.jpa.SpecUtils;
 import cn.com.qmth.examcloud.core.print.common.utils.Check;
 import cn.com.qmth.examcloud.core.print.entity.ObjectiveQuestionStructure;
 import cn.com.qmth.examcloud.core.print.entity.SubjectiveQuestionStructure;
@@ -15,10 +17,10 @@ import cn.com.qmth.examcloud.core.print.repository.SubjectiveQuestionStructureRe
 import cn.com.qmth.examcloud.core.print.service.CommonService;
 import cn.com.qmth.examcloud.core.print.service.QuestionStructureService;
 import cn.com.qmth.examcloud.core.print.service.bean.questionstructure.PaperQuestionStructureInfo;
-import com.google.common.collect.Lists;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.jpa.domain.Specification;
 import org.springframework.stereotype.Service;
 
 import javax.validation.constraints.NotNull;
@@ -65,20 +67,30 @@ public class QuestionStructureServiceImpl implements QuestionStructureService {
 
     @Override
     public List<ObjectiveQuestionStructure> getObjectiveQuestionStructureList(Long examId, String paperId) {
-        //todo
-        ObjectiveQuestionStructure objective = new ObjectiveQuestionStructure();
-        objective.setCourseCode("123");
-        objective.setCourseName("测试课程");
-        return Lists.newArrayList(objective);
+        Check.isNull(examId, "考试ID不能为空!");
+        Check.isEmpty(paperId, "试卷ID不能为空!");
+
+        SearchBuilder searches = new SearchBuilder()
+                .eq("examId", examId)
+                .eq("paperId", paperId);
+
+        Specification<ObjectiveQuestionStructure> spec = SpecUtils.buildSearchers(ObjectiveQuestionStructure.class, searches.build());
+        List<ObjectiveQuestionStructure> list = objectiveQuestionStructureRepository.findAll(spec);
+        return list;
     }
 
     @Override
     public List<SubjectiveQuestionStructure> getSubjectiveQuestionStructureList(Long examId, String paperId) {
-        //todo
-        SubjectiveQuestionStructure subjective = new SubjectiveQuestionStructure();
-        subjective.setCourseCode("123");
-        subjective.setCourseName("测试课程");
-        return Lists.newArrayList(subjective);
+        Check.isNull(examId, "考试ID不能为空!");
+        Check.isEmpty(paperId, "试卷ID不能为空!");
+
+        SearchBuilder searches = new SearchBuilder()
+                .eq("examId", examId)
+                .eq("paperId", paperId);
+
+        Specification<SubjectiveQuestionStructure> spec = SpecUtils.buildSearchers(SubjectiveQuestionStructure.class, searches.build());
+        List<SubjectiveQuestionStructure> list = subjectiveQuestionStructureRepository.findAll(spec);
+        return list;
     }
 
 }

+ 1 - 0
examcloud-core-print-starter/src/main/resources/logback-spring.xml

@@ -51,6 +51,7 @@
     <logger name="org.apache" level="WARN"/>
     <logger name="com.netflix.discovery" level="WARN"/>
     <logger name="springfox.documentation" level="WARN"/>
+    <logger name="cn.afterturn.easypoi" level="WARN"/>
     <logger name="cn.com.qmth.examcloud.commons" level="INFO"/>
 
     <root level="DEBUG">