QuestionApi.java 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package nju.seec.helper.api.bok;
  2. import cn.hutool.core.util.StrUtil;
  3. import com.google.common.collect.ImmutableMap;
  4. import lombok.Data;
  5. import nju.seec.helper.api.bok.constant.QuestionType;
  6. import nju.seec.helper.api.bok.vo.PageableQuestionListVO;
  7. import nju.seec.helper.api.bok.vo.QuestionVO;
  8. import nju.seec.helper.dto.question.BaseQuestionDTO;
  9. import nju.seec.helper.dto.question.ChoiceQuestionDTO;
  10. import nju.seec.helper.dto.question.TrueOrFalseQuestionDTO;
  11. import nju.seec.helper.enums.ExceptionType;
  12. import nju.seec.helper.exception.HelperException;
  13. import nju.seec.helper.util.Consts;
  14. import nju.seec.helper.util.RestRequestUtil;
  15. import nju.seec.helper.util.cache.CaffeineCacheUtils;
  16. import org.springframework.boot.context.properties.ConfigurationProperties;
  17. import org.springframework.data.domain.Page;
  18. import org.springframework.data.domain.PageImpl;
  19. import org.springframework.data.domain.Pageable;
  20. import org.springframework.stereotype.Component;
  21. import java.util.List;
  22. import java.util.Map;
  23. import java.util.stream.Collectors;
  24. /**
  25. * @author xst
  26. * <p>
  27. * updated by cst
  28. */
  29. @Component
  30. @ConfigurationProperties("bok.question")
  31. @Data
  32. public class QuestionApi {
  33. private static final String BOK_QUESTION_CACHE_NAME = Consts.BOK_QUESTION_CACHE_NAME;
  34. private final RestRequestUtil restRequestUtil;
  35. private final CaffeineCacheUtils cacheUtils;
  36. private String url;
  37. private String searchUrl;
  38. public QuestionApi(RestRequestUtil restRequestUtil, CaffeineCacheUtils cacheUtils) {
  39. this.restRequestUtil = restRequestUtil;
  40. this.cacheUtils = cacheUtils;
  41. }
  42. @SuppressWarnings({"unchecked", "rawtypes"})
  43. public Page<QuestionVO> getQuestionList(String stem, String tag, String knowledgeId, Pageable pageable) {
  44. Map<String, ?> urlParams = (Map) ImmutableMap.builder()
  45. .put("stem", stem)
  46. .put("tag", tag)
  47. .put("knowledgeId", knowledgeId)
  48. .put("page", String.valueOf(1 + pageable.getPageNumber()))
  49. .put("size", String.valueOf(pageable.getPageSize()))
  50. .put("sort", pageable.getSort().stream()
  51. .map(order -> StrUtil.concat(true, "sort=", order.getProperty(), ",", order.getDirection().toString()))
  52. .reduce((o1, o2) -> StrUtil.join("&", o1, o2))
  53. .orElse("")
  54. )
  55. .build();
  56. PageableQuestionListVO result = restRequestUtil.sendGetRequest(searchUrl, PageableQuestionListVO.class, urlParams);
  57. List<QuestionVO> questionVOList = result.getQuestions();
  58. cacheUtils.setAll(BOK_QUESTION_CACHE_NAME, questionVOList.parallelStream().collect(Collectors.toMap(QuestionVO::getId, bokQuestion -> bokQuestion)));
  59. return new PageImpl<>(questionVOList, pageable, result.getPage().getTotalElements());
  60. }
  61. public QuestionVO getQuestionsById(String questionId) {
  62. Object cachedBokQuestion = cacheUtils.get(BOK_QUESTION_CACHE_NAME, questionId);
  63. if (cachedBokQuestion instanceof QuestionVO) {
  64. return (QuestionVO) cachedBokQuestion;
  65. }
  66. QuestionVO questionVO = restRequestUtil.sendGetRequest(url + "/" + questionId, QuestionVO.class);
  67. cacheUtils.set(BOK_QUESTION_CACHE_NAME, questionVO.getId(), questionVO);
  68. return questionVO;
  69. }
  70. public QuestionVO createQuestion(String questionId, BaseQuestionDTO baseQuestionDTO) {
  71. QuestionVO questionVO = createQuestionVO(baseQuestionDTO);
  72. questionVO.setId(questionId);
  73. questionVO = restRequestUtil.sendPostRequest(url, questionVO, QuestionVO.class);
  74. return questionVO;
  75. }
  76. public QuestionVO updateQuestion(String questionId, BaseQuestionDTO baseQuestionDTO) {
  77. QuestionVO questionVO = createQuestionVO(baseQuestionDTO);
  78. questionVO.setId(questionId);
  79. restRequestUtil.sendPutRequest(url + "/" + questionId, questionVO);
  80. return getQuestionsById(questionId);
  81. }
  82. public void deleteQuestion(String questionId) {
  83. restRequestUtil.sendDeleteRequest(url + "/" + questionId);
  84. }
  85. private QuestionVO createQuestionVO(BaseQuestionDTO baseQuestionDTO) {
  86. QuestionVO questionVO = new QuestionVO();
  87. questionVO.setType(QuestionType.valueOf(baseQuestionDTO.getType()));
  88. questionVO.setStem(baseQuestionDTO.getStem());
  89. questionVO.setKeyPoints(baseQuestionDTO.getKeyPoints());
  90. questionVO.setTags(baseQuestionDTO.getTags());
  91. questionVO.setKnowledgeId(baseQuestionDTO.getKnowledgeId());
  92. questionVO.setAnalysis(baseQuestionDTO.getAnalysis());
  93. switch (questionVO.getType()) {
  94. case choice:
  95. ChoiceQuestionDTO choiceQuestionDTO = (ChoiceQuestionDTO) baseQuestionDTO;
  96. questionVO.setOptions(choiceQuestionDTO.getOptions());
  97. questionVO.setAnswer(choiceQuestionDTO.getAnswer());
  98. break;
  99. case true_false:
  100. TrueOrFalseQuestionDTO trueOrFalseQuestionDTO = (TrueOrFalseQuestionDTO) baseQuestionDTO;
  101. questionVO.setAnswer(String.valueOf(trueOrFalseQuestionDTO.getAnswer()));
  102. break;
  103. default:
  104. throw HelperException.of(ExceptionType.ERROR, "不支持的题目类型");
  105. }
  106. return questionVO;
  107. }
  108. }