Эх сурвалжийг харах

告警规则点击关联设备报错处理

jingyuanchao 1 жил өмнө
parent
commit
f0cbae970c

+ 37 - 14
soc-modules/soc-modules-iot/src/main/java/com/xunmei/iot/service/impl/IotAlarmRuleSourceServiceImpl.java

@@ -5,11 +5,14 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.xunmei.common.core.constant.CacheConstants;
 import com.xunmei.common.core.domain.iot.domain.IotSensor;
 import com.xunmei.common.core.domain.mediator.domain.IotAlarmRule;
 import com.xunmei.common.core.domain.mediator.domain.IotAlarmRuleSource;
+import com.xunmei.common.core.enums.OrgTypeEnum;
 import com.xunmei.common.core.utils.IDHelper;
 import com.xunmei.common.core.utils.StringUtils;
+import com.xunmei.common.redis.utils.RedisUtils;
 import com.xunmei.iot.dto.alarm.IotAlarmRuleSourceDetailDto;
 import com.xunmei.iot.dto.alarm.IotAlarmRuleSourceDeviceBatchJoin;
 import com.xunmei.iot.dto.alarm.IotAlarmRuleSourceDeviceDto;
@@ -22,14 +25,12 @@ import com.xunmei.iot.mapper.IotAlarmRuleSourceMapper;
 import com.xunmei.iot.mapper.IotCommonSensorMapper;
 import com.xunmei.iot.service.IotAlarmRuleSourceService;
 import com.xunmei.iot.vo.alarm.*;
+import com.xunmei.system.api.vo.SysOrgVO;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
 import java.util.function.Function;
 import java.util.stream.Collectors;
 
@@ -60,8 +61,9 @@ public class IotAlarmRuleSourceServiceImpl extends ServiceImpl<IotAlarmRuleSourc
         //2.获取告警规则对应有哪些设备的告警属性规则。
         List<ProductTypeDataVo> productTypeDataVos = iotAlarmRuleSourceMapper.selectRuleDevice(detailVo.getRuleId());
         //3.处理其他sql获取不了的数据
+        final List<SysOrgVO> cacheList = RedisUtils.getCacheList(CacheConstants.ORG_CACHE_LIST_KEY);
         for (ProductTypeDataVo pdv : productTypeDataVos) {
-            dealDevice(pdv);
+            dealDevice(pdv, cacheList);
         }
         iotAlarmRuleSourceDetailVo.setTypeDataList(productTypeDataVos);
         return iotAlarmRuleSourceDetailVo;
@@ -73,9 +75,31 @@ public class IotAlarmRuleSourceServiceImpl extends ServiceImpl<IotAlarmRuleSourc
         page.setSize(param.getSize());
         page.setCurrent(param.getPage());
         Page<IotAlarmRuleSourceDeviceVo> result = iotAlarmRuleSourceMapper.selectDeviceList(page, param);
+        final List<IotAlarmRuleSourceDeviceVo> records = result.getRecords();
+        if (ObjectUtil.isEmpty(records)) {
+            return result;
+        }
+        final List<SysOrgVO> cacheList = RedisUtils.getCacheList(CacheConstants.ORG_CACHE_LIST_KEY);
+
+        for (IotAlarmRuleSourceDeviceVo record : records) {
+            final String orgName = dealOrgName(cacheList, record.getOrgPath(), record.getOrgName());
+            record.setOrgName(orgName);
+        }
         return result;
     }
 
