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.ExtractConfigProviderService;
import cn.com.qmth.examcloud.question.commons.core.paper.DefaultPaper;
import cn.com.qmth.examcloud.support.CacheConstants;
import cn.com.qmth.examcloud.support.cache.bean.BasePaperCacheBean;
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 BasePaperCache extends RandomObjectRedisCache<BasePaperCacheBean> {

    @Autowired
    private ExtractConfigProviderService extractConfigProviderService;

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

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

        DefaultPaper defaultPaper = extractConfigProviderService.getBaseDefaultPaper(paperId);

        BasePaperCacheBean cacheBean = new BasePaperCacheBean();
        cacheBean.setDefaultPaper(defaultPaper);
        return cacheBean;
    }

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

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

}