|
|
@@ -2,6 +2,7 @@ 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;
|
|
|
@@ -18,7 +19,10 @@ import org.springframework.data.domain.PageImpl;
|
|
|
import org.springframework.data.domain.Pageable;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
-import java.util.*;
|
|
|
+import java.util.Collections;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Objects;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
@@ -31,10 +35,10 @@ import java.util.stream.Collectors;
|
|
|
public class BokUtil {
|
|
|
private final RestRequestUtil restRequestUtil;
|
|
|
private final GuavaCacheUtil cacheUtils;
|
|
|
- @Value("${bok.url}")
|
|
|
- private String bokUrl = "http://bok.seecoder.cn";
|
|
|
- private String searchUrl = bokUrl + "/api/tq/search/";
|
|
|
- private String tqUrl = bokUrl + "/api/tq/";
|
|
|
+ @Value("${bok.searchUrl}")
|
|
|
+ private String searchUrl;
|
|
|
+ @Value("${bok.tqUrl}")
|
|
|
+ private String tqUrl;
|
|
|
|
|
|
public BokUtil(RestRequestUtil restRequestUtil, GuavaCacheUtil cacheUtils) {
|
|
|
this.restRequestUtil = restRequestUtil;
|
|
|
@@ -45,48 +49,46 @@ public class BokUtil {
|
|
|
Map<String, String> urlParams = ImmutableMap.of(
|
|
|
"content", stem,
|
|
|
"page", String.valueOf(1 + pageable.getPageNumber()),
|
|
|
- "size", String.valueOf(pageable.getPageSize()),
|
|
|
- "sort", pageable.getSort().toString().replaceAll(" ", "").replaceAll(":", ",")
|
|
|
+ "size", String.valueOf(pageable.getPageSize())
|
|
|
);
|
|
|
BokSearchResult result = restRequestUtil.sendGetRequest(
|
|
|
- searchUrl + "findByStemLike?content={content}&sort={sort}&size={size}&page={page}", BokSearchResult.class, urlParams);
|
|
|
+ searchUrl + "findByStemLike?content={content}&size={size}&page={page}", BokSearchResult.class, urlParams);
|
|
|
|
|
|
- List<BokQuestion> questions = result.getEmbedded().getChoiceQuestions();
|
|
|
+ List<BokQuestion> bokQuestions = result.getEmbedded().getQuestions();
|
|
|
cacheUtils.setAll(BOK_CACHE_NAME,
|
|
|
- questions.parallelStream()
|
|
|
- .collect(Collectors.toMap(BokQuestion::getTqId, bokQuestion -> bokQuestion)));
|
|
|
- return new PageImpl<>(questions, pageable, result.getPage().getTotalElements());
|
|
|
+ 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) {
|
|
|
- final ImmutableMap<Object, Object> bokQuestionsMap = cacheUtils.multiGet(BOK_CACHE_NAME, ids);
|
|
|
-
|
|
|
- List<String> requestIds = new ArrayList<>(ids);
|
|
|
- Map<String, BokQuestion> bokQuestionMap = Maps.newHashMapWithExpectedSize(ids.size());
|
|
|
-
|
|
|
- bokQuestionsMap
|
|
|
- .values()
|
|
|
- .forEach(value -> {
|
|
|
- BokQuestion bokQuestion = (BokQuestion) value;
|
|
|
- bokQuestionMap.put(bokQuestion.getTqId(), bokQuestion);
|
|
|
- requestIds.remove(bokQuestion.getTqId());
|
|
|
- });
|
|
|
+ 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(searchUrl + "findByIdIn?id={id}", BokSearchResult.class, urlParams);
|
|
|
- List<BokQuestion> bokQuestions = result.getEmbedded().getChoiceQuestions();
|
|
|
- bokQuestions.forEach(bokQuestion -> {
|
|
|
- cacheUtils.set(BOK_CACHE_NAME, bokQuestion.getTqId(), bokQuestion);
|
|
|
- bokQuestionMap.put(bokQuestion.getTqId(), bokQuestion);
|
|
|
- });
|
|
|
+ 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(bokQuestionMap::get)
|
|
|
+ .map(bokQuestionsMap::get)
|
|
|
.collect(Collectors.toList());
|
|
|
}
|
|
|
|
|
|
@@ -96,21 +98,21 @@ public class BokUtil {
|
|
|
return (BokQuestion) cachedBokQuestion;
|
|
|
}
|
|
|
BokQuestion bokQuestion = restRequestUtil.sendGetRequest(tqUrl + questionId, BokQuestion.class, Collections.emptyMap());
|
|
|
- cacheUtils.set(BOK_CACHE_NAME, bokQuestion.getTqId(), bokQuestion);
|
|
|
+ cacheUtils.set(BOK_CACHE_NAME, bokQuestion.getId(), bokQuestion);
|
|
|
return bokQuestion;
|
|
|
}
|
|
|
|
|
|
- public BokQuestion createQuestion(BaseQuestionDTO baseQuestionDTO) {
|
|
|
+ public BokQuestion createQuestion(String questionId, BaseQuestionDTO baseQuestionDTO) {
|
|
|
BokQuestion bokQuestion = getQuestion(baseQuestionDTO);
|
|
|
- bokQuestion.setTqId(UUID.randomUUID().toString());
|
|
|
+ bokQuestion.setId(questionId);
|
|
|
bokQuestion = restRequestUtil.sendPostRequest(tqUrl, bokQuestion, BokQuestion.class);
|
|
|
return bokQuestion;
|
|
|
}
|
|
|
|
|
|
public BokQuestion modifyQuestion(String questionId, BaseQuestionDTO baseQuestionDTO) {
|
|
|
BokQuestion bokQuestion = getQuestion(baseQuestionDTO);
|
|
|
- bokQuestion.setTqId(questionId);
|
|
|
- restRequestUtil.sendPutRequest(tqUrl, bokQuestion);
|
|
|
+ bokQuestion.setId(questionId);
|
|
|
+ restRequestUtil.sendPutRequest(tqUrl + questionId, bokQuestion);
|
|
|
return bokFindById(questionId);
|
|
|
}
|
|
|
|
|
|
@@ -122,6 +124,9 @@ public class BokUtil {
|
|
|
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)) {
|
|
|
@@ -145,12 +150,12 @@ public class BokUtil {
|
|
|
@Data
|
|
|
private static class BokSearchResult {
|
|
|
@JsonProperty("_embedded")
|
|
|
- private Embedded embedded;
|
|
|
+ private Embedded embedded = new Embedded();
|
|
|
private Page page;
|
|
|
|
|
|
@Data
|
|
|
private static class Embedded {
|
|
|
- private List<BokQuestion> choiceQuestions;
|
|
|
+ private List<BokQuestion> questions = Collections.emptyList();
|
|
|
}
|
|
|
|
|
|
@Data
|