package org.sky.resource.rule.sms;

import com.qiniu.sms.SmsManager;
import com.qiniu.util.Auth;
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.QiniuSmsTemplate;
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.QINIU_SMS_RULE, name = "七牛SMS构建")
public class QiniuSmsRule 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.setSignName(sms.getSignName());
		Auth auth = Auth.create(smsProperties.getAccessKey(), smsProperties.getSecretKey());
		SmsManager smsManager = new SmsManager(auth);
		SmsTemplate smsTemplate = new QiniuSmsTemplate(smsProperties, smsManager, kdRedis);

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

	}
}
