|
|
@@ -2,6 +2,7 @@ package nju.seec.helper.util;
|
|
|
|
|
|
import com.fasterxml.jackson.annotation.JsonInclude;
|
|
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
|
+import com.google.common.collect.ImmutableMap;
|
|
|
import lombok.Data;
|
|
|
import nju.seec.helper.util.enums.ExceptionType;
|
|
|
import nju.seec.helper.util.enums.QuestionKindState;
|
|
|
@@ -27,13 +28,14 @@ public class BOKUtil {
|
|
|
@Autowired
|
|
|
private RestRequestUtil restRequestUtil;
|
|
|
@Autowired
|
|
|
- private CacheUtils cacheUtils;
|
|
|
+ private 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/";
|
|
|
// 应该做成bok的库
|
|
|
public void BokFindByStemLike(String stem, List<QuestionVO> Questions, Map page, Pageable pageable) {
|
|
|
+
|
|
|
Map<String,String> urlParams=new HashMap<>();
|
|
|
urlParams.put("content",stem);
|
|
|
urlParams.put("sort",pageable.getSort().toString().replaceAll(" ","").replaceAll(":",","));
|
|
|
@@ -58,25 +60,26 @@ public class BOKUtil {
|
|
|
}
|
|
|
private static String CACHE_NAME="BOK";
|
|
|
public List<QuestionVO> BokFindByIdIn(List<String> Ids) {
|
|
|
- //todo: 缓存Ids
|
|
|
- List<String> newIds = new ArrayList<>(Ids);
|
|
|
- List<String> bok = cacheUtils.multiGet(CACHE_NAME, newIds);
|
|
|
- Map<String, QuestionVO> forSortMap = new HashMap<>(Ids.size());
|
|
|
- bok.forEach(json -> {
|
|
|
- if (json != null && !json.equals("null")) {
|
|
|
- Map voMap = JsonUtils.fromJson(json, Map.class);
|
|
|
- QuestionVO questionVO = parseRedisRet(voMap);
|
|
|
- forSortMap.put(questionVO.getQuestionId(), questionVO);
|
|
|
- newIds.remove(questionVO.getQuestionId());
|
|
|
+ //todo: 从缓存中取Ids
|
|
|
+ Set<String> requestIds = new HashSet<>(Ids);
|
|
|
+ final ImmutableMap<Object, Object> bok = cacheUtils.multiGet(CACHE_NAME,requestIds );
|
|
|
+
|
|
|
+ Map<String, QuestionVO> QuestionsMap = new HashMap<>(Ids.size());
|
|
|
+ bok.values().forEach(vo -> {
|
|
|
+ if (vo instanceof QuestionVO) {
|
|
|
+ QuestionVO questionVO = (QuestionVO) vo;
|
|
|
+ QuestionsMap.put(questionVO.getQuestionId(), questionVO);
|
|
|
+ requestIds.remove(questionVO.getQuestionId());
|
|
|
}
|
|
|
});
|
|
|
- if(newIds.size()>0){
|
|
|
+ //未缓存的去这里拿
|
|
|
+ if(requestIds.size()>0){
|
|
|
Map<String, String> urlParams = new HashMap<>();
|
|
|
- String idsStr = newIds.stream().reduce((a, b) -> {
|
|
|
+ String idsStr = requestIds.stream().reduce((a, b) -> {
|
|
|
return a + "," + b;
|
|
|
}).orElse("");
|
|
|
urlParams.put("id", idsStr);
|
|
|
- urlParams.put("size", String.valueOf(newIds.size()));
|
|
|
+ urlParams.put("size", String.valueOf(requestIds.size()));
|
|
|
urlParams.put("page", "1");
|
|
|
List<Map> tqs = null;
|
|
|
try {
|
|
|
@@ -85,19 +88,21 @@ public class BOKUtil {
|
|
|
tqs = ((List) ((Map) ret.get("_embedded")).get("choiceQuestions"));
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
- throw new RuntimeException("bok request error!for findByIdIn:" + Arrays.toString(newIds.toArray()));
|
|
|
+ throw new RuntimeException("bok request error!for findByIdIn:" + Arrays.toString(requestIds.toArray()));
|
|
|
}
|
|
|
List<QuestionVO> collect = parseBody(tqs);
|
|
|
+ Map<String,Object> newGet=new HashMap<>(collect.size());
|
|
|
for (QuestionVO vo : collect) {
|
|
|
- forSortMap.put(vo.getQuestionId(), vo);
|
|
|
- //todo:异步批量存储
|
|
|
- cacheUtils.set(CACHE_NAME, vo.getQuestionId(), JsonUtils.toJson(vo), 12, TimeUnit.HOURS);
|
|
|
+ QuestionsMap.put(vo.getQuestionId(), vo);
|
|
|
+ newGet.put(vo.getQuestionId(),vo);
|
|
|
}
|
|
|
+ cacheUtils.setAll(CACHE_NAME,newGet);
|
|
|
}
|
|
|
- // todo: 重排序
|
|
|
+
|
|
|
+ // todo: 按照Ids重排序
|
|
|
List<QuestionVO> ret=new ArrayList<>();
|
|
|
for (String id:Ids){
|
|
|
- ret.add(forSortMap.get(id));
|
|
|
+ ret.add(QuestionsMap.get(id));
|
|
|
}
|
|
|
return (ret);
|
|
|
}
|
|
|
@@ -107,21 +112,21 @@ public class BOKUtil {
|
|
|
return collect;
|
|
|
}
|
|
|
|
|
|
- private static QuestionVO parseRedisRet(Map q){
|
|
|
- switch ((String) q.get("kind")) {
|
|
|
- case "CHOICE":
|
|
|
- return ChoiceQuestionVO.builder().stem((String) q.get("stem")).kind(QuestionKindState.CHOICE)
|
|
|
- .options((Map<String, String>) q.get("options")).answer(((String) q.get("answer")))
|
|
|
- .analysis((String) q.get("analysis")).QuestionId((String) q.get("QuestionId")).build();
|
|
|
-
|
|
|
- case "TRUE_FALSE":
|
|
|
- return TrueOrFalseQuestionVO.builder().stem((String) q.get("stem")).kind(QuestionKindState.TRUE_FALSE)
|
|
|
- .answer(Boolean.valueOf((String) q.get("answer")))
|
|
|
- .analysis((String) q.get("analysis")).QuestionId((String) q.get("QuestionId")).build();
|
|
|
-
|
|
|
- }
|
|
|
- throw HelperException.of(ExceptionType.ERROR,"BOK-unknown type:" + (String) q.get("type"));
|
|
|
- }
|
|
|
+// private static QuestionVO parseRedisRet(Map q){
|
|
|
+// switch ((String) q.get("kind")) {
|
|
|
+// case "CHOICE":
|
|
|
+// return ChoiceQuestionVO.builder().stem((String) q.get("stem")).kind(QuestionKindState.CHOICE)
|
|
|
+// .options((Map<String, String>) q.get("options")).answer(((String) q.get("answer")))
|
|
|
+// .analysis((String) q.get("analysis")).QuestionId((String) q.get("QuestionId")).build();
|
|
|
+//
|
|
|
+// case "TRUE_FALSE":
|
|
|
+// return TrueOrFalseQuestionVO.builder().stem((String) q.get("stem")).kind(QuestionKindState.TRUE_FALSE)
|
|
|
+// .answer(Boolean.valueOf((String) q.get("answer")))
|
|
|
+// .analysis((String) q.get("analysis")).QuestionId((String) q.get("QuestionId")).build();
|
|
|
+//
|
|
|
+// }
|
|
|
+// throw HelperException.of(ExceptionType.ERROR,"BOK-unknown type:" + (String) q.get("type"));
|
|
|
+// }
|
|
|
private static QuestionVO parseOneQuestion(Map q){
|
|
|
switch ((String) q.get("type")) {
|
|
|
case "choice":
|
|
|
@@ -139,15 +144,14 @@ public class BOKUtil {
|
|
|
}
|
|
|
public QuestionVO BokFindById(String questionId) {
|
|
|
Map<String,String> urlParams=new HashMap<>();
|
|
|
- String json = cacheUtils.get(CACHE_NAME, questionId);
|
|
|
- if (json != null && !json.equals("null")) {
|
|
|
- Map voMap = JsonUtils.fromJson(json, Map.class);
|
|
|
- return parseRedisRet(voMap);
|
|
|
+ Object cachedVo = cacheUtils.get(CACHE_NAME, questionId);
|
|
|
+ if (cachedVo instanceof QuestionVO) {
|
|
|
+ return (QuestionVO) cachedVo;
|
|
|
}
|
|
|
try {
|
|
|
Map ret = (Map)restRequestUtil.sendGetRequest(tqUrl+questionId, urlParams);
|
|
|
final QuestionVO vo = parseOneQuestion(ret);
|
|
|
- cacheUtils.set(CACHE_NAME, vo.getQuestionId(), JsonUtils.toJson(vo), 12, TimeUnit.HOURS);
|
|
|
+ cacheUtils.set(CACHE_NAME, vo.getQuestionId(), vo);
|
|
|
return vo;
|
|
|
} catch (JsonProcessingException e) {
|
|
|
throw HelperException.of(ExceptionType.ERROR,"请求BOK题库数据错误:"+e.getMessage());
|
|
|
@@ -205,9 +209,9 @@ public class BOKUtil {
|
|
|
throw HelperException.of(ExceptionType.PARAM_ERROR,"should POST a present question");
|
|
|
}
|
|
|
}
|
|
|
+ restRequestUtil.sendPutRequest(tqUrl+questionId, new BOK_TQ(vo));
|
|
|
|
|
|
- BOK_TQ bok_tq=new BOK_TQ(vo);
|
|
|
- restRequestUtil.sendPutRequest(tqUrl+questionId, bok_tq);
|
|
|
+ cacheUtils.remove(CACHE_NAME,questionId);
|
|
|
return vo;
|
|
|
}
|
|
|
public void deleteQuestion(String questionId) {
|
|
|
@@ -216,8 +220,9 @@ public class BOKUtil {
|
|
|
} catch (JsonProcessingException e) {
|
|
|
throw HelperException.of(ExceptionType.ERROR,"请求BOK题库数据错误:"+e.getMessage());
|
|
|
}
|
|
|
+ cacheUtils.remove(CACHE_NAME,questionId);
|
|
|
}
|
|
|
-
|
|
|
+ // 来自restBok接口的对象
|
|
|
@JsonInclude(JsonInclude.Include.NON_NULL)
|
|
|
@Data
|
|
|
class BOK_TQ{
|