123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323 |
- package com.ruoyi.manage.service.impl;
- import com.alibaba.fastjson2.JSONObject;
- import com.ruoyi.common.core.domain.AjaxResult;
- import com.ruoyi.common.core.redis.RedisCache;
- import com.ruoyi.common.model.MqttMessage;
- import com.ruoyi.common.utils.DateUtils;
- import com.ruoyi.common.utils.StringUtils;
- import com.ruoyi.manage.domain.WarnManage;
- import com.ruoyi.manage.mapper.WarnManageMapper;
- import com.ruoyi.manage.service.IWarnManageService;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
- import java.text.DecimalFormat;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import java.util.stream.Collectors;
- import static com.ruoyi.common.constant.Constants.*;
- /**
- * 告警管理Service业务层处理
- *
- * @author boman
- * @date 2025-06-19
- */
- @Service
- public class WarnManageServiceImpl implements IWarnManageService {
- @Autowired
- private WarnManageMapper warnManageMapper;
- @Autowired
- private RedisCache redisCache;
- /**
- * 查询告警管理
- *
- * @param warnId 告警管理主键
- * @return 告警管理
- */
- @Override
- public WarnManage selectWarnManageByWarnId(Long warnId) {
- return warnManageMapper.selectWarnManageByWarnId(warnId);
- }
- /**
- * 查询告警管理列表
- *
- * @param warnManage 告警管理
- * @return 告警管理
- */
- @Override
- public List<WarnManage> selectWarnManageList(WarnManage warnManage) {
- return warnManageMapper.selectWarnManageList(warnManage);
- }
- /**
- * 新增告警管理
- *
- * @param warnManage 告警管理
- * @return 结果
- */
- @Override
- public int insertWarnManage(WarnManage warnManage) {
- warnManage.setCreateTime(DateUtils.getNowDate());
- return warnManageMapper.insertWarnManage(warnManage);
- }
- /**
- * 修改告警管理
- *
- * @param warnManage 告警管理
- * @return 结果
- */
- @Override
- public int updateWarnManage(WarnManage warnManage) {
- warnManage.setUpdateTime(DateUtils.getNowDate());
- return warnManageMapper.updateWarnManage(warnManage);
- }
- /**
- * 批量删除告警管理
- *
- * @param warnIds 需要删除的告警管理主键
- * @return 结果
- */
- @Override
- public int deleteWarnManageByWarnIds(Long[] warnIds) {
- return warnManageMapper.deleteWarnManageByWarnIds(warnIds);
- }
- /**
- * 删除告警管理信息
- *
- * @param warnId 告警管理主键
- * @return 结果
- */
- @Override
- public int deleteWarnManageByWarnId(Long warnId) {
- return warnManageMapper.deleteWarnManageByWarnId(warnId);
- }
- /**
- * 告警内容+部门统计
- *
- * @return
- */
- @Override
- public AjaxResult getWarnManageIndex() {
- //定义返回值
- Map<String, Object> map = new HashMap<>(9);
- WarnManage warnManage = new WarnManage();
- warnManage.setWarnTime(DateUtils.getNowDate());
- //当月告警数据
- List<WarnManage> warnManages = warnManageMapper.selectWarnManageDeptListByTime(warnManage);
- int warnMonthAll = 0;
- int warnMonthLeave = 0;
- int warnMonthPlay = 0;
- int warnMonthLastAll = 0;
- int warnMonthLastLeave = 0;
- int warnMonthLastPlay = 0;
- //本月部门数据
- List<Map<String, String>> deptNameMapList = new ArrayList<>();
- //上月部门数据
- List<Map<String, String>> deptNameLastMapList = new ArrayList<>();
- //计算告警数据
- if (warnManages != null && !warnManages.isEmpty()) {
- warnMonthAll = warnManages.size();
- Map<String, List<WarnManage>> collect = warnManages.stream().collect(Collectors.groupingBy(WarnManage::getAlgorithmType));
- if (!collect.isEmpty()) {
- //离岗
- if (!collect.get(ZERO).isEmpty()) {
- warnMonthLastLeave = collect.get(ZERO).size();
- }
- //玩手机
- if (!collect.get(ONE).isEmpty()) {
- warnMonthLastPlay = collect.get(ONE).size();
- }
- }
- //插入当月部门数据
- Map<String, List<WarnManage>> deptNameCollect = warnManages.stream().collect(Collectors.groupingBy(WarnManage::getDeptName));
- List<Map<String, String>> finalDeptNameMapList = new ArrayList<>();
- deptNameCollect.forEach((deptName, v) -> {
- Map<String, String> deptMap = new HashMap<>(9);
- deptMap.put("deptName", deptName);
- deptMap.put("warnNum", String.valueOf(v.size()));
- finalDeptNameMapList.add(deptMap);
- });
- deptNameMapList = finalDeptNameMapList;
- }
- //上月告警数据
- Map<String, Object> warnLastMonth = redisCache.getCacheMap(WARN_LAST_MONTH + DateUtils.getMonth());
- if (warnLastMonth.isEmpty()) {
- //从数据库查询
- warnManage.setWarnTime(DateUtils.getLastMonth());
- List<WarnManage> warnManagesLast = warnManageMapper.selectWarnManageDeptListByTime(warnManage);
- if (warnManagesLast != null && !warnManagesLast.isEmpty()) {
- warnMonthLastAll = warnManagesLast.size();
- Map<String, List<WarnManage>> collect = warnManagesLast.stream().collect(Collectors.groupingBy(WarnManage::getAlgorithmType));
- if (!collect.isEmpty()) {
- //离岗
- if (!collect.get(ZERO).isEmpty()) {
- warnMonthLeave = collect.get(ZERO).size();
- }
- //玩手机
- if (!collect.get(ONE).isEmpty()) {
- warnMonthLastPlay = collect.get(ONE).size();
- }
- }
- }
- Map<String, Object> lastMap = new HashMap<>();
- lastMap.put("warnMonthLastAll", warnMonthLastAll);
- lastMap.put("warnMonthLastLeave", warnMonthLastLeave);
- lastMap.put("warnMonthLastPlay", warnMonthLastPlay);
- //插入上月部门数据
- Map<String, List<WarnManage>> deptNameCollect = warnManages.stream().collect(Collectors.groupingBy(WarnManage::getDeptName));
- List<Map<String, String>> finalDeptNameLastMapList = new ArrayList<>();
- deptNameCollect.forEach((deptName, v) -> {
- Map<String, String> deptMap = new HashMap<>(9);
- deptMap.put("deptName", deptName);
- deptMap.put("warnNum", String.valueOf(v.size()));
- finalDeptNameLastMapList.add(deptMap);
- });
- lastMap.put("deptNameCollect", finalDeptNameLastMapList);
- deptNameLastMapList = finalDeptNameLastMapList;
- redisCache.setCacheMap(WARN_LAST_MONTH + DateUtils.getMonth(), lastMap);
- } else {
- warnMonthLastAll = Integer.parseInt(warnLastMonth.get("warnMonthLastAll").toString());
- warnMonthLastLeave = Integer.parseInt(warnLastMonth.get("warnMonthLastLeave").toString());
- warnMonthLastPlay = Integer.parseInt(warnLastMonth.get("warnMonthLastPlay").toString());
- deptNameLastMapList = convertObjectToList(warnLastMonth.get("deptNameCollect"));
- }
- //进行本月部门数据和上月部门数据对比,已本月部门数据为主
- if (!deptNameMapList.isEmpty()) {
- List<Map<String, String>> finalDeptNameMapList = deptNameLastMapList;
- deptNameMapList.forEach(deptNameMap -> {
- String deptName = deptNameMap.get("deptName");
- String warnNum = deptNameMap.get("warnNum");
- String warnNumLast = "0";
- if (!finalDeptNameMapList.isEmpty()) {
- for (Map<String, String> lastMonthDeptMap : finalDeptNameMapList) {
- String deptNameLast = lastMonthDeptMap.get(deptName);
- if (StringUtils.isNotEmpty(deptNameLast)) {
- warnNumLast = lastMonthDeptMap.get("warnNum");
- }
- }
- }
- //计算百分比
- Map<String, String> numPt = getNumPt(Double.parseDouble(warnNum), Double.parseDouble(warnNumLast));
- String warnDeptPt = numPt.get("pt");
- String warnDeptPtStatus = numPt.get("ptStatus");
- deptNameMap.put("warnDeptPt", warnDeptPt);
- deptNameMap.put("warnDeptPtStatus", warnDeptPtStatus);
- });
- }
- map.put("warnMonthAll", warnMonthAll);
- Map<String, String> numPt = getNumPt(warnMonthAll, warnMonthLastAll);
- map.put("warnMonthPt", numPt.get("pt"));
- map.put("warnMonthPtStatus", numPt.get("ptStatus"));
- map.put("warnMonthLeave", warnMonthLeave);
- Map<String, String> numPt1 = getNumPt(warnMonthLeave, warnMonthLastLeave);
- map.put("warnMonthLeavePt", numPt1.get("pt"));
- map.put("warnMonthLeavePtStatus", numPt1.get("ptStatus"));
- map.put("warnMonthPlay", warnMonthPlay);
- Map<String, String> numPt2 = getNumPt(warnMonthPlay, warnMonthLastPlay);
- map.put("warnMonthPlayPt", numPt2.get("pt"));
- map.put("warnMonthPlayPtStatus", numPt2.get("ptStatus"));
- map.put("deptNameMapList", deptNameMapList);
- return AjaxResult.success(map);
- }
- public static List<Map<String, String>> convertObjectToList(Object obj) {
- // 1. 检查是否为 List 类型
- if (!(obj instanceof List<?>)) {
- throw new IllegalArgumentException("Object is not a List");
- }
- List<?> rawList = (List<?>) obj;
- // 2. 遍历检查内部元素是否为 Map 类型
- for (Object item : rawList) {
- if (!(item instanceof Map<?, ?>)) {
- throw new IllegalArgumentException("List contains non-Map elements");
- }
- Map<?, ?> map = (Map<?, ?>) item;
- // 3. 检查 Map 的键值类型(可选但推荐)
- for (Map.Entry<?, ?> entry : map.entrySet()) {
- if (!(entry.getKey() instanceof String) || !(entry.getValue() instanceof String)) {
- throw new IllegalArgumentException(
- "Map contains non-String key/value: " + entry.getKey() + "=" + entry.getValue()
- );
- }
- }
- }
- // 4. 安全强制转换(已验证类型)
- return (List<Map<String, String>>) obj;
- }
- //计算两个数的百分比和是上升还是下降
- public static Map<String, String> getNumPt(double a, double b) {
- DecimalFormat df = new DecimalFormat("##.##%");
- Map<String, String> map = new HashMap<>(2);
- if (a > 0 && b < 1) {
- map.put("ptStatus", ONE);
- map.put("pt", "100.00%");
- return map;
- } else if (a < 1 && b > 0) {
- map.put("ptStatus", TWO);
- map.put("pt", "100.00%");
- return map;
- } else if (a < 1 && b < 1) {
- map.put("ptStatus", ONE);
- map.put("pt", "0.00%");
- return map;
- } else if (a > 0 && b > 0) {
- if (a > b) {
- map.put("ptStatus", ONE);
- String format = df.format((a - b) / b);
- map.put("pt", format);
- }
- if (b > a) {
- map.put("ptStatus", TWO);
- String format = df.format((b - a) / b);
- map.put("pt", format);
- }
- }
- return map;
- }
- /**
- * 算法回调新增告警信息
- *
- * @param message
- */
- public void addWarnManage(MqttMessage message, String algorithmType) {
- String payload = message.getPayload();
- JSONObject jsonObject = JSONObject.parseObject(payload);
- String equipmentIp = jsonObject.getString("equipmentIp");
- Long channelId = jsonObject.getLong("channelId");
- String parameterSet = jsonObject.getString("parameterSet");
- String algorithmResult = jsonObject.getString("algorithmResult");
- String photoUrl = jsonObject.getString("photoUrl");
- String videoUrl = jsonObject.getString("videoUrl");
- WarnManage warnManage = new WarnManage();
- warnManage.setEquipmentIp(equipmentIp);
- warnManage.setChannelId(channelId);
- warnManage.setParameterSet(parameterSet);
- warnManage.setAlgorithmType(algorithmType);
- warnManage.setReportStatus(algorithmResult);
- warnManage.setVideoAddress(videoUrl);
- warnManage.setWarnImage(photoUrl);
- warnManage.setCreateTime(DateUtils.getNowDate());
- warnManage.setWarnTime(DateUtils.getNowDate());
- warnManageMapper.insertWarnManage(warnManage);
- }
- }
|