kongzy
2024-09-14 f0f00e9ba8a755e4317e029d73b69a92ad9f9df1
exam-system/src/main/java/com/gkhy/exam/system/service/impl/ExExamPaperServiceImpl.java
@@ -14,20 +14,15 @@
import com.gkhy.exam.system.domain.ExExamPaper;
import com.gkhy.exam.system.domain.ExPaperQuestion;
import com.gkhy.exam.system.domain.ExQuestion;
import com.gkhy.exam.system.mapper.ExExamPaperMapper;
import com.gkhy.exam.system.mapper.ExPaperQuestionMapper;
import com.gkhy.exam.system.mapper.ExPaperStudentMapper;
import com.gkhy.exam.system.mapper.ExQuestionMapper;
import com.gkhy.exam.system.domain.ExQuestionBank;
import com.gkhy.exam.system.mapper.*;
import com.gkhy.exam.system.service.ExExamPaperService;
import com.gkhy.exam.system.utils.SequenceUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Random;
import java.util.*;
import java.util.stream.Collectors;
/**
@@ -47,6 +42,8 @@
    private ExPaperQuestionMapper paperQuestionMapper;
    @Autowired
    private ExQuestionMapper questionMapper;
    @Autowired
    private ExQuestionBankMapper questionBankMapper;
    @Autowired
    private SequenceUtils sequenceUtils;
@@ -65,6 +62,27 @@
    @Override
    public ExExamPaper selectExamPaperById(Long paperId) {
        ExExamPaper examPaper= baseMapper.selectExamPaperById(paperId);
        //获取题库名称
        List<Long> bankIds=new ArrayList<>();
        bankIds.add(examPaper.getSingleBankId());
        bankIds.add(examPaper.getMultiBankId());
        bankIds.add(examPaper.getJudgeBankId());
        bankIds.add(examPaper.getEasyBankId());
        List<ExQuestionBank> questionBanks=questionBankMapper.selectQuestionBankByIds(bankIds);
        questionBanks.forEach(item -> {
            if(Objects.equals(item.getId(), examPaper.getSingleBankId())){
                examPaper.setSingleBankName(item.getName());
            }
            if(Objects.equals(item.getId(), examPaper.getMultiBankId())){
                examPaper.setMultiBankName(item.getName());
            }
            if(Objects.equals(item.getId(), examPaper.getJudgeBankId())){
                examPaper.setJudgeBankName(item.getName());
            }
            if(Objects.equals(item.getId(), examPaper.getEasyBankId())){
                examPaper.setEasyBankName(item.getName());
            }
        });
        SysUser currentUser= SecurityUtils.getLoginUser().getUser();
        if(currentUser.getUserType().equals(UserTypeEnum.SYSTEM_USER.getCode())){
            return examPaper;
@@ -79,6 +97,7 @@
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public int insertExamPaper(ExExamPaper examPaper) {
        validatedData(examPaper);
        checkUserAllowed(examPaper);
        if(!checkNameUnique(examPaper)){
            throw new ApiException("试卷名称已存在");
@@ -87,16 +106,27 @@
        examPaper.setCode(sequenceUtils.getNextSequence(CodeTypeEnum.EXAM_PAPER.getCode()));
        examPaper.setCreateBy(SecurityUtils.getUsername());
        if(examPaper.getLimitTime()>0){
            examPaper.setLimit(1);
            examPaper.setLimited(1);
        }else{
            examPaper.setLimited(0);
        }
        int row=baseMapper.insert(examPaper);
        if(row<1){
            throw new ApiException("新增试卷失败");
        }
        //分配试题
        assignSingleQuestion(examPaper);
        assignMultiQuestion(examPaper);
        assignJudgeQuestion(examPaper);
        if(Optional.ofNullable(examPaper.getSingleNum()).orElse(0)>0) {
            assignSingleQuestion(examPaper);
        }
        if(Optional.ofNullable(examPaper.getMultiNum()).orElse(0)>0) {
            assignMultiQuestion(examPaper);
        }
        if(Optional.ofNullable(examPaper.getJudgeNum()).orElse(0)>0) {
            assignJudgeQuestion(examPaper);
        }
        if(Optional.ofNullable(examPaper.getEasyNum()).orElse(0)>0) {
            assignEasyQuestion(examPaper);
        }
        return row;
    }
@@ -108,8 +138,37 @@
        if(currentUser.getUserType().equals(UserTypeEnum.STUDENT.getCode())){
            throw new ApiException("没有权限操作");
        }
        if(!currentUser.getCompanyId().equals(examPaper.getCompanyId())){
        if(examPaper.getCompanyId()!=null&&!currentUser.getCompanyId().equals(examPaper.getCompanyId())){
            throw new ApiException("没有权限操作其他企业试卷");
        }
    }
    public void validatedData(ExExamPaper examPaper){
        if(Optional.ofNullable(examPaper.getSingleNum()).orElse(0)<=0
        &&Optional.ofNullable(examPaper.getMultiNum()).orElse(0)<=0
        &&Optional.ofNullable(examPaper.getJudgeNum()).orElse(0)<=0
        &&Optional.ofNullable(examPaper.getEasyNum()).orElse(0)<=0){
            throw new ApiException("试卷题目数量不能为空");
        }
        if(Optional.ofNullable(examPaper.getSingleNum()).orElse(0)>0){
            if(Optional.ofNullable(examPaper.getSingleScore()).orElse(0)<=0 || examPaper.getSingleMethod()==null||examPaper.getSingleBankId()==null){
                throw new ApiException("单选题参数错误");
            }
        }
        if(Optional.ofNullable(examPaper.getMultiNum()).orElse(0)>0){
            if(Optional.ofNullable(examPaper.getMultiScore()).orElse(0)<=0 || examPaper.getMultiMethod()==null||examPaper.getMultiBankId()==null){
                throw new ApiException("多选题参数错误");
            }
        }
        if(Optional.ofNullable(examPaper.getJudgeNum()).orElse(0)>0){
            if(Optional.ofNullable(examPaper.getJudgeScore()).orElse(0)<=0 || examPaper.getJudgeMethod()==null||examPaper.getJudgeBankId()==null){
                throw new ApiException("判断题参数错误");
            }
        }
        if(Optional.ofNullable(examPaper.getEasyNum()).orElse(0)>0){
            if(Optional.ofNullable(examPaper.getEasyScore()).orElse(0)<=0 || examPaper.getEasyMethod()==null||examPaper.getEasyBankId()==null){
                throw new ApiException("简答题参数错误");
            }
        }
    }
@@ -134,20 +193,27 @@
        paperQuestionMapper.batchInsert(paperQuestionList);
    }
    //分配简答题
    public void assignEasyQuestion(ExExamPaper examPaper){
        List<ExPaperQuestion> paperQuestionList = getPaperQuestions(examPaper.getId(), examPaper.getEasyBankId(),
                examPaper.getEasyMethod(),examPaper.getEasyNum(), examPaper.getEasyScore(),QuestionTypeEnum.EASY);
        paperQuestionMapper.batchInsert(paperQuestionList);
    }
    private List<ExPaperQuestion> getPaperQuestions(Long paperId,Long bankId,Integer questionMethod,Integer questionCount,Integer questionScore, QuestionTypeEnum questionTypeEnum) {
        SysUser user=SecurityUtils.getLoginUser().getUser();
        Integer totalQuestionCount=questionMapper.selectCountByBankId(user.getCompanyId(), bankId, questionTypeEnum.getCode());
        SysUser currentUser=SecurityUtils.getLoginUser().getUser();
        Integer totalQuestionCount=questionMapper.selectCountByBankId(currentUser.getCompanyId(), bankId, questionTypeEnum.getCode());
        if(totalQuestionCount< questionCount){
            throw new ApiException(String.format("所选题库<%s>数量不足,无法分配",questionTypeEnum.getInfo()));
        }
        List<ExQuestion> questions=new ArrayList<>();
        if(Objects.equals(questionMethod, QuestionAssignEnum.RANDOM.getCode())){//随机分配
            questions=questionMapper.selectRandomQuestion(user.getCompanyId(), bankId,questionTypeEnum.getCode(), questionCount);
            questions=questionMapper.selectRandomQuestion(currentUser.getCompanyId(), bankId,questionTypeEnum.getCode(), questionCount);
        }else{//顺序分配
            int totalPage=questionCount/ questionMethod;//不能整除,忽略最后一页
            int totalPage=totalQuestionCount/questionCount;//不能整除,忽略最后一页
            Random random=new Random();
            int startIndex=random.nextInt(totalPage)+1;
            questions=questionMapper.selectQuestionWithLimit(user.getCompanyId(), bankId,questionTypeEnum.getCode(),startIndex, questionCount);
            questions=questionMapper.selectQuestionWithLimit(currentUser.getCompanyId(), bankId,questionTypeEnum.getCode(),startIndex, questionCount);
        }
        List<ExPaperQuestion> paperQuestionList=questions.stream().map(item -> {
            ExPaperQuestion exPaperQuestion=new ExPaperQuestion();
@@ -162,6 +228,8 @@
    @Override
    public int updateExamPaper(ExExamPaper examPaper) {
        validatedData(examPaper);
        checkUserAllowed(examPaper);
        if(!checkNameUnique(examPaper)){
            throw new ApiException("试卷名称已存在");
        }
@@ -170,8 +238,11 @@
            throw new ApiException("该试卷下已分配学员,不能编辑");
        }
        examPaper.setCode(null);//编号不能修改
        if(examPaper.getLimitTime()==0){
            examPaper.setLimit(0);
        if(examPaper.getLimitTime()>0){
            examPaper.setLimited(1);
        }else{
            examPaper.setLimited(0);
            examPaper.setLimitTime(0);
        }
        int row=baseMapper.updateById(examPaper);
        if(row<1){
@@ -190,6 +261,10 @@
           deletePaperQuestion(examPaper.getId(),QuestionTypeEnum.JUDGE);
           assignJudgeQuestion(examPaper);
        }
        if(examPaper.getEasyRebuild()==1) {
            deletePaperQuestion(examPaper.getId(),QuestionTypeEnum.EASY);
            assignEasyQuestion(examPaper);
        }
        return row;
    }
@@ -199,16 +274,17 @@
    @Override
    public int deleteExamPaperById(Long paperId) {
        checkUserAllowed(baseMapper.selectById(paperId));
        //查询该试卷分配的学员人数
        if(checkPaperHasStudent(paperId)){
            throw new ApiException("该试卷下已分配学员,不能删除");
        }
        int row=baseMapper.deleteById(paperId);
        int row=baseMapper.deletePaperById(paperId);
        if(row<1){
            throw new ApiException("删除试卷失败");
        }
        //删除考卷试题
        paperQuestionMapper.deletebyPapaerId(paperId);
     //   paperQuestionMapper.deletebyPapaerId(paperId);
        return row;
    }
@@ -223,8 +299,9 @@
    @Override
    public boolean checkNameUnique(ExExamPaper examPaper) {
        SysUser currentUser=SecurityUtils.getLoginUser().getUser();
        Long paperId=examPaper.getId()==null?-1L:examPaper.getId();
        ExExamPaper paper= baseMapper.checkNameUnique(examPaper.getName());
        ExExamPaper paper= baseMapper.checkNameUnique(examPaper.getName(),currentUser.getCompanyId());
        if(paper!=null&&paper.getId().longValue()!=paperId.longValue()){
            return UserConstant.NOT_UNIQUE;
        }
@@ -233,9 +310,7 @@
    @Override
    public int changeExamPaperStatus(Long paperId, Integer status) {
        checkUserAllowed(baseMapper.selectById(paperId));
        return baseMapper.updateById(new ExExamPaper().setId(paperId).setStatus(status).setUpdateBy(SecurityUtils.getUsername()));
    }
}