+
+    private String dealOrgName(List<SysOrgVO> cacheList, String orgPath, String orgName) {
+        List<String> orgNameList = new ArrayList<>();
+        final List<Long> collect = Arrays.stream(orgPath.split("-")).map(Long::parseLong).collect(Collectors.toList());
+        //地区名称
+        cacheList.stream().filter(org -> collect.contains(org.getId()) && OrgTypeEnum.DIQU_HANG_SHE.getCode().equals(org.getType())).findFirst().ifPresent(org -> orgNameList.add(org.getShortName()));
+        //行社名称
+        cacheList.stream().filter(org -> collect.contains(org.getId()) && OrgTypeEnum.HANG_SHE.getCode().equals(org.getType())).findFirst().ifPresent(org -> orgNameList.add(org.getShortName()));
+        orgNameList.add(orgName);
+        return String.join("-", orgNameList);
+    }
+
     @Override
     @Transactional(rollbackFor = Exception.class)
     public void updateRuleAndProductDevice(IotAlarmRuleSourceDetailVo detailDto) throws Exception {
@@ -170,7 +194,7 @@ public class IotAlarmRuleSourceServiceImpl extends ServiceImpl<IotAlarmRuleSourc
      * @param pdv
      * @return
      */
-    private void dealDevice(ProductTypeDataVo pdv) {
+    private void dealDevice(ProductTypeDataVo pdv, List<SysOrgVO> cacheList) {
         String valueType = pdv.getValueType();
 
         if (StringUtils.isEmpty(valueType)) {
@@ -190,15 +214,14 @@ public class IotAlarmRuleSourceServiceImpl extends ServiceImpl<IotAlarmRuleSourc
                 List<String> deviceIds = deviceTempList.stream().map(ProductDeviceTempVo::getDeviceId).collect(Collectors.toList());
 
                 List<IotSensor> iotSensors = iotCommonSensorMapper.selectByIds(deviceIds);
-                final Map<String, List<IotSensor>> deviceIdAndName = iotSensors.stream().collect(Collectors.groupingBy(IotSensor::getDeviceCode));
+                // CONCAT(org_id,'-',device_code) as device_code
+                final Map<String, IotSensor> deviceIdAndName = iotSensors.stream().collect(Collectors.toMap(IotSensor::getDeviceCode, Function.identity()));
                 for (ProductDeviceTempVo ptv : deviceTempList) {
-                    final List<IotSensor> sensorList = deviceIdAndName.get(ptv.getDeviceId());
-                    if (ObjectUtil.isNull(sensorList)) {
-                        continue;
-                    }
-                    for (IotSensor iotSensor : sensorList) {
-                        ptv.setDeviceName(iotSensor.getDeviceName());
-                        ptv.setOrgName(iotSensor.getOrgName());
+                    final IotSensor sensor = deviceIdAndName.get(ptv.getStrDeviceId());
+                    if (sensor != null) {
+                        ptv.setDeviceName(sensor.getDeviceName());
+                        final String orgName = dealOrgName(cacheList, sensor.getOrgPath(), sensor.getOrgName());
+                        ptv.setOrgName(orgName);
                     }
                 }
             }

+ 3 - 0
soc-modules/soc-modules-iot/src/main/java/com/xunmei/iot/vo/alarm/ProductDeviceTempVo.java

@@ -2,6 +2,7 @@ package com.xunmei.iot.vo.alarm;
 
 import com.fasterxml.jackson.databind.annotation.JsonSerialize;
 import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
+import io.swagger.annotations.ApiModelProperty;
 import lombok.Data;
 
 /**
@@ -15,6 +16,8 @@ public class ProductDeviceTempVo {
      * 设备id
      */
     private String deviceId;
+    @ApiModelProperty(value = "设备唯一id",notes = "临时拼接,由机构id+传感器code")
+    private String strDeviceId;
     /**
      * 设备名称
      */

+ 3 - 1
soc-modules/soc-modules-iot/src/main/resources/mapper/IotAlarmRuleSourceMapper.xml

@@ -20,6 +20,7 @@
         <result property="typeText" column="type_text"/>
         <collection property="deviceTempList" ofType="com.xunmei.iot.vo.alarm.ProductDeviceTempVo">
             <result property="deviceId" column="device_id"/>
+            <result property="strDeviceId" column="strDeviceId"/>
             <result property="orgId" column="org_id"/>
             <result property="orgName" column="org_name"/>
             <result property="orgPath" column="org_path"/>
@@ -33,6 +34,7 @@
             d.source_type_des as type_text,
             b.value_type,
             b.`value` AS device_id,
+            concat(b.org_id,'-',b.value) as strDeviceId,
             b.org_id,
             c.`name` AS org_name,
             c.path AS org_path
@@ -49,7 +51,7 @@
         d.device_name AS deviceName,
         d.org_id AS orgId,
         d.org_path AS orgPath,
-        CONCAT(b.affiliated_area,'-',b.affiliated_bank,'-',b.short_name) as org_name
+        b.short_name as org_name
         FROM
         iot_sensor d
         LEFT JOIN sys_org b ON d.org_id = b.id

+ 4 - 3
soc-modules/soc-modules-iot/src/main/resources/mapper/IotSensorMapper.xml

@@ -89,10 +89,11 @@
     <select id="selectByIds" resultType="com.xunmei.common.core.domain.iot.domain.IotSensor">
         SELECT
         org_id,
-        CONCAT(b.affiliated_area,'-',b.affiliated_bank,'-',b.short_name) as org_name,
-        device_name,
+        org_path,
+        b.short_name as org_name,
+        device_name as deviceName,
         infos AS info,
-        device_code
+        CONCAT(org_id,'-',device_code) as deviceCode
         FROM
         iot_sensor a
         LEFT JOIN sys_org b ON a.org_id = b.id