| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218 |
- 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<PaperType> 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<LearnExaminee> 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<LearnExaminee> examineeList = examineeMapper.selectLearnExamineeList(examinee);
- Set<Long> 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<LearnPaper> 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<LearnExaminee> 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<TestPaperVO.TypeDef> typeDefList= Lists.newArrayList();
- typeDefList.add(new TestPaperVO.TypeDef("单选题", "单选题", 80, 1));
- typeDefList.add(new TestPaperVO.TypeDef("判断题", "判断题", 10, 2));
- paperDef.setTypes(typeDefList);
- List<LearnPaperQuestion> 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<Long> 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<LearnPaper> 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;
- }
- }
|