WarnManageServiceImpl.java 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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.EquipmentManage;
  9. import com.ruoyi.manage.domain.WarnManage;
  10. import com.ruoyi.manage.mapper.EquipmentManageMapper;
  11. import com.ruoyi.manage.mapper.WarnManageMapper;
  12. import com.ruoyi.manage.service.IWarnManageService;
  13. import lombok.extern.slf4j.Slf4j;
  14. import org.springframework.beans.factory.annotation.Autowired;
  15. import org.springframework.stereotype.Service;
  16. import java.text.DecimalFormat;
  17. import java.text.SimpleDateFormat;
  18. import java.util.*;
  19. import java.util.stream.Collectors;
  20. import static com.ruoyi.common.constant.Constants.*;
  21. /**
  22. * 告警管理Service业务层处理
  23. *
  24. * @author boman
  25. * @date 2025-06-19
  26. */
  27. @Slf4j
  28. @Service
  29. public class WarnManageServiceImpl implements IWarnManageService {
  30. @Autowired
  31. private WarnManageMapper warnManageMapper;
  32. @Autowired
  33. private EquipmentManageMapper equipmentManageMapper;
  34. @Autowired
  35. private RedisCache redisCache;
  36. /**
  37. * 查询告警管理
  38. *
  39. * @param warnId 告警管理主键
  40. * @return 告警管理
  41. */
  42. @Override
  43. public WarnManage selectWarnManageByWarnId(Long warnId) {
  44. return warnManageMapper.selectWarnManageByWarnId(warnId);
  45. }
  46. /**
  47. * 查询告警管理列表
  48. *
  49. * @param warnManage 告警管理
  50. * @return 告警管理
  51. */
  52. @Override
  53. public List<WarnManage> selectWarnManageList(WarnManage warnManage) {
  54. return warnManageMapper.selectWarnManageList(warnManage);
  55. }
  56. /**
  57. * 新增告警管理
  58. *
  59. * @param warnManage 告警管理
  60. * @return 结果
  61. */
  62. @Override
  63. public int insertWarnManage(WarnManage warnManage) {
  64. warnManage.setCreateTime(DateUtils.getNowDate());
  65. return warnManageMapper.insertWarnManage(warnManage);
  66. }
  67. /**
  68. * 修改告警管理
  69. *
  70. * @param warnManage 告警管理
  71. * @return 结果
  72. */
  73. @Override
  74. public int updateWarnManage(WarnManage warnManage) {
  75. warnManage.setUpdateTime(DateUtils.getNowDate());
  76. return warnManageMapper.updateWarnManage(warnManage);
  77. }
  78. /**
  79. * 批量删除告警管理
  80. *
  81. * @param warnIds 需要删除的告警管理主键
  82. * @return 结果
  83. */
  84. @Override
  85. public int deleteWarnManageByWarnIds(Long[] warnIds) {
  86. return warnManageMapper.deleteWarnManageByWarnIds(warnIds);
  87. }
  88. /**
  89. * 删除告警管理信息
  90. *
  91. * @param warnId 告警管理主键
  92. * @return 结果
  93. */
  94. @Override
  95. public int deleteWarnManageByWarnId(Long warnId) {
  96. return warnManageMapper.deleteWarnManageByWarnId(warnId);
  97. }
  98. /**
  99. * 告警内容+部门统计
  100. *
  101. * @return
  102. */
  103. @Override
  104. public AjaxResult getWarnManageIndex() {
  105. //定义返回值
  106. Map<String, Object> map = new HashMap<>(9);
  107. WarnManage warnManage = new WarnManage();
  108. warnManage.setWarnTime(DateUtils.getNowDate());
  109. //当月告警数据
  110. List<WarnManage> warnManages = warnManageMapper.selectWarnManageDeptListByTime(warnManage);
  111. int warnMonthAll = 0;
  112. int warnMonthLeave = 0;
  113. int warnMonthPlay = 0;
  114. int warnMonthLastAll = 0;
  115. int warnMonthLastLeave = 0;
  116. int warnMonthLastPlay = 0;
  117. //本月部门数据
  118. List<Map<String, Object>> deptNameMapList = new ArrayList<>();
  119. //上月部门数据
  120. List<Map<String, Object>> deptNameLastMapList = new ArrayList<>();
  121. //计算告警数据
  122. if (warnManages != null && !warnManages.isEmpty()) {
  123. warnMonthAll = warnManages.size();
  124. Map<String, List<WarnManage>> collect = warnManages.stream().collect(Collectors.groupingBy(WarnManage::getAlgorithmType));
  125. if (!collect.isEmpty()) {
  126. //离岗
  127. if (collect.get(ZERO) != null && !collect.get(ZERO).isEmpty()) {
  128. warnMonthLeave = collect.get(ZERO).size();
  129. }
  130. //玩手机
  131. if (collect.get(ONE) != null && !collect.get(ONE).isEmpty()) {
  132. warnMonthPlay = collect.get(ONE).size();
  133. }
  134. }
  135. //插入当月部门数据
  136. Map<Long, List<WarnManage>> deptNameCollect = warnManages.stream().filter(e -> e.getDeptId() != null).collect(Collectors.groupingBy(WarnManage::getDeptId));
  137. List<Map<String, Object>> finalDeptNameMapList = new ArrayList<>();
  138. deptNameCollect.forEach((deptId, v) -> {
  139. Map<String, Object> deptMap = new HashMap<>(3);
  140. deptMap.put("deptName", v.get(0).getDeptName());
  141. deptMap.put("warnNum", String.valueOf(v.size()));
  142. deptMap.put("deptId", deptId.toString());
  143. finalDeptNameMapList.add(deptMap);
  144. });
  145. deptNameMapList = finalDeptNameMapList;
  146. }
  147. //上月告警数据
  148. Map<String, Object> warnLastMonth = redisCache.getCacheMap(WARN_LAST_MONTH + DateUtils.getMonth());
  149. if (warnLastMonth.isEmpty()) {
  150. Map<String, Object> lastMap = new HashMap<>();
  151. //从数据库查询
  152. warnManage.setWarnTime(DateUtils.getLastMonth());
  153. List<WarnManage> warnManagesLast = warnManageMapper.selectWarnManageDeptListByTime(warnManage);
  154. if (warnManagesLast != null && !warnManagesLast.isEmpty()) {
  155. warnMonthLastAll = warnManagesLast.size();
  156. Map<String, List<WarnManage>> collect = warnManagesLast.stream().collect(Collectors.groupingBy(WarnManage::getAlgorithmType));
  157. if (!collect.isEmpty()) {
  158. //离岗
  159. if (collect.get(ZERO) != null && !collect.get(ZERO).isEmpty()) {
  160. warnMonthLastLeave = collect.get(ZERO).size();
  161. }
  162. //玩手机
  163. if (collect.get(ONE) != null && !collect.get(ONE).isEmpty()) {
  164. warnMonthLastPlay = collect.get(ONE).size();
  165. }
  166. }
  167. //插入上月部门数据
  168. Map<Long, List<WarnManage>> deptNameCollect = warnManagesLast.stream().filter(e -> e.getDeptId() != null).collect(Collectors.groupingBy(WarnManage::getDeptId));
  169. List<Map<String, Object>> finalDeptNameLastMapList = new ArrayList<>();
  170. deptNameCollect.forEach((deptId, v) -> {
  171. Map<String, Object> deptMap = new HashMap<>(3);
  172. deptMap.put("deptName", v.get(0).getDeptName());
  173. deptMap.put("warnNum", String.valueOf(v.size()));
  174. deptMap.put("deptId", deptId.toString());
  175. finalDeptNameLastMapList.add(deptMap);
  176. });
  177. deptNameLastMapList = finalDeptNameLastMapList;
  178. }
  179. lastMap.put("deptNameCollect", deptNameLastMapList);
  180. lastMap.put("warnMonthLastAll", warnMonthLastAll);
  181. lastMap.put("warnMonthLastLeave", warnMonthLastLeave);
  182. lastMap.put("warnMonthLastPlay", warnMonthLastPlay);
  183. redisCache.setCacheMap(WARN_LAST_MONTH + DateUtils.getMonth(), lastMap);
  184. } else {
  185. warnMonthLastAll = Integer.parseInt(warnLastMonth.get("warnMonthLastAll").toString());
  186. warnMonthLastLeave = Integer.parseInt(warnLastMonth.get("warnMonthLastLeave").toString());
  187. warnMonthLastPlay = Integer.parseInt(warnLastMonth.get("warnMonthLastPlay").toString());
  188. deptNameLastMapList = convertObjectToList(warnLastMonth.get("deptNameCollect"));
  189. }
  190. //进行本月部门数据和上月部门数据对比,已本月部门数据为主
  191. if (!deptNameMapList.isEmpty()) {
  192. List<Map<String, Object>> finalDeptNameMapList = deptNameLastMapList;
  193. deptNameMapList.forEach(deptNameMap -> {
  194. String deptId = deptNameMap.get("deptId").toString();
  195. String warnNum = deptNameMap.get("warnNum").toString();
  196. String warnNumLast = "0";
  197. if (!finalDeptNameMapList.isEmpty()) {
  198. for (Map<String, Object> lastMonthDeptMap : finalDeptNameMapList) {
  199. String deptIdLast = lastMonthDeptMap.get(("deptId")).toString();
  200. if (deptId.equals(deptIdLast)) {
  201. warnNumLast = lastMonthDeptMap.get("warnNum").toString();
  202. }
  203. }
  204. }
  205. //计算百分比
  206. Map<String, String> numPt = getNumPt(Double.parseDouble(warnNum), Double.parseDouble(warnNumLast));
  207. String warnDeptPt = numPt.get("pt");
  208. String warnDeptPtStatus = numPt.get("ptStatus");
  209. deptNameMap.put("warnDeptPt", warnDeptPt);
  210. deptNameMap.put("warnDeptPtStatus", warnDeptPtStatus);
  211. });
  212. }
  213. map.put("warnMonthAll", warnMonthAll);
  214. Map<String, String> numPt = getNumPt(warnMonthAll, warnMonthLastAll);
  215. map.put("warnMonthPt", numPt.get("pt"));
  216. map.put("warnMonthPtStatus", numPt.get("ptStatus"));
  217. map.put("warnMonthLeave", warnMonthLeave);
  218. Map<String, String> numPt1 = getNumPt(warnMonthLeave, warnMonthLastLeave);
  219. map.put("warnMonthLeavePt", numPt1.get("pt"));
  220. map.put("warnMonthLeavePtStatus", numPt1.get("ptStatus"));
  221. map.put("warnMonthPlay", warnMonthPlay);
  222. Map<String, String> numPt2 = getNumPt(warnMonthPlay, warnMonthLastPlay);
  223. map.put("warnMonthPlayPt", numPt2.get("pt"));
  224. map.put("warnMonthPlayPtStatus", numPt2.get("ptStatus"));
  225. map.put("deptNameMapList", deptNameMapList);
  226. return AjaxResult.success(map);
  227. }
  228. /**
  229. * 年度告警统计
  230. * 接收年份查询
  231. */
  232. @Override
  233. public AjaxResult warnManageIndexYear(String year) {
  234. Map<String, Object> map = new HashMap<>();
  235. //当前年份数据从数据库查询
  236. if (StringUtils.isNotEmpty(year) && DateUtils.getYear().equals(year)) {
  237. return warnManageIndexYearData(year);
  238. } else {
  239. map = redisCache.getCacheMap(WARN_MANAGE_LAST_YEAR + year);
  240. if (map == null) {
  241. return warnManageIndexYearData(year);
  242. }
  243. }
  244. return AjaxResult.success(map);
  245. }
  246. /**
  247. * 从数据库查询
  248. *
  249. * @param year
  250. * @return
  251. */
  252. public AjaxResult warnManageIndexYearData(String year) {
  253. //定义返回值
  254. Map<String, Object> map = new HashMap<>(3);
  255. List<String> x = new ArrayList<>(Arrays.asList("01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"));
  256. List<Integer> y1 = new ArrayList<>();
  257. List<Integer> y2 = new ArrayList<>();
  258. WarnManage warnManage = new WarnManage();
  259. Map<String, Object> params = new HashMap<>();
  260. params.put("year", year);
  261. warnManage.setParams(params);
  262. //按年份检索的数据
  263. List<WarnManage> warnManages = warnManageMapper.selectWarnManageList(warnManage);
  264. if (warnManages != null && !warnManages.isEmpty()) {
  265. // 使用Collectors.groupingBy按月份分组
  266. Map<String, List<WarnManage>> WarnManageCollectMonth = warnManages.stream().collect(Collectors.groupingBy(item ->
  267. new SimpleDateFormat("MM").format(item.getWarnTime())));
  268. for (String month : x) {
  269. List<WarnManage> warnManagesMonthList = WarnManageCollectMonth.get(month);
  270. //如果当月没有告警
  271. if (warnManagesMonthList != null && !warnManagesMonthList.isEmpty()) {
  272. Map<String, List<WarnManage>> algorithmTypeCollect = warnManagesMonthList.stream().collect(Collectors.groupingBy(WarnManage::getAlgorithmType));
  273. if (algorithmTypeCollect != null && !algorithmTypeCollect.isEmpty()) {
  274. //离岗
  275. if (algorithmTypeCollect.get(ZERO) != null && !algorithmTypeCollect.get(ZERO).isEmpty()) {
  276. y1.add(algorithmTypeCollect.get(ZERO).size());
  277. } else {
  278. y1.add(0);
  279. }
  280. //玩手机
  281. if (algorithmTypeCollect.get(ONE) != null && !algorithmTypeCollect.get(ONE).isEmpty()) {
  282. y2.add(algorithmTypeCollect.get(ONE).size());
  283. } else {
  284. y2.add(0);
  285. }
  286. }
  287. } else {
  288. y1.add(0);
  289. y2.add(0);
  290. }
  291. }
  292. }
  293. map.put("x", x);
  294. map.put("y1", y1);
  295. map.put("y2", y2);
  296. //把非当前年份的数据存储到redis
  297. if (StringUtils.isNotEmpty(year) && !DateUtils.getYear().equals(year)) {
  298. redisCache.setCacheMap(WARN_MANAGE_LAST_YEAR + year, map);
  299. }
  300. return AjaxResult.success(map);
  301. }
  302. public static List<Map<String, Object>> convertObjectToList(Object obj) {
  303. // 1. 检查是否为 List 类型
  304. if (!(obj instanceof List<?>)) {
  305. throw new IllegalArgumentException("Object is not a List");
  306. }
  307. List<?> rawList = (List<?>) obj;
  308. // 2. 遍历检查内部元素是否为 Map 类型
  309. for (Object item : rawList) {
  310. if (!(item instanceof Map<?, ?>)) {
  311. throw new IllegalArgumentException("List contains non-Map elements");
  312. }
  313. Map<?, ?> map = (Map<?, ?>) item;
  314. // 3. 检查 Map 的键值类型(可选但推荐)
  315. for (Map.Entry<?, ?> entry : map.entrySet()) {
  316. if (!(entry.getKey() instanceof String) || !(entry.getValue() instanceof String)) {
  317. throw new IllegalArgumentException(
  318. "Map contains non-String key/value: " + entry.getKey() + "=" + entry.getValue()
  319. );
  320. }
  321. }
  322. }
  323. // 4. 安全强制转换(已验证类型)
  324. return (List<Map<String, Object>>) obj;
  325. }
  326. //计算两个数的百分比和是上升还是下降
  327. public static Map<String, String> getNumPt(double a, double b) {
  328. DecimalFormat df = new DecimalFormat("##.##%");
  329. Map<String, String> map = new HashMap<>(2);
  330. if (a > 0 && b < 1) {
  331. map.put("ptStatus", ONE);
  332. map.put("pt", "100.00%");
  333. return map;
  334. } else if (a < 1 && b > 0) {
  335. map.put("ptStatus", TWO);
  336. map.put("pt", "100.00%");
  337. return map;
  338. } else if (a < 1 && b < 1) {
  339. map.put("ptStatus", ONE);
  340. map.put("pt", "0.00%");
  341. return map;
  342. } else if (a > 0 && b > 0) {
  343. if (a > b) {
  344. map.put("ptStatus", ONE);
  345. String format = df.format((a - b) / b);
  346. map.put("pt", format);
  347. }
  348. if (b > a) {
  349. map.put("ptStatus", TWO);
  350. String format = df.format((b - a) / b);
  351. map.put("pt", format);
  352. }
  353. }
  354. return map;
  355. }
  356. /**
  357. * 算法回调新增告警信息
  358. *
  359. * @param message
  360. */
  361. public void addWarnManage(MqttMessage message, String algorithmType) {
  362. try {
  363. String payload = message.getPayload();
  364. JSONObject jsonObject = JSONObject.parseObject(payload);
  365. String equipmentIp = jsonObject.getString("equipmentIp");
  366. EquipmentManage equipmentManage = new EquipmentManage();
  367. equipmentManage.setEquipmentIp(equipmentIp);
  368. EquipmentManage equipmentManageOld = equipmentManageMapper.selectEquipmentManageByEquipmentIp(equipmentManage);
  369. Long channelId = jsonObject.getLong("channelId");
  370. String parameterSet = jsonObject.getString("parameterSet");
  371. String algorithmResult = jsonObject.getString("algorithmResult");
  372. String photoUrl = "http://114.99.51.58:15010/prod-api/profile" + jsonObject.getString("photoUrl");
  373. String videoUrl = jsonObject.getString("videoUrl");
  374. String rangName = jsonObject.getString("name");
  375. WarnManage warnManage = new WarnManage();
  376. warnManage.setEquipmentIp(equipmentIp);
  377. warnManage.setEquipmentName(equipmentManageOld.getEquipmentName());
  378. warnManage.setEquipmentId(equipmentManageOld.getEquipmentId());
  379. warnManage.setChannelId(channelId);
  380. warnManage.setParameterSet(parameterSet);
  381. warnManage.setAlgorithmType(algorithmType);
  382. warnManage.setReportStatus(algorithmResult);
  383. warnManage.setVideoAddress(videoUrl);
  384. warnManage.setWarnImage(photoUrl);
  385. warnManage.setCreateTime(DateUtils.getNowDate());
  386. warnManage.setWarnTime(DateUtils.getNowDate());
  387. warnManage.setRangName(rangName);
  388. warnManageMapper.insertWarnManage(warnManage);
  389. } catch (Exception e) {
  390. log.info("新增告警信息异常:{}", e.getMessage());
  391. }
  392. }
  393. }