| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- package nju.seec.helper.api.bok;
- import cn.hutool.core.util.StrUtil;
- import com.google.common.collect.ImmutableMap;
- import lombok.Data;
- import nju.seec.helper.api.bok.constant.QuestionType;
- import nju.seec.helper.api.bok.vo.PageableQuestionListVO;
- import nju.seec.helper.api.bok.vo.QuestionVO;
- import nju.seec.helper.dto.question.BaseQuestionDTO;
- import nju.seec.helper.dto.question.ChoiceQuestionDTO;
- import nju.seec.helper.dto.question.TrueOrFalseQuestionDTO;
- import nju.seec.helper.enums.ExceptionType;
- import nju.seec.helper.exception.HelperException;
- import nju.seec.helper.util.Consts;
- import nju.seec.helper.util.RestRequestUtil;
- import nju.seec.helper.util.cache.CaffeineCacheUtils;
- import org.springframework.boot.context.properties.ConfigurationProperties;
- import org.springframework.data.domain.Page;
- import org.springframework.data.domain.PageImpl;
- import org.springframework.data.domain.Pageable;
- import org.springframework.stereotype.Component;
- import java.util.List;
- import java.util.Map;
- import java.util.stream.Collectors;
- /**
- * @author xst
- * <p>
- * updated by cst
- */
- @Component
- @ConfigurationProperties("bok.question")
- @Data
- public class QuestionApi {
- private static final String BOK_QUESTION_CACHE_NAME = Consts.BOK_QUESTION_CACHE_NAME;
- private final RestRequestUtil restRequestUtil;
- private final CaffeineCacheUtils cacheUtils;
- private String url;
- private String searchUrl;
- public QuestionApi(RestRequestUtil restRequestUtil, CaffeineCacheUtils cacheUtils) {
- this.restRequestUtil = restRequestUtil;
- this.cacheUtils = cacheUtils;
- }
- @SuppressWarnings({"unchecked", "rawtypes"})
- public Page<QuestionVO> getQuestionList(String stem, String tag, String knowledgeId, Pageable pageable) {
- Map<String, ?> urlParams = (Map) ImmutableMap.builder()
- .put("stem", stem)
- .put("tag", tag)
- .put("knowledgeId", knowledgeId)
- .put("page", String.valueOf(1 + pageable.getPageNumber()))
- .put("size", String.valueOf(pageable.getPageSize()))
- .put("sort", pageable.getSort().stream()
- .map(order -> StrUtil.concat(true, "sort=", order.getProperty(), ",", order.getDirection().toString()))
- .reduce((o1, o2) -> StrUtil.join("&", o1, o2))
- .orElse("")
- )
- .build();
- PageableQuestionListVO result = restRequestUtil.sendGetRequest(searchUrl, PageableQuestionListVO.class, urlParams);
- List<QuestionVO> questionVOList = result.getQuestions();
- cacheUtils.setAll(BOK_QUESTION_CACHE_NAME, questionVOList.parallelStream().collect(Collectors.toMap(QuestionVO::getId, bokQuestion -> bokQuestion)));
- return new PageImpl<>(questionVOList, pageable, result.getPage().getTotalElements());
- }
- public QuestionVO getQuestionsById(String questionId) {
- Object cachedBokQuestion = cacheUtils.get(BOK_QUESTION_CACHE_NAME, questionId);
- if (cachedBokQuestion instanceof QuestionVO) {
- return (QuestionVO) cachedBokQuestion;
- }
- QuestionVO questionVO = restRequestUtil.sendGetRequest(url + "/" + questionId, QuestionVO.class);
- cacheUtils.set(BOK_QUESTION_CACHE_NAME, questionVO.getId(), questionVO);
- return questionVO;
- }
- public QuestionVO createQuestion(String questionId, BaseQuestionDTO baseQuestionDTO) {
- QuestionVO questionVO = createQuestionVO(baseQuestionDTO);
- questionVO.setId(questionId);
- questionVO = restRequestUtil.sendPostRequest(url, questionVO, QuestionVO.class);
- return questionVO;
- }
- public QuestionVO updateQuestion(String questionId, BaseQuestionDTO baseQuestionDTO) {
- QuestionVO questionVO = createQuestionVO(baseQuestionDTO);
- questionVO.setId(questionId);
- restRequestUtil.sendPutRequest(url + "/" + questionId, questionVO);
- return getQuestionsById(questionId);
- }
- public void deleteQuestion(String questionId) {
- restRequestUtil.sendDeleteRequest(url + "/" + questionId);
- }
- private QuestionVO createQuestionVO(BaseQuestionDTO baseQuestionDTO) {
- QuestionVO questionVO = new QuestionVO();
- questionVO.setType(QuestionType.valueOf(baseQuestionDTO.getType()));
- questionVO.setStem(baseQuestionDTO.getStem());
- questionVO.setKeyPoints(baseQuestionDTO.getKeyPoints());
- questionVO.setTags(baseQuestionDTO.getTags());
- questionVO.setKnowledgeId(baseQuestionDTO.getKnowledgeId());
- questionVO.setAnalysis(baseQuestionDTO.getAnalysis());
- switch (questionVO.getType()) {
- case choice:
- ChoiceQuestionDTO choiceQuestionDTO = (ChoiceQuestionDTO) baseQuestionDTO;
- questionVO.setOptions(choiceQuestionDTO.getOptions());
- questionVO.setAnswer(choiceQuestionDTO.getAnswer());
- break;
- case true_false:
- TrueOrFalseQuestionDTO trueOrFalseQuestionDTO = (TrueOrFalseQuestionDTO) baseQuestionDTO;
- questionVO.setAnswer(String.valueOf(trueOrFalseQuestionDTO.getAnswer()));
- break;
- default:
- throw HelperException.of(ExceptionType.ERROR, "不支持的题目类型");
- }
- return questionVO;
- }
- }
|