PushConfigServiceImpl.java 7.3 KB

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