xiangpei
2024-07-02 6f58f2cb4ab7d8717cfc8995142ba62f2be09af1
题目导入完善
5个文件已修改
271 ■■■■■ 已修改文件
src/main/java/com/mindskip/xzs/controller/admin/QuestionController.java 225 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/mindskip/xzs/repository/QuestionMapper.java 9 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/mindskip/xzs/service/QuestionService.java 7 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/mindskip/xzs/service/impl/QuestionServiceImpl.java 21 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/resources/mapper/QuestionMapper.xml 9 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/mindskip/xzs/controller/admin/QuestionController.java
@@ -63,7 +63,7 @@
    private final QuestionSubjectService questionSubjectService;
    private final DeptQuestionMapper deptQuestionMapper;
    private static final String SPLIT = "、";
    private static final String SPLIT = ",";
    public QuestionController(QuestionService questionService, TextContentService textContentService, SubjectMapper subjectMapper, DepartmentMapper departmentMapper, QuestionSubjectService questionSubjectService, DeptQuestionMapper deptQuestionMapper) {
        this.questionService = questionService;
@@ -237,114 +237,114 @@
     * @param file
     * @throws IOException
     */
    @PostMapping("/question/import")
    @Transactional(rollbackFor = Exception.class)
    public RestResponse importQuestion(@RequestPart("file") MultipartFile file) throws IOException {
        Consumer<List<QuestionImportVO>> consumer = (data) -> {
            // 题目的课目信息
            List<QuestionSubject> questionSubjectsList = new ArrayList<>(48);
            for (int i = 0; i < data.size(); i++) {
                // 题目实体
                Question question = new Question();
                // 读取的题目
                QuestionImportVO excelQuestion = data.get(i);
                // 如果是第一条完整数据,那么继续往后读取选项内容
                if (excelQuestion.master()) {
                    // 该题的选项
                    List<QuestionItemObject> options = new ArrayList<>(4);
                    // 选项内容
                    QuestionItemObject option = new QuestionItemObject();
                    option.setPrefix(excelQuestion.getOptionName());
                    option.setContent(excelQuestion.getOptionValue());
                    options.add(option);
                    int next = 1;
                    // 继续往后读选项
                    while (Boolean.TRUE) {
                        // 判断是否是最后一条
                        if (next + i == data.size()) {
                            break;
                        }
                        QuestionImportVO nextQuestion = data.get(next + i);
                        if (nextQuestion.master()) {
                            break;
                        }
                        QuestionItemObject nextOption = new QuestionItemObject();
                        nextOption.setPrefix(nextQuestion.getOptionName());
                        nextOption.setContent(nextQuestion.getOptionValue());
                        options.add(nextOption);
                        next++;
                    }
                    i += next;
                    // 保存题目内容
                    QuestionObject questionObject = new QuestionObject();
                    questionObject.setQuestionItemObjects(options);
                    questionObject.setAnalyze(excelQuestion.getAnalyze());
                    questionObject.setTitleContent(excelQuestion.getTitle());
                    questionObject.setCorrect(excelQuestion.getCorrect());
                    TextContent textContent = new TextContent();
                    textContent.setContent(JSON.toJSONString(questionObject));
                    textContent.setCreateTime(new Date());
                    textContentService.insert(textContent);
                    // 保存题目信息
                    // 设置题型
                    question.setQuestionType(QuestionTypeEnum.get(excelQuestion.getQuestionType()));
                    // 答案(多选需要用,分割保存字符串到数据库)
                    String[] corrects = excelQuestion.getCorrect().split(SPLIT);
                    if (corrects.length > 1) {
                        question.setCorrect(Arrays.asList(corrects).stream().collect(Collectors.joining(",")));
                    } else {
                        question.setCorrect(excelQuestion.getCorrect());
                    }
                    // 难度
                    question.setDifficult(excelQuestion.getDifficult());
                    // 分数
                    question.setScore(ExamUtil.scoreFromVM(String.valueOf(excelQuestion.getScore())));
                    // 创建人
                    question.setCreateUser(1);
                    question.setStatus(QuestionStatusEnum.OK.getCode());
                    question.setCreateTime(new Date());
                    question.setDeleted(Boolean.FALSE);
                    question.setInfoTextContentId(textContent.getId());
                    questionService.insert(question);
                    // 查出所有的课目
                    List<Subject> subjects = subjectMapper.allSubject(new ArrayList<>(), Boolean.TRUE);
                    List<String> subjectNames = Arrays.asList(excelQuestion.getSubjectName().split(SPLIT));
                    List<Subject> targetSubject = subjects.stream()
                            .filter(subject -> subjectNames.contains(subject.getName()))
                            .collect(Collectors.toList());
                    if (CollectionUtils.isEmpty(targetSubject)) {
                        // todo 记录这个错误
                        continue;
                    }
                    // 构建课目-题目信息
                    questionSubjectsList = targetSubject.stream().map(subject -> {
                        QuestionSubject questionSubject = new QuestionSubject();
                        questionSubject.setQuestionId(question.getId());
                        questionSubject.setSubjectId(subject.getId());
                        questionSubject.setDeleted(0);
                        return questionSubject;
                    }).collect(Collectors.toList());
                }
                System.out.println(question);
            }
            // 批量保存题目-课目信息
            if (! CollectionUtils.isEmpty(questionSubjectsList)) {
                questionSubjectService.saves(questionSubjectsList);
            }
        };
        EasyExcel.read(file.getInputStream(), QuestionImportVO.class, new CurrencyDataListener(consumer)).sheet("模板").doRead();
        return RestResponse.ok();
    }
//    @PostMapping("/question/import")
//    @Transactional(rollbackFor = Exception.class)
//    public RestResponse importQuestion(@RequestPart("file") MultipartFile file) throws IOException {
//        Consumer<List<QuestionImportVO>> consumer = (data) -> {
//            // 题目的课目信息
//            List<QuestionSubject> questionSubjectsList = new ArrayList<>(48);
//            for (int i = 0; i < data.size(); i++) {
//                // 题目实体
//                Question question = new Question();
//                // 读取的题目
//                QuestionImportVO excelQuestion = data.get(i);
//
//                // 如果是第一条完整数据,那么继续往后读取选项内容
//                if (excelQuestion.master()) {
//                    // 该题的选项
//                    List<QuestionItemObject> options = new ArrayList<>(4);
//                    // 选项内容
//                    QuestionItemObject option = new QuestionItemObject();
//                    option.setPrefix(excelQuestion.getOptionName());
//                    option.setContent(excelQuestion.getOptionValue());
//                    options.add(option);
//                    int next = 1;
//                    // 继续往后读选项
//                    while (Boolean.TRUE) {
//                        // 判断是否是最后一条
//                        if (next + i == data.size()) {
//                            break;
//                        }
//                        QuestionImportVO nextQuestion = data.get(next + i);
//                        if (nextQuestion.master()) {
//                            break;
//                        }
//                        QuestionItemObject nextOption = new QuestionItemObject();
//                        nextOption.setPrefix(nextQuestion.getOptionName());
//                        nextOption.setContent(nextQuestion.getOptionValue());
//                        options.add(nextOption);
//                        next++;
//                    }
//                    i += next;
//
//                    // 保存题目内容
//                    QuestionObject questionObject = new QuestionObject();
//                    questionObject.setQuestionItemObjects(options);
//                    questionObject.setAnalyze(excelQuestion.getAnalyze());
//                    questionObject.setTitleContent(excelQuestion.getTitle());
//                    questionObject.setCorrect(excelQuestion.getCorrect());
//
//                    TextContent textContent = new TextContent();
//                    textContent.setContent(JSON.toJSONString(questionObject));
//                    textContent.setCreateTime(new Date());
//                    textContentService.insert(textContent);
//
//                    // 保存题目信息
//                    // 设置题型
//                    question.setQuestionType(QuestionTypeEnum.get(excelQuestion.getQuestionType()));
//                    // 答案(多选需要用,分割保存字符串到数据库)
//                    String[] corrects = excelQuestion.getCorrect().split(SPLIT);
//                    if (corrects.length > 1) {
//                        question.setCorrect(Arrays.asList(corrects).stream().collect(Collectors.joining(",")));
//                    } else {
//                        question.setCorrect(excelQuestion.getCorrect());
//                    }
//
//                    // 难度
//                    question.setDifficult(excelQuestion.getDifficult());
//                    // 分数
//                    question.setScore(ExamUtil.scoreFromVM(String.valueOf(excelQuestion.getScore())));
//                    // 创建人
//                    question.setCreateUser(1);
//                    question.setStatus(QuestionStatusEnum.OK.getCode());
//                    question.setCreateTime(new Date());
//                    question.setDeleted(Boolean.FALSE);
//                    question.setInfoTextContentId(textContent.getId());
//                    questionService.insert(question);
//
//
//                    // 查出所有的课目
//                    List<Subject> subjects = subjectMapper.allSubject(new ArrayList<>(), Boolean.TRUE);
//                    List<String> subjectNames = Arrays.asList(excelQuestion.getSubjectName().split(SPLIT));
//                    List<Subject> targetSubject = subjects.stream()
//                            .filter(subject -> subjectNames.contains(subject.getName()))
//                            .collect(Collectors.toList());
//                    if (CollectionUtils.isEmpty(targetSubject)) {
//                        // todo 记录这个错误
//                        continue;
//                    }
//                    // 构建课目-题目信息
//                    questionSubjectsList = targetSubject.stream().map(subject -> {
//                        QuestionSubject questionSubject = new QuestionSubject();
//                        questionSubject.setQuestionId(question.getId());
//                        questionSubject.setSubjectId(subject.getId());
//                        questionSubject.setDeleted(0);
//                        return questionSubject;
//                    }).collect(Collectors.toList());
//                }
//                System.out.println(question);
//            }
//            // 批量保存题目-课目信息
//            if (! CollectionUtils.isEmpty(questionSubjectsList)) {
//                questionSubjectService.saves(questionSubjectsList);
//            }
//        };
//        EasyExcel.read(file.getInputStream(), QuestionImportVO.class, new CurrencyDataListener(consumer)).sheet("模板").doRead();
//        return RestResponse.ok();
//    }
    @PostMapping("/import")
    public RestResponse importUser(@RequestPart("file") MultipartFile file) throws Exception {
    public RestResponse importQuestion(@RequestPart("file") MultipartFile file) throws Exception {
        List<QuestionEditVO> questionEditVOS = ExcelUtils.readMultipartFile(file, QuestionEditVO.class)
                .stream().map(e -> {
                    e.setQuestionType(QuestionTypeEnum.get(e.getType()));
@@ -360,6 +360,12 @@
        //组装题目
        for (QuestionEditRequestVM vm : list) {
            // 判断题干是否重复,重复不添加
            Integer num = questionService.countQuestionByTitle(vm.getTitle());
            if (num > 0) {
                continue;
            }
            questionEditItemVM.setPrefix("A");
            questionEditItemVM.setContent(vm.getA());
@@ -381,9 +387,10 @@
                questionEditItemVMS.add(questionEditItemVM);
            }
            vm.setItems(questionEditItemVMS);
            List<String> str = Arrays.asList(vm.getCorrect().split(","));
            // 清空前后的逗号,以免后续作答判断为错误
            List<String> str = Arrays.asList(vm.getCorrect().replaceAll("^,+|,+$", "").split(SPLIT));
            List<Subject> subjectList = subjectMapper.getNames(vm.getSbNames().split(","));
            List<Subject> subjectList = subjectMapper.getNames(vm.getSbNames().split(SPLIT));
            Integer[] arr =subjectList.stream()
                    .map(Subject::getId).toArray(Integer[]::new);
src/main/java/com/mindskip/xzs/repository/QuestionMapper.java
@@ -46,6 +46,11 @@
    QuestionVO getAnswer(Integer id);
    /**
     * 查抄题干符合的题目数量
     *
     * @param title
     * @return
     */
    Integer countQuestionByTitle(@Param("title") String title);
}
src/main/java/com/mindskip/xzs/service/QuestionService.java
@@ -61,4 +61,11 @@
    /** 获取题目答案、解析 */
    RestResponse getAnswer(Integer id);
    /**
     * 查找是否已经存在题干相同的题
     *
     * @param title
     * @return
     */
    Integer countQuestionByTitle(String title);
}
src/main/java/com/mindskip/xzs/service/impl/QuestionServiceImpl.java
@@ -104,14 +104,16 @@
        questionMapper.insertSelective(question);
        // 题目所属部门插入
        List<DeptQuestion> deptQuestions = model.getDeptIds().stream().map(deptId -> {
            DeptQuestion deptQuestion = new DeptQuestion();
            deptQuestion.setQuestionId(question.getId());
            deptQuestion.setDeptId(deptId);
            return deptQuestion;
        }).collect(Collectors.toList());
        if (! CollectionUtils.isEmpty(model.getDeptIds())) {
            deptQuestionMapper.add(deptQuestions);
            List<DeptQuestion> deptQuestions = model.getDeptIds().stream().map(deptId -> {
                DeptQuestion deptQuestion = new DeptQuestion();
                deptQuestion.setQuestionId(question.getId());
                deptQuestion.setDeptId(deptId);
                return deptQuestion;
            }).collect(Collectors.toList());
            if (! CollectionUtils.isEmpty(model.getDeptIds())) {
                deptQuestionMapper.add(deptQuestions);
            }
        }
        //批量添加
@@ -342,6 +344,11 @@
        return RestResponse.ok(null);
    }
    @Override
    public Integer countQuestionByTitle(String title) {
        return questionMapper.countQuestionByTitle(title);
    }
    /**
     * 处理题目内容JSON
     *
src/main/resources/mapper/QuestionMapper.xml
@@ -373,4 +373,13 @@
                 INNER JOIN t_text_content ttc ON tq.info_text_content_id = ttc.id AND tq.id = #{id} AND tq.deleted = 0
    </select>
    <select id="countQuestionByTitle" resultType="integer">
        SELECT
               COUNT(q.id)
        FROM
             t_question q
                 INNER JOIN t_text_content d on q.info_text_content_id = d.id
        WHERE JSON_VALUE(d.content, '$.titleContent') = #{title} AND q.deleted = 0
    </select>
</mapper>