xiangpei
2025-05-14 47cd9ecc0eff38ffe6b3b794b2bf197e958f4403
src/main/java/com/mindskip/xzs/service/impl/ExamPaperServiceImpl.java
@@ -1,37 +1,36 @@
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.ExamTemplatesVO;
import com.mindskip.xzs.domain.vo.PaperExcelVO;
import com.mindskip.xzs.domain.vo.UserVO;
import com.mindskip.xzs.repository.ExamPaperAnswerMapper;
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.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;
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.apache.commons.lang3.ObjectUtils;
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;
@@ -45,6 +44,7 @@
    protected final static ModelMapper modelMapper = ModelMapperSingle.Instance();
    private final ExamPaperMapper examPaperMapper;
    private final ExamPaperAnswerMapper examPaperAnswerMapper;
    private final QuestionMapper questionMapper;
    private final TextContentService textContentService;
    private final QuestionService questionService;
@@ -60,9 +60,10 @@
    @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) {
    public ExamPaperServiceImpl(ExamPaperMapper examPaperMapper, ExamPaperAnswerMapper examPaperAnswerMapper, 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.examPaperAnswerMapper = examPaperAnswerMapper;
        this.questionMapper = questionMapper;
        this.textContentService = textContentService;
        this.questionService = questionService;
@@ -120,6 +121,7 @@
        String frameTextContentStr = JsonUtil.toJsonStr(frameTextContentList);
        ExamPaper examPaper;
        Integer[] userIds = examPaperEditRequestVM.getUserIds();
        if (actionEnum == ActionEnum.ADD) {
            examPaper = modelMapper.map(examPaperEditRequestVM, ExamPaper.class);
            TextContent frameTextContent = new TextContent(frameTextContentStr, now);
@@ -137,7 +139,10 @@
            TextContent frameTextContent = textContentService.selectById(examPaper.getFrameTextContentId());
            frameTextContent.setContent(frameTextContentStr);
            textContentService.updateByIdFilter(frameTextContent);
            examPaperEditRequestVM.setScore(null);
            examPaperEditRequestVM.setUserIds(null);
            modelMapper.map(examPaperEditRequestVM, examPaper);
            examPaperEditRequestVM.setUserIds(userIds);
            examPaperFromVM(examPaperEditRequestVM, examPaper, titleItemsVM);
            examPaperMapper.updateByPrimaryKeySelective(examPaper);
            //批量修改
@@ -151,9 +156,12 @@
    }
    @Override
    public ExamPaperEditRequestVM examPaperToVM(Integer id) {
    public ExamPaperEditRequestVO examPaperToVM(Integer id) {
        ExamPaper examPaper = examPaperMapper.selectByPrimaryKey(id);
        ExamPaperEditRequestVM vm = modelMapper.map(examPaper, ExamPaperEditRequestVM.class);
        if (Objects.isNull(examPaper)) {
            throw new RuntimeException("该考试已被删除,无法查看");
        }
        ExamPaperEditRequestVO vm = modelMapper.map(examPaper, ExamPaperEditRequestVO.class);
        vm.setLevel(examPaper.getGradeLevel());
        vm.setMenuIds(examPaper.getUserIds());
        TextContent frameTextContent = textContentService.selectById(examPaper.getFrameTextContentId());
@@ -162,7 +170,11 @@
                .flatMap(t -> t.getQuestionItems().stream()
                        .map(q -> q.getId()))
                .collect(Collectors.toList());
        if (ObjectUtils.isEmpty(questionIds)) {
            throw new RuntimeException("该试卷没有题目");
        }
        List<Question> questions = questionMapper.selectByIds(questionIds);
        //单选数量
        Integer singleChoice = questions.stream().filter(e -> e.getQuestionType() == 1).collect(Collectors.toList()).size();
        //多选数量
@@ -172,11 +184,13 @@
        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)),
@@ -184,7 +198,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;
@@ -202,12 +216,18 @@
        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];
        List<Integer> userIds = new ArrayList();
        List<String> userNames = new ArrayList();
        for (int i = 0; i < examPaperUsers.size(); i++) {
            Integer[] userId = {userService.getUserById(examPaperUsers.get(i).getUserId()).getUserLevel(), examPaperUsers.get(i).getUserId()};
            userIds[i] = userId;
            User user = userService.getUserById(examPaperUsers.get(i).getUserId());
            if (ObjectUtils.isNotEmpty(user)) {
                Integer userId = examPaperUsers.get(i).getUserId();
                userIds.add(userId);
                userNames.add(user.getRealName());
            }
        }
        vm.setUserId(userIds);
        vm.setUserIds(userIds);
        vm.setUserNames(userNames);
        return vm;
    }
