沙钢智慧能源系统后端代码
houzhongjian
2024-10-09 97edd72e5e6dbb134cedae4b72c95be8c948c5ec
提交 | 用户 | 时间
97edd7 1 package com.iailab.module.shasteel.framework.sms.core.client.impl;
H 2
3 import com.iailab.module.shasteel.framework.sms.core.client.SmsClient;
4 import com.iailab.module.shasteel.framework.sms.core.client.SmsClientFactory;
5 import com.iailab.module.shasteel.framework.sms.core.enums.SmsChannelEnum;
6 import com.iailab.module.shasteel.framework.sms.core.property.SmsChannelProperties;
7 import lombok.extern.slf4j.Slf4j;
8 import org.springframework.util.Assert;
9 import org.springframework.validation.annotation.Validated;
10
11 import java.util.Arrays;
12 import java.util.concurrent.ConcurrentHashMap;
13 import java.util.concurrent.ConcurrentMap;
14
15 /**
16  * 短信客户端工厂接口
17  *
18  * @author zzf
19  */
20 @Validated
21 @Slf4j
22 public class SmsClientFactoryImpl implements SmsClientFactory {
23
24     /**
25      * 短信客户端 Map
26      * key:渠道编号,使用 {@link SmsChannelProperties#getId()}
27      */
28     private final ConcurrentMap<Long, AbstractSmsClient> channelIdClients = new ConcurrentHashMap<>();
29
30     /**
31      * 短信客户端 Map
32      * key:渠道编码,使用 {@link SmsChannelProperties#getCode()} ()}
33      *
34      * 注意,一些场景下,需要获得某个渠道类型的客户端,所以需要使用它。
35      * 例如说,解析短信接收结果,是相对通用的,不需要使用某个渠道编号的 {@link #channelIdClients}
36      */
37     private final ConcurrentMap<String, AbstractSmsClient> channelCodeClients = new ConcurrentHashMap<>();
38
39     public SmsClientFactoryImpl() {
40         // 初始化 channelCodeClients 集合
41         Arrays.stream(SmsChannelEnum.values()).forEach(channel -> {
42             // 创建一个空的 SmsChannelProperties 对象
43             SmsChannelProperties properties = new SmsChannelProperties().setCode(channel.getCode())
44                     .setApiKey("default default").setApiSecret("default");
45             // 创建 Sms 客户端
46             AbstractSmsClient smsClient = createSmsClient(properties);
47             channelCodeClients.put(channel.getCode(), smsClient);
48         });
49     }
50
51     @Override
52     public SmsClient getSmsClient(Long channelId) {
53         return channelIdClients.get(channelId);
54     }
55
56     @Override
57     public SmsClient getSmsClient(String channelCode) {
58         return channelCodeClients.get(channelCode);
59     }
60
61     @Override
62     public void createOrUpdateSmsClient(SmsChannelProperties properties) {
63         AbstractSmsClient client = channelIdClients.get(properties.getId());
64         if (client == null) {
65             client = this.createSmsClient(properties);
66             client.init();
67             channelIdClients.put(client.getId(), client);
68         } else {
69             client.refresh(properties);
70         }
71     }
72
73     private AbstractSmsClient createSmsClient(SmsChannelProperties properties) {
74         SmsChannelEnum channelEnum = SmsChannelEnum.getByCode(properties.getCode());
75         Assert.notNull(channelEnum, String.format("渠道类型(%s) 为空", channelEnum));
76         // 创建客户端
77         switch (channelEnum) {
78             case ALIYUN: return new AliyunSmsClient(properties);
79             case DEBUG_DING_TALK: return new DebugDingTalkSmsClient(properties);
80             case TENCENT: return new TencentSmsClient(properties);
81         }
82         // 创建失败,错误日志 + 抛出异常
83         log.error("[createSmsClient][配置({}) 找不到合适的客户端实现]", properties);
84         throw new IllegalArgumentException(String.format("配置(%s) 找不到合适的客户端实现", properties));
85     }
86
87 }