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 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 map = new HashMap<>(9); WarnManage warnManage = new WarnManage(); warnManage.setWarnTime(DateUtils.getNowDate()); //当月告警数据 List warnManages = warnManageMapper.selectWarnManageDeptListByTime(warnManage); int warnMonthAll = 0; int warnMonthLeave = 0; int warnMonthPlay = 0; int warnMonthLastAll = 0; int warnMonthLastLeave = 0; int warnMonthLastPlay = 0; //本月部门数据 List> deptNameMapList = new ArrayList<>(); //上月部门数据 List> deptNameLastMapList = new ArrayList<>(); //计算告警数据 if (warnManages != null && !warnManages.isEmpty()) { warnMonthAll = warnManages.size(); Map> 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> deptNameCollect = warnManages.stream().collect(Collectors.groupingBy(WarnManage::getDeptName)); List> finalDeptNameMapList = new ArrayList<>(); deptNameCollect.forEach((deptName, v) -> { Map deptMap = new HashMap<>(9); deptMap.put("deptName", deptName); deptMap.put("warnNum", String.valueOf(v.size())); finalDeptNameMapList.add(deptMap); }); deptNameMapList = finalDeptNameMapList; } //上月告警数据 Map warnLastMonth = redisCache.getCacheMap(WARN_LAST_MONTH + DateUtils.getMonth()); if (warnLastMonth.isEmpty()) { //从数据库查询 warnManage.setWarnTime(DateUtils.getLastMonth()); List warnManagesLast = warnManageMapper.selectWarnManageDeptListByTime(warnManage); if (warnManagesLast != null && !warnManagesLast.isEmpty()) { warnMonthLastAll = warnManagesLast.size(); Map> 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 lastMap = new HashMap<>(); lastMap.put("warnMonthLastAll", warnMonthLastAll); lastMap.put("warnMonthLastLeave", warnMonthLastLeave); lastMap.put("warnMonthLastPlay", warnMonthLastPlay); //插入上月部门数据 Map> deptNameCollect = warnManages.stream().collect(Collectors.groupingBy(WarnManage::getDeptName)); List> finalDeptNameLastMapList = new ArrayList<>(); deptNameCollect.forEach((deptName, v) -> { Map 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> finalDeptNameMapList = deptNameLastMapList; deptNameMapList.forEach(deptNameMap -> { String deptName = deptNameMap.get("deptName"); String warnNum = deptNameMap.get("warnNum"); String warnNumLast = "0"; if (!finalDeptNameMapList.isEmpty()) { for (Map lastMonthDeptMap : finalDeptNameMapList) { String deptNameLast = lastMonthDeptMap.get(deptName); if (StringUtils.isNotEmpty(deptNameLast)) { warnNumLast = lastMonthDeptMap.get("warnNum"); } } } //计算百分比 Map 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 numPt = getNumPt(warnMonthAll, warnMonthLastAll); map.put("warnMonthPt", numPt.get("pt")); map.put("warnMonthPtStatus", numPt.get("ptStatus")); map.put("warnMonthLeave", warnMonthLeave); Map numPt1 = getNumPt(warnMonthLeave, warnMonthLastLeave); map.put("warnMonthLeavePt", numPt1.get("pt")); map.put("warnMonthLeavePtStatus", numPt1.get("ptStatus")); map.put("warnMonthPlay", warnMonthPlay); Map 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> 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>) obj; } //计算两个数的百分比和是上升还是下降 public static Map getNumPt(double a, double b) { DecimalFormat df = new DecimalFormat("##.##%"); Map 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); } }