Browse Source

fix:客观题导入

caozixuan 1 năm trước cách đây
mục cha
commit
214a9a9c88

+ 1 - 0
distributed-print-business/src/main/java/com/qmth/distributed/print/business/templete/execute/SyncObjectiveStructImportService.java

@@ -45,6 +45,7 @@ public class SyncObjectiveStructImportService extends SyncImportTaskTemplate {
             // 执行导入考务数据
             taskLogicService.executeImportObjectiveStructLogic(map);
             tbTask.setResult(TaskResultEnum.SUCCESS);
+            stringJoinerSummary.add("导入客观题结构成功");
         } catch (Exception e) {
             // 导入异常错误信息写入summary
             stringJoinerSummary.add("导入客观题结构失败," + EXCEPTION_DATA + System.lineSeparator() + e.getMessage());

+ 31 - 21
distributed-print-business/src/main/java/com/qmth/distributed/print/business/templete/service/impl/TaskLogicServiceImpl.java

@@ -2338,7 +2338,13 @@ public class TaskLogicServiceImpl implements TaskLogicService {
         List<MarkPaper> markPaperList = markPaperService.list(
                 new QueryWrapper<MarkPaper>().lambda().eq(MarkPaper::getExamId, examId));
         // 题号重复校验
-        Set<String> paperCheckSet = new HashSet<>();
+        // 导入的客观题题号集合
+        Set<String> questionNumberCheckSet = new HashSet<>();
+        // 该试卷已存在的主观题题号集合
+        Set<String> dbSubjectiveQuestionNumberSet = markQuestionService.list(
+                        new QueryWrapper<MarkQuestion>().lambda().eq(MarkQuestion::getExamId, examId)
+                                .eq(MarkQuestion::getObjective, false)).stream()
+                .map(e -> e.getPaperNumber() + SystemConstant.HYPHEN + e.getMainNumber() + SystemConstant.HYPHEN + e.getSubNumber()).collect(Collectors.toSet());
 
         ExcelResult<ObjectiveStructDto> excelResult = ConvertUtil.analyzeExcel(inputStream, ObjectiveStructDto.class,
                 true, 0);
@@ -2365,11 +2371,14 @@ public class TaskLogicServiceImpl implements TaskLogicService {
             if (SystemConstant.strNotNull(paperNumber) && mainNumber != null && subNumber != null) {
                 logicErrorCommonNotice = String.format("试卷编号为[%s],大题号为[%s],小题号为[%s]的数据异常 : ", paperNumber, mainNumber,
                         subNumber);
-                String questionKey = paperNumber + SystemConstant.HYPHEN + mainNumber + SystemConstant.HYPHEN + subNumber;
-                if (paperCheckSet.contains(questionKey)) {
-                    logicErrorList.add("题号重复");
+                String questionNumberKey = paperNumber + SystemConstant.HYPHEN + mainNumber + SystemConstant.HYPHEN + subNumber;
+                if (questionNumberCheckSet.contains(questionNumberKey)) {
+                    logicErrorList.add("excel中题号重复");
                 } else {
-                    paperCheckSet.add(questionKey);
+                    questionNumberCheckSet.add(questionNumberKey);
+                }
+                if (dbSubjectiveQuestionNumberSet.contains(questionNumberKey)) {
+                    logicErrorList.add("该题号已存在主观题中");
                 }
             }
 
@@ -2396,23 +2405,31 @@ public class TaskLogicServiceImpl implements TaskLogicService {
 
             // 4.判断答案是否符合题型
             if (questionType != null && SystemConstant.strNotNull(answer)) {
-                //单选题的答案只能有一个
-                if (Objects.equals(QuestionType.SINGLE.getValue(), questionType)) {
+                switch (questionType) {
+                case 1:
                     if (answer.length() > 1) {
                         logicErrorList.add("单选题答案只能有一个");
                     }
-                }
-                //判断题的答案只能为A&B
-                if (Objects.equals(QuestionType.TRUE_OR_FALSE.getValue(), questionType)) {
+                    break;
+                case 2:
+                    break;
+                case 3:
                     if (!OptionsEnum.A.getCode().equals(answer) && !OptionsEnum.B.getCode().equals(answer)) {
                         logicErrorList.add("判断题答案只能为A&B");
                     }
+                    break;
+                default:
+                    logicErrorList.add("[题型(1-单选,2-多选,3-判断)]必须从1、2、3中填写");
                 }
             }
 
             // 整理异常信息
             if (CollectionUtils.isNotEmpty(logicErrorList)) {
-                errorMsg = errorMsg + logicErrorCommonNotice + String.join(";", logicErrorList);
+                if (SystemConstant.strNotNull(errorMsg)) {
+                    errorMsg = errorMsg + "、" + logicErrorCommonNotice + String.join("、", logicErrorList);
+                } else {
+                    errorMsg = logicErrorCommonNotice + String.join("、", logicErrorList);
+                }
                 objectiveStructDto.setErrorMsg(errorMsg);
             } else {
                 MarkQuestion markQuestion = new MarkQuestion();
@@ -2442,16 +2459,9 @@ public class TaskLogicServiceImpl implements TaskLogicService {
                 // 优先删除该试卷编号下的客观题
                 markQuestionService.remove(new QueryWrapper<MarkQuestion>().lambda().eq(MarkQuestion::getExamId, examId)
                         .eq(MarkQuestion::getPaperNumber, k).eq(MarkQuestion::getObjective, true));
-
-                MarkQuestionParams markQuestionParams = new MarkQuestionParams();
-                markQuestionParams.setQuestions(v);
-                markQuestionParams.setExamId(examId);
-                markQuestionParams.setPaperNumber(k);
-                try {
-                    markQuestionService.saveQuestions(markQuestionParams);
-                } catch (Exception e) {
-                    throw ExceptionResultEnum.ERROR.exception(e.getMessage());
-                }
+                // 保存客观题
+                markQuestionService.saveBatch(v);
+                markQuestionService.updateMarkPaperScore(examId, k, null);
             });
         }
         return map;

+ 9 - 0
distributed-print/src/main/java/com/qmth/distributed/print/api/mark/MarkSettingController.java

@@ -146,4 +146,13 @@ public class MarkSettingController {
         map.put("examId", SystemConstant.convertIdToLong(examId));
         return syncObjectiveStructImportService.importTask(map);
     }
+
+    @ApiOperation(value = "主观题导入")
+    @RequestMapping(value = "/subjective_struct/import", method = RequestMethod.POST)
+    public Result subjectiveStructImport(@ApiParam(value = "考试ID", required = true) @RequestParam String examId,
+            @ApiParam(value = "标答excel文件",required = true) @RequestParam MultipartFile file) throws Exception {
+        Map<String, Object> map = printCommonService.saveTask(file, SystemConstant.convertIdToLong(examId), TaskTypeEnum.OBJECTIVE_STRUCT_IMPORT);
+        map.put("examId", SystemConstant.convertIdToLong(examId));
+        return syncObjectiveStructImportService.importTask(map);
+    }
 }

+ 34 - 2
teachcloud-common/src/main/java/com/qmth/teachcloud/common/util/ConvertUtil.java

@@ -23,6 +23,7 @@ import java.awt.image.BufferedImage;
 import java.io.*;
 import java.lang.reflect.Field;
 import java.net.URLEncoder;
+import java.text.ParseException;
 import java.text.SimpleDateFormat;
 import java.util.*;
 import java.util.concurrent.atomic.AtomicInteger;
@@ -201,7 +202,7 @@ public class ConvertUtil {
                     ExcelColumn annotation = field.getAnnotation(ExcelColumn.class);
                     if (SystemConstant.strNotNull(v)) {
                         field.setAccessible(true);
-                        field.set(dto, v);
+                        field.set(dto, convertAttributeValue(field.getType().getSimpleName(), v));
                         emptyCell = false;
                     } else if (annotation.nullable()) {
                         excelErrorList.add(String.format("[%s]必填", annotation.name()));
@@ -217,7 +218,7 @@ public class ConvertUtil {
                     String errorMsg = String.format("第[%s]行,%s", index, String.join(",", excelErrorList));
                     if (fillError && Objects.nonNull(errorField)) {
                         errorField.setAccessible(true);
-                        errorField.set(dto, errorMsg);
+                        errorField.set(dto, convertAttributeValue(errorField.getType().getSimpleName(), errorMsg));
                     }
                     errorMap.put(index, errorMsg);
                 }
@@ -231,6 +232,37 @@ public class ConvertUtil {
         return excelResult;
     }
 
+    /**
+     * 将属性的值从String类型转换成他们各自对应的数据类型的值
+     *
+     * @param type  属性的类型
+     * @param value 属性的值
+     * @return 对应数据类型的值
+     */
+    private static Object convertAttributeValue(String type, String value) throws ParseException {
+        Object result;
+        if ("int".equals(type) || "Integer".equals(type)) {
+            result = Integer.parseInt(value);
+        } else if ("double".equals(type) || "Double".equals(type)) {
+            result = Double.parseDouble(value);
+        } else if ("long".equals(type) || "Long".equals(type)) {
+            result = Long.parseLong(value);
+        } else if ("Date".equals(type)) {
+            SimpleDateFormat sdf;
+            if (value.matches("\\d{4}-\\d{2}-\\d{2}")) {
+                sdf = new SimpleDateFormat("yyyy-MM-dd");
+            } else if (value.matches("\\d{4}-\\d{2}-\\d{2} \\d{2}\\d{2}\\d{2}")) {
+                sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+            } else {
+                return new Date();
+            }
+            return sdf.parse(value);
+        } else {
+            return value;
+        }
+        return result;
+    }
+
     public static void delFolder(String folderPath) {
         try {
             delAllFile(folderPath); //删除完里面所有内容

+ 9 - 0
teachcloud-mark/src/main/java/com/qmth/teachcloud/mark/service/MarkQuestionService.java

@@ -78,4 +78,13 @@ public interface MarkQuestionService extends IService<MarkQuestion> {
     List<MarkQuestion> listByExamIdAndPaperNumberAndPaperType(Long examId, String paperNumber, String paperType, Boolean objective);
 
     void deleteByExamIdAndPaperNumber(Long examId, String paperNumber);
+
+    /**
+     * 更新试卷总分
+     *
+     * @param examId      考试id
+     * @param paperNumber 试卷编号
+     * @param paperType   试卷类型
+     */
+    void updateMarkPaperScore(Long examId, String paperNumber, String paperType);
 }

+ 17 - 0
teachcloud-mark/src/main/java/com/qmth/teachcloud/mark/service/impl/MarkQuestionServiceImpl.java

@@ -472,4 +472,21 @@ public class MarkQuestionServiceImpl extends ServiceImpl<MarkQuestionMapper, Mar
         this.remove(updateWrapper);
     }
 
+    @Transactional
+    @Override
+    public void updateMarkPaperScore(Long examId, String paperNumber, String paperType) {
+        List<MarkQuestion> markQuestions = this.listQuestionByExamIdAndPaperNumberAndPaperType(examId, paperNumber, paperType);
+        Double objectiveScore = markQuestions.stream().filter(MarkQuestion::getObjective)
+                .mapToDouble(MarkQuestion::getTotalScore).sum();
+        Double subjectiveScore = markQuestions.stream().filter(m -> !m.getObjective())
+                .mapToDouble(MarkQuestion::getTotalScore).sum();
+        Double totalScore = markQuestions.stream().mapToDouble(MarkQuestion::getTotalScore).sum();
+        UpdateWrapper<MarkPaper> updateWrapper = new UpdateWrapper<>();
+        updateWrapper.lambda().set(MarkPaper::getObjectiveScore, objectiveScore)
+                .set(MarkPaper::getSubjectiveScore, subjectiveScore)
+                .set(MarkPaper::getTotalScore, totalScore)
+                .eq(MarkPaper::getExamId, examId)
+                .eq(MarkPaper::getPaperNumber, paperNumber);
+        markPaperService.update(updateWrapper);
+    }
 }