|
|
@@ -1,30 +1,27 @@
|
|
|
package nju.seec.helper.api.bok;
|
|
|
|
|
|
-import com.fasterxml.jackson.annotation.JsonProperty;
|
|
|
import com.google.common.collect.ImmutableMap;
|
|
|
-import com.google.common.collect.Lists;
|
|
|
-import com.google.common.collect.Maps;
|
|
|
import lombok.Data;
|
|
|
+import nju.seec.helper.api.bok.constant.QuestionType;
|
|
|
+import nju.seec.helper.api.bok.vo.PageableQuestionListVO;
|
|
|
+import nju.seec.helper.api.bok.vo.QuestionVO;
|
|
|
import nju.seec.helper.dto.question.BaseQuestionDTO;
|
|
|
import nju.seec.helper.dto.question.ChoiceQuestionDTO;
|
|
|
import nju.seec.helper.dto.question.TrueOrFalseQuestionDTO;
|
|
|
import nju.seec.helper.enums.ExceptionType;
|
|
|
-import nju.seec.helper.enums.QuestionType;
|
|
|
import nju.seec.helper.exception.HelperException;
|
|
|
-import nju.seec.helper.util.Consts;
|
|
|
import nju.seec.helper.util.RestRequestUtil;
|
|
|
-import nju.seec.helper.util.cache.GuavaCacheUtils;
|
|
|
-import nju.seec.helper.vo.question.BokQuestionVO;
|
|
|
+import nju.seec.helper.util.StringUtils;
|
|
|
+import nju.seec.helper.util.cache.CaffeineCacheUtils;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.boot.context.properties.ConfigurationProperties;
|
|
|
import org.springframework.data.domain.Page;
|
|
|
import org.springframework.data.domain.PageImpl;
|
|
|
import org.springframework.data.domain.Pageable;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
-import java.util.Collections;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
-import java.util.Objects;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
@@ -33,139 +30,88 @@ import java.util.stream.Collectors;
|
|
|
* updated by cst
|
|
|
*/
|
|
|
@Component
|
|
|
+@ConfigurationProperties("bok.question")
|
|
|
+@Data
|
|
|
public class QuestionApi {
|
|
|
+ @Value("${bok.question-cache-name}")
|
|
|
+ private String bokQuestionCacheName;
|
|
|
+
|
|
|
private final RestRequestUtil restRequestUtil;
|
|
|
- private final GuavaCacheUtils cacheUtils;
|
|
|
- @Value("${bok.tqUrl}")
|
|
|
- private String tqUrl;
|
|
|
- @Value("${bok.tqStemSearchUrl}")
|
|
|
- private String tqStemSearchUrl;
|
|
|
- @Value("${bok.tqIdSearchUrl}")
|
|
|
- private String tqIdSearchUrl;
|
|
|
+ private final CaffeineCacheUtils cacheUtils;
|
|
|
+ private String url;
|
|
|
+ private String searchUrl;
|
|
|
|
|
|
- public QuestionApi(RestRequestUtil restRequestUtil, GuavaCacheUtils cacheUtils) {
|
|
|
+ public QuestionApi(RestRequestUtil restRequestUtil, CaffeineCacheUtils cacheUtils) {
|
|
|
this.restRequestUtil = restRequestUtil;
|
|
|
this.cacheUtils = cacheUtils;
|
|
|
}
|
|
|
|
|
|
- public Page<BokQuestionVO> bokFindByStemLike(String stem, Pageable pageable) {
|
|
|
- Map<String, String> urlParams = ImmutableMap.of(
|
|
|
- "content", stem,
|
|
|
- "page", String.valueOf(1 + pageable.getPageNumber()),
|
|
|
- "size", String.valueOf(pageable.getPageSize())
|
|
|
- );
|
|
|
- BokSearchResult result = restRequestUtil.sendGetRequest(tqStemSearchUrl, BokSearchResult.class, urlParams);
|
|
|
-
|
|
|
- List<BokQuestionVO> bokQuestionVOList = result.getEmbedded().getQuestions();
|
|
|
- cacheUtils.setAll(BOK_QUESTION_CACHE_NAME, bokQuestionVOList.parallelStream().collect(Collectors.toMap(BokQuestionVO::getId, bokQuestion -> bokQuestion)));
|
|
|
- return new PageImpl<>(bokQuestionVOList, pageable, result.getPage().getTotalElements());
|
|
|
- }
|
|
|
-
|
|
|
- private static final String BOK_QUESTION_CACHE_NAME = Consts.SYS_NAME + "_bok_question";
|
|
|
-
|
|
|
- @SuppressWarnings({"unchecked", "rawtypes"})
|
|
|
- public List<BokQuestionVO> bokFindByIdIn(final List<String> ids) {
|
|
|
- Map<String, BokQuestionVO> bokQuestionsMap = Maps.newHashMapWithExpectedSize(ids.size());
|
|
|
-
|
|
|
- final Map<String, BokQuestionVO> cacheBokQuestionsMap = (Map) cacheUtils.multiGet(BOK_QUESTION_CACHE_NAME, ids);
|
|
|
- bokQuestionsMap.putAll(cacheBokQuestionsMap);
|
|
|
-
|
|
|
- List<String> requestIds = Lists.newArrayList(ids);
|
|
|
- requestIds.removeAll(cacheBokQuestionsMap.keySet());
|
|
|
- //未缓存的去这里拿
|
|
|
- if (!requestIds.isEmpty()) {
|
|
|
- Map<String, String> urlParams = ImmutableMap.of("id", requestIds.stream().reduce((a, b) -> a + "," + b).orElse(""));
|
|
|
-
|
|
|
- BokSearchResult result = restRequestUtil.sendGetRequest(tqIdSearchUrl, BokSearchResult.class, urlParams);
|
|
|
- List<BokQuestionVO> bokQuestionVOList = result.getEmbedded().getQuestions();
|
|
|
-
|
|
|
- Map<String, BokQuestionVO> remoteBokQuestionsMap = bokQuestionVOList.parallelStream().collect(Collectors.toMap(BokQuestionVO::getId, bokQuestion -> bokQuestion));
|
|
|
-
|
|
|
- bokQuestionsMap.putAll(remoteBokQuestionsMap);
|
|
|
- cacheUtils.setAll(BOK_QUESTION_CACHE_NAME, (Map) remoteBokQuestionsMap);
|
|
|
- }
|
|
|
-
|
|
|
- return ids.stream()
|
|
|
- .map(bokQuestionsMap::get)
|
|
|
- .collect(Collectors.toList());
|
|
|
+ public Page<QuestionVO> getQuestionList(String stem, String tag, String knowledgeId, Pageable pageable) {
|
|
|
+ Map<String, ?> urlParams = ImmutableMap.<String, Object>builder()
|
|
|
+ .put("stem", stem)
|
|
|
+ .put("tag", tag)
|
|
|
+ .put("knowledgeId", knowledgeId)
|
|
|
+ .put("page", 1 + pageable.getPageNumber())
|
|
|
+ .put("size", pageable.getPageSize())
|
|
|
+ .put("sort", StringUtils.getSortString(pageable.getSort()))
|
|
|
+ .build();
|
|
|
+ PageableQuestionListVO result = restRequestUtil.sendGetRequest(searchUrl, PageableQuestionListVO.class, urlParams);
|
|
|
+
|
|
|
+ List<QuestionVO> questionVOList = result.getQuestions();
|
|
|
+ cacheUtils.setAll(bokQuestionCacheName, questionVOList.parallelStream().collect(Collectors.toMap(QuestionVO::getId, bokQuestion -> bokQuestion)));
|
|
|
+ return new PageImpl<>(questionVOList, pageable, result.getPage().getTotalElements());
|
|
|
}
|
|
|
|
|
|
- public BokQuestionVO bokFindById(String questionId) {
|
|
|
- Object cachedBokQuestion = cacheUtils.get(BOK_QUESTION_CACHE_NAME, questionId);
|
|
|
- if (cachedBokQuestion instanceof BokQuestionVO) {
|
|
|
- return (BokQuestionVO) cachedBokQuestion;
|
|
|
+ public QuestionVO getQuestionsById(String questionId) {
|
|
|
+ Object cachedBokQuestion = cacheUtils.get(bokQuestionCacheName, questionId);
|
|
|
+ if (cachedBokQuestion instanceof QuestionVO) {
|
|
|
+ return (QuestionVO) cachedBokQuestion;
|
|
|
}
|
|
|
- BokQuestionVO bokQuestionVO = restRequestUtil.sendGetRequest(tqUrl + "/" + questionId, BokQuestionVO.class, Collections.emptyMap());
|
|
|
- cacheUtils.set(BOK_QUESTION_CACHE_NAME, bokQuestionVO.getId(), bokQuestionVO);
|
|
|
- return bokQuestionVO;
|
|
|
+ QuestionVO questionVO = restRequestUtil.sendGetRequest(url + "/" + questionId, QuestionVO.class);
|
|
|
+ cacheUtils.set(bokQuestionCacheName, questionVO.getId(), questionVO);
|
|
|
+ return questionVO;
|
|
|
}
|
|
|
|
|
|
- public BokQuestionVO createQuestion(String questionId, BaseQuestionDTO baseQuestionDTO) {
|
|
|
- BokQuestionVO bokQuestionVO = getQuestion(baseQuestionDTO);
|
|
|
- bokQuestionVO.setId(questionId);
|
|
|
- bokQuestionVO = restRequestUtil.sendPostRequest(tqUrl, bokQuestionVO, BokQuestionVO.class);
|
|
|
- return bokQuestionVO;
|
|
|
+ public QuestionVO createQuestion(String questionId, BaseQuestionDTO baseQuestionDTO) {
|
|
|
+ QuestionVO questionVO = createQuestionVO(baseQuestionDTO);
|
|
|
+ questionVO.setId(questionId);
|
|
|
+ questionVO = restRequestUtil.sendPostRequest(url, questionVO, QuestionVO.class);
|
|
|
+ return questionVO;
|
|
|
}
|
|
|
|
|
|
- public BokQuestionVO modifyQuestion(String questionId, BaseQuestionDTO baseQuestionDTO) {
|
|
|
- BokQuestionVO bokQuestionVO = getQuestion(baseQuestionDTO);
|
|
|
- bokQuestionVO.setId(questionId);
|
|
|
- restRequestUtil.sendPutRequest(tqUrl + "/" + questionId, bokQuestionVO);
|
|
|
- return bokFindById(questionId);
|
|
|
+ public QuestionVO updateQuestion(String questionId, BaseQuestionDTO baseQuestionDTO) {
|
|
|
+ QuestionVO questionVO = createQuestionVO(baseQuestionDTO);
|
|
|
+ questionVO.setId(questionId);
|
|
|
+ restRequestUtil.sendPutRequest(url + "/" + questionId, questionVO);
|
|
|
+ return getQuestionsById(questionId);
|
|
|
}
|
|
|
|
|
|
public void deleteQuestion(String questionId) {
|
|
|
- restRequestUtil.sendDeleteRequest(tqUrl + "/" + questionId);
|
|
|
- }
|
|
|
-
|
|
|
- public void starQuestion(String userId, String questionId) {
|
|
|
-
|
|
|
+ restRequestUtil.sendDeleteRequest(url + "/" + questionId);
|
|
|
}
|
|
|
|
|
|
- private BokQuestionVO getQuestion(BaseQuestionDTO baseQuestionDTO) {
|
|
|
- BokQuestionVO bokQuestionVO = new BokQuestionVO();
|
|
|
- bokQuestionVO.setType(baseQuestionDTO.getType());
|
|
|
- bokQuestionVO.setStem(baseQuestionDTO.getStem());
|
|
|
- bokQuestionVO.setKeyPoints(baseQuestionDTO.getKeyPoints());
|
|
|
- bokQuestionVO.setTags(baseQuestionDTO.getTags());
|
|
|
- bokQuestionVO.setKnowledgeId(baseQuestionDTO.getKnowledgeId());
|
|
|
-
|
|
|
- final QuestionType questionType = QuestionType.getQuestionType(baseQuestionDTO.getType());
|
|
|
- switch (Objects.requireNonNull(questionType)) {
|
|
|
- case CHOICE:
|
|
|
+ private QuestionVO createQuestionVO(BaseQuestionDTO baseQuestionDTO) {
|
|
|
+ QuestionVO questionVO = new QuestionVO();
|
|
|
+ questionVO.setType(QuestionType.valueOf(baseQuestionDTO.getType()));
|
|
|
+ questionVO.setStem(baseQuestionDTO.getStem());
|
|
|
+ questionVO.setKeyPoints(baseQuestionDTO.getKeyPoints());
|
|
|
+ questionVO.setTags(baseQuestionDTO.getTags());
|
|
|
+ questionVO.setKnowledgeId(baseQuestionDTO.getKnowledgeId());
|
|
|
+ questionVO.setAnalysis(baseQuestionDTO.getAnalysis());
|
|
|
+
|
|
|
+ switch (questionVO.getType()) {
|
|
|
+ case choice:
|
|
|
ChoiceQuestionDTO choiceQuestionDTO = (ChoiceQuestionDTO) baseQuestionDTO;
|
|
|
- bokQuestionVO.setOptions(choiceQuestionDTO.getOptions());
|
|
|
- bokQuestionVO.setAnswer(choiceQuestionDTO.getAnswer());
|
|
|
- bokQuestionVO.setAnalysis(choiceQuestionDTO.getAnalysis());
|
|
|
+ questionVO.setOptions(choiceQuestionDTO.getOptions());
|
|
|
+ questionVO.setAnswer(choiceQuestionDTO.getAnswer());
|
|
|
break;
|
|
|
- case TRUE_FALSE:
|
|
|
+ case true_false:
|
|
|
TrueOrFalseQuestionDTO trueOrFalseQuestionDTO = (TrueOrFalseQuestionDTO) baseQuestionDTO;
|
|
|
- bokQuestionVO.setAnswer(String.valueOf(trueOrFalseQuestionDTO.getAnswer()));
|
|
|
- bokQuestionVO.setAnalysis(trueOrFalseQuestionDTO.getAnalysis());
|
|
|
+ questionVO.setAnswer(String.valueOf(trueOrFalseQuestionDTO.getAnswer()));
|
|
|
break;
|
|
|
default:
|
|
|
throw HelperException.of(ExceptionType.ERROR, "不支持的题目类型");
|
|
|
}
|
|
|
- return bokQuestionVO;
|
|
|
- }
|
|
|
-
|
|
|
- @Data
|
|
|
- private static class BokSearchResult {
|
|
|
- @JsonProperty("_embedded")
|
|
|
- private Embedded embedded = new Embedded();
|
|
|
- private Page page;
|
|
|
-
|
|
|
- @Data
|
|
|
- private static class Embedded {
|
|
|
- private List<BokQuestionVO> questions = Collections.emptyList();
|
|
|
- }
|
|
|
-
|
|
|
- @Data
|
|
|
- private static class Page {
|
|
|
- private int size;
|
|
|
- private int number;
|
|
|
- private int totalPages;
|
|
|
- private long totalElements;
|
|
|
- }
|
|
|
+ return questionVO;
|
|
|
}
|
|
|
}
|