deason 2 years ago
parent
commit
074e2f1293

+ 5 - 0
pom.xml

@@ -73,6 +73,11 @@
             <artifactId>easyexcel</artifactId>
             <version>3.0.5</version>
         </dependency>
+        <dependency>
+            <groupId>org.jsoup</groupId>
+            <artifactId>jsoup</artifactId>
+            <version>1.15.2</version>
+        </dependency>
 
         <dependency>
             <groupId>org.springframework.boot</groupId>

+ 110 - 24
src/main/java/cn/com/qmth/examcloud/tool/service/export_student_answer_and_score_detail/ExportStudentAnswerAndScoreDetailTask.java

@@ -3,10 +3,7 @@ package cn.com.qmth.examcloud.tool.service.export_student_answer_and_score_detai
 import cn.com.qmth.examcloud.tool.config.SysProperty;
 import cn.com.qmth.examcloud.tool.entity.TaskEntity;
 import cn.com.qmth.examcloud.tool.service.CommonService;
-import cn.com.qmth.examcloud.tool.service.export_student_answer_and_score_detail.vo.CourseVO;
-import cn.com.qmth.examcloud.tool.service.export_student_answer_and_score_detail.vo.ExamQuestionVO;
-import cn.com.qmth.examcloud.tool.service.export_student_answer_and_score_detail.vo.ExamRecordQuestionsVO;
-import cn.com.qmth.examcloud.tool.service.export_student_answer_and_score_detail.vo.ExamStudentScoreVO;
+import cn.com.qmth.examcloud.tool.service.export_student_answer_and_score_detail.vo.*;
 import cn.com.qmth.examcloud.tool.utils.*;
 import cn.com.qmth.examcloud.tool.vo.Pager;
 import cn.com.qmth.examcloud.tool.vo.User;
