|
|
@@ -0,0 +1,78 @@
|
|
|
+package nju.seec.helper.service.impl;
|
|
|
+
|
|
|
+import com.fasterxml.jackson.core.JsonProcessingException;
|
|
|
+import nju.seec.helper.dto.LoginUser;
|
|
|
+import nju.seec.helper.service.QuestionService;
|
|
|
+import nju.seec.helper.util.JsonUtils;
|
|
|
+import nju.seec.helper.util.RestRequestUtil;
|
|
|
+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.omg.CORBA.portable.UnknownException;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.data.domain.Page;
|
|
|
+import org.springframework.data.domain.PageImpl;
|
|
|
+import org.springframework.data.domain.Pageable;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+@Service
|
|
|
+public class QuestionServiceImpl implements QuestionService {
|
|
|
+ @Autowired
|
|
|
+ private RestRequestUtil restRequestUtil;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Page<QuestionVO> getQuestionList(LoginUser user, String stem, Pageable pageable) throws JsonProcessingException {
|
|
|
+ List<QuestionVO> Questions = new ArrayList<QuestionVO>();
|
|
|
+ Map page = new HashMap();
|
|
|
+ BokFindByStemLike(stem, Questions,page,pageable);
|
|
|
+ Page<QuestionVO> ret =new PageImpl<QuestionVO>(Questions,pageable,Long.valueOf((Integer)page.get("totalElements")));
|
|
|
+ return ret;
|
|
|
+ }
|
|
|
+ // 应该做成bok的库
|
|
|
+ private 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(
|
|
|
+ "http://bok.seecoder.cn/api/tq/search/findByStemLike?content={content}&sort={sort}&size={size}&page={page}", urlParams);
|
|
|
+
|
|
|
+ List<Map> tqs= ((List)((Map)ret.get("_embedded")).get("choiceQuestions"));
|
|
|
+
|
|
|
+ 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());
|
|
|
+ try {
|
|
|
+ page.putAll((Map)ret.get("page"));
|
|
|
+ Questions.addAll(collect);
|
|
|
+ }catch (Exception e){
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ }catch (Exception e){
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|