龚焕茏
2024-05-07 49429bad1036c81c056faeadfa009c53ba777fad
src/main/java/com/mindskip/xzs/service/impl/ExamPaperServiceImpl.java
@@ -8,18 +8,18 @@
import com.mindskip.xzs.domain.exam.ExamPaperQuestionItemObject;
import com.mindskip.xzs.domain.exam.ExamPaperTitleItemObject;
import com.mindskip.xzs.domain.other.KeyValue;
import com.mindskip.xzs.domain.vo.PaperExcelVO;
import com.mindskip.xzs.repository.ExamPaperMapper;
import com.mindskip.xzs.repository.QuestionMapper;
import com.mindskip.xzs.repository.UserDepartmentMapper;
import com.mindskip.xzs.service.*;
import com.mindskip.xzs.service.enums.ActionEnum;
import com.mindskip.xzs.utility.DateTimeUtil;
import com.mindskip.xzs.utility.JsonUtil;
import com.mindskip.xzs.utility.ModelMapperSingle;
import com.mindskip.xzs.utility.ExamUtil;
import com.mindskip.xzs.viewmodel.admin.exam.ExamPaperEditRequestVM;
import com.mindskip.xzs.viewmodel.admin.exam.ExamPaperPageRequestVM;
import com.mindskip.xzs.viewmodel.admin.exam.ExamPaperTitleItemVM;
import com.mindskip.xzs.viewmodel.admin.exam.QuestionTypeVM;
import com.mindskip.xzs.viewmodel.admin.exam.*;
import com.mindskip.xzs.viewmodel.admin.question.ExamQuestionVO;
import com.mindskip.xzs.viewmodel.admin.question.QuestionEditRequestVM;
import com.mindskip.xzs.viewmodel.student.dashboard.PaperFilter;
import com.mindskip.xzs.viewmodel.student.dashboard.PaperInfo;
@@ -30,6 +30,7 @@
import com.mindskip.xzs.domain.Question;
import com.mindskip.xzs.domain.User;
import org.modelmapper.ModelMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@@ -52,9 +53,13 @@
    private final QuestionSubjectService questionSubjectService;
    private final ExamPaperUserService examPaperUserService;
    private final UserService userService;
    private final UserDepartmentMapper userDepartmentMapper;
    private final DepartmentService departmentService;
    @Autowired
    public ExamPaperServiceImpl(ExamPaperMapper examPaperMapper, QuestionMapper questionMapper, TextContentService textContentService, QuestionService questionService, SubjectService subjectService, ExamPaperDepartmentService examPaperDepartmentService, ExamPaperSubjectService examPaperSubjectService, QuestionSubjectService questionSubjectService, ExamPaperUserService examPaperUserService, UserService userService) {
    public ExamPaperServiceImpl(ExamPaperMapper examPaperMapper, QuestionMapper questionMapper, TextContentService textContentService, QuestionService questionService, SubjectService subjectService, ExamPaperDepartmentService examPaperDepartmentService, ExamPaperSubjectService examPaperSubjectService, QuestionSubjectService questionSubjectService, ExamPaperUserService examPaperUserService, UserService userService, UserDepartmentMapper userDepartmentMapper, DepartmentService departmentService) {
        super(examPaperMapper);
        this.examPaperMapper = examPaperMapper;
        this.questionMapper = questionMapper;
@@ -66,6 +71,8 @@
        this.questionSubjectService = questionSubjectService;
        this.examPaperUserService = examPaperUserService;
        this.userService = userService;
        this.userDepartmentMapper = userDepartmentMapper;
        this.departmentService = departmentService;
    }
@@ -120,6 +127,7 @@
            examPaper.setCreateTime(now);
            examPaper.setCreateUser(user.getId());
            examPaper.setDeleted(false);
            examPaper.setUserIds(examPaperEditRequestVM.getMenuIds());
            examPaperFromVM(examPaperEditRequestVM, examPaper, titleItemsVM);
            examPaperMapper.insertSelective(examPaper);
@@ -142,10 +150,11 @@
    }
    @Override
    public ExamPaperEditRequestVM examPaperToVM(Integer id) {
    public ExamPaperEditRequestVO examPaperToVM(Integer id) {
        ExamPaper examPaper = examPaperMapper.selectByPrimaryKey(id);
        ExamPaperEditRequestVM vm = modelMapper.map(examPaper, ExamPaperEditRequestVM.class);
        ExamPaperEditRequestVO vm = modelMapper.map(examPaper, ExamPaperEditRequestVO.class);
        vm.setLevel(examPaper.getGradeLevel());
        vm.setMenuIds(examPaper.getUserIds());
        TextContent frameTextContent = textContentService.selectById(examPaper.getFrameTextContentId());
        List<ExamPaperTitleItemObject> examPaperTitleItemObjects = JsonUtil.toJsonListObject(frameTextContent.getContent(), ExamPaperTitleItemObject.class);
        List<Integer> questionIds = examPaperTitleItemObjects.stream()
@@ -162,11 +171,12 @@
        Integer order = 0;
        Set<Integer> generatedNumbers = new HashSet<>();
        Random random = new Random();
        List<ExamPaperTitleItemVM> examPaperTitleItemVMS = examPaperTitleItemObjects.stream().map(t -> {
            ExamPaperTitleItemVM tTitleVM = modelMapper.map(t, ExamPaperTitleItemVM.class);
            List<QuestionEditRequestVM> questionItemsVM = t.getQuestionItems().stream().map(i -> {
        List<ExamPaperTitleItemVO> examPaperTitleItemVMS = examPaperTitleItemObjects.stream().map(t -> {
            ExamPaperTitleItemVO tTitleVM = new ExamPaperTitleItemVO();
            BeanUtils.copyProperties(t, tTitleVM);
            List<ExamQuestionVO> questionItemsVM = t.getQuestionItems().stream().map(i -> {
                        Question question = questions.stream().filter(q -> q.getId().equals(i.getId())).findFirst().get();
                        QuestionEditRequestVM questionEditRequestVM = questionService.getQuestionEditRequestVM(question);
                        ExamQuestionVO questionEditRequestVM = questionService.getQuestionEditRequestVM(question);
                        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)),
@@ -174,7 +184,7 @@
//                        questionEditRequestVM.setItemOrder(getRandomNumber(t.getQuestionItems().size() - 1, generatedNumbers, random));
                        return questionEditRequestVM;
                    }).sorted(Comparator.comparing(QuestionEditRequestVM::getItemOrder))
                    }).sorted(Comparator.comparing(ExamQuestionVO::getItemOrder))
                    .collect(Collectors.toList());
            tTitleVM.setQuestionItems(questionItemsVM);
            return tTitleVM;
@@ -232,6 +242,22 @@
    @Override
    public List<ExamPaper> gets(Integer[] ids) {
        return examPaperMapper.gets(ids);
    }
    @Override
    public List<PaperExcelVO> getPaperExcelById(Integer id) {
        List<PaperExcelVO> paperExcel = examPaperMapper.getPaperExcelById(id);
        paperExcel = paperExcel.stream().map(e->{
            e.setPaperScore(ExamUtil.scoreToVM(Integer.parseInt(e.getPaperScore())));
            e.setUserScore(ExamUtil.scoreToVM(Integer.parseInt(e.getUserScore())));
            List<UserDepartment> userDepartments = userDepartmentMapper.selectByUserId(Integer.parseInt(e.getUserId()));
            if(userDepartments.size() != 0){
                Department byId = departmentService.getById(userDepartments.get(0).getDepartmentId());
                e.setDepartmentName(byId.getName());
            }
            return e;
        }).collect(Collectors.toList());
        return paperExcel;
    }
    private void examPaperFromVM(ExamPaperEditRequestVM examPaperEditRequestVM, ExamPaper examPaper, List<ExamPaperTitleItemVM> titleItemsVM) {
@@ -334,6 +360,7 @@
        int remainingScore = totalScore;
        for (int i = 0; i < quantity - 1; i++) {
            // 如果题数量超过了题库数量,报错
            if (i >= scoresMap.size()) {
                throw new QuestionException();
            }
@@ -348,7 +375,6 @@
                keys.remove(index);
            }
        }
        if (!values.isEmpty()) {
            int index = random.nextInt(values.size());
            int score = values.get(index);
@@ -364,7 +390,7 @@
     * @param frameTextContentList
     * @param titleItemsVM           title
     */
    private void randomQuestion(ExamPaperEditRequestVM examPaperEditRequestVM, List<ExamPaperTitleItemObject> frameTextContentList, List<ExamPaperTitleItemVM> titleItemsVM) throws QuestionException {
    private void randomQuesrandomQuestiontion(ExamPaperEditRequestVM examPaperEditRequestVM, List<ExamPaperTitleItemObject> frameTextContentList, List<ExamPaperTitleItemVM> titleItemsVM) throws QuestionException {
        Map<Integer, Integer> subjectSource = examPaperEditRequestVM.getSubjectSource();
        //总数量
        Integer sum = subjectSource.values().stream().mapToInt(Integer::intValue).sum();
@@ -457,13 +483,17 @@
     * @param titleItemsVM           title
     */
    private void randomQuestionType(ExamPaperEditRequestVM examPaperEditRequestVM, List<ExamPaperTitleItemObject> frameTextContentList, List<ExamPaperTitleItemVM> titleItemsVM) throws QuestionException {
        //单选
        Integer singleChoice = examPaperEditRequestVM.getQuestionTypeVMS().stream().mapToInt(QuestionTypeVM::getSingleChoice).sum();
        //多选
        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) != new Integer(20)) {
        if ((singleChoice * 2 + multipleChoice * 2 + judgment * 2) != new Integer(100)) {
            throw new QuestionException();
        }
@@ -491,15 +521,15 @@
            Map<Integer, Integer> multipleMap = list.stream()
                    .filter(e -> e.getQuestionType().equals(QuestionTypeEnum.MultipleChoice.getCode()))
                    .collect(Collectors.toMap(Question::getId, Question::getScore));
            Integer multipleSource = questionTypeVM.getMultipleChoice() * 5;
            Integer multipleSource = questionTypeVM.getMultipleChoice() * 2;
            selectRandomScores(multiple, multipleMap, questionTypeVM.getMultipleChoice(), multipleSource);
            //判断
            Map<Integer, Integer> judgmentMap = list.stream()
                    .filter(e -> e.getQuestionType().equals(QuestionTypeEnum.TrueFalse.getCode()))
                    .collect(Collectors.toMap(Question::getId, Question::getScore));
            Integer trueFalse = questionTypeVM.getTrueFalse() * 2;
            List<Question> collect1 = list.stream().filter(e -> e.getQuestionType().equals(QuestionTypeEnum.TrueFalse.getCode())).collect(Collectors.toList());
            Map<Integer, Integer> judgmentMap = collect1.stream().collect(Collectors.toMap(Question::getId, Question::getScore));
            Integer trueFalse1 = questionTypeVM.getTrueFalse();
            Integer trueFalse = trueFalse1 * 2;
            selectRandomScores(multiple, judgmentMap, questionTypeVM.getTrueFalse(), trueFalse);
            //单选分数