|
|
@@ -2,12 +2,10 @@ package cn.reghao.autodop.dmaster.notification.service;
|
|
|
|
|
|
import cn.reghao.autodop.common.http.DefaultWebRequest;
|
|
|
import cn.reghao.autodop.common.http.WebRequest;
|
|
|
+import cn.reghao.autodop.dmaster.notification.db.query.DingAccountQuery;
|
|
|
+import cn.reghao.autodop.dmaster.notification.db.query.NotifyGroupQuery;
|
|
|
import cn.reghao.autodop.dmaster.notification.model.po.*;
|
|
|
import cn.reghao.autodop.common.util.thread.ThreadPoolWrapper;
|
|
|
-import cn.reghao.autodop.dmaster.notification.db.repository.DingAccountRepository;
|
|
|
-import cn.reghao.autodop.dmaster.notification.db.repository.EmailAccountRepository;
|
|
|
-import cn.reghao.autodop.dmaster.notification.db.repository.NotifyGroupRepository;
|
|
|
-import cn.reghao.autodop.dmaster.notification.db.repository.SmsAccountRepository;
|
|
|
import cn.reghao.autodop.dmaster.notification.service.notifier.Notify;
|
|
|
import cn.reghao.autodop.dmaster.notification.service.notifier.ding.DingMsg;
|
|
|
import cn.reghao.autodop.dmaster.notification.service.notifier.ding.DingNotify;
|
|
|
@@ -32,23 +30,16 @@ import java.util.concurrent.ExecutorService;
|
|
|
@Slf4j
|
|
|
@Service
|
|
|
public class NotifyService {
|
|
|
- private ExecutorService threadPool = ThreadPoolWrapper.threadPool("notify-service");
|
|
|
- private Map<NotifyGroup, Notify> notifierMap = new HashMap<>();
|
|
|
- private WebRequest webRequest;
|
|
|
- private NotifyGroupRepository groupRepository;
|
|
|
- private DingAccountRepository dingRepository;
|
|
|
- private EmailAccountRepository emailRepository;
|
|
|
- private SmsAccountRepository smsRepository;
|
|
|
+ private final ExecutorService threadPool = ThreadPoolWrapper.threadPool("notify-service");
|
|
|
+ private final Map<NotifyGroup, Notify> notifierMap = new HashMap<>();
|
|
|
+ private final WebRequest webRequest;
|
|
|
+ private final NotifyGroupQuery notifyGroupQuery;
|
|
|
+ private final DingAccountQuery dingAccountQuery;
|
|
|
|
|
|
- public NotifyService(NotifyGroupRepository groupRepository,
|
|
|
- DingAccountRepository dingRepository,
|
|
|
- EmailAccountRepository emailRepository,
|
|
|
- SmsAccountRepository smsRepository) {
|
|
|
+ public NotifyService(NotifyGroupQuery notifyGroupQuery, DingAccountQuery dingAccountQuery) {
|
|
|
this.webRequest = new DefaultWebRequest();
|
|
|
- this.groupRepository = groupRepository;
|
|
|
- this.dingRepository = dingRepository;
|
|
|
- this.emailRepository = emailRepository;
|
|
|
- this.smsRepository = smsRepository;
|
|
|
+ this.notifyGroupQuery = notifyGroupQuery;
|
|
|
+ this.dingAccountQuery = dingAccountQuery;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -60,7 +51,7 @@ public class NotifyService {
|
|
|
*/
|
|
|
@PostConstruct
|
|
|
private void initNotifier() throws UnsupportedEncodingException {
|
|
|
- for (NotifyGroup group : groupRepository.findAll()) {
|
|
|
+ for (NotifyGroup group : notifyGroupQuery.findAll()) {
|
|
|
addNotifier(group);
|
|
|
}
|
|
|
}
|
|
|
@@ -75,23 +66,11 @@ public class NotifyService {
|
|
|
String notifyAccountId = notifyGroup.getNotifyAccountId();
|
|
|
switch (NotifyType.valueOf(notifyType)) {
|
|
|
case ding:
|
|
|
- DingAccount dingAccount = dingRepository.findDingAccountByNotifyAccountId(notifyAccountId);
|
|
|
+ DingAccount dingAccount = dingAccountQuery.findByNotifyAccountId(notifyAccountId);
|
|
|
notifierMap.put(notifyGroup, new DingNotify(webRequest, dingAccount));
|
|
|
break;
|
|
|
- case email:
|
|
|
- EmailAccount emailAccount = emailRepository.findEmailAccountByNotifyAccountId(notifyAccountId);
|
|
|
- if (emailAccount != null) {
|
|
|
- notifierMap.put(notifyGroup, new EmailNotify(emailAccount));
|
|
|
- }
|
|
|
- break;
|
|
|
- case sms:
|
|
|
- SmsAccount smsAccount = smsRepository.findSmsAccountByNotifyAccountId(notifyAccountId);
|
|
|
- if (smsAccount != null) {
|
|
|
- notifierMap.put(notifyGroup, new SmsNotify(webRequest, smsAccount));
|
|
|
- }
|
|
|
- break;
|
|
|
default:
|
|
|
- log.error("通知类型不存在");
|
|
|
+ log.error("{} 通知类型暂未实现", notifyType);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -104,31 +83,18 @@ public class NotifyService {
|
|
|
}
|
|
|
|
|
|
String notifyType = notifyGroup.getNotifyType();
|
|
|
- /*switch (NotifyType.valueOf(notifyType)) {
|
|
|
+ switch (NotifyType.valueOf(notifyType)) {
|
|
|
case ding:
|
|
|
if (!(msg instanceof DingMsg)) {
|
|
|
log.error("{} 消息格式不正确, 不是 DingMsg", msg);
|
|
|
}
|
|
|
- notifyGroup.getReceivers()
|
|
|
- .forEach(receiver -> threadPool.execute(new NotifyTask(notify, receiver, msg)));
|
|
|
- break;
|
|
|
- case email:
|
|
|
- if (!(msg instanceof EmailMsg)) {
|
|
|
- log.error("{} 消息格式不正确, 不是 EmailMsg", msg);
|
|
|
- }
|
|
|
- notifyGroup.getReceivers()
|
|
|
- .forEach(receiver -> threadPool.execute(new NotifyTask(notify, receiver, msg)));
|
|
|
- break;
|
|
|
- case sms:
|
|
|
- if (!(msg instanceof String)) {
|
|
|
- log.error("{} 消息格式不正确, 不是 SMS 消息", msg);
|
|
|
- }
|
|
|
- notifyGroup.getReceivers()
|
|
|
- .forEach(receiver -> threadPool.execute(new NotifyTask(notify, receiver, msg)));
|
|
|
+ DingMsg dingMsg = (DingMsg) msg;
|
|
|
+ String receiver = notifyGroup.getReceiver();
|
|
|
+ threadPool.execute(new NotifyTask<DingMsg>(notify, receiver, dingMsg));
|
|
|
break;
|
|
|
default:
|
|
|
- log.error("通知类型不存在");
|
|
|
- }*/
|
|
|
+ log.error("{} 通知类型暂未实现", notifyType);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -139,9 +105,9 @@ public class NotifyService {
|
|
|
* @date 2021-06-23 上午9:29
|
|
|
*/
|
|
|
static class NotifyTask<T> implements Runnable {
|
|
|
- private Notify<T> notify;
|
|
|
- private String receiver;
|
|
|
- private T msg;
|
|
|
+ private final Notify<T> notify;
|
|
|
+ private final String receiver;
|
|
|
+ private final T msg;
|
|
|
|
|
|
public NotifyTask(Notify<T> notify, String receiver, T msg) {
|
|
|
this.notify = notify;
|