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.support.CacheConstants;
import cn.com.qmth.examcloud.support.cache.bean.ExtractConfigCacheBean;
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 ExtractConfigCache extends RandomObjectRedisCache<ExtractConfigCacheBean> {
    @Autowired
    private ExtractConfigProviderService extractConfigProviderService;

    @Override
    public ExtractConfigCacheBean loadFromResource(Object... keys) {
        Long examId = (Long) keys[0];
        String courseCode = String.valueOf(keys[1]);

        if (examId == null) {
            throw new StatusException("400", "examId is null");
        }

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

        return extractConfigProviderService.getExtractConfig(examId, courseCode);
    }

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

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

}