xiangpei
2024-05-11 d470757bba19143fd3fc441365fcbb2362dfd0f0
src/main/java/com/mindskip/xzs/service/impl/ExamPaperServiceImpl.java
@@ -1,41 +1,41 @@
package com.mindskip.xzs.service.impl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mindskip.xzs.configuration.spring.exception.QuestionException;
import com.mindskip.xzs.domain.*;
import com.mindskip.xzs.domain.TextContent;
import com.mindskip.xzs.domain.enums.ExamPaperTypeEnum;
import com.mindskip.xzs.domain.enums.QuestionTypeEnum;
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.queue.ExamPaperTimeTask;
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.ExamUtil;
import com.mindskip.xzs.utility.JsonUtil;
import com.mindskip.xzs.utility.ModelMapperSingle;
import com.mindskip.xzs.utility.ExamUtil;
import com.mindskip.xzs.utility.minio.DateUtils;
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;
import com.mindskip.xzs.viewmodel.student.exam.ExamPaperPageVM;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mindskip.xzs.domain.ExamPaper;
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;
import javax.annotation.PostConstruct;
import java.util.*;
import java.util.concurrent.DelayQueue;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
@@ -55,8 +55,24 @@
    private final UserService userService;
    private final UserDepartmentMapper userDepartmentMapper;
    private final DepartmentService departmentService;
    private final DelayQueue<ExamPaperTimeTask> QUEUE = new DelayQueue<>();
    /**
     * 将进行中的定时试卷加入到队列中,并启动一个线程来轮询队列
     */
    @PostConstruct
    public void pollQueue() {
        examPaperMapper.selectTimeTaskPaper(DateUtils.getNowDate()).forEach(this::addTimeTask);
        new Thread(() -> {
            try {
                ExamPaperTimeTask task = QUEUE.take();
                saveMissExamUser(task.getExamPaperId());
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        }).start();
    }
    @Autowired
    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) {
@@ -519,14 +535,14 @@
            Map<Integer, Integer> multiple = new HashMap<>();
            //多选
            Map<Integer, Integer> multipleMap = list.stream()
                    .filter(e -> e.getQuestionType().equals(QuestionTypeEnum.MultipleChoice.getCode()))
                    .filter(e -> Objects.equals(QuestionTypeEnum.MultipleChoice.getCode(), e.getQuestionType()))
                    .collect(Collectors.toMap(Question::getId, Question::getScore));
            Integer multipleSource = questionTypeVM.getMultipleChoice() * 2;
            selectRandomScores(multiple, multipleMap, questionTypeVM.getMultipleChoice(), multipleSource);
            //判断
            List<Question> collect1 = list.stream().filter(e -> e.getQuestionType().equals(QuestionTypeEnum.TrueFalse.getCode())).collect(Collectors.toList());
            List<Question> collect1 = list.stream().filter(e -> Objects.equals(e.getQuestionType(), 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;
@@ -536,7 +552,7 @@
            Integer radioSource = questionTypeVM.getSingleChoice() * 4;
            //单选
            Map<Integer, Integer> radioMap = list.stream()
                    .filter(e -> e.getQuestionType().equals(QuestionTypeEnum.SingleChoice.getCode()))
                    .filter(e -> Objects.equals(e.getQuestionType(), QuestionTypeEnum.SingleChoice.getCode()))
                    .collect(Collectors.toMap(Question::getId, Question::getScore));
            selectRandomScores(multiple, radioMap, questionTypeVM.getSingleChoice(), radioSource);
@@ -599,4 +615,17 @@
        return randomNumber;
    }
    @Override
    public void addTimeTask(ExamPaper examPaper) {
        ExamPaperTimeTask examPaperTimeTask = new ExamPaperTimeTask();
        examPaperTimeTask.setExamPaperId(examPaper.getId());
        examPaperTimeTask.setExpiry(examPaper.getLimitEndTime().getTime());
        QUEUE.add(examPaperTimeTask);
    }
    @Override
    public void saveMissExamUser(long examPaperId) {
        examPaperMapper.saveMissExamUser(examPaperId);
    }
}