44import io .github .opensabre .sysadmin .notification .enums .NotificationType ;
55import org .springframework .beans .factory .annotation .Autowired ;
66import org .springframework .stereotype .Component ;
7+ import org .springframework .util .Assert ;
78
89import java .util .List ;
910import java .util .Map ;
10- import java .util .concurrent .ConcurrentHashMap ;
1111import java .util .stream .Collectors ;
1212
1313/**
1717@ Component
1818public class NotificationServiceManager {
1919
20- private final Map <String , INotificationService > notificationServices ;
20+ private final Map <NotificationType , INotificationService > notificationServices ;
2121
2222 @ Autowired
2323 public NotificationServiceManager (List <INotificationService > services ) {
24- this .notificationServices = services .stream ()
25- .collect (Collectors .toMap (service -> service .getType ().getCode ().toUpperCase (), service -> service ));
24+ this .notificationServices = services .stream ().collect (Collectors .toMap (INotificationService ::getType , service -> service ));
2625 }
2726
2827 /**
@@ -34,11 +33,9 @@ public NotificationServiceManager(List<INotificationService> services) {
3433 * @param args 模板参数
3534 * @return 发送结果
3635 */
37- public String sendNotification (String type , String target , NotificationTemplate template , Object ... args ) {
38- INotificationService service = notificationServices .get (type .toUpperCase ());
39- if (service == null ) {
40- throw new IllegalArgumentException ("Unsupported notification type: " + type );
41- }
36+ public String sendNotification (NotificationType type , String target , NotificationTemplate template , Object ... args ) {
37+ INotificationService service = notificationServices .get (type );
38+ Assert .notNull (service , "No notification service found for type: " + type );
4239 return service .send (target , template , args );
4340 }
4441
@@ -51,11 +48,9 @@ public String sendNotification(String type, String target, NotificationTemplate
5148 * @param args 模板参数 (Map格式)
5249 * @return 发送结果
5350 */
54- public String sendNotification (String type , String target , NotificationTemplate template , Map <String , String > args ) {
55- INotificationService service = notificationServices .get (type .toUpperCase ());
56- if (service == null ) {
57- throw new IllegalArgumentException ("Unsupported notification type: " + type );
58- }
51+ public String sendNotification (NotificationType type , String target , NotificationTemplate template , Map <String , String > args ) {
52+ INotificationService service = notificationServices .get (type );
53+ Assert .notNull (service , "No notification service found for type: " + type );
5954 return service .send (target , template , args );
6055 }
6156
@@ -65,8 +60,8 @@ public String sendNotification(String type, String target, NotificationTemplate
6560 * @param type 通知类型
6661 * @return 通知服务实例
6762 */
68- public INotificationService getService ( String type ) {
69- return notificationServices .get (type . toUpperCase () );
63+ public INotificationService get ( NotificationType type ) {
64+ return notificationServices .get (type );
7065 }
7166
7267 /**
@@ -75,7 +70,7 @@ public INotificationService getService(String type) {
7570 * @param type 通知类型
7671 * @return 是否支持
7772 */
78- public boolean supportsType (String type ) {
79- return notificationServices .containsKey (type . toUpperCase () );
73+ public boolean supportsType (NotificationType type ) {
74+ return notificationServices .containsKey (type );
8075 }
8176}
0 commit comments