
package org.sky.resource.rule.sms;

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.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.FINALLY_SMS_RULE, name = "SMS构建后置处理")
public class FinallySmsRule 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();

		if (contextBean.getIsCached()) {
			SmsTemplate template = templatePool.get(tenantId);
			contextBean.setSmsTemplate(template);
		} else {
			Sms sms = contextBean.getSms();
			SmsTemplate template = contextBean.getSmsTemplate();
			if (Func.hasEmpty(template, sms)) {
				throw new ServiceException("SMS接口读取失败!");
			} else {
				templatePool.put(tenantId, template);
				smsPool.put(tenantId, sms);
			}
		}

	}
}
