package cn.com.qmth.examcloud.core.questions.service.cache;

import cn.com.qmth.examcloud.commons.exception.StatusException;
import cn.com.qmth.examcloud.core.questions.service.QuesService;
import cn.com.qmth.examcloud.core.questions.service.bean.QuestionAnswerBean;
import cn.com.qmth.examcloud.support.CacheConstants;
import cn.com.qmth.examcloud.support.cache.bean.QuestionAnswerCacheBean;
import cn.com.qmth.examcloud.web.cache.RandomObjectRedisCache;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class QuestionAnswerCache extends RandomObjectRedisCache<QuestionAnswerCacheBean> {

    @Autowired
    private QuesService quesService;

    @Override
    public QuestionAnswerCacheBean loadFromResource(Object... keys) {
        String questionId = String.valueOf(keys[0]);

        if (StringUtils.isBlank(questionId)) {
            throw new StatusException("400", "questionId is empty");
        }

        QuestionAnswerBean bean = quesService.findQuestionAnswer(questionId);

        QuestionAnswerCacheBean cacheBean = new QuestionAnswerCacheBean();
        cacheBean.setQuestionId(bean.getQuestionId());
        cacheBean.setRightAnswers(bean.getRightAnswers());

        return cacheBean;
    }

    @Override
    protected String getKeyPrefix() {
        return CacheConstants.CACHE_Q_QUESTION_ANSWER;
    }

    @Override
    protected int getTimeout() {
        return 2 * 60 * 60;// N小时
    }

}