QuestionNoService.java 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. package com.qxgmat.service.inline;
  2. import com.github.pagehelper.Page;
  3. import com.nuliji.tools.AbstractService;
  4. import com.nuliji.tools.PageResult;
  5. import com.nuliji.tools.Transform;
  6. import com.nuliji.tools.exception.ParameterException;
  7. import com.nuliji.tools.exception.SystemException;
  8. import com.nuliji.tools.mybatis.Example;
  9. import com.qxgmat.data.constants.enums.status.DirectionStatus;
  10. import com.qxgmat.data.dao.QuestionNoMapper;
  11. import com.qxgmat.data.dao.entity.Question;
  12. import com.qxgmat.data.dao.entity.QuestionNo;
  13. import com.qxgmat.data.dao.entity.UserQuestion;
  14. import com.qxgmat.data.inline.PaperStat;
  15. import com.qxgmat.data.relation.QuestionNoRelationMapper;
  16. import com.qxgmat.data.relation.entity.QuestionNoRelation;
  17. import org.slf4j.Logger;
  18. import org.slf4j.LoggerFactory;
  19. import org.springframework.stereotype.Service;
  20. import org.springframework.transaction.annotation.Transactional;
  21. import javax.annotation.Resource;
  22. import java.util.*;
  23. import java.util.stream.Collectors;
  24. @Service
  25. public class QuestionNoService extends AbstractService {
  26. private static final Logger logger = LoggerFactory.getLogger(QuestionNoService.class);
  27. protected boolean SOFT_FLAG = true;
  28. @Resource
  29. private QuestionNoMapper questionNoMapper;
  30. @Resource
  31. private QuestionNoRelationMapper questionNoRelationMapper;
  32. @Resource
  33. private QuestionService questionService;
  34. // private List<String> stops = new ArrayList<String>(){{
  35. // add(".");
  36. // add(",");
  37. // add("!");
  38. // add(":");
  39. // add(";");
  40. // add("?");
  41. // }};
  42. /**
  43. * 根据题干搜索相似题目: 相似度80%
  44. * @param page
  45. * @param size
  46. * @param stem
  47. * @return
  48. */
  49. public PageResult<QuestionNoRelation> searchStem(int page, int size, String stem){
  50. // String[] stems = stem.replaceAll("\\.?,?!?:?;?\\??", "").split(" ");
  51. Page<QuestionNo> p = page(()->{
  52. questionNoRelationMapper.searchStem(stem);
  53. }, page, size);
  54. Collection ids = Transform.getIds(p, QuestionNo.class, "id");
  55. // 获取详细数据
  56. List<QuestionNo> list = select(ids);
  57. return new PageResult<>(relation(list), p.getTotal());
  58. }
  59. /**
  60. * 根据题目编号搜索相似题目
  61. * @param page
  62. * @param size
  63. * @param no
  64. * @param module
  65. * @return
  66. */
  67. public PageResult<QuestionNoRelation> searchNo(int page, int size, String no, String module){
  68. Example example = new Example(QuestionNo.class);
  69. example.and(
  70. example.createCriteria()
  71. .andLike("no", no)
  72. );
  73. if (module != null)
  74. example.and(
  75. example.createCriteria()
  76. .andEqualTo("module", module)
  77. );
  78. example.orderBy("id").asc();
  79. Page<QuestionNo> p = page(()->select(questionNoMapper, example), page, size);
  80. return new PageResult<>(relation(p), p.getTotal());
  81. }
  82. /**
  83. * 根据题目标号列表获取题目
  84. * @param nos
  85. * @param module
  86. * @return
  87. */
  88. public List<QuestionNoRelation> listWithRelationByNos(String[] nos, String module){
  89. if (nos.length == 0) return new ArrayList<>();
  90. Example example = new Example(QuestionNo.class);
  91. example.and(
  92. example.createCriteria()
  93. .andIn("no", Arrays.stream(nos).collect(Collectors.toList()))
  94. );
  95. if (module != null)
  96. example.and(
  97. example.createCriteria()
  98. .andEqualTo("module", module)
  99. );
  100. List<QuestionNo> p = select(questionNoMapper, example);
  101. return relation(p);
  102. }
  103. /**
  104. * 根据题目获取关联的题目编号
  105. * @param questionId
  106. * @return
  107. */
  108. public List<QuestionNo> listByQuestion(Number questionId){
  109. Example example = new Example(QuestionNo.class);
  110. example.and(
  111. example.createCriteria()
  112. .andEqualTo("questionId", questionId)
  113. );
  114. return select(questionNoMapper, example);
  115. }
  116. /**
  117. * 根据题目编号id列表获取关联题目
  118. * @param ids
  119. * @return
  120. */
  121. public List<QuestionNoRelation> listWithRelationByIds(Number[] ids){
  122. List<QuestionNo> p = select(questionNoMapper, ids);
  123. return relation(p);
  124. }
  125. /**
  126. * 根据题目编号id获取关联题目
  127. * @param id
  128. * @return
  129. */
  130. public QuestionNoRelation getWithRelation(Number id){
  131. QuestionNo questionNo = get(id);
  132. return relation(questionNo);
  133. }
  134. /**
  135. * 绑定no和question
  136. * @param ids
  137. * @param questionId
  138. * @return
  139. */
  140. public Boolean bindQuestion(Integer[] ids, Integer questionId){
  141. if (ids.length == 0) return false;
  142. Example example = new Example(QuestionNo.class);
  143. example.and(
  144. example.createCriteria()
  145. .andIn("id", Arrays.stream(ids).collect(Collectors.toList()))
  146. );
  147. int result = update(questionNoMapper, example, QuestionNo.builder().questionId(questionId).deleteTime(null).build());
  148. return result > 0;
  149. }
  150. /**
  151. * 根据题目获取总试卷统计信息
  152. * @param questionNoList
  153. * @return
  154. */
  155. public PaperStat statPaper(List<QuestionNo> questionNoList){
  156. PaperStat stat = new PaperStat();
  157. Integer totalTime = 0;
  158. Integer totalNumber = 0;
  159. Integer totalCorrect = 0;
  160. for(QuestionNo questionNo : questionNoList){
  161. totalTime += questionNo.getTotalTime();
  162. totalNumber += questionNo.getTotalNumber();
  163. totalCorrect += questionNo.getTotalCorrect();
  164. }
  165. stat.setTotalCorrect(totalCorrect);
  166. stat.setTotalNumber(totalNumber);
  167. stat.setTotalTime(totalTime);
  168. return stat;
  169. }
  170. /**
  171. * 累加做题记录到questionNo
  172. * @param question
  173. */
  174. public void accumulation(UserQuestion question){
  175. questionNoRelationMapper.accumulation(question.getQuestionNoId(), 1, question.getTime(), question.getIsCorrect());
  176. }
  177. /**
  178. * 根据试卷分组获取统计信息
  179. * @param questionNoIdsMap
  180. * @return
  181. */
  182. public Map<Integer, PaperStat> statPaperMap(Map<Integer, Integer[]> questionNoIdsMap){
  183. Map<Integer, PaperStat> relationMap = new HashMap<>();
  184. List<Integer> ids = new ArrayList<>();
  185. for(Integer[] questionNoIds : questionNoIdsMap.values()){
  186. ids.addAll(Arrays.stream(questionNoIds).collect(Collectors.toList()));
  187. }
  188. List<QuestionNo> questionNoList = select(ids);
  189. Map questionNoMap = Transform.getMap(questionNoList, QuestionNo.class, "id");
  190. List<QuestionNo> l = new ArrayList<>();
  191. for(Integer k: questionNoIdsMap.keySet()){
  192. l.clear();
  193. for (Integer questionNoId : questionNoIdsMap.get(k)){
  194. l.add((QuestionNo)questionNoMap.get(questionNoId));
  195. }
  196. relationMap.put(k, statPaper(l));
  197. }
  198. return relationMap;
  199. }
  200. private List<QuestionNoRelation> relation(List<QuestionNo> p){
  201. List<QuestionNoRelation> relationList = Transform.convert(p, QuestionNoRelation.class);
  202. Collection questionIds = Transform.getIds(p, QuestionNo.class, "questionId");
  203. List<Question> questions = questionService.select(questionIds);
  204. Transform.combine(relationList, questions, QuestionNoRelation.class, "questionId", "question", Question.class, "id");
  205. return relationList;
  206. }
  207. private QuestionNoRelation relation(QuestionNo p){
  208. QuestionNoRelation relation = Transform.convert(p, QuestionNoRelation.class);
  209. Question question = questionService.get(p.getQuestionId());
  210. relation.setQuestion(question);
  211. return relation;
  212. }
  213. /**
  214. * 通过题目编号获取
  215. * @param no
  216. * @return
  217. */
  218. public QuestionNo getByNo(String no, String module){
  219. return one(questionNoMapper, QuestionNo.builder().no(no).module(module).build());
  220. }
  221. @Transactional
  222. public QuestionNo add(QuestionNo question){
  223. QuestionNo in = getByNo(question.getNo(), question.getModule());
  224. if (in != null){
  225. return in;
  226. }
  227. int result = insert(questionNoMapper, question);
  228. question = one(questionNoMapper, question.getId());
  229. if(question == null){
  230. throw new SystemException("题目添加失败");
  231. }
  232. return question;
  233. }
  234. @Deprecated
  235. public QuestionNo edit(QuestionNo question){
  236. QuestionNo in = one(questionNoMapper, question.getId());
  237. if(in == null){
  238. throw new ParameterException("题目不存在");
  239. }
  240. int result = update(questionNoMapper, question);
  241. return question;
  242. }
  243. public boolean delete(Number id){
  244. QuestionNo in = one(questionNoMapper, id);
  245. if(in == null){
  246. throw new ParameterException("题目不存在");
  247. }
  248. int result = delete(questionNoMapper, id, SOFT_FLAG);
  249. return result > 0;
  250. }
  251. public QuestionNo get(Number id){
  252. QuestionNo in = one(questionNoMapper, id);
  253. if(in == null){
  254. throw new ParameterException("题目不存在");
  255. }
  256. return in;
  257. }
  258. public Page<QuestionNo> select(int page, int pageSize){
  259. return select(questionNoMapper, page, pageSize);
  260. }
  261. public List<QuestionNo> select(Collection ids){
  262. return select(questionNoMapper, ids);
  263. }
  264. }