PushConfigServiceImpl.java 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. package com.jjt.push.service.impl;
  2. import cn.hutool.json.JSONUtil;
  3. import com.jjt.biz.domain.AlarmRecord;
  4. import com.jjt.common.utils.DateUtils;
  5. import com.jjt.common.utils.StringUtils;
  6. import com.jjt.push.domain.PushConfig;
  7. import com.jjt.push.mapper.PushConfigMapper;
  8. import com.jjt.push.service.IMailService;
  9. import com.jjt.push.service.IPushConfigService;
  10. import com.jjt.push.service.ISmsService;
  11. import lombok.extern.slf4j.Slf4j;
  12. import org.springframework.stereotype.Service;
  13. import javax.annotation.Resource;
  14. import java.util.List;
  15. import java.util.Map;
  16. /**
  17. * 推送配置Service业务层处理
  18. *
  19. * @author jjt
  20. * @date 2024-10-08
  21. */
  22. @Service
  23. @Slf4j
  24. public class PushConfigServiceImpl implements IPushConfigService {
  25. @Resource
  26. private PushConfigMapper pushConfigMapper;
  27. @Resource
  28. private IMailService mailService;
  29. @Resource
  30. private ISmsService smsService;
  31. /**
  32. * 查询推送配置
  33. *
  34. * @param pcId 推送配置主键
  35. * @return 推送配置
  36. */
  37. @Override
  38. public PushConfig selectPushConfigByPcId(Long pcId) {
  39. return pushConfigMapper.selectPushConfigByPcId(pcId);
  40. }
  41. /**
  42. * 查询推送配置列表
  43. *
  44. * @param pushConfig 推送配置
  45. * @return 推送配置
  46. */
  47. @Override
  48. public List<PushConfig> selectPushConfigList(PushConfig pushConfig) {
  49. return pushConfigMapper.selectPushConfigList(pushConfig);
  50. }
  51. /**
  52. * 新增推送配置
  53. *
  54. * @param pushConfig 推送配置
  55. * @return 结果
  56. */
  57. @Override
  58. public int insertPushConfig(PushConfig pushConfig) {
  59. return pushConfigMapper.insertPushConfig(pushConfig);
  60. }
  61. /**
  62. * 修改推送配置
  63. *
  64. * @param pushConfig 推送配置
  65. * @return 结果
  66. */
  67. @Override
  68. public int updatePushConfig(PushConfig pushConfig) {
  69. return pushConfigMapper.updatePushConfig(pushConfig);
  70. }
  71. /**
  72. * 批量删除推送配置
  73. *
  74. * @param pcIds 需要删除的推送配置主键
  75. * @return 结果
  76. */
  77. @Override
  78. public int deletePushConfigByPcIds(Long[] pcIds) {
  79. return pushConfigMapper.deletePushConfigByPcIds(pcIds);
  80. }
  81. /**
  82. * 删除推送配置信息
  83. *
  84. * @param pcId 推送配置主键
  85. * @return 结果
  86. */
  87. @Override
  88. public int deletePushConfigByPcId(Long pcId) {
  89. return pushConfigMapper.deletePushConfigByPcId(pcId);
  90. }
  91. /**
  92. * 发送短信
  93. *
  94. * @param phone 手机号
  95. * @param content 内容
  96. */
  97. @Override
  98. public void sendSms(String phone, String content) throws Exception {
  99. smsService.send(phone, content);
  100. }
  101. /**
  102. * 推送告警信息
  103. *
  104. * @param record 告警记录
  105. */
  106. @Override
  107. public void push(AlarmRecord record) {
  108. //获取短信接口发送地址
  109. PushConfig pc = pushConfigMapper.selectPushConfigByPcId(1L);
  110. String title = record.getObjName() + "-" + record.getMetricsName() + "超过阈值" + record.getRemark() + ",当前状态:" + record.getAlarmValue();
  111. String content = "";
  112. String phones = "";
  113. String mails = "";
  114. content += "告警指标:" + record.getMetricsName() + "\n";
  115. content += "告警对象:" + record.getObjName() + "\n";
  116. content += "告警内容: 指标值达到告警条件" + record.getRemark() + "\n";
  117. content += "告警时间:" + DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, record.getAlarmTime()) + "\n";
  118. switch (record.getAlarmLevel()) {
  119. case "low":
  120. title = "轻度告警:" + title;
  121. phones = pc.getLowSms();
  122. mails = pc.getLowMail();
  123. content += "告警等级:低\n";
  124. break;
  125. case "mid":
  126. title = "一般告警:" + title;
  127. phones = pc.getMidSms();
  128. mails = pc.getMidMail();
  129. content += "告警等级:中\n";
  130. break;
  131. case "high":
  132. title = "严重告警:" + title;
  133. phones = pc.getHighSms();
  134. mails = pc.getHighMail();
  135. content += "告警等级:高\n";
  136. break;
  137. default:
  138. phones = "";
  139. }
  140. content += "当前状态:" + record.getAlarmValue() + "\n";
  141. String open = "Y";
  142. if (open.equals(pc.getFlagSms())) {
  143. //短信开关打开
  144. try {
  145. Map map = JSONUtil.toBean(pc.getConfigSms(), Map.class);
  146. String uri = (String) map.get("uri");
  147. if (StringUtils.isNotEmpty(phones)) {
  148. String[] ps = phones.split("\n");
  149. for (String p : ps) {
  150. try {
  151. smsService.send(uri, p, content);
  152. } catch (Exception e) {
  153. log.error("短信发送失败:{}", e.getMessage());
  154. }
  155. }
  156. }
  157. } catch (Exception e) {
  158. e.printStackTrace();
  159. }
  160. }
  161. if (open.equals(pc.getFlagMail())) {
  162. //邮件开关打开
  163. try {
  164. if (StringUtils.isNotEmpty(mails)) {
  165. mails = mails.replace("\n", ",");
  166. mailService.send(mails, title, content);
  167. }
  168. } catch (Exception e) {
  169. e.printStackTrace();
  170. }
  171. }
  172. // String
  173. }
  174. /**
  175. * 发送邮件
  176. *
  177. * @param mail 邮箱
  178. * @param title 标题
  179. * @param content 内容
  180. */
  181. @Override
  182. public void sendMail(String mail, String title, String content) throws Exception {
  183. mailService.send(mail, title, content);
  184. }
  185. }