deason 6 år sedan
förälder
incheckning
e01281c269

+ 7 - 26
examcloud-core-questions-service/src/main/java/cn/com/qmth/examcloud/core/questions/service/ExportPaperService.java

@@ -1,7 +1,8 @@
 package cn.com.qmth.examcloud.core.questions.service;
 
-import javax.servlet.http.HttpServletResponse;
+import cn.com.qmth.examcloud.core.questions.dao.entity.Course;
 
+import javax.servlet.http.HttpServletResponse;
 import java.io.IOException;
 import java.util.List;
 
@@ -9,52 +10,32 @@ public interface ExportPaperService {
 
     /**
      * 导出单个试卷,答案,机考数据包
-     *
-     * @param examId
-     * @param courseNo
-     * @param paperType
-     * @param response
-     * @param exportContentList
-     * @throws Exception
      */
     public void exportPaperFile(String paperId, String serviceName, String exportContentList, HttpServletResponse response, String loginName, String examType, String psw) throws Exception;
 
     /**
      * 批量导出试卷,答案,机考数据包
-     *
-     * @param paperIds
-     * @param serviceName
-     * @param exportContentList
-     * @param response
-     * @param loginName
-     * @throws Exception
      */
     public void exportPaperFiles(List<String> paperList, String serviceName, String exportContentList, HttpServletResponse response, String loginName, String examType) throws Exception;
 
     /**
      * 导出课程下的试题分布Excel(按课程)
-     *
-     * @param courseNo
-     * @param response
      */
     public void downQuestionDistribute(String courseNo, HttpServletResponse response) throws IOException;
 
+    /**
+     * 导出课程下的试题分布Excel(按课程)
+     */
+    void downQuestionDistributes(List<Course> courses) throws IOException;
 
     /**
      * 导出课程下的试题分布Excel(按试卷)
-     *
-     * @param courseNo
-     * @param response
      */
     public void downQuestionDistributeByPapers(String paperIds, HttpServletResponse response) throws IOException;
 
     /**
      * 导出原始的导入试卷
-     *
-     * @param paperId
-     * @param response
-     * @throws Exception
      */
     public void downOriginalPaper(String paperId, String loginName, HttpServletResponse response) throws Exception;
 
-}
+}

+ 114 - 83
examcloud-core-questions-service/src/main/java/cn/com/qmth/examcloud/core/questions/service/impl/ExportPaperServiceImpl.java

@@ -23,6 +23,7 @@ import cn.com.qmth.examcloud.core.questions.service.export.ExportPaperAbstractSe
 import com.google.common.collect.Lists;
 import com.google.gson.Gson;
 import main.java.com.UpYun;
+import org.apache.commons.collections.CollectionUtils;
 import org.apache.commons.io.FileUtils;
 import org.apache.commons.io.IOUtils;
 import org.apache.commons.lang3.StringUtils;
