SentenceQuestionService.java 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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.nuliji.tools.mybatis.Example;
  8. import com.qxgmat.data.dao.SentenceQuestionMapper;
  9. import com.qxgmat.data.dao.entity.Manager;
  10. import com.qxgmat.data.dao.entity.Question;
  11. import com.qxgmat.data.dao.entity.SentenceQuestion;
  12. import com.qxgmat.data.dao.entity.UserQuestion;
  13. import com.qxgmat.data.inline.PaperStat;
  14. import com.qxgmat.data.relation.SentenceQuestionRelationMapper;
  15. import com.qxgmat.data.relation.entity.SentenceQuestionRelation;
  16. import org.slf4j.Logger;
  17. import org.slf4j.LoggerFactory;
  18. import org.springframework.stereotype.Service;
  19. import javax.annotation.Resource;
  20. import java.util.*;
  21. import java.util.stream.Collectors;
  22. @Service
  23. public class SentenceQuestionService extends AbstractService {
  24. private static final Logger logger = LoggerFactory.getLogger(SentenceQuestionService.class);
  25. @Resource
  26. private SentenceQuestionMapper sentenceQuestionMapper;
  27. @Resource
  28. private QuestionService questionService;
  29. @Resource
  30. private SentenceQuestionRelationMapper sentenceQuestionRelationMapper;
  31. /**
  32. * 后台搜索长难句
  33. * @param page
  34. * @param size
  35. * @param keyword
  36. * @return
  37. */
  38. public Page<SentenceQuestion> searchAdmin(int page, int size, String keyword){
  39. Example example = new Example(SentenceQuestion.class);
  40. if(keyword != null)
  41. example.and(
  42. example.createCriteria()
  43. .orLike("title", "%"+keyword+"%")
  44. .orLike("subject", "%"+keyword+"%")
  45. );
  46. return page(() -> select(sentenceQuestionMapper, example), page, size);
  47. }
  48. /**
  49. * 根据题目编号id列表获取关联题目
  50. * @param ids
  51. * @return
  52. */
  53. public List<SentenceQuestionRelation> listWithRelationByIds(Number[] ids){
  54. List<SentenceQuestion> p = select(sentenceQuestionMapper, ids);
  55. return relation(p);
  56. }
  57. /**
  58. * 根据题目编号id列表获取关联题目
  59. * @param ids
  60. * @return
  61. */
  62. public Map<Number, SentenceQuestionRelation> mapWithRelationByIds(Number[] ids){
  63. List<SentenceQuestion> p = select(sentenceQuestionMapper, ids);
  64. List<SentenceQuestionRelation> list = relation(p);
  65. Map<Number, SentenceQuestionRelation> map = new HashMap<>();
  66. for(SentenceQuestionRelation relation : list){
  67. map.put(relation.getId(), relation);
  68. }
  69. return map;
  70. }
  71. /**
  72. * 累加做题记录到sentenceQuestion
  73. * @param question
  74. */
  75. public void accumulation(UserQuestion question){
  76. sentenceQuestionRelationMapper.accumulation(question.getQuestionNoId(), 1, question.getTime(), question.getIsCorrect());
  77. }
  78. /**
  79. * 根据题目获取总试卷统计信息
  80. * @param questionNoList
  81. * @return
  82. */
  83. public PaperStat statPaper(List<SentenceQuestion> questionNoList){
  84. PaperStat stat = new PaperStat();
  85. Integer totalTime = 0;
  86. Integer totalNumber = 0;
  87. Integer totalCorrect = 0;
  88. for(SentenceQuestion questionNo : questionNoList){
  89. totalTime += questionNo.getTotalTime();
  90. totalNumber += questionNo.getTotalNumber();
  91. totalCorrect += questionNo.getTotalCorrect();
  92. }
  93. stat.setTotalCorrect(totalCorrect);
  94. stat.setTotalNumber(totalNumber);
  95. stat.setTotalTime(totalTime);
  96. return stat;
  97. }
  98. /**
  99. * 根据试卷分组获取统计信息
  100. * @param questionNoIdsMap
  101. * @return
  102. */
  103. public Map<Integer, PaperStat> statPaperMap(Map<Integer, Integer[]> questionNoIdsMap){
  104. Map<Integer, PaperStat> relationMap = new HashMap<>();
  105. List<Integer> ids = new ArrayList<>();
  106. for(Integer[] questionNoIds : questionNoIdsMap.values()){
  107. ids.addAll(Arrays.stream(questionNoIds).collect(Collectors.toList()));
  108. }
  109. List<SentenceQuestion> questionNoList = select(ids);
  110. Map questionNoMap = Transform.getMap(questionNoList, SentenceQuestion.class, "id");
  111. List<SentenceQuestion> l = new ArrayList<>();
  112. for(Integer k: questionNoIdsMap.keySet()){
  113. l.clear();
  114. for (Integer questionNoId : questionNoIdsMap.get(k)){
  115. l.add((SentenceQuestion)questionNoMap.get(questionNoId));
  116. }
  117. relationMap.put(k, statPaper(l));
  118. }
  119. return relationMap;
  120. }
  121. /**
  122. * 根据长难句题目关系,获取完整题目:列表
  123. * @param p
  124. * @return
  125. */
  126. public List<SentenceQuestionRelation> relation(List<SentenceQuestion> p){
  127. List<SentenceQuestionRelation> relationList = Transform.convert(p, SentenceQuestionRelation.class);
  128. Collection questionIds = Transform.getIds(p, SentenceQuestion.class, "questionId");
  129. List<Question> questions = questionService.select(questionIds);
  130. Transform.combine(relationList, questions, SentenceQuestionRelation.class, "questionId", "question", Question.class, "id");
  131. return relationList;
  132. }
  133. /**
  134. * 根据长难句题目关系,获取完整题目:单个
  135. * @param p
  136. * @return
  137. */
  138. public SentenceQuestionRelation relation(SentenceQuestion p){
  139. SentenceQuestionRelation relation = Transform.convert(p, SentenceQuestionRelation.class);
  140. Question question = questionService.get(p.getQuestionId());
  141. relation.setQuestion(question);
  142. return relation;
  143. }
  144. /**
  145. * 根据序号查询
  146. * @param no
  147. * @return
  148. */
  149. public SentenceQuestion getByNo(Integer no){
  150. Example example = new Example(SentenceQuestion.class);
  151. example.and(
  152. example.createCriteria()
  153. .andEqualTo("no", no)
  154. );
  155. return one(sentenceQuestionMapper, example);
  156. }
  157. /**
  158. * 获取所有组卷试题
  159. * @return
  160. */
  161. public List<SentenceQuestion> allPaper(){
  162. Example example = new Example(SentenceQuestion.class);
  163. example.and(
  164. example.createCriteria()
  165. .andEqualTo("isPaper", 1)
  166. );
  167. example.orderBy("no").asc();
  168. return select(sentenceQuestionMapper, example);
  169. }
  170. public SentenceQuestion add(SentenceQuestion question){
  171. int result = insert(sentenceQuestionMapper, question);
  172. question = one(sentenceQuestionMapper, question.getId());
  173. if(question == null){
  174. throw new SystemException("题目添加失败");
  175. }
  176. return question;
  177. }
  178. public SentenceQuestion edit(SentenceQuestion question){
  179. SentenceQuestion in = one(sentenceQuestionMapper, question.getId());
  180. if(in == null){
  181. throw new ParameterException("题目不存在");
  182. }
  183. int result = update(sentenceQuestionMapper, question);
  184. return question;
  185. }
  186. public boolean delete(Number id){
  187. SentenceQuestion in = one(sentenceQuestionMapper, id);
  188. if(in == null){
  189. throw new ParameterException("题目不存在");
  190. }
  191. int result = delete(sentenceQuestionMapper, id);
  192. return result > 0;
  193. }
  194. public SentenceQuestion get(Number id){
  195. SentenceQuestion in = one(sentenceQuestionMapper, id);
  196. if(in == null){
  197. throw new ParameterException("题目不存在");
  198. }
  199. return in;
  200. }
  201. public Page<SentenceQuestion> select(int page, int pageSize){
  202. return select(sentenceQuestionMapper, page, pageSize);
  203. }
  204. public Page<SentenceQuestion> select(Integer[] ids){
  205. return page(()-> select(sentenceQuestionMapper, ids), 1, ids.length);
  206. }
  207. public List<SentenceQuestion> select(Collection ids){
  208. return select(sentenceQuestionMapper, ids);
  209. }
  210. }