WarnManageServiceImpl.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. package com.ruoyi.manage.service.impl;
  2. import com.alibaba.fastjson2.JSONObject;
  3. import com.ruoyi.common.core.domain.AjaxResult;
  4. import com.ruoyi.common.core.redis.RedisCache;
  5. import com.ruoyi.common.model.MqttMessage;
  6. import com.ruoyi.common.utils.DateUtils;
  7. import com.ruoyi.common.utils.StringUtils;
  8. import com.ruoyi.manage.domain.WarnManage;
  9. import com.ruoyi.manage.mapper.WarnManageMapper;
  10. import com.ruoyi.manage.service.IWarnManageService;
  11. import org.springframework.beans.factory.annotation.Autowired;
  12. import org.springframework.stereotype.Service;
  13. import java.text.DecimalFormat;
  14. import java.util.ArrayList;
  15. import java.util.HashMap;
  16. import java.util.List;
  17. import java.util.Map;
  18. import java.util.stream.Collectors;
  19. import static com.ruoyi.common.constant.Constants.*;
  20. /**
  21. * 告警管理Service业务层处理
  22. *
  23. * @author boman
  24. * @date 2025-06-19
  25. */
  26. @Service
  27. public class WarnManageServiceImpl implements IWarnManageService {
  28. @Autowired
  29. private WarnManageMapper warnManageMapper;
  30. @Autowired
  31. private RedisCache redisCache;
  32. /**
  33. * 查询告警管理
  34. *
  35. * @param warnId 告警管理主键
  36. * @return 告警管理
  37. */
  38. @Override
  39. public WarnManage selectWarnManageByWarnId(Long warnId) {
  40. return warnManageMapper.selectWarnManageByWarnId(warnId);
  41. }
  42. /**
  43. * 查询告警管理列表
  44. *
  45. * @param warnManage 告警管理
  46. * @return 告警管理
  47. */
  48. @Override
  49. public List<WarnManage> selectWarnManageList(WarnManage warnManage) {
  50. return warnManageMapper.selectWarnManageList(warnManage);
  51. }
  52. /**
  53. * 新增告警管理
  54. *
  55. * @param warnManage 告警管理
  56. * @return 结果
  57. */
  58. @Override
  59. public int insertWarnManage(WarnManage warnManage) {
  60. warnManage.setCreateTime(DateUtils.getNowDate());
  61. return warnManageMapper.insertWarnManage(warnManage);
  62. }
  63. /**
  64. * 修改告警管理
  65. *
  66. * @param warnManage 告警管理
  67. * @return 结果
  68. */
  69. @Override
  70. public int updateWarnManage(WarnManage warnManage) {
  71. warnManage.setUpdateTime(DateUtils.getNowDate());
  72. return warnManageMapper.updateWarnManage(warnManage);
  73. }
  74. /**
  75. * 批量删除告警管理
  76. *
  77. * @param warnIds 需要删除的告警管理主键
  78. * @return 结果
  79. */
  80. @Override
  81. public int deleteWarnManageByWarnIds(Long[] warnIds) {
  82. return warnManageMapper.deleteWarnManageByWarnIds(warnIds);
  83. }
  84. /**
  85. * 删除告警管理信息
  86. *
  87. * @param warnId 告警管理主键
  88. * @return 结果
  89. */
  90. @Override
  91. public int deleteWarnManageByWarnId(Long warnId) {
  92. return warnManageMapper.deleteWarnManageByWarnId(warnId);
  93. }
  94. /**
  95. * 告警内容+部门统计
  96. *
  97. * @return
  98. */
  99. @Override
  100. public AjaxResult getWarnManageIndex() {
  101. //定义返回值
  102. Map<String, Object> map = new HashMap<>(9);
  103. WarnManage warnManage = new WarnManage();
  104. warnManage.setWarnTime(DateUtils.getNowDate());
  105. //当月告警数据
  106. List<WarnManage> warnManages = warnManageMapper.selectWarnManageDeptListByTime(warnManage);
  107. int warnMonthAll = 0;
  108. int warnMonthLeave = 0;
  109. int warnMonthPlay = 0;
  110. int warnMonthLastAll = 0;
  111. int warnMonthLastLeave = 0;
  112. int warnMonthLastPlay = 0;
  113. //本月部门数据
  114. List<Map<String, String>> deptNameMapList = new ArrayList<>();
  115. //上月部门数据
  116. List<Map<String, String>> deptNameLastMapList = new ArrayList<>();
  117. //计算告警数据
  118. if (warnManages != null && !warnManages.isEmpty()) {
  119. warnMonthAll = warnManages.size();
  120. Map<String, List<WarnManage>> collect = warnManages.stream().collect(Collectors.groupingBy(WarnManage::getAlgorithmType));
  121. if (!collect.isEmpty()) {
  122. //离岗
  123. if (!collect.get(ZERO).isEmpty()) {
  124. warnMonthLastLeave = collect.get(ZERO).size();
  125. }
  126. //玩手机
  127. if (!collect.get(ONE).isEmpty()) {
  128. warnMonthLastPlay = collect.get(ONE).size();
  129. }
  130. }
  131. //插入当月部门数据
  132. Map<String, List<WarnManage>> deptNameCollect = warnManages.stream().collect(Collectors.groupingBy(WarnManage::getDeptName));
  133. List<Map<String, String>> finalDeptNameMapList = new ArrayList<>();
  134. deptNameCollect.forEach((deptName, v) -> {
  135. Map<String, String> deptMap = new HashMap<>(9);
  136. deptMap.put("deptName", deptName);
  137. deptMap.put("warnNum", String.valueOf(v.size()));
  138. finalDeptNameMapList.add(deptMap);
  139. });
  140. deptNameMapList = finalDeptNameMapList;
  141. }
  142. //上月告警数据
  143. Map<String, Object> warnLastMonth = redisCache.getCacheMap(WARN_LAST_MONTH + DateUtils.getMonth());
  144. if (warnLastMonth.isEmpty()) {
  145. //从数据库查询
  146. warnManage.setWarnTime(DateUtils.getLastMonth());
  147. List<WarnManage> warnManagesLast = warnManageMapper.selectWarnManageDeptListByTime(warnManage);
  148. if (warnManagesLast != null && !warnManagesLast.isEmpty()) {
  149. warnMonthLastAll = warnManagesLast.size();
  150. Map<String, List<WarnManage>> collect = warnManagesLast.stream().collect(Collectors.groupingBy(WarnManage::getAlgorithmType));
  151. if (!collect.isEmpty()) {
  152. //离岗
  153. if (!collect.get(ZERO).isEmpty()) {
  154. warnMonthLeave = collect.get(ZERO).size();
  155. }
  156. //玩手机
  157. if (!collect.get(ONE).isEmpty()) {
  158. warnMonthLastPlay = collect.get(ONE).size();
  159. }
  160. }
  161. }
  162. Map<String, Object> lastMap = new HashMap<>();
  163. lastMap.put("warnMonthLastAll", warnMonthLastAll);
  164. lastMap.put("warnMonthLastLeave", warnMonthLastLeave);
  165. lastMap.put("warnMonthLastPlay", warnMonthLastPlay);
  166. //插入上月部门数据
  167. Map<String, List<WarnManage>> deptNameCollect = warnManages.stream().collect(Collectors.groupingBy(WarnManage::getDeptName));
  168. List<Map<String, String>> finalDeptNameLastMapList = new ArrayList<>();
  169. deptNameCollect.forEach((deptName, v) -> {
  170. Map<String, String> deptMap = new HashMap<>(9);
  171. deptMap.put("deptName", deptName);
  172. deptMap.put("warnNum", String.valueOf(v.size()));
  173. finalDeptNameLastMapList.add(deptMap);
  174. });
  175. lastMap.put("deptNameCollect", finalDeptNameLastMapList);
  176. deptNameLastMapList = finalDeptNameLastMapList;
  177. redisCache.setCacheMap(WARN_LAST_MONTH + DateUtils.getMonth(), lastMap);
  178. } else {
  179. warnMonthLastAll = Integer.parseInt(warnLastMonth.get("warnMonthLastAll").toString());
  180. warnMonthLastLeave = Integer.parseInt(warnLastMonth.get("warnMonthLastLeave").toString());
  181. warnMonthLastPlay = Integer.parseInt(warnLastMonth.get("warnMonthLastPlay").toString());
  182. deptNameLastMapList = convertObjectToList(warnLastMonth.get("deptNameCollect"));
  183. }
  184. //进行本月部门数据和上月部门数据对比,已本月部门数据为主
  185. if (!deptNameMapList.isEmpty()) {
  186. List<Map<String, String>> finalDeptNameMapList = deptNameLastMapList;
  187. deptNameMapList.forEach(deptNameMap -> {
  188. String deptName = deptNameMap.get("deptName");
  189. String warnNum = deptNameMap.get("warnNum");
  190. String warnNumLast = "0";
  191. if (!finalDeptNameMapList.isEmpty()) {
  192. for (Map<String, String> lastMonthDeptMap : finalDeptNameMapList) {
  193. String deptNameLast = lastMonthDeptMap.get(deptName);
  194. if (StringUtils.isNotEmpty(deptNameLast)) {
  195. warnNumLast = lastMonthDeptMap.get("warnNum");
  196. }
  197. }
  198. }
  199. //计算百分比
  200. Map<String, String> numPt = getNumPt(Double.parseDouble(warnNum), Double.parseDouble(warnNumLast));
  201. String warnDeptPt = numPt.get("pt");
  202. String warnDeptPtStatus = numPt.get("ptStatus");
  203. deptNameMap.put("warnDeptPt", warnDeptPt);
  204. deptNameMap.put("warnDeptPtStatus", warnDeptPtStatus);
  205. });
  206. }
  207. map.put("warnMonthAll", warnMonthAll);
  208. Map<String, String> numPt = getNumPt(warnMonthAll, warnMonthLastAll);
  209. map.put("warnMonthPt", numPt.get("pt"));
  210. map.put("warnMonthPtStatus", numPt.get("ptStatus"));
  211. map.put("warnMonthLeave", warnMonthLeave);
  212. Map<String, String> numPt1 = getNumPt(warnMonthLeave, warnMonthLastLeave);
  213. map.put("warnMonthLeavePt", numPt1.get("pt"));
  214. map.put("warnMonthLeavePtStatus", numPt1.get("ptStatus"));
  215. map.put("warnMonthPlay", warnMonthPlay);
  216. Map<String, String> numPt2 = getNumPt(warnMonthPlay, warnMonthLastPlay);
  217. map.put("warnMonthPlayPt", numPt2.get("pt"));
  218. map.put("warnMonthPlayPtStatus", numPt2.get("ptStatus"));
  219. map.put("deptNameMapList", deptNameMapList);
  220. return AjaxResult.success(map);
  221. }
  222. public static List<Map<String, String>> convertObjectToList(Object obj) {
  223. // 1. 检查是否为 List 类型
  224. if (!(obj instanceof List<?>)) {
  225. throw new IllegalArgumentException("Object is not a List");
  226. }
  227. List<?> rawList = (List<?>) obj;
  228. // 2. 遍历检查内部元素是否为 Map 类型
  229. for (Object item : rawList) {
  230. if (!(item instanceof Map<?, ?>)) {
  231. throw new IllegalArgumentException("List contains non-Map elements");
  232. }
  233. Map<?, ?> map = (Map<?, ?>) item;
  234. // 3. 检查 Map 的键值类型(可选但推荐)
  235. for (Map.Entry<?, ?> entry : map.entrySet()) {
  236. if (!(entry.getKey() instanceof String) || !(entry.getValue() instanceof String)) {
  237. throw new IllegalArgumentException(
  238. "Map contains non-String key/value: " + entry.getKey() + "=" + entry.getValue()
  239. );
  240. }
  241. }
  242. }
  243. // 4. 安全强制转换(已验证类型)
  244. return (List<Map<String, String>>) obj;
  245. }
  246. //计算两个数的百分比和是上升还是下降
  247. public static Map<String, String> getNumPt(double a, double b) {
  248. DecimalFormat df = new DecimalFormat("##.##%");
  249. Map<String, String> map = new HashMap<>(2);
  250. if (a > 0 && b < 1) {
  251. map.put("ptStatus", ONE);
  252. map.put("pt", "100.00%");
  253. return map;
  254. } else if (a < 1 && b > 0) {
  255. map.put("ptStatus", TWO);
  256. map.put("pt", "100.00%");
  257. return map;
  258. } else if (a < 1 && b < 1) {
  259. map.put("ptStatus", ONE);
  260. map.put("pt", "0.00%");
  261. return map;
  262. } else if (a > 0 && b > 0) {
  263. if (a > b) {
  264. map.put("ptStatus", ONE);
  265. String format = df.format((a - b) / b);
  266. map.put("pt", format);
  267. }
  268. if (b > a) {
  269. map.put("ptStatus", TWO);
  270. String format = df.format((b - a) / b);
  271. map.put("pt", format);
  272. }
  273. }
  274. return map;
  275. }
  276. /**
  277. * 算法回调新增告警信息
  278. *
  279. * @param message
  280. */
  281. public void addWarnManage(MqttMessage message, String algorithmType) {
  282. String payload = message.getPayload();
  283. JSONObject jsonObject = JSONObject.parseObject(payload);
  284. String equipmentIp = jsonObject.getString("equipmentIp");
  285. Long channelId = jsonObject.getLong("channelId");
  286. String parameterSet = jsonObject.getString("parameterSet");
  287. String algorithmResult = jsonObject.getString("algorithmResult");
  288. String photoUrl = jsonObject.getString("photoUrl");
  289. String videoUrl = jsonObject.getString("videoUrl");
  290. WarnManage warnManage = new WarnManage();
  291. warnManage.setEquipmentIp(equipmentIp);
  292. warnManage.setChannelId(channelId);
  293. warnManage.setParameterSet(parameterSet);
  294. warnManage.setAlgorithmType(algorithmType);
  295. warnManage.setReportStatus(algorithmResult);
  296. warnManage.setVideoAddress(videoUrl);
  297. warnManage.setWarnImage(photoUrl);
  298. warnManage.setCreateTime(DateUtils.getNowDate());
  299. warnManage.setWarnTime(DateUtils.getNowDate());
  300. warnManageMapper.insertWarnManage(warnManage);
  301. }
  302. }