123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229 |
- 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.domain.PushRecord;
- 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.math.RoundingMode;
- 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;
- @Resource
- private PushRecordServiceImpl pushRecordService;
- /**
- * 查询推送配置
- *
- * @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().setScale(2, RoundingMode.HALF_UP) + "\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) {
- PushRecord pr = new PushRecord();
- pr.setPushType("sms");
- pr.setAlarmLevel(record.getAlarmLevel());
- // pr.setSendTitle(title);
- pr.setSendContent(content);
- pr.setSendObj(p);
- try {
- smsService.send(uri, p, content);
- pr.setSendSuccess("Y");
- } catch (Exception e) {
- pr.setSendSuccess("N");
- log.error("短信发送失败:{}", e.getMessage());
- pr.setRemark(e.getMessage());
- }
- pushRecordService.insertPushRecord(pr);
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- if (open.equals(pc.getFlagMail())) {
- //邮件开关打开
- if (StringUtils.isNotEmpty(mails)) {
- try {
- String[] mail = mails.split("\n");
- for (String p : mail) {
- PushRecord pr = new PushRecord();
- pr.setPushType("mail");
- pr.setAlarmLevel(record.getAlarmLevel());
- pr.setSendTitle(title);
- pr.setSendContent(content);
- pr.setSendObj(p);
- try {
- mailService.send(p, title, content);
- pr.setSendSuccess("Y");
- } catch (Exception e) {
- e.printStackTrace();
- pr.setSendSuccess("N");
- log.error("邮件发送失败:{}", e.getMessage());
- pr.setRemark(e.getMessage());
- }
- pushRecordService.insertPushRecord(pr);
- }
- } 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);
- }
- }
|