@@ -433,10 +434,6 @@ public class ExportPaperServiceImpl implements ExportPaperService {
 
     /**
      * 获取图片里面的路径,长度,宽度
-     *
-     * @param s
-     * @param str
-     * @return
      */
     private List<String> getImg(String s, String str) {
         String regex;
@@ -533,93 +530,116 @@ public class ExportPaperServiceImpl implements ExportPaperService {
     }
 
     @Override
-    public void downQuestionDistribute(String courseNo, HttpServletResponse response) throws IOException {
+    public void downQuestionDistribute(String courseCode, HttpServletResponse response) throws IOException {
+        //生成导出Excle的list集合
+        List<QuestionDistributeDto> questionDistributes = this.downQuestionDistribute(courseCode);
+
+        final String fileName = courseCode + "试题分布.xlsx";
+        final String filePath = TEMP_FILE_EXP + File.separator + fileName;
+
+        //生成Excel导出
+        File tempFile = this.writeExcel(questionDistributes, filePath);
+        FileDisposeUtil.downloadFile(fileName, filePath, response);
+        FileUtils.deleteQuietly(tempFile);
+    }
+
+    @Override
+    public void downQuestionDistributes(List<Course> courses) throws IOException {
+        if (CollectionUtils.isEmpty(courses)) {
+            return;
+        }
+
+        for (Course course : courses) {
+            List<QuestionDistributeDto> questionDistributes = this.downQuestionDistribute(course.getCode());
+            final String fileName = course.getCode() + "试题分布.xlsx";
+            final String filePath = TEMP_FILE_EXP + File.separator + fileName;
+            this.writeExcel(questionDistributes, filePath);
+        }
+    }
+
+    private List<QuestionDistributeDto> downQuestionDistribute(String courseCode) {
         //1.生成导出Excle的list集合
-        List<QuestionDistributeDto> questionDistributeDtos = new ArrayList<QuestionDistributeDto>();
-        //2.根据课程code查询课程属性集合
-        List<CourseProperty> courseProperties = coursePropertyRepo.findByCourseCodeAndEnable(courseNo, true);
-        //3.遍历课程属性集合,根据课程属性查询一级
-        if (courseProperties != null && courseProperties.size() > 0) {
-            for (CourseProperty courseProperty : courseProperties) {
-                List<Property> parentProperties = propertyService.findPropertyParents(courseProperty.getId(), courseProperty.getOrgId());
-                if (parentProperties != null && parentProperties.size() > 0) {
-                    for (Property parentProperty : parentProperties) {
-                        List<Property> sonProperties = propertyService.findPropertySons(parentProperty.getId());
-                        if (sonProperties != null && sonProperties.size() > 0) {
-                            for (Property sonProperty : sonProperties) {
-                                //单选题集合
-                                List<Question> sinList = questionList(courseNo, courseProperty, QuesStructType.SINGLE_ANSWER_QUESTION, parentProperty, sonProperty);
-                                //多选题集合
-                                List<Question> mulList = questionList(courseNo, courseProperty, QuesStructType.MULTIPLE_ANSWER_QUESTION, parentProperty, sonProperty);
-                                //判断题集合
-                                List<Question> bolList = questionList(courseNo, courseProperty, QuesStructType.BOOL_ANSWER_QUESTION, parentProperty, sonProperty);
+        List<QuestionDistributeDto> questionDistributes = new ArrayList<>();
 
-                                List<Question> fillList = null;
-                                //fillList = questionList(courseNo, courseProperty, QuesStructType.FILL_BLANK_QUESTION, parentProperty, sonProperty);
-                                List<Question> textList = null;
-                                //textList = questionList(courseNo, courseProperty, QuesStructType.TEXT_ANSWER_QUESTION, parentProperty, sonProperty);
+        //2.根据课程code查询课程属性集合
+        List<CourseProperty> courseProperties = coursePropertyRepo.findByCourseCodeAndEnable(courseCode, true);
+        if (CollectionUtils.isEmpty(courseProperties)) {
+            return questionDistributes;
+        }
 
-                                //计算所有题型数量
-                                Map<Long, Long> map = countQuesType(sinList, mulList, bolList, fillList, textList);
-                                QuestionDistributeDto questionDistributeDto = new QuestionDistributeDto(courseProperty.getName(), parentProperty.getName(), sonProperty.getName(), map);
-                                questionDistributeDtos.add(questionDistributeDto);
-                            }
-                        } else {
-                            //一级属性不为空,二级属性为空
+        //3.遍历课程属性集合,根据课程属性查询一级
+        for (CourseProperty courseProperty : courseProperties) {
+            List<Property> parentProperties = propertyService.findPropertyParents(courseProperty.getId(), courseProperty.getOrgId());
+            if (parentProperties != null && parentProperties.size() > 0) {
+                for (Property parentProperty : parentProperties) {
+                    List<Property> sonProperties = propertyService.findPropertySons(parentProperty.getId());
+                    if (sonProperties != null && sonProperties.size() > 0) {
+                        for (Property sonProperty : sonProperties) {
                             //单选题集合
-                            List<Question> sinList = questionList(courseNo, courseProperty, QuesStructType.SINGLE_ANSWER_QUESTION, parentProperty, null);
+                            List<Question> sinList = questionList(courseCode, courseProperty, QuesStructType.SINGLE_ANSWER_QUESTION, parentProperty, sonProperty);
                             //多选题集合
-                            List<Question> mulList = questionList(courseNo, courseProperty, QuesStructType.MULTIPLE_ANSWER_QUESTION, parentProperty, null);
+                            List<Question> mulList = questionList(courseCode, courseProperty, QuesStructType.MULTIPLE_ANSWER_QUESTION, parentProperty, sonProperty);
                             //判断题集合
-                            List<Question> bolList = questionList(courseNo, courseProperty, QuesStructType.BOOL_ANSWER_QUESTION, parentProperty, null);
+                            List<Question> bolList = questionList(courseCode, courseProperty, QuesStructType.BOOL_ANSWER_QUESTION, parentProperty, sonProperty);
 
                             List<Question> fillList = null;
-                            //fillList = questionList(courseNo, courseProperty, QuesStructType.FILL_BLANK_QUESTION, parentProperty, null);
+                            //fillList = questionList(courseCode, courseProperty, QuesStructType.FILL_BLANK_QUESTION, parentProperty, sonProperty);
                             List<Question> textList = null;
-                            //textList = questionList(courseNo, courseProperty, QuesStructType.TEXT_ANSWER_QUESTION, parentProperty, null);
+                            //textList = questionList(courseCode, courseProperty, QuesStructType.TEXT_ANSWER_QUESTION, parentProperty, sonProperty);
 
                             //计算所有题型数量
                             Map<Long, Long> map = countQuesType(sinList, mulList, bolList, fillList, textList);
-                            QuestionDistributeDto questionDistributeDto = new QuestionDistributeDto(courseProperty.getName(), parentProperty.getName(), null, map);
-                            questionDistributeDtos.add(questionDistributeDto);
+                            QuestionDistributeDto questionDistributeDto = new QuestionDistributeDto(courseProperty.getName(), parentProperty.getName(), sonProperty.getName(), map);
+                            questionDistributes.add(questionDistributeDto);
                         }
+                    } else {
+                        //一级属性不为空,二级属性为空
+                        //单选题集合
+                        List<Question> sinList = questionList(courseCode, courseProperty, QuesStructType.SINGLE_ANSWER_QUESTION, parentProperty, null);
+                        //多选题集合
+                        List<Question> mulList = questionList(courseCode, courseProperty, QuesStructType.MULTIPLE_ANSWER_QUESTION, parentProperty, null);
+                        //判断题集合
+                        List<Question> bolList = questionList(courseCode, courseProperty, QuesStructType.BOOL_ANSWER_QUESTION, parentProperty, null);
+
+                        List<Question> fillList = null;
+                        //fillList = questionList(courseCode, courseProperty, QuesStructType.FILL_BLANK_QUESTION, parentProperty, null);
+                        List<Question> textList = null;
+                        //textList = questionList(courseCode, courseProperty, QuesStructType.TEXT_ANSWER_QUESTION, parentProperty, null);
+
+                        //计算所有题型数量
+                        Map<Long, Long> map = countQuesType(sinList, mulList, bolList, fillList, textList);
+                        QuestionDistributeDto questionDistributeDto = new QuestionDistributeDto(courseProperty.getName(), parentProperty.getName(), null, map);
+                        questionDistributes.add(questionDistributeDto);
                     }
-                } else {
-                    //一级属性为空
-                    QuestionDistributeDto questionDistributeDto = new QuestionDistributeDto(courseProperty.getName(), null, null, null);
-                    questionDistributeDtos.add(questionDistributeDto);
                 }
+            } else {
+                //一级属性为空
+                QuestionDistributeDto questionDistributeDto = new QuestionDistributeDto(courseProperty.getName(), null, null, null);
+                questionDistributes.add(questionDistributeDto);
             }
         }
-        //生成Excel导出
-        writeExcel(questionDistributeDtos, 21, courseNo, response);
+
+        return questionDistributes;
     }
 
-    /**
-     * 数据源		questionDistributeDtos
-     * Excel列数	cloumnCount
-     *
-     * @param questionDistributeDtos
-     * @param cloumnCount
-     * @throws IOException
-     */
     @SuppressWarnings("resource")
-    private void writeExcel(List<QuestionDistributeDto> questionDistributeDtos, int cloumnCount, String courseNo, HttpServletResponse response) throws IOException {
+    private File writeExcel(List<QuestionDistributeDto> questionDistributes, String filePath) throws IOException {
         //读取Excel模板
         InputStream in = this.getClass().getResourceAsStream("/quesDistinct.xlsx");
         //InputStream in = this.getClass().getResourceAsStream("/quesDistinctAll.xlsx");
 
-        Workbook workBook = new XSSFWorkbook(in);
-        //System.out.println(workBook.getAllNames());
         //获取第一个工作页
+        Workbook workBook = new XSSFWorkbook(in);
         Sheet sheet = workBook.getSheetAt(0);
-        System.out.println(sheet.getSheetName());
+
         //往Excel中写入数据,从第5行开始
-        for (int i = 0; i < questionDistributeDtos.size(); i++) {
+        for (int i = 0; i < questionDistributes.size(); i++) {
             //创建一行:从第五行开始,跳过表头
             Row row = sheet.createRow(i + 4);
+
             //获取这行的记录
-            QuestionDistributeDto questionDistributeDto = questionDistributeDtos.get(i);
+            QuestionDistributeDto questionDistributeDto = questionDistributes.get(i);
+
             //每列赋值
             row.createCell(0).setCellValue(questionDistributeDto.getCoursePropertyName());
             row.createCell(1).setCellValue(questionDistributeDto.getFirstPropertyName());
@@ -637,12 +657,12 @@ public class ExportPaperServiceImpl implements ExportPaperService {
                 }
             }
         }
-        File file = new File(TEMP_FILE_EXP + File.separator + "试题分布.xlsx");
+
+        File file = new File(filePath);
         OutputStream out = new FileOutputStream(file);
         workBook.write(out);
         out.close();
-        FileDisposeUtil.downloadFile(courseNo + "试题分布.xlsx", TEMP_FILE_EXP + File.separator + "试题分布.xlsx", response);
-        FileUtils.deleteQuietly(file);
+        return file;
     }
 
     private Map<Long, Long> countQuesType(List<Question> sinList, List<Question> mulList, List<Question> bolList, List<Question> fillList, List<Question> textList) {
@@ -811,27 +831,32 @@ public class ExportPaperServiceImpl implements ExportPaperService {
 
     @Override
     public void downQuestionDistributeByPapers(String paperIds, HttpServletResponse response) throws IOException {
-        List<QuestionDistributeDto> questionDistributeDtos = new ArrayList<QuestionDistributeDto>();
+        List<QuestionDistributeDto> questionDistributes = new ArrayList<>();
+
         //定义课程集合
-        List<String> courseNos = new ArrayList<String>();
+        List<String> courseCodes = new ArrayList<>();
+
         //定义试题集合
-        List<Question> questions = new ArrayList<Question>();
-        // 查询试卷集合
+        List<Question> questions = new ArrayList<>();
+
+        //查询试卷集合
         String[] paperIdArray = paperIds.split(",");
         for (int i = 0; i < paperIdArray.length; i++) {
             Paper basePaper = Model.of(paperRepo.findById(paperIdArray[i]));
-            courseNos.add(basePaper.getCourse().getCode());
+            courseCodes.add(basePaper.getCourse().getCode());
             //将小题全部取出来,只取一次,减少对数据库的查询
             List<PaperDetailUnit> allPaperDetailUnits = paperDetailUnitRepo.findByPaper(basePaper);
             for (PaperDetailUnit unit : allPaperDetailUnits) {
-                if (unit.getQuestionType().equals(QuesStructType.SINGLE_ANSWER_QUESTION) || unit.getQuestionType().equals(QuesStructType.MULTIPLE_ANSWER_QUESTION) || unit.getQuestionType().equals(QuesStructType.BOOL_ANSWER_QUESTION)) {
+                if (unit.getQuestionType().equals(QuesStructType.SINGLE_ANSWER_QUESTION) || unit.getQuestionType().equals(QuesStructType.MULTIPLE_ANSWER_QUESTION)
+                        || unit.getQuestionType().equals(QuesStructType.BOOL_ANSWER_QUESTION)) {
                     questions.add(unit.getQuestion());
                 }
             }
         }
+
         //根据课程code查询课程属性集合
-        for (String courseNo : courseNos) {
-            List<CourseProperty> courseProperties = coursePropertyRepo.findByCourseCodeAndEnable(courseNo, true);
+        for (String courseCode : courseCodes) {
+            List<CourseProperty> courseProperties = coursePropertyRepo.findByCourseCodeAndEnable(courseCode, true);
             //遍历课程属性集合,根据课程属性查询一级
             if (courseProperties != null && courseProperties.size() > 0) {
                 for (CourseProperty courseProperty : courseProperties) {
@@ -842,11 +867,11 @@ public class ExportPaperServiceImpl implements ExportPaperService {
                             if (sonProperties != null && sonProperties.size() > 0) {
                                 for (Property sonProperty : sonProperties) {
                                     //单选题集合
-                                    List<Question> sinList = questionList2(courseNo, courseProperty, QuesStructType.SINGLE_ANSWER_QUESTION, parentProperty, sonProperty, questions);
+                                    List<Question> sinList = questionList2(courseCode, courseProperty, QuesStructType.SINGLE_ANSWER_QUESTION, parentProperty, sonProperty, questions);
                                     //多选题集合
-                                    List<Question> mulList = questionList2(courseNo, courseProperty, QuesStructType.MULTIPLE_ANSWER_QUESTION, parentProperty, sonProperty, questions);
+                                    List<Question> mulList = questionList2(courseCode, courseProperty, QuesStructType.MULTIPLE_ANSWER_QUESTION, parentProperty, sonProperty, questions);
                                     //判断题集合
-                                    List<Question> bolList = questionList2(courseNo, courseProperty, QuesStructType.BOOL_ANSWER_QUESTION, parentProperty, sonProperty, questions);
+                                    List<Question> bolList = questionList2(courseCode, courseProperty, QuesStructType.BOOL_ANSWER_QUESTION, parentProperty, sonProperty, questions);
 
                                     List<Question> fillList = null;
                                     //fillList = questionList2(courseNo, courseProperty, QuesStructType.FILL_BLANK_QUESTION, parentProperty, sonProperty, questions);
@@ -856,16 +881,16 @@ public class ExportPaperServiceImpl implements ExportPaperService {
                                     //计算所有题型数量
                                     Map<Long, Long> map = countQuesType(sinList, mulList, bolList, fillList, textList);
                                     QuestionDistributeDto questionDistributeDto = new QuestionDistributeDto(courseProperty.getName(), parentProperty.getName(), sonProperty.getName(), map);
-                                    questionDistributeDtos.add(questionDistributeDto);
+                                    questionDistributes.add(questionDistributeDto);
                                 }
                             } else {
                                 //一级属性不为空,二级属性为空
                                 //单选题集合
-                                List<Question> sinList = questionList2(courseNo, courseProperty, QuesStructType.SINGLE_ANSWER_QUESTION, parentProperty, null, questions);
+                                List<Question> sinList = questionList2(courseCode, courseProperty, QuesStructType.SINGLE_ANSWER_QUESTION, parentProperty, null, questions);
                                 //多选题集合
-                                List<Question> mulList = questionList2(courseNo, courseProperty, QuesStructType.MULTIPLE_ANSWER_QUESTION, parentProperty, null, questions);
+                                List<Question> mulList = questionList2(courseCode, courseProperty, QuesStructType.MULTIPLE_ANSWER_QUESTION, parentProperty, null, questions);
                                 //判断题集合
-                                List<Question> bolList = questionList2(courseNo, courseProperty, QuesStructType.BOOL_ANSWER_QUESTION, parentProperty, null, questions);
+                                List<Question> bolList = questionList2(courseCode, courseProperty, QuesStructType.BOOL_ANSWER_QUESTION, parentProperty, null, questions);
 
                                 List<Question> fillList = null;
                                 //fillList = questionList2(courseNo, courseProperty, QuesStructType.FILL_BLANK_QUESTION, parentProperty, null, questions);
@@ -875,19 +900,25 @@ public class ExportPaperServiceImpl implements ExportPaperService {
                                 //计算所有题型数量
                                 Map<Long, Long> map = countQuesType(sinList, mulList, bolList, fillList, textList);
                                 QuestionDistributeDto questionDistributeDto = new QuestionDistributeDto(courseProperty.getName(), parentProperty.getName(), null, map);
-                                questionDistributeDtos.add(questionDistributeDto);
+                                questionDistributes.add(questionDistributeDto);
                             }
                         }
                     } else {
                         //一级属性为空
                         QuestionDistributeDto questionDistributeDto = new QuestionDistributeDto(courseProperty.getName(), null, null, null);
-                        questionDistributeDtos.add(questionDistributeDto);
+                        questionDistributes.add(questionDistributeDto);
                     }
                 }
             }
+
+            final String fileName = System.currentTimeMillis() + "试题分布.xlsx";
+            final String filePath = TEMP_FILE_EXP + File.separator + fileName;
+
             //生成Excel导出
-            writeExcel(questionDistributeDtos, 21, new Date().toString(), response);
+            File tempFile = this.writeExcel(questionDistributes, filePath);
+            FileDisposeUtil.downloadFile(fileName, filePath, response);
+            FileUtils.deleteQuietly(tempFile);
         }
     }
 
-}
+}

+ 2 - 1
examcloud-core-questions-starter/src/main/java/cn/com/qmth/examcloud/core/questions/starter/IndexController.java

@@ -111,6 +111,7 @@ public class IndexController {
 
         List<Course> courses = new ArrayList<>();
         courses.add(new Course("课程0627", "course_0627"));
+        courses.add(new Course("课程0628", "course_0628"));
 
 //        courses.add(new Course("仓储与配送管理", "183007"));
 //        courses.add(new Course("操作系统原理", "021008"));
@@ -140,7 +141,7 @@ public class IndexController {
 //        courses.add(new Course("工程力学", "423002"));
 //        courses.add(new Course("工程造价案例分析", "282003"));
 
-        exportPaperService.downQuestionDistribute(courses.get(0).getCode(), response);
+        exportPaperService.downQuestionDistributes(courses);
     }
 
 }