ExamService.java 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  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.annotation.Excel;
  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.IDzSubjectService;
  13. import com.ruoyi.dz.service.impl.DzSubjectServiceImpl;
  14. import com.ruoyi.enums.ExamineeStatus;
  15. import com.ruoyi.enums.PaperStatus;
  16. import com.ruoyi.enums.PaperType;
  17. import com.ruoyi.enums.UserTypeEnum;
  18. import com.ruoyi.ie.domain.AMarjorPlan;
  19. import com.ruoyi.ie.service.IAMarjorPlanService;
  20. import com.ruoyi.learn.domain.*;
  21. import com.ruoyi.learn.mapper.*;
  22. import com.ruoyi.learn.service.ILearnPaperService;
  23. import com.ruoyi.learn.service.ILearnPlanService;
  24. import com.ruoyi.mxjb.domain.MxjbContants;
  25. import com.ruoyi.system.service.ISysUserService;
  26. import io.swagger.annotations.ApiModelProperty;
  27. import org.apache.commons.collections4.CollectionUtils;
  28. import org.apache.commons.lang3.StringUtils;
  29. import org.springframework.stereotype.Service;
  30. import org.springframework.transaction.annotation.Transactional;
  31. import javax.validation.ValidationException;
  32. import java.util.*;
  33. import java.util.stream.Collectors;
  34. /**
  35. * 考试服务
  36. */
  37. @Service
  38. public class ExamService {
  39. private final LearnAnswerMapper learnAnswerMapper;
  40. private final LearnExamineeMapper learnExamineeMapper;
  41. private final LearnStudentMapper learnStudentMapper;
  42. private final ILearnPlanService learnPlanService;
  43. private final LearnQuestionsMapper learnQuestionsMapper;
  44. private final IDzSubjectService dzSubjectService;
  45. private Set<PaperType> paperTypeSet = Sets.newHashSet(PaperType.Real, PaperType.Custom, PaperType.Test);
  46. private final LearnPaperMapper paperMapper;
  47. private final LearnKnowledgeTreeMapper knowledgeTreeMapper;
  48. private final LearnExamineeMapper examineeMapper;
  49. private final LearnWrongBookMapper wrongBookMapper;
  50. private final LearnWrongDetailMapper wrongDetailMapper;
  51. private final ILearnPaperService learnPaperService;
  52. private final PaperService paperService;
  53. private final IAMarjorPlanService marjorPlanService;
  54. private final ISysUserService sysUserService;
  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) {
  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. }
  72. /**
  73. * 开卷 (卷, 实时组卷)
  74. * @return
  75. */
  76. @Transactional(rollbackFor = Exception.class)
  77. public AnswerSheet openExaminee(boolean directed, PaperType paperType, Long relatedId, Long subjectId) {
  78. SysUser user = SecurityUtils.getLoginUser().getUser();
  79. if(paperTypeSet.contains(paperType)) {
  80. if(UserTypeEnum.isCard(user.getUserType()) && !UserRegStatus.Student.equals(user.getRegStatus())) {
  81. throw new RuntimeException("VIP功能不可用");
  82. }
  83. return openExaminee(paperType, relatedId, SecurityUtils.getUserId());
  84. } else if(PaperType.Practice.equals(paperType)) {
  85. if(UserTypeEnum.isCard(user.getUserType()) && !UserRegStatus.Student.equals(user.getRegStatus())) {
  86. throw new RuntimeException("VIP功能不可用");
  87. }
  88. return openExaminee(relatedId, SecurityUtils.getUserId(), directed);
  89. } else if(PaperType.Simulated.equals(paperType)) {
  90. SysUser exist = sysUserService.selectUserById(SecurityUtils.getUserId());
  91. LearnStudent ls = learnStudentMapper.selectLearnStudentByStudentId(exist.getUserId());
  92. if(null == ls) {
  93. throw new RuntimeException("请先定设置定向学习" + exist.getUserId());
  94. }
  95. return openExaminee(ls, subjectId, exist);
  96. }
  97. throw new RuntimeException("错误类型: " + paperType);
  98. }
  99. public AnswerSheet loadExaminee(Long examineeId, Boolean examContinue) {
  100. LearnExaminee examinee = examineeMapper.selectLearnExamineeByExamineeId(examineeId);
  101. if(examContinue) {
  102. if(ExamineeStatus.Sign.getVal().equals(examinee.getState())) {
  103. LearnExaminee upExaminee = new LearnExaminee();
  104. upExaminee.setExamineeId(examineeId);
  105. upExaminee.setBeginTime(new Date());
  106. upExaminee.setState(ExamineeStatus.Exam.getVal());
  107. examineeMapper.updateLearnExaminee(upExaminee);
  108. } else if(!ExamineeStatus.Exam.getVal().equals(examinee.getState())) {
  109. throw new RuntimeException("已考完的卷不能继续做");
  110. }
  111. } else if(ExamineeStatus.Exam.getVal().equals(examinee.getState())) {
  112. throw new RuntimeException("考试中,不能提取答案");
  113. }
  114. LearnAnswer aCond = new LearnAnswer();
  115. aCond.setExamineeId(examineeId);
  116. Map<Long, LearnAnswer> answerMap = learnAnswerMapper.selectLearnAnswerList(aCond).stream().collect(Collectors.toMap(LearnAnswer::getQuestionId, t -> t));
  117. LearnPaper learnPaper = learnPaperService.selectLearnPaperById(examinee.getPaperId());
  118. List<PaperVO.QuestionAnswer> questionList = paperService.loadPaperQuestionAnswers(SecurityUtils.getUserId(), examinee.getPaperId(), answerMap, !examContinue);
  119. AnswerSheet answerSheet = buildAnswerSheet(learnPaper, examinee);
  120. answerSheet.setTotalCount(learnPaper.getNumber());
  121. answerSheet.setWrongCount(examinee.getWrongCount());
  122. answerSheet.setQuestions(questionList);
  123. if(null != examinee.getPaperInfo()) {
  124. JSONObject info = JSONObject.parseObject(examinee.getPaperInfo());
  125. answerSheet.setCollegeId(info.getLong("universityId"));
  126. answerSheet.setCollegeName(info.getString("universityName"));
  127. answerSheet.setMajorId(info.getLong("planId"));
  128. answerSheet.setMajorName(info.getString("majorName"));
  129. answerSheet.setSubjectId(learnPaper.getSubjectId());
  130. answerSheet.setSubjectName(dzSubjectService.selectDzSubjectBySubjectId(learnPaper.getSubjectId()).getSubjectName());
  131. }
  132. if(null != examinee.getStats()) {
  133. answerSheet.setStats(JSONObject.parseObject(examinee.getStats()));
  134. }
  135. return answerSheet;
  136. }
  137. /**
  138. * 交卷
  139. * @param answerSheet
  140. */
  141. @Transactional(rollbackFor = Exception.class)
  142. public void commitAnswerSheet(AnswerSheet answerSheet) {
  143. LearnExaminee exitExaminee = learnExamineeMapper.selectLearnExamineeByExamineeId(answerSheet.getExamineeId());
  144. if(null == exitExaminee) {
  145. throw new RuntimeException("无此答卷" + answerSheet.getExamineeId());
  146. }
  147. LearnAnswer aCond = new LearnAnswer();
  148. aCond.setExamineeId(answerSheet.getExamineeId());
  149. Map<Long, LearnAnswer> answerMap = learnAnswerMapper.selectLearnAnswerList(aCond).stream().collect(Collectors.toMap(LearnAnswer::getQuestionId, t -> t));
  150. List<PaperVO.QuestionAnswer> qaList = paperService.loadPaperQuestionAnswers(null, exitExaminee.getPaperId(), answerMap, true);
  151. Map<Long, PaperVO.QuestionAnswer> questionMap = Maps.newHashMap();
  152. for(PaperVO.QuestionAnswer qa : qaList) {
  153. if(CollectionUtils.isNotEmpty(qa.getSubQuestions())) {
  154. for(PaperVO.QuestionAnswer sqa : qa.getSubQuestions()) {
  155. questionMap.put(sqa.getId(), sqa);
  156. }
  157. } else {
  158. questionMap.put(qa.getId(), qa);
  159. }
  160. }
  161. List<LearnAnswer> answersList = Lists.newArrayList();
  162. Integer wrongCount = 0;
  163. Integer totalScore = 0;
  164. Integer score = 0;
  165. for(PaperVO.QuestionAnswer question : answerSheet.getQuestions()) {
  166. if(CollectionUtils.isNotEmpty(question.getSubQuestions())) {
  167. for(PaperVO.QuestionAnswer sq : question.getSubQuestions()) {
  168. PaperVO.QuestionAnswer stdAnswer = questionMap.get(sq.getId());
  169. LearnAnswer answer = buildAnswer(answerSheet, questionMap, sq, answersList);
  170. if(answerSheet.getIsDone()) {
  171. totalScore += stdAnswer.getTotalScore();
  172. score += answer.getScore();
  173. if(!answer.getState().equals(1)) {
  174. wrongCount++;
  175. }
  176. }
  177. }
  178. } else {
  179. PaperVO.QuestionAnswer stdAnswer = questionMap.get(question.getId());
  180. LearnAnswer answer = buildAnswer(answerSheet, questionMap, question, answersList);
  181. if(answerSheet.getIsDone()) {
  182. totalScore += stdAnswer.getTotalScore();
  183. score += answer.getScore();
  184. if(!answer.getState().equals(1)) {
  185. wrongCount++;
  186. }
  187. }
  188. }
  189. }
  190. if(answersList.size() > 0) {
  191. answersList.stream().forEach(t -> {
  192. if(null == t.getAnswerId()) {
  193. learnAnswerMapper.insertLearnAnswer(t);
  194. } else {
  195. learnAnswerMapper.updateLearnAnswer(t);
  196. }
  197. });
  198. }
  199. LearnExaminee upExaminee = new LearnExaminee();
  200. upExaminee.setExamineeId(answerSheet.getExamineeId());
  201. upExaminee.setDuration(answerSheet.getDuration());
  202. if(answerSheet.getIsDone()) {
  203. upExaminee.setEndTime(new Date());
  204. upExaminee.setState(ExamineeStatus.Commit.getVal());
  205. upExaminee.setScore(score);
  206. upExaminee.setScoreRate(score * 100 / totalScore);
  207. upExaminee.setWrongCount(wrongCount);
  208. learnExamineeMapper.updateLearnExaminee(upExaminee);
  209. if(PaperType.Simulated.getVal().equals(exitExaminee.getPaperType())) {
  210. List<JSONObject> paperStatList = learnExamineeMapper.selectExamRankingStats(upExaminee.getScore(), Lists.newArrayList(exitExaminee.getPaperId()));
  211. JSONObject o = paperStatList.get(0);
  212. Integer totalCount = o.getInteger("totalCount");
  213. Integer maxScore = o.getInteger("maxScore");
  214. Integer avgScore = o.getInteger("avgScore");
  215. Integer lowCount = o.getInteger("lowCount");
  216. JSONObject stats = new JSONObject();
  217. stats.put("score", upExaminee.getScore());
  218. stats.put("rate", upExaminee.getScoreRate());
  219. stats.put("maxScore", maxScore);
  220. stats.put("averageScore", avgScore);
  221. stats.put("hitRate", lowCount * 100 / totalCount);
  222. upExaminee.setStats(JSONObject.toJSONString(stats));
  223. learnExamineeMapper.updateLearnExaminee(upExaminee);
  224. }
  225. } else {
  226. learnExamineeMapper.updateLearnExaminee(upExaminee);
  227. }
  228. if(answerSheet.getIsDone()) {
  229. // 关闭试卷
  230. LearnPaper up = new LearnPaper();
  231. up.setId(exitExaminee.getPaperId());
  232. up.setStatus(PaperStatus.Valid.getVal());
  233. paperMapper.updateLearnPaper(up);
  234. if(PaperType.Practice.getVal().equals(exitExaminee.getPaperType())) {
  235. learnPlanService.updateLearnPlan(exitExaminee.getStudentId());
  236. calculateWrongAnswers(exitExaminee, questionMap, answersList, PaperType.Practice.name());
  237. } else if(PaperType.Simulated.getVal().equals(exitExaminee.getPaperType())) {
  238. calculateWrongAnswers(exitExaminee, questionMap, answersList, PaperType.Simulated.name());
  239. }
  240. }
  241. }
  242. public Set<Integer> invalidStatusSet = Sets.newHashSet(2, 3);
  243. public void calculateWrongAnswers(LearnExaminee examinee, Map<Long, PaperVO.QuestionAnswer> questionMap, List<LearnAnswer> answersList, String source) {
  244. if (CollectionUtils.isEmpty(answersList)){
  245. return;
  246. }
  247. LearnPaper learnPaper = paperMapper.selectLearnPaperById(examinee.getPaperId());
  248. answersList.forEach(t -> {
  249. if (invalidStatusSet.contains(t.getState())) {
  250. LearnWrongBook wrongBookCond = new LearnWrongBook();
  251. wrongBookCond.setStudentId(examinee.getStudentId());
  252. wrongBookCond.setQuestionId(t.getQuestionId());
  253. PaperVO.QuestionAnswer stdAnswer = questionMap.get(t.getQuestionId());
  254. wrongBookCond.setKnownledgeId(stdAnswer.getKnowledgeId());
  255. List<LearnWrongBook> wrongBookList = wrongBookMapper.selectLearnWrongBookList(wrongBookCond);
  256. if (CollectionUtils.isNotEmpty(wrongBookList)) {
  257. LearnWrongBook wrongBook = wrongBookList.get(0);
  258. if (null == wrongBook.getWrongCount()) {
  259. wrongBook.setWrongCount(0L);
  260. }
  261. if (null == wrongBook.getTotalCount()) {
  262. wrongBook.setTotalCount(0L);
  263. }
  264. wrongBookCond.setWrongId(wrongBook.getWrongId());
  265. wrongBookCond.setStudentId(null);
  266. wrongBookCond.setQuestionId(null);
  267. wrongBookCond.setWrongCount(wrongBook.getWrongCount() + 1);
  268. wrongBookCond.setTotalCount(wrongBook.getTotalCount() + 1);
  269. } else {
  270. wrongBookCond.setAnswer1(stdAnswer.getAnswer1());
  271. wrongBookCond.setAnswer2(stdAnswer.getAnswer2());
  272. wrongBookCond.setSubjectId(learnPaper.getSubjectId());
  273. wrongBookCond.setRightCount(0L);
  274. wrongBookCond.setWrongCount(1L);
  275. wrongBookCond.setTotalCount(1L);
  276. // wrongBookCond.setScoreTotal(standard.getScoreTotal());
  277. }
  278. // wrongBookCond.setKnownledgeId(standard.getKnowledgeId());
  279. wrongBookCond.setPaperId(examinee.getPaperId());
  280. wrongBookCond.setScore(t.getScore());
  281. wrongBookCond.setSource(source);
  282. wrongBookCond.setAnswer(t.getAnswer());
  283. wrongBookCond.setState(MxjbContants.QuestionScoreWrong);
  284. if(null == wrongBookCond.getWrongId()) {
  285. wrongBookMapper.insertLearnWrongBook(wrongBookCond);
  286. } else {
  287. wrongBookMapper.updateLearnWrongBook(wrongBookCond);
  288. }
  289. LearnWrongDetail wrongDetail = new LearnWrongDetail();
  290. wrongDetail.setWrongId(wrongBookCond.getWrongId());
  291. wrongDetail.setCreatedTime(new Date());
  292. wrongDetail.setKnownledgeId(stdAnswer.getKnowledgeId());
  293. wrongDetail.setSource(wrongBookCond.getSource());
  294. wrongDetail.setExamineeId(examinee.getExamineeId());
  295. wrongDetail.setStudentId(examinee.getStudentId());
  296. wrongDetail.setPaperId(examinee.getPaperId());
  297. wrongDetailMapper.insertLearnWrongDetail(wrongDetail);
  298. }
  299. });
  300. }
  301. private LearnAnswer buildAnswer(AnswerSheet answerSheet, Map<Long, PaperVO.QuestionAnswer> questionMap, PaperVO.QuestionAnswer question,
  302. List<LearnAnswer> answerList) {
  303. LearnAnswer answer = new LearnAnswer();
  304. PaperVO.QuestionAnswer stdAnswer = questionMap.get(question.getId());
  305. if(null == stdAnswer.getAnswerId()) {
  306. answer.setExamineeId(answerSheet.getExamineeId());
  307. answer.setStudentId(SecurityUtils.getUserId());
  308. answer.setSeq(question.getSeq());
  309. } else {
  310. answer.setAnswerId(stdAnswer.getAnswerId());
  311. }
  312. answerList.add(answer);
  313. answer.setQuestionId(question.getId()); // 错题计算要使用
  314. answer.setKnowledgeId(stdAnswer.getKnowledgeId());
  315. answer.setAnswer(StringUtils.join(question.getAnswers(), ","));
  316. answer.setMark(null != question.getIsMark() && question.getIsMark());
  317. answer.setNotKnow(null != question.getIsNotKnow() && question.getIsNotKnow());
  318. answer.setState(question.calcState(stdAnswer, answerSheet.getIsDone()));
  319. if (answerSheet.getIsDone()) {
  320. answer.setScore(answer.getState() == 1 ? stdAnswer.getTotalScore() : 0);
  321. answer.setScoreRate(answer.getScore() * 100 / stdAnswer.getTotalScore());
  322. }
  323. return answer;
  324. }
  325. private AnswerSheet openExaminee(PaperType paperType, Long paperId, Long studentId) {
  326. LearnPaper paper = paperMapper.selectLearnPaperById(paperId);
  327. LearnExaminee learnExaminee = new LearnExaminee();
  328. learnExaminee.setStudentId(studentId);
  329. learnExaminee.setPaperId(paperId);
  330. learnExaminee.setPaperType(paperType.getVal());
  331. learnExaminee.setState(ExamineeStatus.Exam.getVal());
  332. List<LearnExaminee> examineeList = examineeMapper.selectLearnExamineeList(learnExaminee);
  333. if (CollectionUtils.isNotEmpty(examineeList)) {
  334. learnExaminee = examineeList.get(0);
  335. } else {
  336. learnExaminee.setState(ExamineeStatus.Exam.getVal());
  337. learnExaminee.setBeginTime(new Date());
  338. learnExaminee.setDuration(0L);
  339. examineeMapper.insertLearnExaminee(learnExaminee);
  340. }
  341. return buildAnswerSheet(paper, learnExaminee);
  342. }
  343. private AnswerSheet openExaminee(LearnStudent ls, Long subjectId, SysUser sysUser) {
  344. AMarjorPlan plan = marjorPlanService.selectAMarjorPlanById(ls.getMajorPlanId());
  345. if(null == plan) {
  346. throw new ValidationException("专业id无效");
  347. }
  348. LearnExaminee examinee = new LearnExaminee();
  349. examinee.setStudentId(SecurityUtils.getLoginUser().getUser().getUserId());
  350. examinee.setPaperType(PaperType.Simulated.getVal());
  351. examinee.setPaperKey(subjectId.toString());
  352. List<LearnExaminee> examineeList = examineeMapper.selectLearnExamineeList(examinee);
  353. Set<Long> existPaperIdSet = Sets.newHashSet();
  354. for(LearnExaminee e : examineeList) {
  355. if(ExamineeStatus.Sign.getVal().equals(e.getState())) {
  356. LearnPaper learnPaper = paperMapper.selectLearnPaperById(e.getPaperId());
  357. AnswerSheet answerSheet = buildAnswerSheet(learnPaper, e);
  358. if(null != e.getPaperInfo()) {
  359. JSONObject info = JSONObject.parseObject(e.getPaperInfo());
  360. answerSheet.setCollegeId(info.getLong("universityId"));
  361. answerSheet.setCollegeName(info.getString("universityName"));
  362. answerSheet.setMajorId(info.getLong("planId"));
  363. answerSheet.setMajorName(info.getString("majorName"));
  364. answerSheet.setSubjectId(learnPaper.getSubjectId());
  365. answerSheet.setSubjectName(dzSubjectService.selectDzSubjectBySubjectId(learnPaper.getSubjectId()).getSubjectName());
  366. }
  367. return answerSheet;
  368. }
  369. existPaperIdSet.add(e.getPaperId());
  370. }
  371. LearnPaper paper = getBestPaper(sysUser.getExamType(), plan, subjectId, existPaperIdSet);
  372. examinee.setPaperId(paper.getId());
  373. examinee.setState(ExamineeStatus.Sign.getVal());
  374. examinee.setDuration(0L);
  375. JSONObject evalCountObj = JSONObject.parseObject(sysUser.getEvalCounts());
  376. Integer evalCount = evalCountObj.getInteger(subjectId.toString());
  377. if(null == evalCount || evalCount <= 0) {
  378. throw new ValidationException("超过最大次数限制" + evalCount );
  379. }
  380. SysUser upUser = new SysUser();
  381. upUser.setUserId(sysUser.getUserId());
  382. evalCountObj.put(subjectId.toString(), evalCount - 1);
  383. upUser.setEvalCounts(evalCountObj.toJSONString());
  384. sysUserService.updateUserProfile(upUser);
  385. JSONObject info = new JSONObject();
  386. info.put("universityId", ls.getUniversityId());
  387. info.put("universityName", plan.getUniversityName());
  388. info.put("planId", ls.getMajorPlanId());
  389. info.put("majorName", plan.getMajorName());
  390. examinee.setPaperInfo(info.toJSONString());
  391. examineeMapper.insertLearnExaminee(examinee);
  392. AnswerSheet answerSheet = buildAnswerSheet(paper, examinee);
  393. answerSheet.setCollegeId(ls.getUniversityId());
  394. answerSheet.setCollegeName(plan.getUniversityName());
  395. answerSheet.setMajorId(ls.getMajorPlanId());
  396. answerSheet.setMajorName(plan.getMajorName());
  397. answerSheet.setSubjectId(paper.getSubjectId());
  398. answerSheet.setSubjectName(dzSubjectService.selectDzSubjectBySubjectId(paper.getSubjectId()).getSubjectName());
  399. return answerSheet;
  400. }
  401. /**
  402. * 根据知识点生成一次性练习卷并开始做题
  403. * @return
  404. */
  405. private AnswerSheet openExaminee(Long knowledgeId, Long studentId, Boolean directed) {
  406. String directKey;
  407. AMarjorPlan plan = null;
  408. LearnStudent ls = null;
  409. if(!directed) {
  410. directKey = "";
  411. } else {
  412. ls = learnStudentMapper.selectLearnStudentByStudentId(SecurityUtils.getUserId());
  413. if(null == (plan = marjorPlanService.selectAMarjorPlanById(ls.getMajorPlanId()))) {
  414. throw new ValidationException("专业id无效");
  415. }
  416. directKey = StringUtils.trimToEmpty(ls.getDirectKey());
  417. }
  418. LearnKnowledgeTree knowledgeTree = knowledgeTreeMapper.selectLearnKnowledgeTreeById(knowledgeId);
  419. LearnPaper paper = new LearnPaper();
  420. paper.setPaperType(PaperType.Practice.name());
  421. paper.setRelateId(knowledgeId);
  422. paper.setYear(Calendar.getInstance().get(Calendar.YEAR));
  423. paper.setStatus(PaperStatus.TmpValid.getVal());
  424. paper.setDirectKey(studentId + "_" + directKey);
  425. List<LearnPaper> paperList = paperMapper.selectLearnPaperList(paper);
  426. if (CollectionUtils.isNotEmpty(paperList)) { // 有未做完的
  427. LearnPaper existPaper = paperList.get(0);
  428. LearnExaminee learnExaminee = new LearnExaminee();
  429. learnExaminee.setStudentId(studentId);
  430. learnExaminee.setPaperId(existPaper.getId());
  431. learnExaminee.setPaperType(PaperType.Practice.getVal());
  432. learnExaminee.setState(ExamineeStatus.Exam.getVal());
  433. List<LearnExaminee> examineeList = examineeMapper.selectLearnExamineeList(learnExaminee);
  434. if (CollectionUtils.isNotEmpty(examineeList) && ExamineeStatus.Exam.getVal().equals(examineeList.get(0).getState())) {
  435. LearnExaminee examinee = examineeList.get(0);
  436. LearnAnswer aCond = new LearnAnswer();
  437. aCond.setExamineeId(examinee.getExamineeId());
  438. Map<Long, LearnAnswer> answerMap = learnAnswerMapper.selectLearnAnswerList(aCond).stream().collect(Collectors.toMap(LearnAnswer::getQuestionId, t -> t));
  439. List<PaperVO.QuestionAnswer> questionList = paperService.loadPaperQuestionAnswers(SecurityUtils.getUserId(), examinee.getPaperId(), answerMap, true);
  440. AnswerSheet answerSheet = buildAnswerSheet(existPaper, examinee);
  441. answerSheet.setQuestions(questionList);
  442. answerSheet.setKnowledgeId(existPaper.getRelateId());
  443. return answerSheet;
  444. }
  445. // 关闭试卷
  446. LearnPaper up = new LearnPaper();
  447. up.setId(learnExaminee.getPaperId());
  448. up.setStatus(PaperStatus.Valid.getVal());
  449. paperMapper.updateLearnPaper(up);
  450. }
  451. TestPaperVO.PaperDef paperDef = new TestPaperVO.PaperDef();
  452. paperDef.setTotal(15L);
  453. paperDef.setFillExclude(true);
  454. paperDef.setKnowIds(Lists.newArrayList(knowledgeId));
  455. List<TestPaperVO.TypeDef> typeDefList= Lists.newArrayList();
  456. typeDefList.add(new TestPaperVO.TypeDef("单选题", "单选题", 9, 1));
  457. typeDefList.add(new TestPaperVO.TypeDef("判断题", "判断题", 4, 1));
  458. typeDefList.add(new TestPaperVO.TypeDef("主观题", "主观题", 2, 1));
  459. paperDef.setTypes(typeDefList);
  460. List<LearnPaperQuestion> pqList = paperService.getQuestions(studentId, paperDef);
  461. paper.setPaperSource(0);
  462. paper.setFenshu(0);
  463. paper.setSubjectId(knowledgeTree.getSubjectId());
  464. paper.setPaperName(studentId + "-" + knowledgeTree.getName() + "-" + DateUtils.format(new Date(), "yyyyMMddHHmmss"));
  465. paper.setRelateId(knowledgeId);
  466. paperService.savePaper(paper, pqList);
  467. LearnExaminee learnExaminee = new LearnExaminee();
  468. learnExaminee.setStudentId(studentId);
  469. learnExaminee.setPaperType(PaperType.Practice.getVal());
  470. learnExaminee.setPaperId(paper.getId());
  471. learnExaminee.setPaperKey(String.valueOf(knowledgeId));
  472. learnExaminee.setState(ExamineeStatus.Exam.getVal());
  473. learnExaminee.setBeginTime(new Date());
  474. learnExaminee.setDuration(0L);
  475. if(directed && null != ls.getMajorPlanId()) {
  476. JSONObject info = new JSONObject();
  477. info.put("universityId", ls.getUniversityId());
  478. info.put("universityName", plan.getUniversityName());
  479. info.put("planId", ls.getMajorPlanId());
  480. info.put("majorName", plan.getMajorName());
  481. learnExaminee.setPaperInfo(info.toJSONString());
  482. }
  483. examineeMapper.insertLearnExaminee(learnExaminee);
  484. AnswerSheet answerSheet = buildAnswerSheet(paper, learnExaminee);
  485. List<PaperVO.QuestionAnswer> questionList = paperService.loadPaperQuestionAnswers(SecurityUtils.getUserId(), paper.getId(), null, true);
  486. answerSheet.setQuestions(questionList);
  487. answerSheet.setKnowledgeId(paper.getRelateId());
  488. return answerSheet;
  489. }
  490. private LearnPaper getBestPaper(ExamType examType, AMarjorPlan plan, Long subjectId, Set<Long> existPaperIdSet) {
  491. String groupName = StringUtils.trimToEmpty(plan.getMajorGroup());
  492. LearnPaper paperCond = new LearnPaper();
  493. paperCond.setSubjectId(subjectId);
  494. paperCond.setPaperType(PaperType.Simulated.name());
  495. String baseKey = plan.getUniversityId() + "_" + examType.name();
  496. for(int i = 3; i>0; i--) {
  497. if(i == 3) {
  498. paperCond.setDirectKey(baseKey + "_" + groupName + "_" + plan.getMajorName());
  499. } else if(i == 2) {
  500. paperCond.setDirectKey(baseKey + "_" + groupName);
  501. } else if(StringUtils.isBlank(groupName)) {
  502. break;
  503. } else {
  504. paperCond.setDirectKey(baseKey);
  505. }
  506. List<LearnPaper> paperList = learnPaperService.selectLearnPaperList(paperCond);
  507. for(LearnPaper paper : paperList) {
  508. if(existPaperIdSet.add(paper.getId())) {
  509. return paper;
  510. }
  511. }
  512. }
  513. throw new ValidationException("未初始化院校定向模拟题库: " + subjectId + ":" + plan.getId());
  514. }
  515. private AnswerSheet buildAnswerSheet(LearnPaper paper, LearnExaminee examinee) {
  516. AnswerSheet answerSheet = new AnswerSheet();
  517. answerSheet.setExamineeId(examinee.getExamineeId());
  518. answerSheet.setPaperId(examinee.getPaperId());
  519. answerSheet.setName(paper.getPaperName());
  520. answerSheet.setBeginTime(examinee.getBeginTime());
  521. answerSheet.setDuration(examinee.getDuration());
  522. if(StringUtils.isNotBlank(paper.getPaperInfo())) {
  523. answerSheet.setPaperInfo(JSONObject.parseObject(paper.getPaperInfo(), AnswerSheet.PaperCond.class));
  524. }
  525. if(PaperType.Practice.name().equals(paper.getPaperType())) {
  526. answerSheet.setKnowledgeId(paper.getRelateId());
  527. }
  528. answerSheet.setScoringType("1");
  529. answerSheet.setMode(0L);
  530. answerSheet.setState(examinee.getState());
  531. answerSheet.setAllowAnswer(true);
  532. answerSheet.setAllowScore(false);
  533. return answerSheet;
  534. }
  535. }