|
|
@@ -10,6 +10,7 @@ import nju.seec.helper.vo.quiz.ChoiceQuestionVO;
|
|
|
import nju.seec.helper.vo.quiz.QuestionVO;
|
|
|
import nju.seec.helper.vo.quiz.TrueOrFalseQuestionVO;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.data.domain.Pageable;
|
|
|
import org.springframework.http.HttpStatus;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
@@ -18,15 +19,19 @@ import org.springframework.transaction.annotation.Transactional;
|
|
|
import org.springframework.web.client.HttpClientErrorException;
|
|
|
|
|
|
import java.util.*;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
@Component
|
|
|
public class BOKUtil {
|
|
|
@Autowired
|
|
|
private RestRequestUtil restRequestUtil;
|
|
|
-
|
|
|
- private String searchUrl="http://bok.seecoder.cn/api/tq/search/";
|
|
|
- private String tqUrl="http://bok.seecoder.cn/api/tq/";
|
|
|
+ @Autowired
|
|
|
+ private CacheUtils 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<>();
|
|
|
@@ -51,34 +56,72 @@ public class BOKUtil {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
+ private static String CACHE_NAME="BOK";
|
|
|
public List<QuestionVO> BokFindByIdIn(List<String> Ids) {
|
|
|
//todo: 缓存Ids
|
|
|
-
|
|
|
- Map<String,String> urlParams=new HashMap<>();
|
|
|
- String idsStr=Ids.stream().reduce((a,b)->{return a+","+b;}).orElse("");
|
|
|
- urlParams.put("id",idsStr);
|
|
|
- urlParams.put("size",String.valueOf(Ids.size()));
|
|
|
- urlParams.put("page","1");
|
|
|
- try {
|
|
|
- Map ret = (Map)restRequestUtil.sendGetRequest(
|
|
|
- searchUrl+"findByIdIn?id={id}&size={size}&page={page}", urlParams);
|
|
|
- List<Map> tqs= ((List)((Map)ret.get("_embedded")).get("choiceQuestions"));
|
|
|
+ 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());
|
|
|
+ }
|
|
|
+ });
|
|
|
+ if(newIds.size()>0){
|
|
|
+ Map<String, String> urlParams = new HashMap<>();
|
|
|
+ String idsStr = newIds.stream().reduce((a, b) -> {
|
|
|
+ return a + "," + b;
|
|
|
+ }).orElse("");
|
|
|
+ urlParams.put("id", idsStr);
|
|
|
+ urlParams.put("size", String.valueOf(newIds.size()));
|
|
|
+ urlParams.put("page", "1");
|
|
|
+ List<Map> tqs = null;
|
|
|
+ try {
|
|
|
+ Map ret = (Map) restRequestUtil.sendGetRequest(
|
|
|
+ searchUrl + "findByIdIn?id={id}&size={size}&page={page}", urlParams);
|
|
|
+ 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()));
|
|
|
+ }
|
|
|
List<QuestionVO> collect = parseBody(tqs);
|
|
|
-
|
|
|
- // todo: 重排序
|
|
|
- return (collect);
|
|
|
-
|
|
|
- }catch (Exception e){
|
|
|
- e.printStackTrace();
|
|
|
- throw new RuntimeException("bok request error!for findByIdIn:"+ Arrays.toString(Ids.toArray()));
|
|
|
+ for (QuestionVO vo : collect) {
|
|
|
+ forSortMap.put(vo.getQuestionId(), vo);
|
|
|
+ //todo:异步批量存储
|
|
|
+ cacheUtils.set(CACHE_NAME, vo.getQuestionId(), JsonUtils.toJson(vo), 12, TimeUnit.HOURS);
|
|
|
+ }
|
|
|
}
|
|
|
+ // todo: 重排序
|
|
|
+ List<QuestionVO> ret=new ArrayList<>();
|
|
|
+ for (String id:Ids){
|
|
|
+ ret.add(forSortMap.get(id));
|
|
|
+ }
|
|
|
+ return (ret);
|
|
|
}
|
|
|
|
|
|
private List parseBody(List<Map> tqs) {
|
|
|
final List collect = tqs.stream().map(BOKUtil::parseOneQuestion).collect(Collectors.toList());
|
|
|
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 parseOneQuestion(Map q){
|
|
|
switch ((String) q.get("type")) {
|
|
|
case "choice":
|
|
|
@@ -87,7 +130,7 @@ public class BOKUtil {
|
|
|
.analysis((String) q.get("analysis")).QuestionId(String.valueOf((Integer) q.get("tq_id"))).build();
|
|
|
|
|
|
case "true_false":
|
|
|
- return TrueOrFalseQuestionVO.builder().stem((String) q.get("stem")).kind(QuestionKindState.CHOICE)
|
|
|
+ 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.valueOf((Integer) q.get("tq_id"))).build();
|
|
|
|
|
|
@@ -96,9 +139,16 @@ 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);
|
|
|
+ }
|
|
|
try {
|
|
|
Map ret = (Map)restRequestUtil.sendGetRequest(tqUrl+questionId, urlParams);
|
|
|
- return parseOneQuestion(ret);
|
|
|
+ final QuestionVO vo = parseOneQuestion(ret);
|
|
|
+ cacheUtils.set(CACHE_NAME, vo.getQuestionId(), JsonUtils.toJson(vo), 12, TimeUnit.HOURS);
|
|
|
+ return vo;
|
|
|
} catch (JsonProcessingException e) {
|
|
|
throw HelperException.of(ExceptionType.ERROR,"请求BOK题库数据错误:"+e.getMessage());
|
|
|
}catch (HttpClientErrorException e){
|