qirong
2023-07-28 fbdd6af3039a83cd4727a03cecb7c5914277371f
src/main/java/com/mindskip/xzs/service/impl/ExamPaperServiceImpl.java
@@ -50,9 +50,11 @@
    private final ExamPaperDepartmentService examPaperDepartmentService;
    private final ExamPaperSubjectService examPaperSubjectService;
    private final QuestionSubjectService questionSubjectService;
    private final ExamPaperUserService examPaperUserService;
    private final UserService userService;
    @Autowired
    public ExamPaperServiceImpl(ExamPaperMapper examPaperMapper, QuestionMapper questionMapper, TextContentService textContentService, QuestionService questionService, SubjectService subjectService, ExamPaperDepartmentService examPaperDepartmentService, ExamPaperSubjectService examPaperSubjectService, QuestionSubjectService questionSubjectService) {
    public ExamPaperServiceImpl(ExamPaperMapper examPaperMapper, QuestionMapper questionMapper, TextContentService textContentService, QuestionService questionService, SubjectService subjectService, ExamPaperDepartmentService examPaperDepartmentService, ExamPaperSubjectService examPaperSubjectService, QuestionSubjectService questionSubjectService, ExamPaperUserService examPaperUserService, UserService userService) {
        super(examPaperMapper);
        this.examPaperMapper = examPaperMapper;
        this.questionMapper = questionMapper;
@@ -62,6 +64,8 @@
        this.examPaperDepartmentService = examPaperDepartmentService;
        this.examPaperSubjectService = examPaperSubjectService;
        this.questionSubjectService = questionSubjectService;
        this.examPaperUserService = examPaperUserService;
        this.userService = userService;
    }
@@ -131,7 +135,8 @@
            examPaperDepartmentService.removeByExamPaperId(examPaper.getId());
            examPaperSubjectService.removeByExamPaperId(examPaper.getId());
        }
        addExamPaperDepartment(examPaperEditRequestVM, examPaper);
//        addExamPaperDepartment(examPaperEditRequestVM, examPaper);
        addExamPaperUser(examPaperEditRequestVM, examPaper);    //批量添加试卷关联用户
        addExamPaperSubject(examPaperEditRequestVM, examPaper);
        return examPaper;
    }
@@ -148,6 +153,12 @@
                        .map(q -> q.getId()))
                .collect(Collectors.toList());
        List<Question> questions = questionMapper.selectByIds(questionIds);
        //单选数量
        Integer singleChoice = questions.stream().filter(e -> e.getQuestionType() == 1).collect(Collectors.toList()).size();
        //多选数量
        Integer multipleChoice = questions.stream().filter(e -> e.getQuestionType() == 2).collect(Collectors.toList()).size();
        //判断数量
        Integer trueFalse = questions.stream().filter(e -> e.getQuestionType() == 3).collect(Collectors.toList()).size();
        Integer order = 0;
        Set<Integer> generatedNumbers = new HashSet<>();
        Random random = new Random();
@@ -156,7 +167,12 @@
            List<QuestionEditRequestVM> questionItemsVM = t.getQuestionItems().stream().map(i -> {
                        Question question = questions.stream().filter(q -> q.getId().equals(i.getId())).findFirst().get();
                        QuestionEditRequestVM questionEditRequestVM = questionService.getQuestionEditRequestVM(question);
                        questionEditRequestVM.setItemOrder(getRandomNumber(t.getQuestionItems().size() - 1, generatedNumbers, random));
                        questionEditRequestVM.setTitle("(" + QuestionTypeEnum.fromCode(questionEditRequestVM.getQuestionType()).getName() + ") " + questionEditRequestVM.getTitle());
                        questionEditRequestVM.setItemOrder(generateRandomNumber(questionEditRequestVM.getQuestionType() == 1 ? 0 : ((questionEditRequestVM.getQuestionType() == 2 ? singleChoice : multipleChoice + singleChoice)),
                                questionEditRequestVM.getQuestionType() == 1 ? singleChoice : ((questionEditRequestVM.getQuestionType() == 2 ? multipleChoice + singleChoice : trueFalse + multipleChoice + singleChoice)),
                                generatedNumbers, random));
//                        questionEditRequestVM.setItemOrder(getRandomNumber(t.getQuestionItems().size() - 1, generatedNumbers, random));
                        return questionEditRequestVM;
                    }).sorted(Comparator.comparing(QuestionEditRequestVM::getItemOrder))
                    .collect(Collectors.toList());
