ExamService.java 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. package com.ruoyi.web.service;
  2. import com.alibaba.fastjson2.util.DateUtils;
  3. import com.google.common.collect.Lists;
  4. import com.google.common.collect.Sets;
  5. import com.ruoyi.common.utils.SecurityUtils;
  6. import com.ruoyi.enums.ExamineeStatus;
  7. import com.ruoyi.enums.PaperStatus;
  8. import com.ruoyi.enums.PaperType;
  9. import com.ruoyi.ie.domain.AMarjorPlan;
  10. import com.ruoyi.ie.service.IAMarjorPlanService;
  11. import com.ruoyi.learn.domain.*;
  12. import com.ruoyi.learn.mapper.LearnExamineeMapper;
  13. import com.ruoyi.learn.mapper.LearnKnowledgeTreeMapper;
  14. import com.ruoyi.learn.mapper.LearnPaperMapper;
  15. import com.ruoyi.learn.service.ILearnPaperService;
  16. import org.apache.commons.collections4.CollectionUtils;
  17. import org.apache.commons.lang3.StringUtils;
  18. import org.springframework.stereotype.Service;
  19. import org.springframework.transaction.annotation.Transactional;
  20. import javax.validation.ValidationException;
  21. import java.util.Calendar;
  22. import java.util.Date;
  23. import java.util.List;
  24. import java.util.Set;
  25. /**
  26. * 考试服务
  27. */
  28. @Service
  29. public class ExamService {
  30. private Set<PaperType> paperTypeSet = Sets.newHashSet(PaperType.Real, PaperType.Custom, PaperType.Test);
  31. private final LearnPaperMapper paperMapper;
  32. private final LearnKnowledgeTreeMapper knowledgeTreeMapper;
  33. private final LearnExamineeMapper examineeMapper;
  34. private final ILearnPaperService learnPaperService;
  35. private final PaperService paperService;
  36. private final IAMarjorPlanService marjorPlanService;
  37. public ExamService(LearnPaperMapper paperMapper, LearnKnowledgeTreeMapper knowledgeTreeMapper, LearnExamineeMapper examineeMapper, ILearnPaperService learnPaperService, PaperService paperService, IAMarjorPlanService marjorPlanService) {
  38. this.paperMapper = paperMapper;
  39. this.knowledgeTreeMapper = knowledgeTreeMapper;
  40. this.examineeMapper = examineeMapper;
  41. this.learnPaperService = learnPaperService;
  42. this.paperService = paperService;
  43. this.marjorPlanService = marjorPlanService;
  44. }
  45. /**
  46. * 开卷 (卷, 实时组卷)
  47. * @return
  48. */
  49. @Transactional(rollbackFor = Exception.class)
  50. public AnswerSheet openExaminee(PaperType paperType, Long relatedId) {
  51. if(paperTypeSet.contains(paperType)) {
  52. return openExaminee(paperType, relatedId, SecurityUtils.getUserId());
  53. } else if(PaperType.Practice.equals(paperType)) { // TODO 先连接学生的定向信息
  54. return openExaminee(relatedId, SecurityUtils.getUserId(), "");
  55. } else if(PaperType.Simulated.equals(paperType)) {
  56. Integer cnt = 3;
  57. return openExaminee(SecurityUtils.getUserId(), cnt);
  58. }
  59. throw new RuntimeException("错误类型: " + paperType);
  60. }
  61. /**
  62. * 临时保存
  63. */
  64. public void updateAnswerSheet(AnswerSheet answer) {
  65. }
  66. /**
  67. * 交卷
  68. * @param answer
  69. */
  70. public void commitAnswerSheet(AnswerSheet answer) {
  71. }
  72. private AnswerSheet openExaminee(PaperType paperType, Long paperId, Long studentId) {
  73. LearnPaper paper = paperMapper.selectLearnPaperById(paperId);
  74. LearnExaminee learnExaminee = new LearnExaminee();
  75. learnExaminee.setStudentId(studentId);
  76. learnExaminee.setPaperId(paperId);
  77. learnExaminee.setPaperType(paperType.getVal());
  78. learnExaminee.setState(ExamineeStatus.Exam.getVal());
  79. List<LearnExaminee> examineeList = examineeMapper.selectLearnExamineeList(learnExaminee);
  80. if (CollectionUtils.isNotEmpty(examineeList)) {
  81. learnExaminee = examineeList.get(0);
  82. } else {
  83. learnExaminee.setState(ExamineeStatus.Exam.getVal());
  84. learnExaminee.setBeginTime(new Date());
  85. examineeMapper.insertLearnExaminee(learnExaminee);
  86. }
  87. return buildAnswerSheet(paper, learnExaminee);
  88. }
  89. private AnswerSheet openExaminee(Long planId, Integer maxCount) {
  90. LearnExaminee examinee = new LearnExaminee();
  91. examinee.setStudentId(SecurityUtils.getLoginUser().getUser().getUserId());
  92. examinee.setPaperType(PaperType.Simulated.getVal());
  93. List<LearnExaminee> examineeList = examineeMapper.selectLearnExamineeList(examinee);
  94. Set<Long> existPaperIdSet = Sets.newHashSet();
  95. for(LearnExaminee e : examineeList) {
  96. if(ExamineeStatus.Exam.getVal().equals(e.getState())) {
  97. return buildAnswerSheet(learnPaperService.selectLearnPaperById(e.getPaperId()), e);
  98. }
  99. existPaperIdSet.add(e.getPaperId());
  100. }
  101. if(existPaperIdSet.size() >= maxCount) {
  102. throw new ValidationException("超过最大次数限制" + maxCount );
  103. }
  104. AMarjorPlan plan = marjorPlanService.selectAMarjorPlanById(planId);
  105. if(null == plan) {
  106. throw new ValidationException("专业id无效");
  107. }
  108. LearnPaper paper = getBestPaper(plan, existPaperIdSet);
  109. examinee.setPaperKey(PaperType.Simulated.name() + "_" + paper.getId());
  110. examinee.setState(ExamineeStatus.Exam.getVal());
  111. examinee.setBeginTime(new Date());
  112. examineeMapper.insertLearnExaminee(examinee);
  113. return buildAnswerSheet(paper, examinee);
  114. }
  115. /**
  116. * 根据知识点生成一次性练习卷并开始做题
  117. * @return
  118. */
  119. private AnswerSheet openExaminee(Long knowledgeId, Long studentId, String directKey) {
  120. LearnKnowledgeTree knowledgeTree = knowledgeTreeMapper.selectLearnKnowledgeTreeById(knowledgeId);
  121. LearnPaper paper = new LearnPaper();
  122. paper.setPaperType(PaperType.Practice.name());
  123. paper.setRelateId(knowledgeId);
  124. paper.setYear(Calendar.getInstance().get(Calendar.YEAR));
  125. paper.setStatus(PaperStatus.TmpValid.getVal());
  126. paper.setDirectKey(StringUtils.trimToEmpty(directKey));
  127. List<LearnPaper> paperList = paperMapper.selectLearnPaperList(paper);
  128. if (CollectionUtils.isNotEmpty(paperList)) { // 有未做完的
  129. LearnPaper existPaper = paperList.get(0);
  130. LearnExaminee learnExaminee = new LearnExaminee();
  131. learnExaminee.setStudentId(studentId);
  132. learnExaminee.setPaperId(existPaper.getId());
  133. learnExaminee.setPaperType(PaperType.Practice.getVal());
  134. List<LearnExaminee> examineeList = examineeMapper.selectLearnExamineeList(learnExaminee);
  135. if (CollectionUtils.isNotEmpty(examineeList)) {
  136. LearnExaminee examinee = examineeList.get(0);
  137. return buildAnswerSheet(existPaper, examinee);
  138. }
  139. // 关闭试卷
  140. LearnPaper up = new LearnPaper();
  141. up.setId(learnExaminee.getPaperId());
  142. up.setStatus(PaperStatus.Valid.getVal());
  143. paperMapper.updateLearnPaper(up);
  144. }
  145. TestPaperVO.PaperDef paperDef = new TestPaperVO.PaperDef();
  146. paperDef.setTotal(15L);
  147. paperDef.setFillExclude(true);
  148. paperDef.setKnowIds(Lists.newArrayList(knowledgeId));
  149. List<TestPaperVO.TypeDef> typeDefList= Lists.newArrayList();
  150. typeDefList.add(new TestPaperVO.TypeDef("单选题", "单选题", 80, 1));
  151. typeDefList.add(new TestPaperVO.TypeDef("判断题", "判断题", 10, 2));
  152. paperDef.setTypes(typeDefList);
  153. List<LearnPaperQuestion> pqList = paperService.getQuestions(studentId, paperDef);
  154. paper.setPaperSource(0);
  155. paper.setSubjectId(knowledgeTree.getSubjectId());
  156. paper.setPaperName(studentId + "-" + knowledgeTree.getName() + "-" + DateUtils.format(new Date(), "yyyyMMddHHmmss"));
  157. paperService.savePaper(paper, pqList);
  158. LearnExaminee learnExaminee = new LearnExaminee();
  159. learnExaminee.setStudentId(studentId);
  160. learnExaminee.setPaperType(PaperType.Practice.getVal());
  161. learnExaminee.setPaperId(paper.getId());
  162. learnExaminee.setState(ExamineeStatus.Exam.getVal());
  163. learnExaminee.setBeginTime(new Date());
  164. examineeMapper.insertLearnExaminee(learnExaminee);
  165. return buildAnswerSheet(paper, learnExaminee);
  166. }
  167. private LearnPaper getBestPaper(AMarjorPlan plan, Set<Long> existPaperIdSet) {
  168. String groupName = StringUtils.trimToEmpty(plan.getMajorGroup());
  169. LearnPaper paperCond = new LearnPaper();
  170. paperCond.setPaperType(PaperType.Simulated.name());
  171. for(int i = 3; i>0; i--) {
  172. if(i == 3) {
  173. paperCond.setDirectKey(plan.getUniversityId() + "_" + groupName + "_" + plan.getMajorName());
  174. } else if(i == 2) {
  175. paperCond.setDirectKey(plan.getUniversityId() + "_" + groupName);
  176. } else if(StringUtils.isBlank(groupName)) {
  177. break;
  178. }
  179. List<LearnPaper> paperList = learnPaperService.selectLearnPaperList(paperCond);
  180. for(LearnPaper paper : paperList) {
  181. if(existPaperIdSet.add(paper.getId())) {
  182. return paper;
  183. }
  184. }
  185. }
  186. throw new ValidationException("未初始化院校定向模拟题库: " + plan.getId());
  187. }
  188. private AnswerSheet buildAnswerSheet(LearnPaper paper, LearnExaminee examinee) {
  189. AnswerSheet answerSheet = new AnswerSheet();
  190. answerSheet.setExamineeId(examinee.getExamineeId());
  191. answerSheet.setPaperId(examinee.getPaperId());
  192. answerSheet.setName(paper.getPaperName());
  193. answerSheet.setBeginTime(examinee.getBeginTime());
  194. answerSheet.setScoringType("1");
  195. answerSheet.setMode(0L);
  196. answerSheet.setState(examinee.getState());
  197. answerSheet.setAllowAnswer(true);
  198. answerSheet.setAllowScore(false);
  199. return answerSheet;
  200. }
  201. }