Newer
Older
newfiber-termite / newfiber-system / newfiber-system-sms / src / main / java / com / newfiber / system / builder / SmsBuilder.java
package com.newfiber.system.builder;


import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.newfiber.common.redis.service.RedisService;
import com.newfiber.system.domain.SmsConfig;
import com.newfiber.system.enums.SmsEnum;
import com.newfiber.system.service.ISmsConfigService;
import com.newfiber.system.template.SmsTemplate;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Sms短信服务统一构建类
 *
 * @author Chill
 */
public class SmsBuilder {

	private final ISmsConfigService smsConfigService;
	private final RedisService redisService;

	public SmsBuilder(ISmsConfigService smsConfigService, RedisService redisService) {
		this.smsConfigService = smsConfigService;
		this.redisService = redisService;
	}

	/**
	 * SmsTemplate配置缓存池
	 */
	private final Map<String, SmsTemplate> templatePool = new ConcurrentHashMap<>();

	/**
	 * Sms配置缓存池
	 */
	private final Map<String, SmsConfig> smsPool = new ConcurrentHashMap<>();

	/**
	 * 获取template
	 *
	 * @return SmsTemplate
	 */
	public SmsTemplate template() {
		return template(StringPool.EMPTY);
	}

	/**
	 * 获取template
	 *
	 * @param smsCode 资源编号
	 * @return SmsTemplate
	 */
	public SmsTemplate template(String smsCode) {
		SmsConfig smsConfig = smsConfigService.selectDetail(smsCode);
		SmsTemplate template = null;
		synchronized (SmsBuilder.class) {
			if (smsConfig.getCategory().equals(SmsEnum.QINIU.getCode())) {
				template = QiniuSmsBuilder.template(smsConfig, redisService);
			} else if (smsConfig.getCategory().equals(SmsEnum.ALI.getCode())) {
				template = AliSmsBuilder.template(smsConfig, redisService);
			} else if (smsConfig.getCategory().equals(SmsEnum.TENCENT.getCode())) {
				template = TencentSmsBuilder.template(smsConfig, redisService);
			}
		}
		return template;
	}

}