@@ -22,10 +19,7 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 
 import java.io.File;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
 
 @Component
 public class ExportStudentAnswerAndScoreDetailTask {
@@ -82,60 +76,89 @@ public class ExportStudentAnswerAndScoreDetailTask {
 
         for (CourseVO course : todoCourses) {
             // 按考试课程逐个导出
-            this.export(examId, course, user, tempDir);
+            this.export(examId, course, needSubjective, user, tempDir);
         }
     }
 
-    private void export(Long examId, CourseVO course, User user, String tempDir) {
+    private void export(Long examId, CourseVO course, Boolean needSubjective, User user, String tempDir) {
         List<ExamStudentScoreVO> examStudentScoreList = this.getExamStudentScoreList(user.getKey(), user.getToken(), examId, course.getCourseId());
 
         // Excel动态列
         List<String> dynamicExcelHeaders = new ArrayList<>();
 
-        List<List<Object>> excelRows = new ArrayList<>();
+        List<List<String>> excelRows = new ArrayList<>();
         for (ExamStudentScoreVO vo : examStudentScoreList) {
-            List<Object> rowValues = Lists.newArrayList(
+            List<String> rowValues = Lists.newArrayList(
                     vo.getOrgName(),
                     vo.getCourseCode(),
                     vo.getCourseName(),
                     vo.getCourseLevel(),
-                    vo.getIsAbsent(),
                     vo.getIdentityNumber(),
                     vo.getStudentCode(),
                     vo.getStudentName(),
                     vo.getGrade(),
                     vo.getSpecialtyName(),
-                    vo.getObjectiveScore(),
-                    vo.getSubjectiveScore(),
-                    vo.getFinalExamScore());
+                    vo.getIsAbsent(),
+                    vo.getObjectiveScore().replace("--", "0"),
+                    vo.getSubjectiveScore().replace("--", "0"),
+                    vo.getFinalExamScore().replace("--", "0"));
             excelRows.add(rowValues);
 
             if (vo.getExamRecordDataId() == null) {
                 // 跳过缺考的情况
-                log.info("identityNumber={} finished={} absent={}", vo.getIdentityNumber(), vo.getIsFinished(), vo.getIsAbsent());
+                log.info("examId={} courseId={} identityNumber={} finished={} absent={}", examId, course.getCourseId(),
+                        vo.getIdentityNumber(), vo.getIsFinished(), vo.getIsAbsent());
                 continue;
             }
 
             // 获取考试试题作答记录
-            List<ExamQuestionVO> questions = this.getExamRecordQuestions(user.getKey(), user.getToken(), vo.getExamRecordDataId());
+            List<ExamQuestionVO> examQuestions = this.getExamRecordQuestions(user.getKey(), user.getToken(), vo.getExamRecordDataId());
 
             if (dynamicExcelHeaders.isEmpty()) {
-                for (ExamQuestionVO question : questions) {
+                for (ExamQuestionVO question : examQuestions) {
+                    if (!isObjectiveQuestion(question.getQuestionType()) && !needSubjective) {
+                        // 不需要主观题
+                        continue;
+                    }
+
+                    // dynamicExcelHeaders.add(String.format("%s%s-%s答案", question.getQuestionType().getDesc(), question.getMainNumber(), question.getOrder()));
                     dynamicExcelHeaders.add(String.format("%s%s-%s作答", question.getQuestionType().getDesc(), question.getMainNumber(), question.getOrder()));
                     dynamicExcelHeaders.add(String.format("%s%s-%s得分", question.getQuestionType().getDesc(), question.getMainNumber(), question.getOrder()));
                 }
             }
 
             // Excel动态列值
-            List<Object> dynamicColumnValues = new ArrayList<>();
-            for (ExamQuestionVO question : questions) {
-                dynamicColumnValues.add(question.getStudentAnswer());
-                dynamicColumnValues.add("0");
+            List<String> dynamicColumnValues = new ArrayList<>();
+            for (ExamQuestionVO question : examQuestions) {
+                if (isObjectiveQuestion(question.getQuestionType())) {
+                    String studentAnswer, correctAnswer;
+                    if (QuestionType.TRUE_OR_FALSE == question.getQuestionType()) {
+                        studentAnswer = convertTrueFalse(question.getStudentAnswer());
+                        correctAnswer = convertTrueFalse(question.getCorrectAnswer());
+                    } else {
+                        correctAnswer = numbersToLetters(question.getCorrectAnswer());
+                        studentAnswer = numbersToLetters(question.getStudentAnswer());
+                    }
+
+                    // dynamicColumnValues.add(correctAnswer);
+                    dynamicColumnValues.add(studentAnswer);
+                    if (correctAnswer.equals(studentAnswer)) {
+                        dynamicColumnValues.add(question.getQuestionScore() != null ? String.valueOf(question.getQuestionScore()) : "0");
+                    } else {
+                        dynamicColumnValues.add("0");
+                    }
+                } else {
+                    if (needSubjective) {
+                        dynamicColumnValues.add("-");
+                        dynamicColumnValues.add("-");
+                        dynamicColumnValues.add("0");
+                    }
+                }
             }
             rowValues.addAll(dynamicColumnValues);
         }
 
-        List<String> excelHeaders = Lists.newArrayList("学习中心", "课程代码", "课程名称", "课程层次", "是否缺考", "身份证号", "学号", "姓名", "年级", "专业", "客观总分", "主观总分", "总分");
+        List<String> excelHeaders = Lists.newArrayList("学习中心", "课程代码", "课程名称", "课程层次", "身份证号", "学号", "姓名", "年级", "专业", "是否缺考", "客观总分", "主观总分", "总分");
         excelHeaders.addAll(dynamicExcelHeaders);
 
         final String filePath = sysProperty.getDataDir() + "/" + tempDir + "/" + examId + "_" + course.getCourseCode() + ".xlsx";
@@ -144,6 +167,17 @@ public class ExportStudentAnswerAndScoreDetailTask {
                 .registerWriteHandler(ExcelHelper.sheetStrategy())
                 .registerWriteHandler(ExcelHelper.styleStrategy())
                 .sheet().doWrite(excelRows);
+
+        excelRows.clear();
+        excelHeaders.clear();
+        examStudentScoreList.clear();
+    }
+
+    /**
+     * 是否为客观题
+     */
+    private boolean isObjectiveQuestion(QuestionType type) {
+        return QuestionType.SINGLE_CHOICE == type || QuestionType.MULTIPLE_CHOICE == type || QuestionType.TRUE_OR_FALSE == type;
     }
 
     /**
@@ -233,4 +267,56 @@ public class ExportStudentAnswerAndScoreDetailTask {
         return new ArrayList<>();
     }
 
+    private String numbersToLetters(String numberStr) {
+        if (StringUtils.isEmpty(numberStr)) {
+            return "";
+        }
+
+        List<Integer> numbers = new ArrayList<>();
+        if (numberStr.startsWith("[") && numberStr.endsWith("]")) {
+            // Json数据格式,如:[0,1,2,3,4,5,6,7,8,9,...,25]
+            String[] values = numberStr.replace("[", "")
+                    .replace("]", "").split(",");
+            for (String value : values) {
+                if (value.matches("^\\d+$")) {
+                    numbers.add(Integer.parseInt(value.trim()));
+                }
+            }
+        } else {
+            // 旧数据格式,如:0123456789
+            char[] chars = numberStr.toCharArray();
+            for (char c : chars) {
+                if (String.valueOf(c).matches("^\\d+$")) {
+                    numbers.add(c - 48);
+                }
+            }
+        }
+        Collections.sort(numbers);
+
+        List<String> letters = new ArrayList<>();
+        for (int number : numbers) {
+            if (number >= 0 && number <= 25) {
+                // 只处理0-25之间的数字
+                char c = (char) (65 + number);
+                letters.add(String.valueOf(c));
+            }
+        }
+
+        return StringUtils.join(letters, ",");
+    }
+
+    private String convertTrueFalse(String value) {
+        if ("true".equals(value)) {
+            return "对";
+        } else if ("false".equals(value)) {
+            return "错";
+        } else if ("1".equals(value)) {
+            return "对";
+        } else if ("0".equals(value)) {
+            return "错";
+        } else {
+            return "";
+        }
+    }
+
 }

+ 1 - 1
src/main/java/cn/com/qmth/examcloud/tool/utils/ExcelHelper.java

@@ -38,7 +38,7 @@ public class ExcelHelper {
             public void afterSheetCreate(WriteWorkbookHolder workbookHolder, WriteSheetHolder sheetHolder) {
                 Sheet sheet = sheetHolder.getSheet();
                 sheet.createFreezePane(0, 1);// 固定首行
-                sheet.setDefaultColumnWidth(20);
+                sheet.setDefaultColumnWidth(15);
             }
         };
     }