@@ -175,6 +191,13 @@
                .stream().map(ExamPaperSubject::getSubjectId).toArray(Integer[]::new));
        vm.setDepartmentIds(examPaperDepartmentService.getByExamPaperId(examPaper.getId())
                .stream().map(ExamPaperDepartment::getDepartmentId).toArray(Integer[]::new));
        List<ExamPaperUser> examPaperUsers = examPaperUserService.getByExamPaperId(examPaper.getId());
        Integer[][] userIds = new Integer[examPaperUsers.size()][2];
        for (int i = 0; i < examPaperUsers.size(); i++) {
            Integer[] userId = {userService.getUserById(examPaperUsers.get(i).getUserId()).getUserLevel(), examPaperUsers.get(i).getUserId()};
            userIds[i] = userId;
        }
        vm.setUserId(userIds);
        return vm;
    }
@@ -259,7 +282,27 @@
        examPaperDepartmentService.saves(list);
    }
    private void addExamPaperUser(ExamPaperEditRequestVM examPaperEditRequestVM, ExamPaper examPaper) {
        if (examPaperEditRequestVM.getUserIds().length == 0) {
            return;
        }
        if (examPaperUserService.getByExamPaperId(examPaper.getId()).size() > 0) {
            examPaperUserService.removeByExamPaperId(examPaper.getId());
        }
        List<ExamPaperUser> list = Arrays.asList(examPaperEditRequestVM.getUserIds()).stream().map(e -> {
            ExamPaperUser examPaperUser = new ExamPaperUser();
            examPaperUser.setUserId(e);
            examPaperUser.setExamPaperId(examPaper.getId());
            examPaperUser.setDeleted("0");
            return examPaperUser;
        }).collect(Collectors.toList());
        examPaperUserService.saves(list);
    }
    private void addExamPaperSubject(ExamPaperEditRequestVM examPaperEditRequestVM, ExamPaper examPaper) {
        if (examPaperSubjectService.getByExamPaperId(examPaper.getId()).size() > 0) {
            examPaperSubjectService.removeByExamPaperId(examPaper.getId());
        }
        List<ExamPaperSubject> subjectList = Arrays.asList(examPaperEditRequestVM.getSubjectId()).stream().map(e -> {
            ExamPaperSubject examPaperSubject = new ExamPaperSubject();
            examPaperSubject.setSubjectId(e);
@@ -420,7 +463,7 @@
        Integer multipleChoice = examPaperEditRequestVM.getQuestionTypeVMS().stream().mapToInt(QuestionTypeVM::getMultipleChoice).sum();
        //判断
        Integer judgment = examPaperEditRequestVM.getQuestionTypeVMS().stream().mapToInt(QuestionTypeVM::getTrueFalse).sum();
        if ((singleChoice * 4 + multipleChoice * 5 + judgment * 2) != 100) {
        if ((singleChoice * 4 + multipleChoice * 5 + judgment * 2) != new Integer(20)) {
            throw new QuestionException();
        }
@@ -508,4 +551,22 @@
        generatedNumbers.add(randomNumber);
        return randomNumber;
    }
    /**
     * 两个数之前的随机数(不重复)
     *
     * @param a
     * @param b
     * @return
     */
    public static int generateRandomNumber(Integer a, Integer b, Set<Integer> generatedNumbers, Random random) {
        int randomNumber = random.nextInt(b - a) + a;
        while (generatedNumbers.contains(randomNumber)) {
            randomNumber = random.nextInt(b - a) + a;
        }
        generatedNumbers.add(randomNumber);
        return randomNumber;
    }
}