package org.sky.resource.rule.sms;

import org.sky.core.literule.annotation.LiteRuleComponent;
import org.sky.core.literule.core.RuleComponent;
import org.sky.core.sms.SmsTemplate;
import org.sky.core.tool.utils.Func;
import org.sky.resource.pojo.entity.Sms;
import org.sky.resource.rule.constant.SmsRuleConstant;
import org.sky.resource.rule.context.SmsContext;

import java.util.Map;

/**
 * Sms前置处理
 *
 */
@LiteRuleComponent(id = SmsRuleConstant.PRE_SMS_RULE, name = "SMS构建前置处理")
public class PreSmsRule extends RuleComponent {
	@Override
	public void process() {
		String tenantId = this.getRequestData();
		SmsContext contextBean = this.getContextBean(SmsContext.class);
		Map<String, Sms> smsPool = contextBean.getSmsPool();
		Map<String, SmsTemplate> templatePool = contextBean.getTemplatePool();
		Sms sms = contextBean.getSms();
		Sms smsCached = smsPool.get(tenantId);
		SmsTemplate template = templatePool.get(tenantId);
		// 若为空或者不一致，则重新加载
		if (Func.hasEmpty(template, smsCached) || !sms.getTemplateId().equals(smsCached.getTemplateId()) || !sms.getAccessKey().equals(smsCached.getAccessKey())) {
			contextBean.setIsCached(Boolean.FALSE);
		} else {
			contextBean.setIsCached(Boolean.TRUE);
		}
	}

}