@@ -218,8 +238,8 @@
    @Override
    public Integer selectAllCount() {
        return examPaperMapper.selectAllCount();
    public Integer selectAllCount(List<Integer> deptIds) {
        return examPaperMapper.selectAllCount(deptIds);
    }
    @Override
@@ -254,10 +274,36 @@
            if(userDepartments.size() != 0){
                Department byId = departmentService.getById(userDepartments.get(0).getDepartmentId());
                e.setDepartmentName(byId.getName());
                e.setDeptId(byId.getId());
            }
            return e;
        }).collect(Collectors.toList());
        return paperExcel;
    }
    @Override
    public List<PaperExcelVO> getRandomPaperExcelById(Integer id) {
        List<PaperExcelVO> paperExcel = examPaperMapper.getRandomPaperExcelById(id);
        // 如果某考生存在多个部门,每个部门都加一遍数据
        List<PaperExcelVO> needAdd = new ArrayList<>();
        List<PaperExcelVO> needRemove = new ArrayList<>();
        paperExcel = paperExcel.stream().map(e->{
            e.setPaperScore(ExamUtil.scoreToVM(Integer.parseInt(e.getPaperScore())));
            e.setUserScore(ExamUtil.scoreToVM(Integer.parseInt(e.getUserScore())));
            List<Department> userDepartments = userDepartmentMapper.selectDeptByUserId(Integer.parseInt(e.getUserId()));
            for (Department dept : userDepartments) {
                PaperExcelVO vo = new PaperExcelVO();
                BeanUtils.copyProperties(e, vo);
                vo.setDepartmentName(dept.getName());
                vo.setDeptId(dept.getId());
                needAdd.add(vo);
                needRemove.add(e);
            }
            return e;
        }).collect(Collectors.toList());
        paperExcel.removeAll(needRemove);
        paperExcel.addAll(needAdd);
        return paperExcel.stream().filter(e -> Objects.nonNull(e.getDeptId())).collect(Collectors.toList());
    }
    private void examPaperFromVM(ExamPaperEditRequestVM examPaperEditRequestVM, ExamPaper examPaper, List<ExamPaperTitleItemVM> titleItemsVM) {
@@ -360,6 +406,7 @@
        int remainingScore = totalScore;
        for (int i = 0; i < quantity - 1; i++) {
            // 如果题数量超过了题库数量,报错
            if (i >= scoresMap.size()) {
                throw new QuestionException();
            }
@@ -492,7 +539,7 @@
        Integer judgment = examPaperEditRequestVM.getQuestionTypeVMS().stream().mapToInt(QuestionTypeVM::getTrueFalse).sum();
        if ((singleChoice * 2 + multipleChoice * 2 + judgment * 2) != new Integer(10)) {
        if ((singleChoice * 2 + multipleChoice * 2 + judgment * 2) != new Integer(100)) {
            throw new QuestionException();
        }
@@ -511,21 +558,22 @@
        Integer order = 0;
        for (QuestionTypeVM questionTypeVM : questionTypeVMList) {
            List<Integer> questions = questionSubjectService.getSubject(questionTypeVM.getSubjectId())
            List<Integer> questions = questionSubjectService.getSubjectBySubjectIds(examPaperEditRequestVM.getSubjectId())
                    .stream().map(QuestionSubject::getQuestionId).collect(Collectors.toList());
            List<Question> list = questionService.selectByIds(questions);
            // List<Question> list = questionService.getAll();
            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;
@@ -535,7 +583,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);
@@ -598,4 +646,54 @@
        return randomNumber;
    }
    @Override
    @Transactional
    public void missExam(ExamPaperEditRequestVM model) {
        // 修改原来的试卷时间
        if (ObjectUtils.isNotEmpty(model.getLimitDateTime())) {
            ExamPaper examPaper = new ExamPaper();
            examPaper.setId(model.getExamPaperId());
            examPaper.setLimitStartTime(DateTimeUtil.parse(model.getLimitDateTime().get(0), DateTimeUtil.STANDER_FORMAT));
            examPaper.setLimitEndTime(DateTimeUtil.parse(model.getLimitDateTime().get(1), DateTimeUtil.STANDER_FORMAT));
            examPaperMapper.updateByPrimaryKeySelective(examPaper);
        }
        // 根据考试id将选择的补考考生的考试成绩设置为无效
        examPaperAnswerMapper.setMissExam(model);
        // 补考时将没有选择的并且没有参加过考试的考生增加一条零分考试记录,否则没有参加过考试的即使不被选择也可以参加补考
        examPaperAnswerMapper.insertDefault(model);
    }
    @Override
    @Transactional
    public void missExamByTemplateId(ExamTemplatesVO model) {
        List<Integer> id = examPaperMapper.getExamPaperByTemplateId(model);
        model.setPaperIds(id);
        // 根据考试id将选择的补考考生的考试成绩设置为无效
        examPaperAnswerMapper.setMissExamByTemplate(model);
    }
    @Override
    public List<ExamPaper> list(List<Integer> deptIds) {
        List<ExamPaper> list = examPaperMapper.list(deptIds);
        List<ExamPaper> template = examPaperMapper.template(deptIds);
        list.addAll(template);
        return list.stream().sorted(Comparator.comparing(ExamPaper::getCreateTime).reversed()).collect(Collectors.toList());
    }
    @Override
    public PageInfo<UserVO> selectStudent(UserVO userVO, List<Integer> adminDeptIds) {
        userVO.setDeptIds(ObjectUtils.isNotEmpty(userVO.getDeptIds()) ? userVO.getDeptIds() : adminDeptIds);
        return PageHelper.startPage(userVO.getPageIndex(), userVO.getPageSize()).doSelectPageInfo(() ->
                examPaperMapper.selectStudent(userVO));
    }
    @Override
    public List<ExamPaper> queryCondition() {
        List<ExamPaper> examPaperList = examPaperMapper.getExamPaper();
        List<ExamPaper> templateList = examPaperMapper.getTemplate();
        examPaperList.addAll(templateList);
        return examPaperList.stream().sorted(Comparator.comparing(ExamPaper::getCreateTime).reversed()).collect(Collectors.toList());
    }
}