package org.sky.resource.rule.oss;

import org.sky.core.literule.annotation.LiteRuleComponent;
import org.sky.core.literule.core.RuleComponent;
import org.sky.core.log.exception.ServiceException;
import org.sky.core.oss.OssTemplate;
import org.sky.core.tool.utils.Func;
import org.sky.resource.pojo.entity.Oss;
import org.sky.resource.rule.context.OssContext;

import java.util.Map;

import static org.sky.resource.rule.constant.OssRuleConstant.FINALLY_OSS_RULE;

/**
 * Oss后置处理
 *
 */
@LiteRuleComponent(id = FINALLY_OSS_RULE, name = "OSS构建后置处理")
public class FinallyOssRule extends RuleComponent {
	@Override
	public void process() {
		String tenantId = this.getRequestData();
		OssContext contextBean = this.getContextBean(OssContext.class);
		Map<String, Oss> ossPool = contextBean.getOssPool();
		Map<String, OssTemplate> templatePool = contextBean.getTemplatePool();

		if (contextBean.getIsCached()) {
			OssTemplate template = templatePool.get(tenantId);
			contextBean.setOssTemplate(template);
		} else {
			Oss oss = contextBean.getOss();
			OssTemplate template = contextBean.getOssTemplate();
			if (Func.hasEmpty(template, oss)) {
				throw new ServiceException("OSS接口读取失败!");
			} else {
				templatePool.put(tenantId, template);
				ossPool.put(tenantId, oss);
			}
		}

	}
}
