ExamService.java 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728
  1. package com.ruoyi.web.service;
  2. import com.alibaba.fastjson2.JSONObject;
  3. import com.alibaba.fastjson2.util.DateUtils;
  4. import com.google.common.collect.Lists;
  5. import com.google.common.collect.Maps;
  6. import com.google.common.collect.Sets;
  7. import com.ruoyi.common.core.content.VistorContextHolder;
  8. import com.ruoyi.common.core.domain.entity.SysUser;
  9. import com.ruoyi.common.enums.ExamType;
  10. import com.ruoyi.common.enums.UserRegStatus;
  11. import com.ruoyi.common.utils.SecurityUtils;
  12. import com.ruoyi.dz.service.IDzControlService;
  13. import com.ruoyi.dz.service.IDzSubjectService;
  14. import com.ruoyi.enums.*;
  15. import com.ruoyi.ie.domain.AMarjorPlan;
  16. import com.ruoyi.ie.service.IAMarjorPlanService;
  17. import com.ruoyi.learn.domain.*;
  18. import com.ruoyi.learn.mapper.*;
  19. import com.ruoyi.learn.service.ILearnPaperService;
  20. import com.ruoyi.learn.service.ILearnPlanService;
  21. import com.ruoyi.mxjb.domain.MxjbContants;
  22. import com.ruoyi.system.service.ISysUserService;
  23. import org.apache.commons.collections4.CollectionUtils;
  24. import org.apache.commons.lang3.StringUtils;
  25. import org.springframework.stereotype.Service;
  26. import org.springframework.transaction.annotation.Transactional;
  27. import javax.validation.ValidationException;
  28. import java.util.*;
  29. import java.util.stream.Collectors;
  30. import java.util.stream.Stream;
  31. /**
  32. * 考试服务
  33. */
  34. @Service
  35. public class ExamService {
  36. private final LearnAnswerMapper learnAnswerMapper;
  37. private final LearnExamineeMapper learnExamineeMapper;
  38. private final LearnStudentMapper learnStudentMapper;
  39. private final ILearnPlanService learnPlanService;
  40. private final LearnQuestionsMapper learnQuestionsMapper;
  41. private final IDzSubjectService dzSubjectService;
  42. private final LearnKnowledgeCourseMapper learnKnowledgeCourseMapper;
  43. private final LearnTestStudentMapper learnTestStudentMapper;
  44. private Set<PaperType> paperTypeSet = Sets.newHashSet(PaperType.Real, PaperType.Custom);
  45. private final LearnPaperMapper paperMapper;
  46. private final LearnKnowledgeTreeMapper knowledgeTreeMapper;
  47. private final LearnExamineeMapper examineeMapper;
  48. private final LearnWrongBookMapper wrongBookMapper;
  49. private final LearnWrongDetailMapper wrongDetailMapper;
  50. private final ILearnPaperService learnPaperService;
  51. private final PaperService paperService;
  52. private final IAMarjorPlanService marjorPlanService;
  53. private final ISysUserService sysUserService;
  54. private final IDzControlService dzControlService;
  55. public ExamService(LearnPaperMapper paperMapper, LearnKnowledgeTreeMapper knowledgeTreeMapper, LearnExamineeMapper examineeMapper, ILearnPaperService learnPaperService, PaperService paperService, IAMarjorPlanService marjorPlanService, LearnAnswerMapper learnAnswerMapper, LearnExamineeMapper learnExamineeMapper, ISysUserService sysUserService, LearnStudentMapper learnStudentMapper, ILearnPlanService learnPlanService, LearnQuestionsMapper learnQuestionsMapper, IDzSubjectService dzSubjectService, LearnWrongBookMapper wrongBookMapper, LearnWrongDetailMapper wrongDetailMapper, LearnKnowledgeCourseMapper learnKnowledgeCourseMapper, LearnTestStudentMapper learnTestStudentMapper, IDzControlService dzControlService) {
  56. this.paperMapper = paperMapper;
  57. this.knowledgeTreeMapper = knowledgeTreeMapper;
  58. this.examineeMapper = examineeMapper;
  59. this.learnPaperService = learnPaperService;
  60. this.paperService = paperService;
  61. this.marjorPlanService = marjorPlanService;
  62. this.learnAnswerMapper = learnAnswerMapper;
  63. this.learnExamineeMapper = learnExamineeMapper;
  64. this.sysUserService = sysUserService;
  65. this.learnStudentMapper = learnStudentMapper;
  66. this.learnPlanService = learnPlanService;
  67. this.learnQuestionsMapper = learnQuestionsMapper;
  68. this.dzSubjectService = dzSubjectService;
  69. this.wrongBookMapper = wrongBookMapper;
  70. this.wrongDetailMapper = wrongDetailMapper;
  71. this.learnKnowledgeCourseMapper = learnKnowledgeCourseMapper;
  72. this.learnTestStudentMapper = learnTestStudentMapper;
  73. this.dzControlService = dzControlService;
  74. }
  75. /**
  76. * 开卷 (卷, 实时组卷)
  77. * @return
  78. */
  79. @Transactional(rollbackFor = Exception.class)
  80. public AnswerSheet openExaminee(boolean directed, PaperType paperType, Long relatedId, Long subjectId, Integer questionType) {
  81. SysUser user = SecurityUtils.getLoginUser().getUser();
  82. if(paperTypeSet.contains(paperType)) {
  83. if(UserTypeEnum.isCard(user.getUserType()) && !UserRegStatus.Student.equals(user.getRegStatus())) {
  84. throw new RuntimeException("VIP功能不可用");
  85. }
  86. return openExaminee(paperType, relatedId, SecurityUtils.getUserId());
  87. } else if(PaperType.Practice.equals(paperType)) {
  88. if(UserTypeEnum.isCard(user.getUserType()) && !UserRegStatus.Student.equals(user.getRegStatus())) {
  89. throw new RuntimeException("VIP功能不可用");
  90. }
  91. return openExamineeForPractice(relatedId, SecurityUtils.getUserId(), directed, questionType);
  92. } else if(PaperType.Course.equals(paperType)) {
  93. if(UserTypeEnum.isCard(user.getUserType()) && !UserRegStatus.Student.equals(user.getRegStatus())) {
  94. throw new RuntimeException("VIP功能不可用");
  95. }
  96. return openExamineeForCourse(relatedId, SecurityUtils.getUserId(), directed);
  97. } else if(PaperType.Simulated.equals(paperType)) {
  98. if(null == relatedId) { // 直接打开Paper的模拟卷
  99. SysUser exist = sysUserService.selectUserById(SecurityUtils.getUserId());
  100. LearnStudent ls = learnStudentMapper.selectLearnStudentByStudentId(exist.getUserId());
  101. if(null == ls) {
  102. throw new RuntimeException("请先定设置专业学习" + exist.getUserId());
  103. }
  104. return openExaminee(ls, subjectId, exist);
  105. }
  106. return openExamineeByPaper(relatedId);
  107. } else if(PaperType.Test.equals(paperType)) {
  108. SysUser exist = sysUserService.selectUserById(SecurityUtils.getUserId());
  109. LearnTestStudent ts = learnTestStudentMapper.selectLearnTestStudentById(relatedId);
  110. if(null == ts || !ts.getStudentId().equals(exist.getUserId())) {
  111. throw new RuntimeException("无效批次卷" + relatedId);
  112. }
  113. return openExamineeForTest(ts, exist);
  114. }
  115. throw new RuntimeException("错误类型: " + paperType);
  116. }
  117. public AnswerSheet loadExaminee(Long examineeId, Boolean examContinue) {
  118. LearnExaminee examinee = examineeMapper.selectLearnExamineeByExamineeId(examineeId);
  119. if(examContinue) {
  120. if(ExamineeStatus.Sign.getVal().equals(examinee.getState())) {
  121. LearnExaminee upExaminee = new LearnExaminee();
  122. upExaminee.setExamineeId(examineeId);
  123. upExaminee.setBeginTime(new Date());
  124. upExaminee.setState(ExamineeStatus.Exam.getVal());
  125. examineeMapper.updateLearnExaminee(upExaminee);
  126. } else if(!ExamineeStatus.Exam.getVal().equals(examinee.getState())) {
  127. throw new RuntimeException("已考完的卷不能继续做");
  128. }
  129. } else if(ExamineeStatus.Exam.getVal().equals(examinee.getState())) {
  130. throw new RuntimeException("考试中,不能提取答案");
  131. }
  132. LearnPaper learnPaper = learnPaperService.selectLearnPaperById(examinee.getPaperId());
  133. AnswerSheet answerSheet = buildAnswerSheet(learnPaper, examinee, loadQuestionAnswers(examinee, examContinue));
  134. answerSheet.setTotalCount(learnPaper.getNumber());
  135. answerSheet.setWrongCount(examinee.getWrongCount());
  136. if(null != examinee.getPaperInfo()) {
  137. JSONObject info = JSONObject.parseObject(examinee.getPaperInfo());
  138. answerSheet.setCollegeId(info.getLong("universityId"));
  139. answerSheet.setCollegeName(info.getString("universityName"));
  140. answerSheet.setMajorId(info.getLong("planId"));
  141. answerSheet.setMajorName(info.getString("majorName"));
  142. answerSheet.setSubjectId(learnPaper.getSubjectId());
  143. answerSheet.setSubjectName(dzSubjectService.selectDzSubjectBySubjectId(learnPaper.getSubjectId()).getSubjectName());
  144. }
  145. if(null != examinee.getStats()) {
  146. answerSheet.setStats(JSONObject.parseObject(examinee.getStats()));
  147. }
  148. return answerSheet;
  149. }
  150. private List<PaperVO.QuestionAnswer> loadQuestionAnswers(LearnExaminee examinee, boolean examContinue) {
  151. LearnAnswer aCond = new LearnAnswer();
  152. aCond.setExamineeId(examinee.getExamineeId());
  153. Map<Long, LearnAnswer> answerMap = learnAnswerMapper.selectLearnAnswerList(aCond).stream().collect(Collectors.toMap(LearnAnswer::getQuestionId, t -> t));
  154. List<PaperVO.QuestionAnswer> questionList = paperService.loadPaperQuestionAnswers(SecurityUtils.getUserId(), examinee.getPaperId(), answerMap, !examContinue);
  155. return questionList;
  156. }
  157. /**
  158. * 交卷
  159. * @param answerSheet
  160. */
  161. @Transactional(rollbackFor = Exception.class)
  162. public void commitAnswerSheet(AnswerSheet answerSheet) {
  163. LearnExaminee exitExaminee = learnExamineeMapper.selectLearnExamineeByExamineeId(answerSheet.getExamineeId());
  164. if(null == exitExaminee) {
  165. throw new RuntimeException("无此答卷" + answerSheet.getExamineeId());
  166. }
  167. LearnAnswer aCond = new LearnAnswer();
  168. aCond.setExamineeId(answerSheet.getExamineeId());
  169. Map<Long, LearnAnswer> answerMap = learnAnswerMapper.selectLearnAnswerList(aCond).stream().collect(Collectors.toMap(LearnAnswer::getQuestionId, t -> t));
  170. List<PaperVO.QuestionAnswer> qaList = paperService.loadPaperQuestionAnswers(null, exitExaminee.getPaperId(), answerMap, true);
  171. Map<Long, PaperVO.QuestionAnswer> questionMap = Maps.newHashMap();
  172. for(PaperVO.QuestionAnswer qa : qaList) {
  173. if(CollectionUtils.isNotEmpty(qa.getSubQuestions())) {
  174. for(PaperVO.QuestionAnswer sqa : qa.getSubQuestions()) {
  175. questionMap.put(sqa.getId(), sqa);
  176. }
  177. } else {
  178. questionMap.put(qa.getId(), qa);
  179. }
  180. }
  181. List<LearnAnswer> answersList = Lists.newArrayList();
  182. Integer wrongCount = 0;
  183. Integer totalScore = 0;
  184. Integer score = 0;
  185. for(PaperVO.QuestionAnswer question : answerSheet.getQuestions()) {
  186. if(CollectionUtils.isNotEmpty(question.getSubQuestions())) {
  187. for(PaperVO.QuestionAnswer sq : question.getSubQuestions()) {
  188. PaperVO.QuestionAnswer stdAnswer = questionMap.get(sq.getId());
  189. LearnAnswer answer = buildAnswer(answerSheet, questionMap, sq, answersList);
  190. if(answerSheet.getIsDone()) {
  191. totalScore += stdAnswer.getTotalScore();
  192. score += answer.getScore();
  193. if(!answer.getState().equals(1)) {
  194. wrongCount++;
  195. }
  196. }
  197. }
  198. } else {
  199. PaperVO.QuestionAnswer stdAnswer = questionMap.get(question.getId());
  200. LearnAnswer answer = buildAnswer(answerSheet, questionMap, question, answersList);
  201. if(answerSheet.getIsDone()) {
  202. totalScore += stdAnswer.getTotalScore();
  203. score += answer.getScore();
  204. if(!answer.getState().equals(1)) {
  205. wrongCount++;
  206. }
  207. }
  208. }
  209. }
  210. if(answersList.size() > 0) {
  211. answersList.stream().forEach(t -> {
  212. if(null == t.getAnswerId()) {
  213. learnAnswerMapper.insertLearnAnswer(t);
  214. } else {
  215. learnAnswerMapper.updateLearnAnswer(t);
  216. }
  217. });
  218. }
  219. LearnExaminee upExaminee = new LearnExaminee();
  220. upExaminee.setExamineeId(answerSheet.getExamineeId());
  221. upExaminee.setDuration(answerSheet.getDuration());
  222. if(answerSheet.getIsDone()) {
  223. upExaminee.setEndTime(new Date());
  224. upExaminee.setState(ExamineeStatus.Commit.getVal());
  225. upExaminee.setScore(score);
  226. upExaminee.setScoreRate(score * 100 / totalScore);
  227. upExaminee.setWrongCount(wrongCount);
  228. learnExamineeMapper.updateLearnExaminee(upExaminee);
  229. if(PaperType.Simulated.getVal().equals(exitExaminee.getPaperType()) || PaperType.Test.getVal().equals(exitExaminee.getPaperType())) {
  230. List<JSONObject> paperStatList = learnExamineeMapper.selectExamRankingStats(exitExaminee.getPaperType(), upExaminee.getScore(), Lists.newArrayList(exitExaminee.getPaperId()));
  231. JSONObject o = paperStatList.get(0);
  232. Integer totalCount = o.getInteger("totalCount");
  233. Integer maxScore = o.getInteger("maxScore");
  234. Integer avgScore = o.getInteger("avgScore");
  235. Integer lowCount = o.getInteger("lowCount");
  236. JSONObject stats = new JSONObject();
  237. stats.put("score", upExaminee.getScore());
  238. stats.put("rate", upExaminee.getScoreRate());
  239. stats.put("maxScore", maxScore);
  240. stats.put("averageScore", avgScore);
  241. stats.put("hitRate", lowCount * 100 / totalCount);
  242. upExaminee.setStats(JSONObject.toJSONString(stats));
  243. learnExamineeMapper.updateLearnExaminee(upExaminee);
  244. }
  245. } else {
  246. learnExamineeMapper.updateLearnExaminee(upExaminee);
  247. }
  248. if(answerSheet.getIsDone()) {
  249. // 关闭试卷
  250. LearnPaper up = new LearnPaper();
  251. up.setId(exitExaminee.getPaperId());
  252. up.setStatus(PaperStatus.Valid.getVal());
  253. paperMapper.updateLearnPaper(up);
  254. if(PaperType.Practice.getVal().equals(exitExaminee.getPaperType())) {
  255. learnPlanService.updateLearnPlan(exitExaminee.getStudentId());
  256. calculateWrongAnswers(exitExaminee, questionMap, answersList, PaperType.Practice.name());
  257. } else if(PaperType.Course.getVal().equals(exitExaminee.getPaperType())) {
  258. learnPlanService.updateLearnPlan(exitExaminee.getStudentId());
  259. calculateWrongAnswers(exitExaminee, questionMap, answersList, PaperType.Course.name());
  260. } else if(PaperType.Simulated.getVal().equals(exitExaminee.getPaperType())) {
  261. calculateWrongAnswers(exitExaminee, questionMap, answersList, PaperType.Simulated.name());
  262. } else if(PaperType.Test.getVal().equals(exitExaminee.getPaperType())) {
  263. calculateWrongAnswers(exitExaminee, questionMap, answersList, PaperType.Test.name());
  264. LearnTestStudent uts = new LearnTestStudent();
  265. uts.setExamineeId(answerSheet.getExamineeId());
  266. uts.setStatus(upExaminee.getState());
  267. learnTestStudentMapper.updateExamineeStatus(uts);
  268. }
  269. }
  270. }
  271. public Set<Integer> invalidStatusSet = Sets.newHashSet(2, 3);
  272. public void calculateWrongAnswers(LearnExaminee examinee, Map<Long, PaperVO.QuestionAnswer> questionMap, List<LearnAnswer> answersList, String source) {
  273. if (CollectionUtils.isEmpty(answersList)){
  274. return;
  275. }
  276. LearnPaper learnPaper = paperMapper.selectLearnPaperById(examinee.getPaperId());
  277. answersList.forEach(t -> {
  278. if (invalidStatusSet.contains(t.getState())) {
  279. LearnWrongBook wrongBookCond = new LearnWrongBook();
  280. wrongBookCond.setStudentId(examinee.getStudentId());
  281. wrongBookCond.setQuestionId(t.getQuestionId());
  282. PaperVO.QuestionAnswer stdAnswer = questionMap.get(t.getQuestionId());
  283. wrongBookCond.setKnownledgeId(stdAnswer.getKnowledgeId());
  284. List<LearnWrongBook> wrongBookList = wrongBookMapper.selectLearnWrongBookList(wrongBookCond);
  285. if (CollectionUtils.isNotEmpty(wrongBookList)) {
  286. LearnWrongBook wrongBook = wrongBookList.get(0);
  287. if (null == wrongBook.getWrongCount()) {
  288. wrongBook.setWrongCount(0L);
  289. }
  290. if (null == wrongBook.getTotalCount()) {
  291. wrongBook.setTotalCount(0L);
  292. }
  293. wrongBookCond.setWrongId(wrongBook.getWrongId());
  294. wrongBookCond.setStudentId(null);
  295. wrongBookCond.setQuestionId(null);
  296. wrongBookCond.setWrongCount(wrongBook.getWrongCount() + 1);
  297. wrongBookCond.setTotalCount(wrongBook.getTotalCount() + 1);
  298. } else {
  299. wrongBookCond.setAnswer1(stdAnswer.getAnswer1());
  300. wrongBookCond.setAnswer2(stdAnswer.getAnswer2());
  301. wrongBookCond.setSubjectId(learnPaper.getSubjectId());
  302. wrongBookCond.setRightCount(0L);
  303. wrongBookCond.setWrongCount(1L);
  304. wrongBookCond.setTotalCount(1L);
  305. // wrongBookCond.setScoreTotal(standard.getScoreTotal());
  306. }
  307. // wrongBookCond.setKnownledgeId(standard.getKnowledgeId());
  308. wrongBookCond.setPaperId(examinee.getPaperId());
  309. wrongBookCond.setScore(t.getScore());
  310. wrongBookCond.setSource(source);
  311. wrongBookCond.setAnswer(t.getAnswer());
  312. wrongBookCond.setState(MxjbContants.QuestionScoreWrong);
  313. if(null == wrongBookCond.getWrongId()) {
  314. wrongBookMapper.insertLearnWrongBook(wrongBookCond);
  315. } else {
  316. wrongBookMapper.updateLearnWrongBook(wrongBookCond);
  317. }
  318. LearnWrongDetail wrongDetail = new LearnWrongDetail();
  319. wrongDetail.setWrongId(wrongBookCond.getWrongId());
  320. wrongDetail.setCreatedTime(new Date());
  321. wrongDetail.setKnownledgeId(stdAnswer.getKnowledgeId());
  322. wrongDetail.setSource(wrongBookCond.getSource());
  323. wrongDetail.setExamineeId(examinee.getExamineeId());
  324. wrongDetail.setStudentId(examinee.getStudentId());
  325. wrongDetail.setPaperId(examinee.getPaperId());
  326. wrongDetailMapper.insertLearnWrongDetail(wrongDetail);
  327. }
  328. });
  329. }
  330. private LearnAnswer buildAnswer(AnswerSheet answerSheet, Map<Long, PaperVO.QuestionAnswer> questionMap, PaperVO.QuestionAnswer question,
  331. List<LearnAnswer> answerList) {
  332. LearnAnswer answer = new LearnAnswer();
  333. PaperVO.QuestionAnswer stdAnswer = questionMap.get(question.getId());
  334. if(null == stdAnswer.getAnswerId()) {
  335. answer.setExamineeId(answerSheet.getExamineeId());
  336. answer.setStudentId(SecurityUtils.getUserId());
  337. answer.setSeq(question.getSeq());
  338. } else {
  339. answer.setAnswerId(stdAnswer.getAnswerId());
  340. }
  341. answerList.add(answer);
  342. answer.setQuestionId(question.getId()); // 错题计算要使用
  343. answer.setKnowledgeId(stdAnswer.getKnowledgeId());
  344. answer.setAnswer(StringUtils.join(question.getAnswers(), ","));
  345. answer.setMark(null != question.getIsMark() && question.getIsMark());
  346. answer.setNotKnow(null != question.getIsNotKnow() && question.getIsNotKnow());
  347. answer.setState(question.calcState(stdAnswer, answerSheet.getIsDone()));
  348. answer.setDuration(question.getDuration());
  349. if (answerSheet.getIsDone()) {
  350. answer.setScore(answer.getState() == 1 ? stdAnswer.getTotalScore() : 0);
  351. answer.setScoreRate(answer.getScore() * 100 / stdAnswer.getTotalScore());
  352. }
  353. return answer;
  354. }
  355. private AnswerSheet openExamineeForTest(PaperType paperType, Long paperId, LearnTestStudent ts) {
  356. LearnPaper paper = paperMapper.selectLearnPaperById(paperId);
  357. LearnExaminee learnExaminee = new LearnExaminee();
  358. learnExaminee.setStudentId(ts.getStudentId());
  359. learnExaminee.setPaperId(paperId);
  360. learnExaminee.setPaperType(paperType.getVal());
  361. learnExaminee.setState(ExamineeStatus.Exam.getVal());
  362. List<LearnExaminee> examineeList = examineeMapper.selectLearnExamineeList(learnExaminee);
  363. List<PaperVO.QuestionAnswer> questionAnswerList = null;
  364. if (CollectionUtils.isNotEmpty(examineeList)) {
  365. learnExaminee = examineeList.get(0);
  366. questionAnswerList = loadQuestionAnswers(learnExaminee, true);
  367. } else {
  368. if(StringUtils.isNotBlank(ts.getDirectKey())) { // 全量,只分科目
  369. LearnStudent ls = learnStudentMapper.selectLearnStudentByStudentId(ts.getStudentId());
  370. AMarjorPlan plan = marjorPlanService.selectAMarjorPlanById(ls.getMajorPlanId());
  371. learnExaminee.setPaperInfo(buildPaperInfo(ls, plan));
  372. }
  373. learnExaminee.setState(ExamineeStatus.Exam.getVal());
  374. learnExaminee.setBeginTime(new Date());
  375. learnExaminee.setDuration(0L);
  376. examineeMapper.insertLearnExaminee(learnExaminee);
  377. }
  378. return buildAnswerSheet(paper, learnExaminee, questionAnswerList);
  379. }
  380. private AnswerSheet openExaminee(PaperType paperType, Long paperId, Long studentId) {
  381. LearnPaper paper = paperMapper.selectLearnPaperById(paperId);
  382. LearnExaminee learnExaminee = new LearnExaminee();
  383. learnExaminee.setStudentId(studentId);
  384. learnExaminee.setPaperId(paperId);
  385. learnExaminee.setPaperType(paperType.getVal());
  386. learnExaminee.setState(ExamineeStatus.Exam.getVal());
  387. List<LearnExaminee> examineeList = examineeMapper.selectLearnExamineeList(learnExaminee);
  388. List<PaperVO.QuestionAnswer> questionAnswerList = null;
  389. if (CollectionUtils.isNotEmpty(examineeList)) {
  390. learnExaminee = examineeList.get(0);
  391. questionAnswerList = loadQuestionAnswers(learnExaminee, true);
  392. } else {
  393. learnExaminee.setState(ExamineeStatus.Exam.getVal());
  394. learnExaminee.setBeginTime(new Date());
  395. learnExaminee.setDuration(0L);
  396. examineeMapper.insertLearnExaminee(learnExaminee);
  397. }
  398. return buildAnswerSheet(paper, learnExaminee, questionAnswerList);
  399. }
  400. @Transactional
  401. public AnswerSheet openExamineeForTest(LearnTestStudent ts, SysUser sysUser) {
  402. if(null != ts.getExamineeId()) {
  403. return loadExaminee(ts.getExamineeId(), true);
  404. }
  405. AnswerSheet answerSheet = openExamineeForTest(PaperType.Test, ts.getPaperId(), ts);
  406. LearnTestStudent uts = new LearnTestStudent();
  407. uts.setId(ts.getId());
  408. uts.setExamineeId(answerSheet.getExamineeId());
  409. uts.setStatus(answerSheet.getState());
  410. learnTestStudentMapper.updateLearnTestStudent(uts);
  411. return answerSheet;
  412. }
  413. private AnswerSheet openExaminee(LearnStudent ls, Long subjectId, SysUser sysUser) {
  414. if(StringUtils.isNotBlank(ls.getModules()) && ls.getModules().equals("技能展示/实践操作")) {
  415. throw new ValidationException("该专业只考技能展示/实践操作");
  416. }
  417. AMarjorPlan plan = marjorPlanService.selectAMarjorPlanById(ls.getMajorPlanId());
  418. if(null == plan || !plan.getMatchYear().equals(dzControlService.selectDzControl(VistorContextHolder.getContext()).getPlanYear())) {
  419. throw new ValidationException("定向没设置或过期,请先重新选择定向");
  420. }
  421. LearnExaminee examinee = new LearnExaminee();
  422. examinee.setStudentId(SecurityUtils.getLoginUser().getUser().getUserId());
  423. examinee.setPaperType(PaperType.Simulated.getVal());
  424. examinee.setPaperKey(subjectId.toString());
  425. List<LearnExaminee> examineeList = examineeMapper.selectLearnExamineeList(examinee);
  426. Set<Long> existPaperIdSet = Sets.newHashSet();
  427. for(LearnExaminee e : examineeList) {
  428. if(ExamineeStatus.Sign.getVal().equals(e.getState())) {
  429. LearnPaper learnPaper = paperMapper.selectLearnPaperById(e.getPaperId());
  430. return buildAnswerSheet(learnPaper, e, loadQuestionAnswers(e, true));
  431. }
  432. existPaperIdSet.add(e.getPaperId());
  433. }
  434. LearnPaper paper = getBestPaper(sysUser.getExamType(), plan, subjectId, existPaperIdSet); // getAvailPaper(sysUser.getExamType(), ls, subjectId, existPaperIdSet)
  435. JSONObject evalCountObj = JSONObject.parseObject(sysUser.getEvalCounts());
  436. Integer evalCount = evalCountObj.getInteger(subjectId.toString());
  437. if(null == evalCount || evalCount <= 0) {
  438. throw new ValidationException("超过最大次数限制" + evalCount );
  439. }
  440. SysUser upUser = new SysUser();
  441. upUser.setUserId(sysUser.getUserId());
  442. evalCountObj.put(subjectId.toString(), evalCount - 1);
  443. upUser.setEvalCounts(evalCountObj.toJSONString());
  444. sysUserService.updateUserProfile(upUser);
  445. examinee.setPaperInfo(buildPaperInfo(ls, plan));
  446. AnswerSheet answerSheet = openNewExamineeByPaper(examinee, paper);
  447. answerSheet.setCollegeId(ls.getUniversityId());
  448. answerSheet.setCollegeName(plan.getUniversityName());
  449. answerSheet.setMajorId(ls.getMajorPlanId());
  450. answerSheet.setMajorName(plan.getMajorName());
  451. return answerSheet;
  452. }
  453. private AnswerSheet openExamineeByPaper(Long paperId) {
  454. LearnPaper learnPaper = paperMapper.selectLearnPaperById(paperId);
  455. LearnExaminee examinee = new LearnExaminee();
  456. examinee.setStudentId(SecurityUtils.getLoginUser().getUser().getUserId());
  457. examinee.setPaperType(PaperType.Simulated.getVal());
  458. examinee.setPaperKey("P" + "_" + paperId + "_" + learnPaper.getSubjectId());
  459. List<LearnExaminee> examineeList = examineeMapper.selectLearnExamineeList(examinee);
  460. if(CollectionUtils.isNotEmpty(examineeList)) {
  461. LearnExaminee exist = examineeList.get(0);
  462. if(exist.getState() >= ExamineeStatus.Commit.getVal()) {
  463. throw new ValidationException("试卷不能重复打开做, 请查问答卷");
  464. }
  465. return buildAnswerSheet(learnPaper, exist, loadQuestionAnswers(exist, true));
  466. }
  467. return openNewExamineeByPaper(examinee, learnPaper);
  468. }
  469. private AnswerSheet openNewExamineeByPaper(LearnExaminee examinee, LearnPaper paper) {
  470. examinee.setPaperId(paper.getId());
  471. examinee.setState(ExamineeStatus.Sign.getVal());
  472. examinee.setDuration(0L);
  473. if(StringUtils.isBlank(examinee.getPaperInfo())) { // 定向有自己的专业信息,否则使用卷名称
  474. JSONObject info = new JSONObject();
  475. info.put("paperName", paper.getPaperName());
  476. examinee.setPaperInfo(info.toJSONString());
  477. }
  478. examineeMapper.insertLearnExaminee(examinee);
  479. AnswerSheet answerSheet = buildAnswerSheet(paper, examinee, null);
  480. answerSheet.setSubjectId(paper.getSubjectId());
  481. answerSheet.setSubjectName(dzSubjectService.selectDzSubjectBySubjectId(paper.getSubjectId()).getSubjectName());
  482. return answerSheet;
  483. }
  484. private String buildPaperInfo(LearnStudent ls, AMarjorPlan plan) {
  485. JSONObject info = new JSONObject();
  486. info.put("universityId", ls.getUniversityId());
  487. info.put("universityName", plan.getUniversityName());
  488. info.put("planId", ls.getMajorPlanId());
  489. info.put("majorName", plan.getMajorName());
  490. return info.toJSONString();
  491. }
  492. private AnswerSheet openExamineeForCourse(Long knowledgeId, Long studentId, Boolean directed) {
  493. LearnKnowledgeCourse kc = learnKnowledgeCourseMapper.selectLearnKnowledgeCourseById(knowledgeId);
  494. if(null == kc || StringUtils.isBlank(kc.getKnowledges())) {
  495. throw new RuntimeException("无效同步知识点");
  496. }
  497. return openExaminee(PaperType.Course, knowledgeId, Stream.of(kc.getKnowledges().split(",")).map(Long::parseLong).collect(Collectors.toList()), studentId, false,
  498. 0L, kc.getName(), null, null, "", 0) ;
  499. }
  500. private AnswerSheet openExamineeForPractice(Long knowledgeId, Long studentId, Boolean directed, Integer questionType) {
  501. String directKey;
  502. AMarjorPlan plan = null;
  503. LearnStudent ls = null;
  504. if(!directed) {
  505. directKey = "";
  506. } else {
  507. ls = learnStudentMapper.selectLearnStudentByStudentId(SecurityUtils.getUserId());
  508. if(null == (plan = marjorPlanService.selectAMarjorPlanById(ls.getMajorPlanId()))) {
  509. throw new ValidationException("专业id无效");
  510. }
  511. directKey = StringUtils.trimToEmpty(ls.getDirectKey());
  512. }
  513. LearnKnowledgeTree knowledgeTree = knowledgeTreeMapper.selectLearnKnowledgeTreeById(knowledgeId);
  514. return openExaminee(PaperType.Practice, knowledgeId, Lists.newArrayList(knowledgeId), studentId, directed,
  515. knowledgeTree.getSubjectId(), knowledgeTree.getName(), ls, plan, directKey, questionType) ;
  516. }
  517. /**
  518. * 根据知识点生成一次性练习卷并开始做题
  519. * @return
  520. */
  521. private AnswerSheet openExaminee(PaperType paperType, Long knowledgeId, List<Long> useKnowledgeIds, Long studentId, Boolean directed,
  522. Long subjectId, String knowledName, LearnStudent ls, AMarjorPlan plan, String directKey, Integer questionType) {
  523. LearnPaper paper = new LearnPaper();
  524. paper.setPaperType(paperType.name());
  525. paper.setRelateId(knowledgeId);
  526. paper.setYear(Calendar.getInstance().get(Calendar.YEAR));
  527. paper.setStatus(PaperStatus.TmpValid.getVal());
  528. paper.setDirectKey(studentId + "_" + directKey + "_" + (null == questionType ? "0" : questionType));
  529. List<LearnPaper> paperList = paperMapper.selectLearnPaperList(paper);
  530. if (CollectionUtils.isNotEmpty(paperList)) { // 有未做完的
  531. LearnPaper existPaper = paperList.get(0);
  532. LearnExaminee learnExaminee = new LearnExaminee();
  533. learnExaminee.setStudentId(studentId);
  534. learnExaminee.setPaperId(existPaper.getId());
  535. learnExaminee.setPaperType(paperType.getVal());
  536. learnExaminee.setState(ExamineeStatus.Exam.getVal());
  537. List<LearnExaminee> examineeList = examineeMapper.selectLearnExamineeList(learnExaminee);
  538. if (CollectionUtils.isNotEmpty(examineeList) && ExamineeStatus.Exam.getVal().equals(examineeList.get(0).getState())) {
  539. LearnExaminee examinee = examineeList.get(0);
  540. AnswerSheet answerSheet = buildAnswerSheet(existPaper, examinee, loadQuestionAnswers(examinee, false));
  541. answerSheet.setKnowledgeId(existPaper.getRelateId());
  542. return answerSheet;
  543. }
  544. // 关闭试卷
  545. LearnPaper up = new LearnPaper();
  546. up.setId(learnExaminee.getPaperId());
  547. up.setStatus(PaperStatus.Valid.getVal());
  548. paperMapper.updateLearnPaper(up);
  549. }
  550. List<String> types = (null == questionType || questionType.equals(0))
  551. ? Arrays.stream(QuestionType.values()).map(QuestionType::getTitle).collect(Collectors.toList())
  552. : Lists.newArrayList(QuestionType.Single.getTitle(), QuestionType.Judgment.getTitle());
  553. List<LearnPaperQuestion> pqList = paperService.getQuestionsByRandom(studentId, 15, useKnowledgeIds, types, 0);
  554. if(CollectionUtils.isEmpty(pqList)) {
  555. throw new RuntimeException("本知识点题已做完");
  556. }
  557. paper.setPaperSource(0);
  558. paper.setFenshu(0);
  559. paper.setSubjectId(subjectId);
  560. paper.setPaperName(studentId + "-" + knowledName + "-" + DateUtils.format(new Date(), "yyyyMMddHHmmss"));
  561. paper.setRelateId(knowledgeId);
  562. paperService.savePaper(paper, pqList);
  563. LearnExaminee learnExaminee = new LearnExaminee();
  564. learnExaminee.setStudentId(studentId);
  565. learnExaminee.setPaperType(paperType.getVal());
  566. learnExaminee.setPaperId(paper.getId());
  567. learnExaminee.setPaperKey(String.valueOf(knowledgeId));
  568. learnExaminee.setState(ExamineeStatus.Exam.getVal());
  569. learnExaminee.setBeginTime(new Date());
  570. learnExaminee.setDuration(0L);
  571. if(directed && null != ls.getMajorPlanId()) {
  572. JSONObject info = new JSONObject();
  573. info.put("universityId", ls.getUniversityId());
  574. info.put("universityName", plan.getUniversityName());
  575. info.put("planId", ls.getMajorPlanId());
  576. info.put("majorName", plan.getMajorName());
  577. learnExaminee.setPaperInfo(info.toJSONString());
  578. }
  579. examineeMapper.insertLearnExaminee(learnExaminee);
  580. AnswerSheet answerSheet = buildAnswerSheet(paper, learnExaminee, null);
  581. List<PaperVO.QuestionAnswer> questionList = paperService.loadPaperQuestionAnswers(SecurityUtils.getUserId(), paper.getId(), null, true);
  582. answerSheet.setQuestions(questionList);
  583. answerSheet.setKnowledgeId(paper.getRelateId());
  584. return answerSheet;
  585. }
  586. private LearnPaper getAvailPaper(ExamType examType, LearnStudent student, Long subjectId, Set<Long> existPaperIdSet) {
  587. LearnPaper paperCond = new LearnPaper();
  588. paperCond.setSubjectId(subjectId);
  589. paperCond.setPaperType(PaperType.Simulated.name());
  590. paperCond.setStatus(1);
  591. paperCond.setDirectKey(student.getUniversityId() + "_" + examType.name() + "_" + student.getDirectKey());
  592. List<LearnPaper> paperList = learnPaperService.selectLearnPaperList(paperCond);
  593. for(LearnPaper paper : paperList) {
  594. if(existPaperIdSet.add(paper.getId())) {
  595. return paper;
  596. }
  597. }
  598. throw new ValidationException("未初始化院校模拟题库: " + subjectId + ":" + student.getDirectKey());
  599. }
  600. private LearnPaper getBestPaper(ExamType examType, AMarjorPlan plan, Long subjectId, Set<Long> existPaperIdSet) {
  601. String groupName = StringUtils.trimToEmpty(plan.getMajorGroup());
  602. LearnPaper paperCond = new LearnPaper();
  603. paperCond.setSubjectId(subjectId);
  604. paperCond.setPaperType(PaperType.Simulated.name());
  605. paperCond.setStatus(1);
  606. String baseKey = plan.getUniversityId() + "_" + examType.name() + "_";
  607. paperCond.setDirectKey(baseKey);
  608. if(!subjectId.equals(11L)) {
  609. List<LearnPaper> paperList = learnPaperService.selectLearnPaperList(paperCond);
  610. if(paperList.isEmpty()) {
  611. paperCond.setDirectKey("");
  612. paperList = learnPaperService.selectLearnPaperList(paperCond);
  613. }
  614. for(LearnPaper paper : paperList) {
  615. if(existPaperIdSet.add(paper.getId())) {
  616. return paper;
  617. }
  618. }
  619. existPaperIdSet.clear(); // TODO 忽略已做的卷
  620. for(LearnPaper paper : paperList) {
  621. if(existPaperIdSet.add(paper.getId())) {
  622. return paper;
  623. }
  624. }
  625. throw new ValidationException("未初始化院校模拟题库: " + subjectId + ":" + plan.getId());
  626. }
  627. List<LearnPaper> paperList = learnPaperService.selectLearnPaperList(paperCond);
  628. for(int i = 3; i>0; i--) {
  629. if(i == 3) {
  630. String prefix = baseKey + groupName + "_";
  631. for(LearnPaper paper : paperList) {
  632. if(paper.getDirectKey().startsWith(prefix)) {
  633. Set<String> majorNameSet = Sets.newHashSet(paper.getDirectKey().substring(prefix.length()).split("、"));
  634. if(majorNameSet.contains(plan.getMajorName()) && existPaperIdSet.add(paper.getId())) {
  635. return paper;
  636. }
  637. }
  638. }
  639. } else if(i == 2) {
  640. String prefix = baseKey + groupName;
  641. for(LearnPaper paper : paperList) {
  642. if(paper.getDirectKey().equals(prefix)) {
  643. if(existPaperIdSet.add(paper.getId())) {
  644. return paper;
  645. }
  646. }
  647. }
  648. } else if(StringUtils.isBlank(groupName)) {
  649. break;
  650. } else {
  651. for(LearnPaper paper : paperList) {
  652. if(paper.getDirectKey().equals(baseKey)) {
  653. if(existPaperIdSet.add(paper.getId())) {
  654. return paper;
  655. }
  656. }
  657. }
  658. }
  659. }
  660. throw new ValidationException("未初始化院校定向模拟题库: " + subjectId + ":" + plan.getId());
  661. }
  662. private AnswerSheet buildAnswerSheet(LearnPaper paper, LearnExaminee examinee, List<PaperVO.QuestionAnswer> questionList) {
  663. AnswerSheet answerSheet = new AnswerSheet();
  664. answerSheet.setExamineeId(examinee.getExamineeId());
  665. answerSheet.setPaperId(examinee.getPaperId());
  666. answerSheet.setName(paper.getPaperName());
  667. answerSheet.setBeginTime(examinee.getBeginTime());
  668. answerSheet.setDuration(examinee.getDuration());
  669. if(StringUtils.isNotBlank(paper.getPaperInfo())) {
  670. answerSheet.setPaperInfo(JSONObject.parseObject(paper.getPaperInfo(), AnswerSheet.PaperCond.class));
  671. }
  672. if(PaperType.Practice.name().equals(paper.getPaperType()) || PaperType.Course.name().equals(paper.getPaperType())) {
  673. answerSheet.setKnowledgeId(paper.getRelateId());
  674. }
  675. answerSheet.setScoringType("1");
  676. answerSheet.setMode(0L);
  677. answerSheet.setState(examinee.getState());
  678. answerSheet.setAllowAnswer(true);
  679. answerSheet.setAllowScore(false);
  680. if(null != examinee.getPaperInfo()) {
  681. JSONObject info = JSONObject.parseObject(examinee.getPaperInfo());
  682. answerSheet.setCollegeId(info.getLong("universityId"));
  683. answerSheet.setCollegeName(info.getString("universityName"));
  684. answerSheet.setMajorId(info.getLong("planId"));
  685. answerSheet.setMajorName(info.getString("majorName"));
  686. answerSheet.setSubjectId(paper.getSubjectId());
  687. answerSheet.setSubjectName(dzSubjectService.selectDzSubjectBySubjectId(paper.getSubjectId()).getSubjectName());
  688. }
  689. answerSheet.setQuestions(questionList);
  690. return answerSheet;
  691. }
  692. }