ExamServiceImpl.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. // return new ExamListAndNumVO(examVOList, (int) examEntities.getTotalElements());
  71. List<Exam> exams = examDao.findByIsHiddenFalse();
  72. List<ExamVO> examVOList = exams.stream()
  73. .sorted(Comparator.comparing(Exam::getStartTime).reversed())
  74. .filter(exam -> !exam.getIsHidden())
  75. .skip(pageSize * (pageNum - 1)).limit(pageSize)
  76. .map(ExamConvertor::convertToVO).collect(Collectors.toList());
  77. User user = userDao.findById(userId).orElse(null);
  78. List<Exam> currentUserExams = user.getExams();
  79. for (ExamVO examVO : examVOList) {
  80. Exam exam = examDao.findById(examVO.getExamId()).orElse(null);
  81. if (currentUserExams.contains(exam)) {
  82. examVO.setIsJoined(true);
  83. }
  84. }
  85. return new ExamListAndNumVO(examVOList, (int) exams.size());
  86. }
  87. @Override
  88. public List<ExamInfoVO> getExamListOfTimeRange(Date startTime, Date endTime) {
  89. List<Exam> examList = examDao.findAllByStartTimeBetweenOrderByStartTime(startTime, endTime);
  90. return examList.stream().map(ExamConvertor::convertToExamInfoVO).collect(Collectors.toList());
  91. }
  92. @Override
  93. public ExamVO getExamById(Integer id) {
  94. Exam exam = examDao.findById(id).orElse(null);
  95. return ExamConvertor.convertToVO(exam);
  96. }
  97. @Override
  98. public QuestionAndScoreVO getExamQuestionAndScoreById(Integer id) {
  99. Exam exam = examDao.findById(id).orElse(null);
  100. if (exam == null) {
  101. throw new MyServiceException("A0411", "没有对应的考试");
  102. }
  103. if (exam.getQuestionAndScore().size() == 0) {
  104. throw new MyServiceException("A0411", "该考试还未设置题目");
  105. }
  106. return new QuestionAndScoreVO(exam.getQuestionAndScore());
  107. }
  108. @Override
  109. public List<String> getCodeQuestionIdsByExamId(Integer id) {
  110. Exam exam = examDao.findById(id).orElse(null);
  111. if (exam == null) {
  112. throw new MyServiceException("A0411", "没有对应的考试");
  113. }
  114. Set<String> questionIds = exam.getQuestionAndScore().keySet();
  115. return questionIds.stream()
  116. .filter(questionId -> questionManager.getQuestionById(questionId).get().getType() == QuestionType.CODE)
  117. .collect(Collectors.toList());
  118. }
  119. @Override
  120. public void updateQuestionsOfExam(Integer examId, QuestionScoreMapVO questionScoreMapVO) {
  121. Exam exam = examDao.findById(examId).orElse(null);
  122. if (exam == null) {
  123. throw new MyServiceException("A0411", "没有对应的考试");
  124. }
  125. Map<String, Double> questionScoreMap = questionScoreMapVO.getQuestionScoreMap();
  126. // for (Map.Entry<String, Double> entry : questionScoreMap.entrySet()) {
  127. // exam.getQuestionAndScore().put(entry.getKey(), entry.getValue());
  128. // }
  129. exam.setQuestionAndScore(questionScoreMap);
  130. examDao.save(exam);
  131. }
  132. @Override
  133. public Optional<Exam> extendExamTime(Integer examId, Date endTime) throws MyServiceException {
  134. Exam exam = examDao.findById(examId).orElse(null);
  135. if (exam != null) {
  136. if (endTime.before(exam.getStartTime()) || endTime.before(exam.getEndTime())) {
  137. throw new MyServiceException("A0420", "结束时间不能早于原先结束时间和开始时间");
  138. }
  139. exam.setEndTime(endTime);
  140. examDao.save(exam);
  141. } else {
  142. throw new MyServiceException("A0411", "没有对应的考试");
  143. }
  144. return Optional.of(exam);
  145. }
  146. @Override
  147. public ExamListAndNumVO getExamsByCurrentStudent(Integer id, int pageSize, int pageNum, int state) {
  148. User student = userDao.findById(id).orElse(null);
  149. if (student != null) {
  150. List<Exam> exams = student.getExams();
  151. return new ExamListAndNumVO(
  152. exams.stream()
  153. .sorted(Comparator.comparing(Exam::getStartTime).reversed())
  154. .filter(exam -> state == 0 || exam.getState().ordinal() + 1 == state)
  155. .filter(exam -> !exam.getIsHidden())
  156. .skip(pageSize * (pageNum - 1)).limit(pageSize)
  157. .map(ExamConvertor::convertToJoinedVO).collect(Collectors.toList()),
  158. //输出总个数,用于前端分页
  159. (int) exams.stream().filter(exam -> state == 0 || exam.getState().ordinal() + 1 == state).count());
  160. } else {
  161. throw new MyServiceException("A0201", "用户账号不存在");
  162. }
  163. }
  164. @Override
  165. public ExamListAndNumVO getExamsCreatedByCurrentTeacherOrHR(Integer id, int pageSize, int pageNum, int state) {
  166. User user = userDao.findById(id).orElse(null);
  167. if (user != null) {
  168. return new ExamListAndNumVO(examDao.findAllByCreator_Id(user.getId()).stream()
  169. .filter(exam -> state == 0 || exam.getState().ordinal() + 1 == state)
  170. .sorted(Comparator.comparing(Exam::getStartTime).reversed())
  171. .skip(pageSize * (pageNum - 1)).limit(pageSize)
  172. .map(ExamConvertor::convertToVO).collect(Collectors.toList()),
  173. //输出总个数,用于前端分页
  174. (int) examDao.findAllByCreator_Id(user.getId()).stream()
  175. .filter(exam -> state == 0 || exam.getState().ordinal() + 1 == state).count());
  176. } else {
  177. throw new MyServiceException("A0201", "用户账号不存在");
  178. }
  179. }
  180. @Override
  181. public boolean takeExam(Integer userId, Integer examId, String inviteCodeInput) {
  182. User user = userDao.findById(userId).orElse(null);
  183. System.out.println(user.getExams());
  184. Exam exam = examDao.findById(examId).orElse(null);
  185. if (user.getExams().contains(exam)) {
  186. throw new MyServiceException("A0506", "已加入考试!");
  187. }
  188. if (user != null) {
  189. String correctInviteCode = exam.getInviteCode();
  190. if (exam.getIsPublic() == 1 || correctInviteCode.equals(inviteCodeInput)) {
  191. // List<Exam> examsOfUser = user.getExams();
  192. // // 更新用户参加的考试
  193. // examsOfUser.add(exam);
  194. // userDao.save(user);
  195. Score score = new Score(user, exam);
  196. scoreDao.save(score);
  197. return true;
  198. } else {
  199. throw new MyServiceException("A0210", "邀请码输入错误");
  200. }
  201. } else {
  202. throw new MyServiceException("A0201", "用户账号不存在");
  203. }
  204. }
  205. @Override
  206. public void setExamQuestionsBySampleExamPaper(int examId, int sampleExamPaperId) {
  207. Exam exam = examDao.findById(examId).orElse(null);
  208. SampleExamPaper sampleExamPaper = sampleExamPaperDao.findById(sampleExamPaperId).orElse(null);
  209. if (exam == null) {
  210. throw new MyServiceException("A0201", "无对应的考试");
  211. }
  212. if (sampleExamPaper == null) {
  213. throw new MyServiceException("A0201", "无对应样卷");
  214. }
  215. Map<String, Double> questionAndScore = new HashMap<>(sampleExamPaper.getQuestionAndScore());
  216. exam.setQuestionAndScore(questionAndScore);
  217. examDao.save(exam);
  218. }
  219. @Override
  220. public boolean updateExamName (Integer userId, Integer examId, String name){
  221. Exam exam = examDao.findById(examId).orElse(null);
  222. if (exam == null) {
  223. throw new MyServiceException("A0201", "无对应的考试");
  224. }
  225. exam.setExamName(name);
  226. examDao.save(exam);
  227. return true;
  228. }
  229. }