ExamServiceImpl.java 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. package com.nju.edu.eval.service.serviceImpl;
  2. import cn.seecoder.bok.common.QuestionType;
  3. import com.nju.edu.eval.data.dao.ExamDao;
  4. import com.nju.edu.eval.data.dao.SampleExamPaperDao;
  5. import com.nju.edu.eval.data.dao.ScoreDao;
  6. import com.nju.edu.eval.data.dao.UserDao;
  7. import com.nju.edu.eval.data.entity.Exam;
  8. import com.nju.edu.eval.data.entity.SampleExamPaper;
  9. import com.nju.edu.eval.data.entity.Score;
  10. import com.nju.edu.eval.data.entity.User;
  11. import com.nju.edu.eval.exception.MyServiceException;
  12. import com.nju.edu.eval.manager.QuestionManager;
  13. import com.nju.edu.eval.model.vo.exam.*;
  14. import com.nju.edu.eval.model.vo.question.QuestionAndScoreVO;
  15. import com.nju.edu.eval.service.ExamService;
  16. import com.nju.edu.eval.service.convertor.ExamConvertor;
  17. import com.nju.edu.eval.util.InviteCodeUtil;
  18. import org.springframework.beans.factory.annotation.Autowired;
  19. import org.springframework.data.domain.Page;
  20. import org.springframework.data.domain.PageRequest;
  21. import org.springframework.data.domain.Sort;
  22. import org.springframework.stereotype.Service;
  23. import java.util.*;
  24. import java.util.stream.Collectors;
  25. /**
  26. * @author mars
  27. */
  28. @Service
  29. public class ExamServiceImpl implements ExamService {
  30. private final ExamDao examDao;
  31. private final UserDao userDao;
  32. private final ScoreDao scoreDao;
  33. private final SampleExamPaperDao sampleExamPaperDao;
  34. private final QuestionManager questionManager;
  35. @Autowired
  36. public ExamServiceImpl(ExamDao examDao, UserDao userDao, ScoreDao scoreDao, QuestionManager questionManager, SampleExamPaperDao sampleExamPaperDao) {
  37. this.examDao = examDao;
  38. this.userDao = userDao;
  39. this.scoreDao = scoreDao;
  40. this.questionManager = questionManager;
  41. this.sampleExamPaperDao = sampleExamPaperDao;
  42. }
  43. @Override
  44. public NewExamVO createExam(CreateExamVO examVO, Integer creatorId) {
  45. User user = userDao.findById(creatorId).get();
  46. Exam exam = new Exam(examVO.getExamName(), examVO.getPosition(),
  47. examVO.getSkills(), examVO.getStartTime(),
  48. examVO.getEndTime(), examVO.getComment(), user);
  49. // 只有非公开的考试才需要邀请码
  50. if (!examVO.getIsPublic()) {
  51. exam.setIsPublic(0);
  52. exam.setInviteCode(InviteCodeUtil.produceInviteCode());
  53. } else {
  54. exam.setIsPublic(1);
  55. }
  56. if (examVO.getAntiCheating()) {
  57. exam.setAntiCheating(1);
  58. } else {
  59. exam.setAntiCheating(0);
  60. }
  61. examDao.save(exam);
  62. // List<CompleteQuestionVO> questionVOs = questionManager.getRecommendQuestions(examVO.getSkills(), examVO.getDifficulty());
  63. return new NewExamVO(exam.getId(), exam.getInviteCode(), exam.getDifficulty());
  64. }
  65. @Override
  66. public ExamListAndNumVO getExamList(int userId, int pageSize, int pageNum) {
  67. Page<Exam> examEntities = examDao.findAll(PageRequest.of(pageNum, pageSize, Sort.by(Sort.Direction.DESC, "startTime")));
  68. List<ExamVO> examVOList = examEntities.get()
  69. .map(ExamConvertor::convertToVO).collect(Collectors.toList());
  70. User user = userDao.findById(userId).orElse(null);
  71. List<Exam> currentUserExams = user.getExams();
  72. for (ExamVO examVO : examVOList) {
  73. Exam exam = examDao.findById(examVO.getExamId()).orElse(null);
  74. if (currentUserExams.contains(exam)) {
  75. examVO.setIsJoined(true);
  76. }
  77. }
  78. return new ExamListAndNumVO(examVOList, (int) examEntities.getTotalElements());
  79. }
  80. @Override
  81. public List<ExamInfoVO> getExamListOfTimeRange(Date startTime, Date endTime) {
  82. List<Exam> examList = examDao.findAllByStartTimeBetweenOrderByStartTime(startTime, endTime);
  83. return examList.stream().map(ExamConvertor::convertToExamInfoVO).collect(Collectors.toList());
  84. }
  85. @Override
  86. public ExamVO getExamById(Integer id) {
  87. Exam exam = examDao.findById(id).orElse(null);
  88. return ExamConvertor.convertToVO(exam);
  89. }
  90. @Override
  91. public QuestionAndScoreVO getExamQuestionAndScoreById(Integer id) {
  92. Exam exam = examDao.findById(id).orElse(null);
  93. if (exam == null) {
  94. throw new MyServiceException("A0411", "没有对应的考试");
  95. }
  96. if (exam.getQuestionAndScore().size() == 0) {
  97. throw new MyServiceException("A0411", "该考试还未设置题目");
  98. }
  99. return new QuestionAndScoreVO(exam.getQuestionAndScore());
  100. }
  101. @Override
  102. public List<String> getCodeQuestionIdsByExamId(Integer id) {
  103. Exam exam = examDao.findById(id).orElse(null);
  104. if (exam == null) {
  105. throw new MyServiceException("A0411", "没有对应的考试");
  106. }
  107. Set<String> questionIds = exam.getQuestionAndScore().keySet();
  108. return questionIds.stream()
  109. .filter(questionId -> questionManager.getQuestionById(questionId).get().getType() == QuestionType.CODE)
  110. .collect(Collectors.toList());
  111. }
  112. @Override
  113. public void updateQuestionsOfExam(Integer examId, QuestionScoreMapVO questionScoreMapVO) {
  114. Exam exam = examDao.findById(examId).orElse(null);
  115. if (exam == null) {
  116. throw new MyServiceException("A0411", "没有对应的考试");
  117. }
  118. Map<String, Double> questionScoreMap = questionScoreMapVO.getQuestionScoreMap();
  119. // for (Map.Entry<String, Double> entry : questionScoreMap.entrySet()) {
  120. // exam.getQuestionAndScore().put(entry.getKey(), entry.getValue());
  121. // }
  122. exam.setQuestionAndScore(questionScoreMap);
  123. examDao.save(exam);
  124. }
  125. @Override
  126. public Optional<Exam> extendExamTime(Integer examId, Date endTime) throws MyServiceException {
  127. Exam exam = examDao.findById(examId).orElse(null);
  128. if (exam != null) {
  129. if (endTime.before(exam.getStartTime()) || endTime.before(exam.getEndTime())) {
  130. throw new MyServiceException("A0420", "结束时间不能早于原先结束时间和开始时间");
  131. }
  132. exam.setEndTime(endTime);
  133. examDao.save(exam);
  134. } else {
  135. throw new MyServiceException("A0411", "没有对应的考试");
  136. }
  137. return Optional.of(exam);
  138. }
  139. @Override
  140. public ExamListAndNumVO getExamsByCurrentStudent(Integer id, int pageSize, int pageNum, int state) {
  141. User student = userDao.findById(id).orElse(null);
  142. if (student != null) {
  143. List<Exam> exams = student.getExams();
  144. return new ExamListAndNumVO(
  145. exams.stream().sorted(Comparator.comparing(Exam::getStartTime).reversed())
  146. .filter(exam -> state == 0 || exam.getState().ordinal() + 1 == state)
  147. .skip(pageSize * (pageNum - 1)).limit(pageSize)
  148. .map(ExamConvertor::convertToJoinedVO).collect(Collectors.toList()),
  149. //输出总个数,用于前端分页
  150. (int) exams.stream().filter(exam -> state == 0 || exam.getState().ordinal() + 1 == state).count());
  151. } else {
  152. throw new MyServiceException("A0201", "用户账号不存在");
  153. }
  154. }
  155. @Override
  156. public ExamListAndNumVO getExamsCreatedByCurrentTeacherOrHR(Integer id, int pageSize, int pageNum, int state) {
  157. User user = userDao.findById(id).orElse(null);
  158. if (user != null) {
  159. return new ExamListAndNumVO(examDao.findAllByCreator_Id(user.getId()).stream()
  160. .filter(exam -> state == 0 || exam.getState().ordinal() + 1 == state)
  161. .sorted(Comparator.comparing(Exam::getStartTime).reversed())
  162. .skip(pageSize * (pageNum - 1)).limit(pageSize)
  163. .map(ExamConvertor::convertToVO).collect(Collectors.toList()),
  164. //输出总个数,用于前端分页
  165. (int) examDao.findAllByCreator_Id(user.getId()).stream()
  166. .filter(exam -> state == 0 || exam.getState().ordinal() + 1 == state).count());
  167. } else {
  168. throw new MyServiceException("A0201", "用户账号不存在");
  169. }
  170. }
  171. @Override
  172. public boolean takeExam(Integer userId, Integer examId, String inviteCodeInput) {
  173. User user = userDao.findById(userId).orElse(null);
  174. System.out.println(user.getExams());
  175. Exam exam = examDao.findById(examId).orElse(null);
  176. if (user.getExams().contains(exam)) {
  177. throw new MyServiceException("A0506", "已加入考试!");
  178. }
  179. if (user != null) {
  180. String correctInviteCode = exam.getInviteCode();
  181. if (exam.getIsPublic() == 1 || correctInviteCode.equals(inviteCodeInput)) {
  182. // List<Exam> examsOfUser = user.getExams();
  183. // // 更新用户参加的考试
  184. // examsOfUser.add(exam);
  185. // userDao.save(user);
  186. Score score = new Score(user, exam);
  187. scoreDao.save(score);
  188. return true;
  189. } else {
  190. throw new MyServiceException("A0210", "邀请码输入错误");
  191. }
  192. } else {
  193. throw new MyServiceException("A0201", "用户账号不存在");
  194. }
  195. }
  196. @Override
  197. public void setExamQuestionsBySampleExamPaper(int examId, int sampleExamPaperId) {
  198. Exam exam = examDao.findById(examId).orElse(null);
  199. SampleExamPaper sampleExamPaper = sampleExamPaperDao.findById(sampleExamPaperId).orElse(null);
  200. if (exam == null) {
  201. throw new MyServiceException("A0201", "无对应的考试");
  202. }
  203. if (sampleExamPaper == null) {
  204. throw new MyServiceException("A0201", "无对应样卷");
  205. }
  206. Map<String, Double> questionAndScore = new HashMap<>(sampleExamPaper.getQuestionAndScore());
  207. exam.setQuestionAndScore(questionAndScore);
  208. examDao.save(exam);
  209. }
  210. }