|
@@ -1,11 +1,20 @@
|
|
|
package com.jjt.push.service.impl;
|
|
|
|
|
|
-import java.util.List;
|
|
|
-import org.springframework.stereotype.Service;
|
|
|
-import com.jjt.push.mapper.PushConfigMapper;
|
|
|
+import cn.hutool.json.JSONUtil;
|
|
|
+import com.jjt.biz.domain.AlarmRecord;
|
|
|
+import com.jjt.common.utils.DateUtils;
|
|
|
+import com.jjt.common.utils.StringUtils;
|
|
|
import com.jjt.push.domain.PushConfig;
|
|
|
+import com.jjt.push.mapper.PushConfigMapper;
|
|
|
+import com.jjt.push.service.IMailService;
|
|
|
import com.jjt.push.service.IPushConfigService;
|
|
|
+import com.jjt.push.service.ISmsService;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
import javax.annotation.Resource;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
|
|
|
/**
|
|
|
* 推送配置Service业务层处理
|
|
@@ -14,9 +23,14 @@ import javax.annotation.Resource;
|
|
|
* @date 2024-10-08
|
|
|
*/
|
|
|
@Service
|
|
|
+@Slf4j
|
|
|
public class PushConfigServiceImpl implements IPushConfigService {
|
|
|
@Resource
|
|
|
private PushConfigMapper pushConfigMapper;
|
|
|
+ @Resource
|
|
|
+ private IMailService mailService;
|
|
|
+ @Resource
|
|
|
+ private ISmsService smsService;
|
|
|
|
|
|
/**
|
|
|
* 查询推送配置
|
|
@@ -48,7 +62,7 @@ public class PushConfigServiceImpl implements IPushConfigService {
|
|
|
*/
|
|
|
@Override
|
|
|
public int insertPushConfig(PushConfig pushConfig) {
|
|
|
- return pushConfigMapper.insertPushConfig(pushConfig);
|
|
|
+ return pushConfigMapper.insertPushConfig(pushConfig);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -83,4 +97,101 @@ public class PushConfigServiceImpl implements IPushConfigService {
|
|
|
public int deletePushConfigByPcId(Long pcId) {
|
|
|
return pushConfigMapper.deletePushConfigByPcId(pcId);
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 发送短信
|
|
|
+ *
|
|
|
+ * @param phone 手机号
|
|
|
+ * @param content 内容
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public void sendSms(String phone, String content) throws Exception {
|
|
|
+ smsService.send(phone, content);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 推送告警信息
|
|
|
+ *
|
|
|
+ * @param record 告警记录
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public void push(AlarmRecord record) {
|
|
|
+ //获取短信接口发送地址
|
|
|
+ PushConfig pc = pushConfigMapper.selectPushConfigByPcId(1L);
|
|
|
+ String title = record.getObjName() + "-" + record.getMetricsName() + "超过阈值";
|
|
|
+ String content = "";
|
|
|
+ String phones = "";
|
|
|
+ String mails = "";
|
|
|
+ content += "告警指标:" + record.getMetricsName() + "\n";
|
|
|
+ content += "告警对象:" + record.getObjName() + "\n";
|
|
|
+ content += "告警时间:" + DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS,record.getAlarmTime()) + "\n";
|
|
|
+ switch (record.getAlarmLevel()) {
|
|
|
+ case "low":
|
|
|
+ title = "轻度告警:" + title;
|
|
|
+ phones = pc.getLowSms();
|
|
|
+ mails = pc.getLowMail();
|
|
|
+ content += "告警等级:低\n";
|
|
|
+ break;
|
|
|
+ case "mid":
|
|
|
+ title = "一般告警:" + title;
|
|
|
+ phones = pc.getMidSms();
|
|
|
+ mails = pc.getMidMail();
|
|
|
+ content += "告警等级:中\n";
|
|
|
+ break;
|
|
|
+ case "high":
|
|
|
+ title = "严重告警:" + title;
|
|
|
+ phones = pc.getHighSms();
|
|
|
+ mails = pc.getHighMail();
|
|
|
+ content += "告警等级:高\n";
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ phones = "";
|
|
|
+ }
|
|
|
+ content += "当前状态:" + record.getAlarmValue() + "\n";
|
|
|
+ String open = "Y";
|
|
|
+ if (open.equals(pc.getFlagSms())) {
|
|
|
+ //短信开关打开
|
|
|
+ try {
|
|
|
+ Map map = JSONUtil.toBean(pc.getConfigSms(), Map.class);
|
|
|
+ String uri = (String) map.get("uri");
|
|
|
+ if (StringUtils.isNotEmpty(phones)) {
|
|
|
+ String[] ps = phones.split("\n");
|
|
|
+ for (String p : ps) {
|
|
|
+ try {
|
|
|
+ smsService.send(uri, p, title);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("短信发送失败:{}", e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (open.equals(pc.getFlagMail())) {
|
|
|
+ //邮件开关打开
|
|
|
+ try {
|
|
|
+ if (StringUtils.isNotEmpty(mails)) {
|
|
|
+ mails = mails.replace("\n", ",");
|
|
|
+ mailService.send(mails, title, content);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+// String
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 发送邮件
|
|
|
+ *
|
|
|
+ * @param mail 邮箱
|
|
|
+ * @param title 标题
|
|
|
+ * @param content 内容
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public void sendMail(String mail, String title, String content) throws Exception {
|
|
|
+ mailService.send(mail, title, content);
|
|
|
+ }
|
|
|
}
|