| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 | 
							- package com.jjt.push.service.impl;
 
- 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业务层处理
 
-  *
 
-  * @author jjt
 
-  * @date 2024-10-08
 
-  */
 
- @Service
 
- @Slf4j
 
- public class PushConfigServiceImpl implements IPushConfigService {
 
-     @Resource
 
-     private PushConfigMapper pushConfigMapper;
 
-     @Resource
 
-     private IMailService mailService;
 
-     @Resource
 
-     private ISmsService smsService;
 
-     /**
 
-      * 查询推送配置
 
-      *
 
-      * @param pcId 推送配置主键
 
-      * @return 推送配置
 
-      */
 
-     @Override
 
-     public PushConfig selectPushConfigByPcId(Long pcId) {
 
-         return pushConfigMapper.selectPushConfigByPcId(pcId);
 
-     }
 
-     /**
 
-      * 查询推送配置列表
 
-      *
 
-      * @param pushConfig 推送配置
 
-      * @return 推送配置
 
-      */
 
-     @Override
 
-     public List<PushConfig> selectPushConfigList(PushConfig pushConfig) {
 
-         return pushConfigMapper.selectPushConfigList(pushConfig);
 
-     }
 
-     /**
 
-      * 新增推送配置
 
-      *
 
-      * @param pushConfig 推送配置
 
-      * @return 结果
 
-      */
 
-     @Override
 
-     public int insertPushConfig(PushConfig pushConfig) {
 
-         return pushConfigMapper.insertPushConfig(pushConfig);
 
-     }
 
-     /**
 
-      * 修改推送配置
 
-      *
 
-      * @param pushConfig 推送配置
 
-      * @return 结果
 
-      */
 
-     @Override
 
-     public int updatePushConfig(PushConfig pushConfig) {
 
-         return pushConfigMapper.updatePushConfig(pushConfig);
 
-     }
 
-     /**
 
-      * 批量删除推送配置
 
-      *
 
-      * @param pcIds 需要删除的推送配置主键
 
-      * @return 结果
 
-      */
 
-     @Override
 
-     public int deletePushConfigByPcIds(Long[] pcIds) {
 
-         return pushConfigMapper.deletePushConfigByPcIds(pcIds);
 
-     }
 
-     /**
 
-      * 删除推送配置信息
 
-      *
 
-      * @param pcId 推送配置主键
 
-      * @return 结果
 
-      */
 
-     @Override
 
-     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() + "超过阈值" + record.getRemark() + ",当前状态:" + record.getAlarmValue();
 
-         String content = "";
 
-         String phones = "";
 
-         String mails = "";
 
-         content += "告警指标:" + record.getMetricsName() + "\n";
 
-         content += "告警对象:" + record.getObjName() + "\n";
 
-         content += "告警内容: 指标值达到告警条件" + record.getRemark() + "\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, content);
 
-                         } 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);
 
-     }
 
- }
 
 
  |