package com.ruoyi.web.service; import com.alibaba.fastjson2.util.DateUtils; import com.google.common.collect.Lists; import com.google.common.collect.Sets; import com.ruoyi.common.utils.SecurityUtils; import com.ruoyi.enums.ExamineeStatus; import com.ruoyi.enums.PaperStatus; import com.ruoyi.enums.PaperType; import com.ruoyi.ie.domain.AMarjorPlan; import com.ruoyi.ie.service.IAMarjorPlanService; import com.ruoyi.learn.domain.*; import com.ruoyi.learn.mapper.LearnExamineeMapper; import com.ruoyi.learn.mapper.LearnKnowledgeTreeMapper; import com.ruoyi.learn.mapper.LearnPaperMapper; import com.ruoyi.learn.service.ILearnPaperService; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import javax.validation.ValidationException; import java.util.Calendar; import java.util.Date; import java.util.List; import java.util.Set; /** * 考试服务 */ @Service public class ExamService { private Set paperTypeSet = Sets.newHashSet(PaperType.Real, PaperType.Custom, PaperType.Test); private final LearnPaperMapper paperMapper; private final LearnKnowledgeTreeMapper knowledgeTreeMapper; private final LearnExamineeMapper examineeMapper; private final ILearnPaperService learnPaperService; private final PaperService paperService; private final IAMarjorPlanService marjorPlanService; public ExamService(LearnPaperMapper paperMapper, LearnKnowledgeTreeMapper knowledgeTreeMapper, LearnExamineeMapper examineeMapper, ILearnPaperService learnPaperService, PaperService paperService, IAMarjorPlanService marjorPlanService) { this.paperMapper = paperMapper; this.knowledgeTreeMapper = knowledgeTreeMapper; this.examineeMapper = examineeMapper; this.learnPaperService = learnPaperService; this.paperService = paperService; this.marjorPlanService = marjorPlanService; } /** * 开卷 (卷, 实时组卷) * @return */ @Transactional(rollbackFor = Exception.class) public AnswerSheet openExaminee(PaperType paperType, Long relatedId) { if(paperTypeSet.contains(paperType)) { return openExaminee(paperType, relatedId, SecurityUtils.getUserId()); } else if(PaperType.Practice.equals(paperType)) { // TODO 先连接学生的定向信息 return openExaminee(relatedId, SecurityUtils.getUserId(), ""); } else if(PaperType.Simulated.equals(paperType)) { Integer cnt = 3; return openExaminee(SecurityUtils.getUserId(), cnt); } throw new RuntimeException("错误类型: " + paperType); } /** * 临时保存 */ public void updateAnswerSheet(AnswerSheet answer) { } /** * 交卷 * @param answer */ public void commitAnswerSheet(AnswerSheet answer) { } private AnswerSheet openExaminee(PaperType paperType, Long paperId, Long studentId) { LearnPaper paper = paperMapper.selectLearnPaperById(paperId); LearnExaminee learnExaminee = new LearnExaminee(); learnExaminee.setStudentId(studentId); learnExaminee.setPaperId(paperId); learnExaminee.setPaperType(paperType.getVal()); learnExaminee.setState(ExamineeStatus.Exam.getVal()); List examineeList = examineeMapper.selectLearnExamineeList(learnExaminee); if (CollectionUtils.isNotEmpty(examineeList)) { learnExaminee = examineeList.get(0); } else { learnExaminee.setState(ExamineeStatus.Exam.getVal()); learnExaminee.setBeginTime(new Date()); examineeMapper.insertLearnExaminee(learnExaminee); } return buildAnswerSheet(paper, learnExaminee); } private AnswerSheet openExaminee(Long planId, Integer maxCount) { LearnExaminee examinee = new LearnExaminee(); examinee.setStudentId(SecurityUtils.getLoginUser().getUser().getUserId()); examinee.setPaperType(PaperType.Simulated.getVal()); List examineeList = examineeMapper.selectLearnExamineeList(examinee); Set existPaperIdSet = Sets.newHashSet(); for(LearnExaminee e : examineeList) { if(ExamineeStatus.Exam.getVal().equals(e.getState())) { return buildAnswerSheet(learnPaperService.selectLearnPaperById(e.getPaperId()), e); } existPaperIdSet.add(e.getPaperId()); } if(existPaperIdSet.size() >= maxCount) { throw new ValidationException("超过最大次数限制" + maxCount ); } AMarjorPlan plan = marjorPlanService.selectAMarjorPlanById(planId); if(null == plan) { throw new ValidationException("专业id无效"); } LearnPaper paper = getBestPaper(plan, existPaperIdSet); examinee.setPaperKey(PaperType.Simulated.name() + "_" + paper.getId()); examinee.setState(ExamineeStatus.Exam.getVal()); examinee.setBeginTime(new Date()); examineeMapper.insertLearnExaminee(examinee); return buildAnswerSheet(paper, examinee); } /** * 根据知识点生成一次性练习卷并开始做题 * @return */ private AnswerSheet openExaminee(Long knowledgeId, Long studentId, String directKey) { LearnKnowledgeTree knowledgeTree = knowledgeTreeMapper.selectLearnKnowledgeTreeById(knowledgeId); LearnPaper paper = new LearnPaper(); paper.setPaperType(PaperType.Practice.name()); paper.setRelateId(knowledgeId); paper.setYear(Calendar.getInstance().get(Calendar.YEAR)); paper.setStatus(PaperStatus.TmpValid.getVal()); paper.setDirectKey(StringUtils.trimToEmpty(directKey)); List paperList = paperMapper.selectLearnPaperList(paper); if (CollectionUtils.isNotEmpty(paperList)) { // 有未做完的 LearnPaper existPaper = paperList.get(0); LearnExaminee learnExaminee = new LearnExaminee(); learnExaminee.setStudentId(studentId); learnExaminee.setPaperId(existPaper.getId()); learnExaminee.setPaperType(PaperType.Practice.getVal()); List examineeList = examineeMapper.selectLearnExamineeList(learnExaminee); if (CollectionUtils.isNotEmpty(examineeList)) { LearnExaminee examinee = examineeList.get(0); return buildAnswerSheet(existPaper, examinee); } // 关闭试卷 LearnPaper up = new LearnPaper(); up.setId(learnExaminee.getPaperId()); up.setStatus(PaperStatus.Valid.getVal()); paperMapper.updateLearnPaper(up); } TestPaperVO.PaperDef paperDef = new TestPaperVO.PaperDef(); paperDef.setTotal(15L); paperDef.setFillExclude(true); paperDef.setKnowIds(Lists.newArrayList(knowledgeId)); List typeDefList= Lists.newArrayList(); typeDefList.add(new TestPaperVO.TypeDef("单选题", "单选题", 80, 1)); typeDefList.add(new TestPaperVO.TypeDef("判断题", "判断题", 10, 2)); paperDef.setTypes(typeDefList); List pqList = paperService.getQuestions(studentId, paperDef); paper.setPaperSource(0); paper.setSubjectId(knowledgeTree.getSubjectId()); paper.setPaperName(studentId + "-" + knowledgeTree.getName() + "-" + DateUtils.format(new Date(), "yyyyMMddHHmmss")); paperService.savePaper(paper, pqList); LearnExaminee learnExaminee = new LearnExaminee(); learnExaminee.setStudentId(studentId); learnExaminee.setPaperType(PaperType.Practice.getVal()); learnExaminee.setPaperId(paper.getId()); learnExaminee.setState(ExamineeStatus.Exam.getVal()); learnExaminee.setBeginTime(new Date()); examineeMapper.insertLearnExaminee(learnExaminee); return buildAnswerSheet(paper, learnExaminee); } private LearnPaper getBestPaper(AMarjorPlan plan, Set existPaperIdSet) { String groupName = StringUtils.trimToEmpty(plan.getMajorGroup()); LearnPaper paperCond = new LearnPaper(); paperCond.setPaperType(PaperType.Simulated.name()); for(int i = 3; i>0; i--) { if(i == 3) { paperCond.setDirectKey(plan.getUniversityId() + "_" + groupName + "_" + plan.getMajorName()); } else if(i == 2) { paperCond.setDirectKey(plan.getUniversityId() + "_" + groupName); } else if(StringUtils.isBlank(groupName)) { break; } List paperList = learnPaperService.selectLearnPaperList(paperCond); for(LearnPaper paper : paperList) { if(existPaperIdSet.add(paper.getId())) { return paper; } } } throw new ValidationException("未初始化院校定向模拟题库: " + plan.getId()); } private AnswerSheet buildAnswerSheet(LearnPaper paper, LearnExaminee examinee) { AnswerSheet answerSheet = new AnswerSheet(); answerSheet.setExamineeId(examinee.getExamineeId()); answerSheet.setPaperId(examinee.getPaperId()); answerSheet.setName(paper.getPaperName()); answerSheet.setBeginTime(examinee.getBeginTime()); answerSheet.setScoringType("1"); answerSheet.setMode(0L); answerSheet.setState(examinee.getState()); answerSheet.setAllowAnswer(true); answerSheet.setAllowScore(false); return answerSheet; } }