|
|
@@ -1,166 +0,0 @@
|
|
|
-package nju.seec.helper.util;
|
|
|
-
|
|
|
-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 lombok.extern.slf4j.Slf4j;
|
|
|
-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.vo.question.BokQuestion;
|
|
|
-import org.springframework.beans.factory.annotation.Value;
|
|
|
-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;
|
|
|
-
|
|
|
-/**
|
|
|
- * @author xst
|
|
|
- * <p>
|
|
|
- * updated by cst
|
|
|
- */
|
|
|
-@Slf4j
|
|
|
-@Component
|
|
|
-public class BokUtil {
|
|
|
- private final RestRequestUtil restRequestUtil;
|
|
|
- private final GuavaCacheUtil cacheUtils;
|
|
|
- @Value("${bok.tqUrl}")
|
|
|
- private String tqUrl;
|
|
|
- @Value("${bok.tqStemSearchUrl}")
|
|
|
- private String tqStemSearchUrl;
|
|
|
- @Value("${bok.tqIdSearchUrl}")
|
|
|
- private String tqIdSearchUrl;
|
|
|
-
|
|
|
- public BokUtil(RestRequestUtil restRequestUtil, GuavaCacheUtil cacheUtils) {
|
|
|
- this.restRequestUtil = restRequestUtil;
|
|
|
- this.cacheUtils = cacheUtils;
|
|
|
- }
|
|
|
-
|
|
|
- public Page<BokQuestion> 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<BokQuestion> bokQuestions = result.getEmbedded().getQuestions();
|
|
|
- cacheUtils.setAll(BOK_CACHE_NAME, bokQuestions.parallelStream().collect(Collectors.toMap(BokQuestion::getId, bokQuestion -> bokQuestion)));
|
|
|
- return new PageImpl<>(bokQuestions, pageable, result.getPage().getTotalElements());
|
|
|
- }
|
|
|
-
|
|
|
- private static String BOK_CACHE_NAME = "BOK";
|
|
|
-
|
|
|
- @SuppressWarnings({"unchecked", "rawtypes"})
|
|
|
- public List<BokQuestion> bokFindByIdIn(final List<String> ids) {
|
|
|
- Map<String, BokQuestion> bokQuestionsMap = Maps.newHashMapWithExpectedSize(ids.size());
|
|
|
-
|
|
|
- final Map<String, BokQuestion> cacheBokQuestionsMap = (Map) cacheUtils.multiGet(BOK_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<BokQuestion> bokQuestions = result.getEmbedded().getQuestions();
|
|
|
-
|
|
|
- Map<String, BokQuestion> remoteBokQuestionsMap = bokQuestions.parallelStream().collect(Collectors.toMap(BokQuestion::getId, bokQuestion -> bokQuestion));
|
|
|
-
|
|
|
- bokQuestionsMap.putAll(remoteBokQuestionsMap);
|
|
|
- cacheUtils.setAll(BOK_CACHE_NAME, (Map) remoteBokQuestionsMap);
|
|
|
- }
|
|
|
-
|
|
|
- return ids.stream()
|
|
|
- .map(bokQuestionsMap::get)
|
|
|
- .collect(Collectors.toList());
|
|
|
- }
|
|
|
-
|
|
|
- public BokQuestion bokFindById(String questionId) {
|
|
|
- Object cachedBokQuestion = cacheUtils.get(BOK_CACHE_NAME, questionId);
|
|
|
- if (cachedBokQuestion instanceof BokQuestion) {
|
|
|
- return (BokQuestion) cachedBokQuestion;
|
|
|
- }
|
|
|
- BokQuestion bokQuestion = restRequestUtil.sendGetRequest(tqUrl + "/" + questionId, BokQuestion.class, Collections.emptyMap());
|
|
|
- cacheUtils.set(BOK_CACHE_NAME, bokQuestion.getId(), bokQuestion);
|
|
|
- return bokQuestion;
|
|
|
- }
|
|
|
-
|
|
|
- public BokQuestion createQuestion(String questionId, BaseQuestionDTO baseQuestionDTO) {
|
|
|
- BokQuestion bokQuestion = getQuestion(baseQuestionDTO);
|
|
|
- bokQuestion.setId(questionId);
|
|
|
- bokQuestion = restRequestUtil.sendPostRequest(tqUrl, bokQuestion, BokQuestion.class);
|
|
|
- return bokQuestion;
|
|
|
- }
|
|
|
-
|
|
|
- public BokQuestion modifyQuestion(String questionId, BaseQuestionDTO baseQuestionDTO) {
|
|
|
- BokQuestion bokQuestion = getQuestion(baseQuestionDTO);
|
|
|
- bokQuestion.setId(questionId);
|
|
|
- restRequestUtil.sendPutRequest(tqUrl + "/" + questionId, bokQuestion);
|
|
|
- return bokFindById(questionId);
|
|
|
- }
|
|
|
-
|
|
|
- public void deleteQuestion(String questionId) {
|
|
|
- restRequestUtil.sendDeleteRequest(tqUrl + "/" + questionId);
|
|
|
- }
|
|
|
-
|
|
|
- private BokQuestion getQuestion(BaseQuestionDTO baseQuestionDTO) {
|
|
|
- BokQuestion bokQuestion = new BokQuestion();
|
|
|
- bokQuestion.setType(baseQuestionDTO.getType());
|
|
|
- bokQuestion.setStem(baseQuestionDTO.getStem());
|
|
|
- bokQuestion.setKeyPoints(baseQuestionDTO.getKeyPoints());
|
|
|
- bokQuestion.setTags(baseQuestionDTO.getTags());
|
|
|
- bokQuestion.setKnowledgeId(baseQuestionDTO.getKnowledgeId());
|
|
|
-
|
|
|
- final QuestionType questionType = QuestionType.getQuestionType(baseQuestionDTO.getType());
|
|
|
- switch (Objects.requireNonNull(questionType)) {
|
|
|
- case CHOICE:
|
|
|
- ChoiceQuestionDTO choiceQuestionDTO = (ChoiceQuestionDTO) baseQuestionDTO;
|
|
|
- bokQuestion.setOptions(choiceQuestionDTO.getOptions());
|
|
|
- bokQuestion.setAnswer(choiceQuestionDTO.getAnswer());
|
|
|
- bokQuestion.setAnalysis(choiceQuestionDTO.getAnalysis());
|
|
|
- break;
|
|
|
- case TRUE_FALSE:
|
|
|
- TrueOrFalseQuestionDTO trueOrFalseQuestionDTO = (TrueOrFalseQuestionDTO) baseQuestionDTO;
|
|
|
- bokQuestion.setAnswer(String.valueOf(trueOrFalseQuestionDTO.getAnswer()));
|
|
|
- bokQuestion.setAnalysis(trueOrFalseQuestionDTO.getAnalysis());
|
|
|
- break;
|
|
|
- default:
|
|
|
- throw HelperException.of(ExceptionType.ERROR, "不支持的题目类型");
|
|
|
- }
|
|
|
- return bokQuestion;
|
|
|
- }
|
|
|
-
|
|
|
- @Data
|
|
|
- private static class BokSearchResult {
|
|
|
- @JsonProperty("_embedded")
|
|
|
- private Embedded embedded = new Embedded();
|
|
|
- private Page page;
|
|
|
-
|
|
|
- @Data
|
|
|
- private static class Embedded {
|
|
|
- private List<BokQuestion> questions = Collections.emptyList();
|
|
|
- }
|
|
|
-
|
|
|
- @Data
|
|
|
- private static class Page {
|
|
|
- private int size;
|
|
|
- private int number;
|
|
|
- private int totalPages;
|
|
|
- private long totalElements;
|
|
|
- }
|
|
|
- }
|
|
|
-}
|