package org.sky.resource.rule.sms;

import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.sms.v20210111.SmsClient;
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.SmsTemplate;
import org.sky.core.sms.TencentSmsTemplate;
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.TENCENT_SMS_RULE, name = "腾讯SMS构建")
public class TencentSmsRule 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.setAppId(sms.getAppId());
		smsProperties.setRegionId(sms.getRegionId());
		smsProperties.setSignName(sms.getSignName());

		Credential cred = new Credential(smsProperties.getAccessKey(), smsProperties.getSecretKey());
		SmsClient client = new SmsClient(cred, smsProperties.getRegionId());
		SmsTemplate smsTemplate = new TencentSmsTemplate(smsProperties, client, kdRedis);

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

	}
}
