package org.sky.resource.rule.oss;

import org.sky.core.literule.annotation.LiteRuleComponent;
import org.sky.core.literule.core.RuleComponent;
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.constant.OssRuleConstant;
import org.sky.resource.rule.context.OssContext;

import java.util.Map;

/**
 * Oss前置处理
 *
 */
@LiteRuleComponent(id = OssRuleConstant.PRE_OSS_RULE, name = "OSS构建前置处理")
public class PreOssRule 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();
		Oss oss = contextBean.getOss();
		Oss ossCached = ossPool.get(tenantId);
		OssTemplate template = templatePool.get(tenantId);
		// 若为空或者不一致，则重新加载
		if (Func.hasEmpty(template, ossCached)
			|| !Func.equalsSafe(oss.getEndpoint(), ossCached.getEndpoint())
			|| !Func.equalsSafe(oss.getTransformEndpoint(), ossCached.getTransformEndpoint())
			|| !Func.equalsSafe(oss.getAccessKey(), ossCached.getAccessKey())) {
			contextBean.setIsCached(Boolean.FALSE);
		} else {
			contextBean.setIsCached(Boolean.TRUE);
		}
	}

}
