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.ExtractConfigPaperCacheBean;
import cn.com.qmth.examcloud.web.cache.HashRedisCache;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class ExtractConfigPaperCache extends HashRedisCache<ExtractConfigPaperCacheBean> {

    @Autowired
    private ExtractConfigProviderService extractConfigProviderService;

    @Override
    public ExtractConfigPaperCacheBean loadFromResource(Object[] keys, Object[] subkeys) {
        Long examId = (Long) subkeys[0];
        String courseCode = String.valueOf(subkeys[1]);
        String groupCode = String.valueOf(subkeys[2]);
        String paperId = String.valueOf(keys[0]);

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

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

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

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

        return extractConfigProviderService.getExtractConfigPaper(examId, courseCode, groupCode, paperId);
    }

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

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

}