BOKUtil.java 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package nju.seec.helper.util;
  2. import com.fasterxml.jackson.core.JsonProcessingException;
  3. import nju.seec.helper.util.enums.QuestionKindState;
  4. import nju.seec.helper.vo.quiz.ChoiceQuestionVO;
  5. import nju.seec.helper.vo.quiz.QuestionVO;
  6. import nju.seec.helper.vo.quiz.TrueOrFalseQuestionVO;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.data.domain.Pageable;
  9. import org.springframework.stereotype.Component;
  10. import java.util.Arrays;
  11. import java.util.HashMap;
  12. import java.util.List;
  13. import java.util.Map;
  14. import java.util.stream.Collectors;
  15. @Component
  16. public class BOKUtil {
  17. @Autowired
  18. private RestRequestUtil restRequestUtil;
  19. private String searchUrl="http://bok.seecoder.cn/api/tq/search/";
  20. // 应该做成bok的库
  21. public void BokFindByStemLike(String stem, List<QuestionVO> Questions, Map page, Pageable pageable) {
  22. Map<String,String> urlParams=new HashMap<>();
  23. urlParams.put("content",stem);
  24. urlParams.put("sort",pageable.getSort().toString().replaceAll(" ","").replaceAll(":",","));
  25. urlParams.put("size",String.valueOf(pageable.getPageSize()));
  26. urlParams.put("page",String.valueOf(1+pageable.getPageNumber()));
  27. try {
  28. Map ret = (Map)restRequestUtil.sendPostGet(
  29. searchUrl+"findByStemLike?content={content}&sort={sort}&size={size}&page={page}", urlParams);
  30. List<Map> tqs= ((List)((Map)ret.get("_embedded")).get("choiceQuestions"));
  31. List collect = parseBody(tqs);
  32. try {
  33. page.putAll((Map)ret.get("page"));
  34. Questions.addAll(collect);
  35. }catch (Exception e){
  36. e.printStackTrace();
  37. }
  38. }catch (Exception e){
  39. e.printStackTrace();
  40. }
  41. }
  42. public List<QuestionVO> BokFindByIdIn(List<String> Ids) {
  43. Map<String,String> urlParams=new HashMap<>();
  44. String idsStr=Ids.stream().reduce((a,b)->{return a+","+b;}).orElse("");
  45. urlParams.put("id",idsStr);
  46. urlParams.put("size",String.valueOf(Ids.size()));
  47. urlParams.put("page","1");
  48. try {
  49. Map ret = (Map)restRequestUtil.sendPostGet(
  50. searchUrl+"findByIdIn?id={id}&sort={sort}&size={size}&page={page}", urlParams);
  51. List<Map> tqs= ((List)((Map)ret.get("_embedded")).get("choiceQuestions"));
  52. List<QuestionVO> collect = parseBody(tqs);
  53. return (collect);
  54. }catch (Exception e){
  55. e.printStackTrace();
  56. throw new RuntimeException("bok request error!for findByIdIn:"+ Arrays.toString(Ids.toArray()));
  57. }
  58. }
  59. private List parseBody(List<Map> tqs) {
  60. final List collect =
  61. tqs.stream().map(q -> {
  62. switch ((String) q.get("type")) {
  63. case "choice":
  64. return ChoiceQuestionVO.builder().stem((String) q.get("stem")).kind(QuestionKindState.CHOICE)
  65. .options((Map<String, String>) q.get("options")).answer(((String) q.get("answer")).replaceAll("【答案】", ""))
  66. .analysis((String) q.get("analysis")).QuestionId(String.valueOf((Integer) q.get("tq_id"))).build();
  67. case "true_false":
  68. return TrueOrFalseQuestionVO.builder().stem((String) q.get("stem")).kind(QuestionKindState.CHOICE)
  69. .answer((Boolean) (q.get("answer")))
  70. .analysis((String) q.get("analysis")).QuestionId(String.valueOf((Integer) q.get("tq_id"))).build();
  71. }
  72. return new Exception("unknown type:" + (String) q.get("type"));
  73. }).collect(Collectors.toList());
  74. return collect;
  75. }
  76. }