QuestionNoService.java 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  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.module.StructModule;
  10. import com.qxgmat.data.constants.enums.status.DirectionStatus;
  11. import com.qxgmat.data.dao.QuestionNoMapper;
  12. import com.qxgmat.data.dao.entity.Question;
  13. import com.qxgmat.data.dao.entity.QuestionNo;
  14. import com.qxgmat.data.dao.entity.UserCollectQuestion;
  15. import com.qxgmat.data.dao.entity.UserQuestion;
  16. import com.qxgmat.data.inline.PaperStat;
  17. import com.qxgmat.data.relation.QuestionNoRelationMapper;
  18. import com.qxgmat.data.relation.entity.QuestionDifficultRelation;
  19. import com.qxgmat.data.relation.entity.QuestionNoRelation;
  20. import org.slf4j.Logger;
  21. import org.slf4j.LoggerFactory;
  22. import org.springframework.stereotype.Service;
  23. import org.springframework.transaction.annotation.Transactional;
  24. import javax.annotation.Resource;
  25. import java.util.*;
  26. import java.util.stream.Collectors;
  27. @Service
  28. public class QuestionNoService extends AbstractService {
  29. private static final Logger logger = LoggerFactory.getLogger(QuestionNoService.class);
  30. protected boolean SOFT_FLAG = false;
  31. @Resource
  32. private QuestionNoMapper questionNoMapper;
  33. @Resource
  34. private QuestionNoRelationMapper questionNoRelationMapper;
  35. @Resource
  36. private QuestionService questionService;
  37. /**
  38. * 根据题干搜索相似题目: 相似度80%
  39. * @param page
  40. * @param size
  41. * @param stem
  42. * @return
  43. */
  44. public PageResult<QuestionNoRelation> searchStem(int page, int size, String stem){
  45. // String[] stems = stem.replaceAll("\\.?,?!?:?;?\\??", "").split(" ");
  46. Page<QuestionNo> p = page(()->{
  47. questionNoRelationMapper.searchStem(stem);
  48. }, page, size);
  49. Collection ids = Transform.getIds(p, QuestionNo.class, "id");
  50. // 获取详细数据
  51. List<QuestionNo> list = select(ids);
  52. return new PageResult<>(relation(list), p.getTotal());
  53. }
  54. /**
  55. * 根据题目编号搜索相似题目
  56. * @param page
  57. * @param size
  58. * @param keyword
  59. * @param module
  60. * @return
  61. */
  62. public PageResult<QuestionNoRelation> searchNo(int page, int size, String keyword, String module, Integer[] ids){
  63. Example example = new Example(QuestionNo.class);
  64. if(keyword != null)
  65. example.and(
  66. example.createCriteria()
  67. .andLike("title", "%"+keyword+"%")
  68. );
  69. if (module != null)
  70. example.and(
  71. example.createCriteria()
  72. .andEqualTo("module", module)
  73. );
  74. if (ids != null)
  75. example.and(
  76. example.createCriteria()
  77. .andIn("id", Arrays.stream(ids).collect(Collectors.toList()))
  78. );
  79. example.and(
  80. example.createCriteria()
  81. .andIsNull("deleteTime")
  82. );
  83. example.orderBy("id").asc();
  84. Page<QuestionNo> p = page(()->select(questionNoMapper, example), page, size);
  85. logger.info("SearchNo result: {}", p.toString());
  86. return new PageResult<>(relation(p), p.getTotal());
  87. }
  88. /**
  89. * 根据题目编号搜索相似题目
  90. * @param page
  91. * @param size
  92. * @param keyword
  93. * @return
  94. */
  95. public Page<QuestionNoRelation> searchStemFulltext(int page, int size, String keyword, String[] questionTypes, String module, Integer[] structIds, String place, String difficult, Integer qxCatId, String order, DirectionStatus direction){
  96. if (direction == null){
  97. direction = DirectionStatus.DESC;
  98. }
  99. String finalOrder = order;
  100. DirectionStatus finalDirection = direction;
  101. Page<QuestionNoRelation> p = page(()->{
  102. questionNoRelationMapper.searchStemFulltext(keyword, questionTypes, module, structIds, place, difficult, qxCatId, finalOrder, finalDirection.key);
  103. }, page, size);
  104. Collection ids = Transform.getIds(p, QuestionNo.class, "id");
  105. // 获取详细数据
  106. List<QuestionNo> list = select(ids);
  107. List<QuestionNoRelation> relationList = relation(list);
  108. Transform.replace(p, relationList, QuestionNoRelation.class, "id");
  109. return p;
  110. }
  111. /**
  112. * 根据题目编号搜索相似题目
  113. * @param page
  114. * @param size
  115. * @param keyword
  116. * @return
  117. */
  118. public Page<QuestionNo> searchNoFulltext(int page, int size, String keyword, String module, Integer qxCatId){
  119. Page<QuestionNo> p = page(()->{
  120. questionNoRelationMapper.searchNoFulltext(keyword, module, qxCatId);
  121. }, page, size);
  122. Collection ids = Transform.getIds(p, QuestionNo.class, "id");
  123. // 获取详细数据
  124. List<QuestionNo> list = select(ids);
  125. Transform.replace(p, list, QuestionNo.class, "id");
  126. return p;
  127. }
  128. /**
  129. * 获取结构模块下的题目列表: 按序号排列
  130. * @param module
  131. * @param structId
  132. * @return
  133. */
  134. public List<QuestionNo> listByStruct(StructModule module, Integer structId){
  135. Example example = new Example(QuestionNo.class);
  136. example.and(
  137. example.createCriteria()
  138. .andEqualTo("module", module.key)
  139. .andGreaterThan("questionId", 0)
  140. .andCondition(String.format(formatSet, structId, "module_struct"))
  141. );
  142. example.and(
  143. example.createCriteria()
  144. .andIsNull("deleteTime")
  145. );
  146. example.orderBy("no").asc();
  147. return select(questionNoMapper, example);
  148. }
  149. /**
  150. * 获取结构模块下的题目列表: 按序号排列
  151. * @param module
  152. * @param structIds
  153. * @return
  154. */
  155. public List<QuestionNo> listByStruct(StructModule module, Integer[] structIds){
  156. Example example = new Example(QuestionNo.class);
  157. example.and(
  158. example.createCriteria()
  159. .andEqualTo("module", module.key)
  160. .andGreaterThan("questionId", 0)
  161. );
  162. if (structIds != null){
  163. Example.Criteria criteria = example.createCriteria();
  164. for(Integer structId : structIds){
  165. criteria.orCondition(String.format(formatSet, structId, "module_struct"));
  166. }
  167. example.and(criteria);
  168. }
  169. example.and(
  170. example.createCriteria()
  171. .andIsNull("deleteTime")
  172. );
  173. example.orderBy("no").asc();
  174. return select(questionNoMapper, example);
  175. }
  176. /**
  177. * 获取结构模块下的题目列表: 按序号排列
  178. * @param module
  179. * @param structId
  180. * @return
  181. */
  182. public List<QuestionNoRelation> listWithRelationByStruct(StructModule module, Integer structId){
  183. return relation(listByStruct(module, structId));
  184. }
  185. /**
  186. * 根据题目标号列表获取题目
  187. * @param nos
  188. * @param module
  189. * @return
  190. */
  191. public List<QuestionNoRelation> listWithRelationByNos(String[] nos, String module){
  192. if (nos.length == 0) return new ArrayList<>();
  193. Example example = new Example(QuestionNo.class);
  194. example.and(
  195. example.createCriteria()
  196. .andIn("no", Arrays.stream(nos).collect(Collectors.toList()))
  197. );
  198. if (module != null)
  199. example.and(
  200. example.createCriteria()
  201. .andEqualTo("module", module)
  202. );
  203. example.and(
  204. example.createCriteria()
  205. .andIsNull("deleteTime")
  206. );
  207. List<QuestionNo> p = select(questionNoMapper, example);
  208. return relation(p);
  209. }
  210. /**
  211. * 根据题目获取关联的题目编号
  212. * @param questionId
  213. * @return
  214. */
  215. public List<QuestionNo> listByQuestion(Number questionId){
  216. Example example = new Example(QuestionNo.class);
  217. example.and(
  218. example.createCriteria()
  219. .andEqualTo("questionId", questionId)
  220. );
  221. example.and(
  222. example.createCriteria()
  223. .andIsNull("deleteTime")
  224. );
  225. return select(questionNoMapper, example);
  226. }
  227. /**
  228. * 根据题目编号id列表获取关联题目
  229. * @param ids
  230. * @return
  231. */
  232. public List<QuestionNoRelation> listWithRelationByIds(Number[] ids){
  233. List<QuestionNo> p = select(questionNoMapper, ids);
  234. return relation(p);
  235. }
  236. /**
  237. * 根据题目编号id列表获取关联题目
  238. * @param ids
  239. * @return
  240. */
  241. public Map<Number, QuestionNoRelation> mapWithRelationByIds(Number[] ids){
  242. List<QuestionNo> p = select(questionNoMapper, ids);
  243. List<QuestionNoRelation> list = relation(p);
  244. Map<Number, QuestionNoRelation> map = new HashMap<>();
  245. for(QuestionNoRelation relation : list){
  246. map.put(relation.getId(), relation);
  247. }
  248. return map;
  249. }
  250. /**
  251. * 根据题目编号id获取关联题目
  252. * @param id
  253. * @return
  254. */
  255. public QuestionNoRelation getWithRelation(Number id){
  256. QuestionNo questionNo = get(id);
  257. return relation(questionNo);
  258. }
  259. /**
  260. * 随机获取对应模块下的试题:排除已做试题
  261. * @param structId
  262. * @param filterIds
  263. * @return
  264. */
  265. public Integer randomExamination(Integer structId, Collection targetTypes, Collection filterIds){
  266. List<QuestionNo> questionNoList = questionNoRelationMapper.randomExamination(structId, targetTypes, filterIds, 1);
  267. if (questionNoList.size() > 0){
  268. return questionNoList.get(0).getId();
  269. }else{
  270. return null;
  271. }
  272. }
  273. /**
  274. * 随机批量获取对应模块下的试题:排除已做试题
  275. * @param structId
  276. * @param targetTypes
  277. * @param filterIds
  278. * @param size
  279. * @return
  280. */
  281. public Integer[] randomExaminationList(Integer structId, Collection targetTypes, Collection filterIds, Integer size){
  282. List<QuestionNo> questionNoList = questionNoRelationMapper.randomExamination(structId, targetTypes, filterIds, size);
  283. if (questionNoList.size() > 0){
  284. Integer[] ids = new Integer[questionNoList.size()];
  285. for(int i = 0; i< questionNoList.size(); i++){
  286. ids[i] = questionNoList.get(i).getId();
  287. }
  288. return ids;
  289. }else{
  290. return new Integer[0];
  291. }
  292. }
  293. /**
  294. * 获取该struct下指定类型的所有题目信息
  295. * @param structId
  296. * @param targetTypes
  297. * @return
  298. */
  299. public List<QuestionDifficultRelation> allExaminationByType(Integer structId, Collection targetTypes){
  300. return questionNoRelationMapper.allExaminationByType(structId, targetTypes);
  301. }
  302. /**
  303. * 随机查找模考阅读题
  304. * @param structId
  305. * @param number
  306. * @param filterIds
  307. * @return
  308. */
  309. public Integer[] randomExaminationRc(Integer structId, Integer number, Collection filterIds){
  310. Example example = new Example(QuestionNo.class);
  311. example.and(
  312. example.createCriteria()
  313. .andGreaterThan("questionId", 0)
  314. .andCondition(String.format(formatSet, structId, "module_struct"))
  315. .andEqualTo("relationNumber", number)
  316. .andNotIn("id", filterIds)
  317. );
  318. example.and(
  319. example.createCriteria()
  320. .andIsNull("deleteTime")
  321. );
  322. example.setOrderByClause("RAND()");
  323. QuestionNo questionNo = one(questionNoMapper, example);
  324. if (questionNo == null){
  325. throw new ParameterException("阅读题查找失败");
  326. }
  327. return Arrays.stream(questionNo.getRelationQuestion()).boxed().toArray(Integer[]::new);
  328. }
  329. /**
  330. * 绑定no和question
  331. * @param ids
  332. * @param questionId
  333. * @return
  334. */
  335. public Boolean bindQuestion(Integer[] ids, Integer questionId){
  336. if (ids == null || ids.length == 0) return false;
  337. Example example = new Example(QuestionNo.class);
  338. example.and(
  339. example.createCriteria()
  340. .andIn("id", Arrays.stream(ids).collect(Collectors.toList()))
  341. );
  342. int result = update(questionNoMapper, example, QuestionNo.builder()
  343. .questionId(questionId)
  344. .deleteTime(null)
  345. .build());
  346. return result > 0;
  347. }
  348. /**
  349. * 设定编号关联关系, 并设定题目数量
  350. * @param ids
  351. * @return
  352. */
  353. public Boolean relationQuestion(int[] ids){
  354. if (ids.length == 0) return false;
  355. Example example = new Example(QuestionNo.class);
  356. example.and(
  357. example.createCriteria()
  358. .andIn("id", Arrays.stream(ids).boxed().collect(Collectors.toList()))
  359. );
  360. int result = update(questionNoMapper, example, QuestionNo.builder()
  361. .relationQuestion(ids)
  362. .relationNumber(ids.length)
  363. .deleteTime(null)
  364. .build());
  365. return result > 0;
  366. }
  367. /**
  368. * 根据题目获取总试卷统计信息
  369. * @param questionNoList
  370. * @return
  371. */
  372. public PaperStat statPaper(List<QuestionNo> questionNoList){
  373. PaperStat stat = new PaperStat();
  374. Integer totalTime = 0;
  375. Integer totalNumber = 0;
  376. Integer totalCorrect = 0;
  377. for(QuestionNo questionNo : questionNoList){
  378. if (questionNo == null) continue;
  379. totalTime += questionNo.getTotalTime();
  380. totalNumber += questionNo.getTotalNumber();
  381. totalCorrect += questionNo.getTotalCorrect();
  382. }
  383. stat.setTotalCorrect(totalCorrect);
  384. stat.setTotalNumber(totalNumber);
  385. stat.setTotalTime(totalTime);
  386. return stat;
  387. }
  388. /**
  389. * 累加做题记录到questionNo
  390. * @param question
  391. */
  392. public void accumulation(UserQuestion question){
  393. questionNoRelationMapper.accumulation(question.getQuestionNoId(), 1, question.getUserTime(), question.getIsCorrect());
  394. }
  395. /**
  396. * 累加收藏记录到questionNo
  397. * @param question
  398. */
  399. public void accumulationCollect(UserCollectQuestion question, int collect){
  400. questionNoRelationMapper.accumulationCollect(question.getQuestionNoId(), collect);
  401. }
  402. /**
  403. * 根据试卷分组获取统计信息
  404. * @param questionNoIdsMap
  405. * @return
  406. */
  407. public Map<Integer, PaperStat> statPaperMap(Map<Integer, Integer[]> questionNoIdsMap){
  408. Map<Integer, PaperStat> relationMap = new HashMap<>();
  409. List<Integer> ids = new ArrayList<>();
  410. for(Integer[] questionNoIds : questionNoIdsMap.values()){
  411. ids.addAll(Arrays.stream(questionNoIds).collect(Collectors.toList()));
  412. }
  413. List<QuestionNo> questionNoList = select(ids);
  414. Map questionNoMap = Transform.getMap(questionNoList, QuestionNo.class, "id");
  415. List<QuestionNo> l = new ArrayList<>();
  416. for(Integer k: questionNoIdsMap.keySet()){
  417. l.clear();
  418. for (Integer questionNoId : questionNoIdsMap.get(k)){
  419. l.add((QuestionNo)questionNoMap.get(questionNoId));
  420. }
  421. relationMap.put(k, statPaper(l));
  422. }
  423. return relationMap;
  424. }
  425. /**
  426. * 根据试卷分组获取统计信息
  427. * @param questionNoIdsMap
  428. * @return
  429. */
  430. public Map<Integer, List<String>> questionTypeMap(Map<Integer, Integer[]> questionNoIdsMap){
  431. Map<Integer, List<String>> relationMap = new HashMap<>();
  432. List<Integer> ids = new ArrayList<>();
  433. for(Integer[] questionNoIds : questionNoIdsMap.values()){
  434. ids.addAll(Arrays.stream(questionNoIds).collect(Collectors.toList()));
  435. }
  436. List<QuestionNo> questionNoList = select(ids);
  437. List<QuestionNoRelation> relationList = relation(questionNoList);
  438. Map questionNoMap = Transform.getMap(relationList, QuestionNoRelation.class, "id");
  439. for(Integer k: questionNoIdsMap.keySet()){
  440. List<String> l = new ArrayList<>();
  441. for (Integer questionNoId : questionNoIdsMap.get(k)){
  442. QuestionNoRelation relation = (QuestionNoRelation)questionNoMap.get(questionNoId);
  443. if (relation == null) continue;
  444. if (relation.getQuestion() == null) continue;
  445. if (!l.contains(relation.getQuestion().getQuestionType())){
  446. l.add(relation.getQuestion().getQuestionType());
  447. }
  448. }
  449. relationMap.put(k, l);
  450. }
  451. return relationMap;
  452. }
  453. public List<QuestionNoRelation> relation(List<QuestionNo> p){
  454. List<QuestionNoRelation> relationList = Transform.convert(p, QuestionNoRelation.class);
  455. Collection questionIds = Transform.getIds(p, QuestionNo.class, "questionId");
  456. List<Question> questions = questionService.select(questionIds);
  457. Transform.combine(relationList, questions, QuestionNoRelation.class, "questionId", "question", Question.class, "id");
  458. return relationList;
  459. }
  460. public QuestionNoRelation relation(QuestionNo p){
  461. QuestionNoRelation relation = Transform.convert(p, QuestionNoRelation.class);
  462. Question question = questionService.get(p.getQuestionId());
  463. relation.setQuestion(question);
  464. return relation;
  465. }
  466. /**
  467. * 通过题目编号获取
  468. * @param title
  469. * @return
  470. */
  471. public QuestionNo getByNo(String title, String module){
  472. return one(questionNoMapper, QuestionNo.builder().title(title).module(module).build());
  473. }
  474. @Transactional
  475. public QuestionNo add(QuestionNo question){
  476. QuestionNo in = getByNo(question.getTitle(), question.getModule());
  477. if (in != null){
  478. if (in.getQuestionId() == 0){
  479. return in;
  480. }
  481. throw new ParameterException("题目已经存在");
  482. }
  483. int result = insert(questionNoMapper, question);
  484. question = one(questionNoMapper, question.getId());
  485. if(question == null){
  486. throw new SystemException("题目添加失败");
  487. }
  488. return question;
  489. }
  490. @Transactional
  491. public QuestionNo edit(QuestionNo question){
  492. QuestionNo in = one(questionNoMapper, question.getId());
  493. if(in == null){
  494. throw new ParameterException("题目不存在");
  495. }
  496. int result = update(questionNoMapper, question);
  497. return question;
  498. }
  499. public boolean delete(Number id){
  500. QuestionNo in = one(questionNoMapper, id);
  501. if(in == null){
  502. throw new ParameterException("题目不存在");
  503. }
  504. int result = delete(questionNoMapper, id, true);
  505. return result > 0;
  506. }
  507. public QuestionNo get(Number id){
  508. QuestionNo in = one(questionNoMapper, id);
  509. if(in == null){
  510. throw new ParameterException("题目不存在");
  511. }
  512. return in;
  513. }
  514. public Page<QuestionNo> select(int page, int pageSize){
  515. return select(questionNoMapper, page, pageSize);
  516. }
  517. public Page<QuestionNo> select(Integer[] ids){
  518. return page(()->select(questionNoMapper, ids), 1, ids.length);
  519. }
  520. public List<QuestionNo> select(Collection ids){
  521. return select(questionNoMapper, ids);
  522. }
  523. }