|
|
@@ -0,0 +1,88 @@
|
|
|
+package nju.seec.helper.util;
|
|
|
+
|
|
|
+import com.fasterxml.jackson.core.JsonProcessingException;
|
|
|
+import nju.seec.helper.util.enums.QuestionKindState;
|
|
|
+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.data.domain.Pageable;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+@Component
|
|
|
+public class BOKUtil {
|
|
|
+ @Autowired
|
|
|
+ private RestRequestUtil restRequestUtil;
|
|
|
+
|
|
|
+ private String searchUrl="http://bok.seecoder.cn/api/tq/search/";
|
|
|
+ // 应该做成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(":",","));
|
|
|
+ urlParams.put("size",String.valueOf(pageable.getPageSize()));
|
|
|
+ urlParams.put("page",String.valueOf(1+pageable.getPageNumber()));
|
|
|
+ try {
|
|
|
+ Map ret = (Map)restRequestUtil.sendPostGet(
|
|
|
+ searchUrl+"findByStemLike?content={content}&sort={sort}&size={size}&page={page}", urlParams);
|
|
|
+
|
|
|
+ List<Map> tqs= ((List)((Map)ret.get("_embedded")).get("choiceQuestions"));
|
|
|
+
|
|
|
+ List collect = parseBody(tqs);
|
|
|
+ try {
|
|
|
+ page.putAll((Map)ret.get("page"));
|
|
|
+ Questions.addAll(collect);
|
|
|
+ }catch (Exception e){
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ }catch (Exception e){
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public List<QuestionVO> BokFindByIdIn(List<String> 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.sendPostGet(
|
|
|
+ searchUrl+"findByIdIn?id={id}&sort={sort}&size={size}&page={page}", urlParams);
|
|
|
+ List<Map> tqs= ((List)((Map)ret.get("_embedded")).get("choiceQuestions"));
|
|
|
+ List<QuestionVO> collect = parseBody(tqs);
|
|
|
+ return (collect);
|
|
|
+
|
|
|
+ }catch (Exception e){
|
|
|
+ e.printStackTrace();
|
|
|
+ throw new RuntimeException("bok request error!for findByIdIn:"+ Arrays.toString(Ids.toArray()));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private List parseBody(List<Map> tqs) {
|
|
|
+ final List collect =
|
|
|
+ tqs.stream().map(q -> {
|
|
|
+ switch ((String) q.get("type")) {
|
|
|
+ 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")).replaceAll("【答案】", ""))
|
|
|
+ .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)
|
|
|
+ .answer((Boolean) (q.get("answer")))
|
|
|
+ .analysis((String) q.get("analysis")).QuestionId(String.valueOf((Integer) q.get("tq_id"))).build();
|
|
|
+
|
|
|
+ }
|
|
|
+ return new Exception("unknown type:" + (String) q.get("type"));
|
|
|
+ }).collect(Collectors.toList());
|
|
|
+ return collect;
|
|
|
+ }
|
|
|
+}
|