QuestionService.java 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. package com.qxgmat.service.inline;
  2. import com.github.pagehelper.Page;
  3. import com.nuliji.tools.AbstractService;
  4. import com.nuliji.tools.Transform;
  5. import com.nuliji.tools.exception.ParameterException;
  6. import com.nuliji.tools.exception.SystemException;
  7. import com.qxgmat.data.dao.QuestionMapper;
  8. import com.qxgmat.data.dao.entity.Question;
  9. import com.qxgmat.data.dao.entity.UserCollectQuestion;
  10. import com.qxgmat.data.dao.entity.UserQuestion;
  11. import com.qxgmat.data.inline.PaperStat;
  12. import com.qxgmat.data.relation.QuestionRelationMapper;
  13. import org.slf4j.Logger;
  14. import org.slf4j.LoggerFactory;
  15. import org.springframework.stereotype.Service;
  16. import javax.annotation.Resource;
  17. import java.util.*;
  18. import java.util.stream.Collectors;
  19. @Service
  20. public class QuestionService extends AbstractService {
  21. private static final Logger logger = LoggerFactory.getLogger(QuestionService.class);
  22. @Resource
  23. private QuestionMapper questionMapper;
  24. @Resource
  25. private QuestionRelationMapper questionRelationMapper;
  26. /**
  27. * 根据题目获取总试卷统计信息
  28. * @param questionNoList
  29. * @return
  30. */
  31. public PaperStat statPaper(List<Question> questionNoList){
  32. PaperStat stat = new PaperStat();
  33. Integer totalTime = 0;
  34. Integer totalNumber = 0;
  35. Integer totalCorrect = 0;
  36. for(Question question : questionNoList){
  37. totalTime += question.getTotalTime();
  38. totalNumber += question.getTotalNumber();
  39. totalCorrect += question.getTotalCorrect();
  40. }
  41. stat.setTotalCorrect(totalCorrect);
  42. stat.setTotalNumber(totalNumber);
  43. stat.setTotalTime(totalTime);
  44. return stat;
  45. }
  46. /**
  47. * 根据试卷分组获取统计信息
  48. * @param questionIdsMap
  49. * @return
  50. */
  51. public Map<Integer, PaperStat> statPaperMap(Map<Integer, Integer[]> questionIdsMap){
  52. Map<Integer, PaperStat> relationMap = new HashMap<>();
  53. List<Integer> ids = new ArrayList<>();
  54. for(Integer[] questionIds : questionIdsMap.values()){
  55. ids.addAll(Arrays.stream(questionIds).collect(Collectors.toList()));
  56. }
  57. List<Question> questionList = select(ids);
  58. Map questionMap = Transform.getMap(questionList, Question.class, "id");
  59. List<Question> l = new ArrayList<>();
  60. for(Integer k: questionIdsMap.keySet()){
  61. l.clear();
  62. for (Integer questionNoId : questionIdsMap.get(k)){
  63. l.add((Question)questionMap.get(questionNoId));
  64. }
  65. relationMap.put(k, statPaper(l));
  66. }
  67. return relationMap;
  68. }
  69. /**
  70. * 累加做题记录到questionNo
  71. * @param question
  72. */
  73. public void accumulation(UserQuestion question){
  74. questionRelationMapper.accumulation(question.getQuestionId(), 1, question.getUserTime(), question.getIsCorrect());
  75. }
  76. /**
  77. * 累加做题记录到questionNo
  78. * @param question
  79. */
  80. public void accumulationCollect(UserCollectQuestion question, int collect){
  81. questionRelationMapper.accumulationCollect(question.getQuestionId(), collect);
  82. }
  83. public Question add(Question question){
  84. // 按实际更新更改更新时间
  85. if (question.getQxContent() != null && !question.getQxContent().isEmpty()){
  86. question.setQxTime(new Date());
  87. }else{
  88. question.setQxContent("");
  89. }
  90. if (question.getOfficialContent() != null && !question.getOfficialContent().isEmpty()){
  91. question.setOfficialTime(new Date());
  92. }else{
  93. question.setOfficialContent("");
  94. }
  95. if (question.getAssociationContent() != null && question.getAssociationContent().length > 0){
  96. question.setAssociationTime(new Date());
  97. }
  98. int result = insert(questionMapper, question);
  99. question = one(questionMapper, question.getId());
  100. if(question == null){
  101. throw new SystemException("题目添加失败");
  102. }
  103. return question;
  104. }
  105. public Question edit(Question question){
  106. Question in = one(questionMapper, question.getId());
  107. if(in == null){
  108. throw new ParameterException("题目不存在");
  109. }
  110. // 按实际更新更改更新时间
  111. if (question.getQxContent() != null && (in.getQxContent() == null || !in.getQxContent().equals(question.getQxContent()))){
  112. question.setQxTime(new Date());
  113. }
  114. if (question.getOfficialContent() != null && (in.getOfficialContent() == null || !in.getOfficialContent().equals(question.getOfficialContent()))){
  115. question.setOfficialTime(new Date());
  116. }
  117. if (question.getAssociationContent() != null && (in.getAssociationContent() == null || in.getAssociationContent().length != question.getAssociationContent().length)){
  118. question.setAssociationTime(new Date());
  119. }
  120. int result = update(questionMapper, question);
  121. return question;
  122. }
  123. public boolean delete(Number id){
  124. Question in = one(questionMapper, id);
  125. if(in == null){
  126. throw new ParameterException("题目不存在");
  127. }
  128. int result = delete(questionMapper, id);
  129. return result > 0;
  130. }
  131. public Question get(Number id){
  132. Question in = one(questionMapper, id);
  133. if(in == null){
  134. throw new ParameterException("题目不存在");
  135. }
  136. return in;
  137. }
  138. public Page<Question> select(int page, int pageSize){
  139. return select(questionMapper, page, pageSize);
  140. }
  141. public Page<Question> select(Integer[] ids){
  142. return page(()->select(questionMapper, ids), 1, ids.length);
  143. }
  144. public List<Question> select(Collection ids){
  145. return select(questionMapper, ids);
  146. }
  147. }