package cn.com.qmth.examcloud.core.questions.starter;

import cn.com.qmth.examcloud.core.questions.service.cache.BasePaperCache;
import cn.com.qmth.examcloud.core.questions.service.cache.QuestionCache;
import cn.com.qmth.examcloud.support.cache.CacheHelper;
import cn.com.qmth.examcloud.support.cache.bean.BasePaperCacheBean;
import cn.com.qmth.examcloud.support.cache.bean.QuestionCacheBean;
import cn.com.qmth.examcloud.web.support.Naked;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Set;

@Controller
@RequestMapping("${api_cqb}/")
public class DemoController {
    private static final Logger log = LoggerFactory.getLogger(DemoController.class);
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    @Autowired
    private BasePaperCache basePaperCache;
    @Autowired
    private QuestionCache questionCache;

    @Naked
    @ResponseBody
    @RequestMapping(value = "/demo", method = RequestMethod.GET)
    public String demo(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Long examId = 307L;
        String courseCode = "course-0815-feng";
        String groupCode = "X";
        String paperId = "2085113d-b74c-4dea-9f94-b22bb4825f2a";
        String questionId = "5d54d08def8fce19f8b3ab98";

        BasePaperCacheBean basePaperCacheBean = CacheHelper.getBasePaper(paperId);
        log.info("basePaperName: " + basePaperCacheBean.getDefaultPaper().getName());
        //basePaperCache.remove(paperId);

        QuestionCacheBean questionCacheBean = CacheHelper.getQuestion(examId, courseCode, groupCode, questionId);
        log.info("questionId: " + questionCacheBean.getDefaultQuestion().getId());
        //questionCache.remove(questionId);

        final String patternKey = "Q_QUESTION:*" + questionId;
        Set<String> keys = redisTemplate.keys(patternKey);
        log.info(StringUtils.join(keys, ","));

        return "ok";
    }

}