package org.sky.resource.rule.sms;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import org.sky.core.literule.annotation.LiteRuleComponent;
import org.sky.core.literule.core.RuleComponent;
import org.sky.core.redis.cache.KdRedis;
import org.sky.core.sms.AliSmsTemplate;
import org.sky.core.sms.SmsTemplate;
import org.sky.core.sms.props.SmsProperties;
import org.sky.resource.pojo.entity.Sms;
import org.sky.resource.rule.constant.SmsRuleConstant;
import org.sky.resource.rule.context.SmsContext;

/**
 * 阿里云短信构建类
 *
 */
@LiteRuleComponent(id = SmsRuleConstant.ALI_SMS_RULE, name = "阿里SMS构建")
public class AliSmsRule extends RuleComponent {

	@Override
	public void process() {
		// 获取上下文
		SmsContext contextBean = this.getContextBean(SmsContext.class);
		Sms sms = contextBean.getSms();
		KdRedis kdRedis = contextBean.getKdRedis();

		SmsProperties smsProperties = new SmsProperties();
		smsProperties.setTemplateId(sms.getTemplateId());
		smsProperties.setAccessKey(sms.getAccessKey());
		smsProperties.setSecretKey(sms.getSecretKey());
		smsProperties.setRegionId(sms.getRegionId());
		smsProperties.setSignName(sms.getSignName());
		IClientProfile profile = DefaultProfile.getProfile(smsProperties.getRegionId(), smsProperties.getAccessKey(), smsProperties.getSecretKey());
		IAcsClient acsClient = new DefaultAcsClient(profile);
		SmsTemplate smsTemplate = new AliSmsTemplate(smsProperties, acsClient, kdRedis);


		// 设置上下文
		contextBean.setSmsTemplate(smsTemplate);

	}
}
