瀏覽代碼

增加批量创建更新报警定义接口

lixing 4 年之前
父節點
當前提交
94657a961b

+ 7 - 0
pom.xml

@@ -51,6 +51,13 @@
             <version>2.2.6.RELEASE</version>
         </dependency>
 
+        <!-- hystrix-->
+        <dependency>
+            <groupId>org.springframework.cloud</groupId>
+            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
+            <version>2.2.1.RELEASE</version>
+        </dependency>
+
         <!-- fastjson -->
         <dependency>
             <groupId>com.alibaba</groupId>

+ 2 - 2
src/main/java/com/persagy/dmp/starter/alarm/aspect/AlarmClientAspect.java

@@ -29,9 +29,9 @@ public class AlarmClientAspect {
     }
 
     @AfterReturning(returning = "res", pointcut = "feignPointCut()")
-    public void after(DmpResult res) throws Exception {
+    public void after(DmpResult res) {
         if (DmpResult.FAILURE.equals(res.getResult())) {
-            throw new Exception(res.getMessage());
+            throw new RuntimeException(res.getMessage());
         }
     }
 }

+ 15 - 3
src/main/java/com/persagy/dmp/starter/alarm/communication/netty/NettyAlarmMsgBaseHandler.java

@@ -22,6 +22,7 @@ import java.util.List;
 import java.util.Map;
 import java.util.Set;
 import java.util.concurrent.ConcurrentHashMap;
+import java.util.stream.Collectors;
 
 /**
  * @description: Netty报警息处理中心
@@ -92,7 +93,10 @@ public class NettyAlarmMsgBaseHandler extends ChannelInboundHandlerAdapter {
             String[] projectIds = source.split(",");
             // 一个项目只能对应一个channel
             for (String projectId : projectIds) {
-                socketChannelMap.put(projectId, channelHandlerContext.channel());
+                // 保留旧的通道,因为当新通道请求连接时,可能老通道已经产生了通信消息
+                if (!socketChannelMap.containsKey(projectId)) {
+                    socketChannelMap.put(projectId, channelHandlerContext.channel());
+                }
             }
         }
     }
@@ -116,8 +120,16 @@ public class NettyAlarmMsgBaseHandler extends ChannelInboundHandlerAdapter {
         if (alarmConfigs == null || alarmConfigs.size() <= 0) {
             return;
         }
-        String projectId = data.getString("projectId");
-        sendMessage(projectId, new NettyAlarmMessage(9, alarmConfigs).toString());
+        // 查询到的报警定义按项目id分组,发送到对应的边缘端
+        Map<String, List<Object>> groups = alarmConfigs.stream().collect(
+                Collectors.groupingBy(alarmConfig ->
+                        ((JSONObject) alarmConfig).getString("projectId")
+                )
+        );
+        groups.forEach((projectId, alarmConfigList) -> {
+            sendMessage(projectId, new NettyAlarmMessage(9, alarmConfigList).toString());
+        });
+
     }
 
     /**

+ 4 - 0
src/main/java/com/persagy/dmp/starter/alarm/constant/RequestUrlConstant.java

@@ -11,7 +11,9 @@ public class RequestUrlConstant {
 
     private static final String QUERY = "query";
     private static final String CREATE = "create";
+    private static final String BATCH_CREATE = "batch_create";
     private static final String UPDATE = "update";
+    private static final String BATCH_UPDATE = "batch_update";
     private static final String DELETE = "delete";
 
     /**
@@ -20,7 +22,9 @@ public class RequestUrlConstant {
     private static final String ALARM_CONFIG = "/alarm/config/";
     public static final String ALARM_CONFIG_QUERY = ALARM_CONFIG + QUERY;
     public static final String ALARM_CONFIG_CREATE = ALARM_CONFIG + CREATE;
+    public static final String ALARM_CONFIG_BATCH_CREATE = ALARM_CONFIG + BATCH_CREATE;
     public static final String ALARM_CONFIG_UPDATE = ALARM_CONFIG + UPDATE;
+    public static final String ALARM_CONFIG_BATCH_UPDATE = ALARM_CONFIG + BATCH_UPDATE;
 
     /**
      * 报警记录

+ 33 - 20
src/main/java/com/persagy/dmp/starter/alarm/feign/AlarmServerFallbackFactory.java

@@ -6,6 +6,8 @@ import com.persagy.dmp.starter.alarm.feign.client.AlarmClient;
 import feign.hystrix.FallbackFactory;
 import lombok.extern.slf4j.Slf4j;
 
+import java.util.List;
+
 
 /**
  * @description: 降级处理
@@ -22,53 +24,64 @@ public class AlarmServerFallbackFactory implements FallbackFactory<AlarmClient>
            private String errorMsg = "feign调用报警中心接口异常,接口名称:[{}], 请求体:[{}]";
 
            @Override
-           public DmpResult<JSONArray> queryAlarmConfig(AlarmUrlParam alarmUrlParam, JSONObject jsonObject) throws Exception {
-               throw new Exception(String.format(errorMsg, "queryAlarmConfig", jsonObject.toString()));
+           public DmpResult<JSONArray> queryAlarmConfig(AlarmUrlParam alarmUrlParam, JSONObject jsonObject) {
+               throw new RuntimeException(String.format(errorMsg, "queryAlarmConfig", jsonObject.toString()));
+           }
+
+           @Override
+           public DmpResult<JSONObject> createAlarmConfig(AlarmUrlParam alarmUrlParam, JSONObject jsonObject){
+               throw new RuntimeException(String.format(errorMsg, "createAlarmConfig", jsonObject.toString()));
            }
 
            @Override
-           public DmpResult<JSONObject> createAlarmConfig(AlarmUrlParam alarmUrlParam, JSONObject jsonObject) throws Exception{
-               throw new Exception(String.format(errorMsg, "createAlarmConfig", jsonObject.toString()));
+           public DmpResult<JSONObject> batchCreateAlarmConfig(AlarmUrlParam alarmUrlParam, JSONObject jsonObject) throws Exception {
+               throw new RuntimeException(String.format(errorMsg, "batchCreateAlarmConfig", jsonObject.toString()));
            }
 
            @Override
-           public DmpResult<JSONObject> updateAlarmConfig(AlarmUrlParam alarmUrlParam, JSONObject jsonObject) throws Exception{
-               throw new Exception(String.format(errorMsg, "updateAlarmConfig", jsonObject.toString()));
+           public DmpResult<JSONObject> updateAlarmConfig(AlarmUrlParam alarmUrlParam, JSONObject jsonObject){
+               throw new RuntimeException(String.format(errorMsg, "updateAlarmConfig", jsonObject.toString()));
+           }
+
+
+           @Override
+           public DmpResult<JSONObject> batchUpdateAlarmConfig(AlarmUrlParam alarmUrlParam, JSONObject jsonObject) throws Exception {
+               throw new RuntimeException(String.format(errorMsg, "batchUpdateAlarmConfig", jsonObject.toString()));
            }
 
            @Override
-           public DmpResult<JSONArray> queryAlarmRecord(AlarmUrlParam alarmUrlParam, JSONObject jsonObject) throws Exception{
-               throw new Exception(String.format(errorMsg, "queryAlarmRecord", jsonObject.toString()));
+           public DmpResult<JSONArray> queryAlarmRecord(AlarmUrlParam alarmUrlParam, JSONObject jsonObject){
+               throw new RuntimeException(String.format(errorMsg, "queryAlarmRecord", jsonObject.toString()));
            }
 
            @Override
-           public DmpResult<JSONObject> createAlarmRecord(AlarmUrlParam alarmUrlParam, JSONObject jsonObject) throws Exception{
-               throw new Exception(String.format(errorMsg, "createAlarmRecord", jsonObject.toString()));
+           public DmpResult<JSONObject> createAlarmRecord(AlarmUrlParam alarmUrlParam, JSONObject jsonObject){
+               throw new RuntimeException(String.format(errorMsg, "createAlarmRecord", jsonObject.toString()));
            }
 
            @Override
-           public DmpResult<JSONObject> updateAlarmRecord(AlarmUrlParam alarmUrlParam, JSONObject jsonObject) throws Exception{
-               throw new Exception(String.format(errorMsg, "updateAlarmRecord", jsonObject.toString()));
+           public DmpResult<JSONObject> updateAlarmRecord(AlarmUrlParam alarmUrlParam, JSONObject jsonObject){
+               throw new RuntimeException(String.format(errorMsg, "updateAlarmRecord", jsonObject.toString()));
            }
 
            @Override
-           public DmpResult<JSONArray> queryAlarmItem(AlarmUrlParam alarmUrlParam, JSONObject jsonObject) throws Exception{
-               throw new Exception(String.format(errorMsg, "queryAlarmItem", jsonObject.toString()));
+           public DmpResult<JSONArray> queryAlarmItem(AlarmUrlParam alarmUrlParam, JSONObject jsonObject){
+               throw new RuntimeException(String.format(errorMsg, "queryAlarmItem", jsonObject.toString()));
            }
 
            @Override
-           public DmpResult<JSONObject> createAlarmComment(AlarmUrlParam alarmUrlParam, JSONObject jsonObject) throws Exception{
-               throw new Exception(String.format(errorMsg, "createAlarmComment", jsonObject.toString()));
+           public DmpResult<JSONObject> createAlarmComment(AlarmUrlParam alarmUrlParam, JSONObject jsonObject){
+               throw new RuntimeException(String.format(errorMsg, "createAlarmComment", jsonObject.toString()));
            }
 
            @Override
-           public DmpResult<JSONArray> queryAlarmComment(AlarmUrlParam alarmUrlParam, JSONObject jsonObject) throws Exception{
-               throw new Exception(String.format(errorMsg, "queryAlarmComment", jsonObject.toString()));
+           public DmpResult<JSONArray> queryAlarmComment(AlarmUrlParam alarmUrlParam, JSONObject jsonObject){
+               throw new RuntimeException(String.format(errorMsg, "queryAlarmComment", jsonObject.toString()));
            }
 
            @Override
-           public DmpResult<JSONArray> queryAlarmDic(AlarmUrlParam alarmUrlParam, JSONObject jsonObject) throws Exception {
-               throw new Exception(String.format(errorMsg, "queryAlarmDic", jsonObject.toString()));
+           public DmpResult<JSONArray> queryAlarmDic(AlarmUrlParam alarmUrlParam, JSONObject jsonObject) {
+               throw new RuntimeException(String.format(errorMsg, "queryAlarmDic", jsonObject.toString()));
            }
        };
     }

+ 29 - 1
src/main/java/com/persagy/dmp/starter/alarm/feign/client/AlarmClient.java

@@ -49,6 +49,20 @@ public interface AlarmClient {
     DmpResult<JSONObject> createAlarmConfig(@SpringQueryMap AlarmUrlParam alarmUrlParam, @RequestBody JSONObject jsonObject) throws Exception;
 
     /**
+     * @description: 批量创建报警定义
+     * @param: alarmUrlParam
+     * @param: jsonObject
+     * @return: com.persagy.dmp.starter.alarm.feign.DmpResult<org.springframework.boot.configurationprocessor.json.JSONObject>
+     * @exception:
+     * @author: lixing
+     * @company: Persagy Technology Co.,Ltd
+     * @since: 2020/11/27 3:41 下午
+     * @version: V1.0
+     */
+    @PostMapping(RequestUrlConstant.ALARM_CONFIG_BATCH_CREATE)
+    DmpResult<JSONObject> batchCreateAlarmConfig(@SpringQueryMap AlarmUrlParam alarmUrlParam, @RequestBody JSONObject jsonObject) throws Exception;
+
+    /**
      * @description: 更新报警定义
      * @param: alarmUrlParam
      * @param: jsonObject
@@ -63,6 +77,20 @@ public interface AlarmClient {
     DmpResult<JSONObject> updateAlarmConfig(@SpringQueryMap AlarmUrlParam alarmUrlParam, @RequestBody JSONObject jsonObject) throws Exception;
 
     /**
+     * @description: 批量更新报警定义
+     * @param: alarmUrlParam
+     * @param: jsonArrayStr
+     * @return: com.persagy.dmp.starter.alarm.feign.DmpResult<com.alibaba.fastjson.JSONObject>
+     * @exception:
+     * @author: lixing
+     * @company: Persagy Technology Co.,Ltd
+     * @since: 2020/12/4 3:59 下午
+     * @version: V1.0
+     */
+    @PostMapping(RequestUrlConstant.ALARM_CONFIG_BATCH_UPDATE)
+    DmpResult<JSONObject> batchUpdateAlarmConfig(@SpringQueryMap AlarmUrlParam alarmUrlParam, @RequestBody JSONObject jsonObject) throws Exception;
+
+    /**
      * @description: 查询报警记录
      * @param: alarmUrlParam
      * @param: jsonObject
@@ -145,7 +173,7 @@ public interface AlarmClient {
      */
     @PostMapping(RequestUrlConstant.ALARM_COMMENT_QUERY)
     DmpResult<JSONArray> queryAlarmComment(@SpringQueryMap AlarmUrlParam alarmUrlParam, @RequestBody JSONObject jsonObject) throws Exception;
-    
+
     /**
      * @description: 查询报警字典
      * @param: alarmUrlParam

+ 0 - 158
src/main/java/com/persagy/dmp/starter/alarm/service/AlarmService.java

@@ -1,158 +0,0 @@
-package com.persagy.dmp.starter.alarm.service;
-
-import com.alibaba.fastjson.JSONArray;
-import com.alibaba.fastjson.JSONObject;
-import com.persagy.dmp.starter.alarm.feign.AlarmUrlParam;
-import com.persagy.dmp.starter.alarm.feign.DmpResult;
-import com.persagy.dmp.starter.alarm.feign.client.AlarmClient;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.stereotype.Service;
-
-/**
- * @description:
- * @author: lixing
- * @company: Persagy Technology Co.,Ltd
- * @since: 2020/11/27 6:57 下午
- * @version: V1.0
- **/
-@Service
-public class AlarmService {
-    @Autowired
-    private AlarmClient alarmClient;
-
-    /**
-     * @description: 查询报警定义
-     * @param: alarmUrlParam
-     * @param: jsonObject
-     * @return: com.persagy.dmp.starter.alarm.feign.DmpResult<org.springframework.boot.configurationprocessor.json.JSONArray>
-     * @exception:
-     * @author: lixing
-     * @company: Persagy Technology Co.,Ltd
-     * @since: 2020/11/27 3:41 下午
-     * @version: V1.0
-     */
-    public DmpResult<JSONArray> queryAlarmConfig(AlarmUrlParam alarmUrlParam, JSONObject jsonObject) throws Exception {
-        return alarmClient.queryAlarmConfig(alarmUrlParam, jsonObject);
-    }
-
-    /**
-     * @description: 创建报警定义
-     * @param: alarmUrlParam
-     * @param: jsonObject
-     * @return: com.persagy.dmp.starter.alarm.feign.DmpResult<org.springframework.boot.configurationprocessor.json.JSONObject>
-     * @exception:
-     * @author: lixing
-     * @company: Persagy Technology Co.,Ltd
-     * @since: 2020/11/27 3:41 下午
-     * @version: V1.0
-     */
-    public DmpResult<JSONObject> createAlarmConfig(AlarmUrlParam alarmUrlParam, JSONObject jsonObject) throws Exception {
-        return alarmClient.createAlarmConfig(alarmUrlParam, jsonObject);
-    }
-
-    /**
-     * @description: 更新报警定义
-     * @param: alarmUrlParam
-     * @param: jsonObject
-     * @return: com.persagy.dmp.starter.alarm.feign.DmpResult<org.springframework.boot.configurationprocessor.json.JSONObject>
-     * @exception:
-     * @author: lixing
-     * @company: Persagy Technology Co.,Ltd
-     * @since: 2020/11/27 3:41 下午
-     * @version: V1.0
-     */
-    public DmpResult<JSONObject> updateAlarmConfig(AlarmUrlParam alarmUrlParam, JSONObject jsonObject) throws Exception {
-        return alarmClient.updateAlarmConfig(alarmUrlParam, jsonObject);
-    }
-
-    /**
-     * @description: 查询报警记录
-     * @param: alarmUrlParam
-     * @param: jsonObject
-     * @return: com.persagy.dmp.starter.alarm.feign.DmpResult<org.springframework.boot.configurationprocessor.json.JSONArray>
-     * @exception:
-     * @author: lixing
-     * @company: Persagy Technology Co.,Ltd
-     * @since: 2020/11/27 3:41 下午
-     * @version: V1.0
-     */
-    public DmpResult<JSONArray> queryAlarmRecord(AlarmUrlParam alarmUrlParam, JSONObject jsonObject) throws Exception {
-        return alarmClient.queryAlarmRecord(alarmUrlParam, jsonObject);
-    }
-
-    /**
-     * @description: 创建报警记录
-     * @param: alarmUrlParam
-     * @param: jsonObject
-     * @return: com.persagy.dmp.starter.alarm.feign.DmpResult<org.springframework.boot.configurationprocessor.json.JSONObject>
-     * @exception:
-     * @author: lixing
-     * @company: Persagy Technology Co.,Ltd
-     * @since: 2020/11/27 3:41 下午
-     * @version: V1.0
-     */
-    public DmpResult<JSONObject> createAlarmRecord(AlarmUrlParam alarmUrlParam, JSONObject jsonObject) throws Exception {
-        return alarmClient.createAlarmRecord(alarmUrlParam, jsonObject);
-    }
-
-    /**
-     * @description: 更新报警记录
-     * @param: alarmUrlParam
-     * @param: jsonObject
-     * @return: com.persagy.dmp.starter.alarm.feign.DmpResult<org.springframework.boot.configurationprocessor.json.JSONObject>
-     * @exception:
-     * @author: lixing
-     * @company: Persagy Technology Co.,Ltd
-     * @since: 2020/11/27 3:41 下午
-     * @version: V1.0
-     */
-    public DmpResult<JSONObject> updateAlarmRecord(AlarmUrlParam alarmUrlParam, JSONObject jsonObject) throws Exception {
-        return alarmClient.updateAlarmRecord(alarmUrlParam, jsonObject);
-    }
-
-    /**
-     * @description: 查询报警条目
-     * @param: alarmUrlParam
-     * @param: jsonObject
-     * @return: com.persagy.dmp.starter.alarm.feign.DmpResult<org.springframework.boot.configurationprocessor.json.JSONArray>
-     * @exception:
-     * @author: lixing
-     * @company: Persagy Technology Co.,Ltd
-     * @since: 2020/11/27 3:41 下午
-     * @version: V1.0
-     */
-    public DmpResult<JSONArray> queryAlarmItem(AlarmUrlParam alarmUrlParam, JSONObject jsonObject) throws Exception {
-        return alarmClient.queryAlarmItem(alarmUrlParam, jsonObject);
-    }
-
-    /**
-     * @description: 创建报警批注
-     * @param: alarmUrlParam
-     * @param: jsonObject
-     * @return: com.persagy.dmp.starter.alarm.feign.DmpResult<org.springframework.boot.configurationprocessor.json.JSONObject>
-     * @exception:
-     * @author: lixing
-     * @company: Persagy Technology Co.,Ltd
-     * @since: 2020/11/27 3:42 下午
-     * @version: V1.0
-     */
-    public DmpResult<JSONObject> createAlarmComment(AlarmUrlParam alarmUrlParam, JSONObject jsonObject) throws Exception {
-        return alarmClient.createAlarmComment(alarmUrlParam, jsonObject);
-    }
-
-    /**
-     * @description: 查询报警批注
-     * @param: alarmUrlParam
-     * @param: jsonObject
-     * @return: com.persagy.dmp.starter.alarm.feign.DmpResult<org.springframework.boot.configurationprocessor.json.JSONArray>
-     * @exception:
-     * @author: lixing
-     * @company: Persagy Technology Co.,Ltd
-     * @since: 2020/11/27 3:42 下午
-     * @version: V1.0
-     */
-    public DmpResult<JSONArray> queryAlarmComment(AlarmUrlParam alarmUrlParam, JSONObject jsonObject) throws Exception {
-        return alarmClient.queryAlarmComment(alarmUrlParam, jsonObject);
-    }
-
-}

+ 27 - 6
src/main/java/com/persagy/dmp/starter/alarm/service/NettyAlarmService.java

@@ -6,6 +6,7 @@ import com.alibaba.fastjson.JSONObject;
 import com.persagy.dmp.starter.alarm.communication.mq.DmpMessage;
 import com.persagy.dmp.starter.alarm.feign.AlarmUrlParam;
 import com.persagy.dmp.starter.alarm.feign.DmpResult;
+import com.persagy.dmp.starter.alarm.feign.client.AlarmClient;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -26,7 +27,7 @@ import java.util.stream.Collectors;
 @Slf4j
 public abstract class NettyAlarmService {
     @Autowired
-    AlarmService alarmService;
+    AlarmClient alarmClient;
 
     /**
      * @description: 参数校验
@@ -62,12 +63,22 @@ public abstract class NettyAlarmService {
      * @version: V1.0
      */
     public AlarmUrlParam getAlarmUrlParam(JSONObject obj) throws Exception {
+        String projectId = obj.getString("projectId");
+        // 如果projectId为空,设置为0,查询集团下所有项目的数据
+        if (StringUtils.isEmpty(projectId)) {
+            obj.put("projectId", "0");
+        }
+        // 如果projectId为多个项目(用逗号分隔), param中projectId传0
+        String[] projectIds = projectId.split(",");
+        if (projectIds.length > 1) {
+            obj.put("projectId", "0");
+        }
         this.checkRequestParam(obj);
 
         return new AlarmUrlParam(
                 obj.getString("userId"),
                 obj.getString("groupCode"),
-                obj.getString("projectId"),
+                projectId,
                 obj.getString("appId")
         );
     }
@@ -110,6 +121,16 @@ public abstract class NettyAlarmService {
             criteria.remove("page");
             criteria.remove("size");
         }
+        /* 如果查询条件中包含projectId,进行如下处理 */
+        String projectId = criteria.getString("projectId");
+        // 如果projectId为空或是0,从查询条件中移除
+        if (StringUtils.isEmpty(projectId) || "0".equals(projectId)) {
+            criteria.remove("projectId");
+        } else {
+            // 将projectId转换为数组,适配projectId为多个项目(用逗号分隔)的情况
+            String[] projectIds = projectId.split(",");
+            criteria.put("projectId", projectIds);
+        }
 
         requestBody.put("criteria", criteria);
         return requestBody;
@@ -126,7 +147,7 @@ public abstract class NettyAlarmService {
      * @version: V1.0
      */
     public JSONArray queryAlarmConfig(JSONObject data) throws Exception {
-        DmpResult<JSONArray> queryResult = alarmService.queryAlarmConfig(getAlarmUrlParam(data), getRequestBody(data));
+        DmpResult<JSONArray> queryResult = alarmClient.queryAlarmConfig(getAlarmUrlParam(data), getRequestBody(data));
         JSONArray alarmConfigs = queryResult.getData();
         // 报警定义中的信息点转换为表号、功能号
         initAlarmConfigInfoCodes(alarmConfigs);
@@ -236,7 +257,7 @@ public abstract class NettyAlarmService {
         data.put("name", getAlarmName(objId));
         data.put("remark", getAlarmRemark(objId));
 
-        DmpResult<JSONObject> alarmRecord = alarmService.createAlarmRecord(getAlarmUrlParam(data), data);
+        DmpResult<JSONObject> alarmRecord = alarmClient.createAlarmRecord(getAlarmUrlParam(data), data);
         return alarmRecord.getData().getString("id");
     }
 
@@ -251,7 +272,7 @@ public abstract class NettyAlarmService {
      * @version: V1.0
      */
     public void updateAlarmRecord(JSONObject data) throws Exception {
-        alarmService.updateAlarmRecord(getAlarmUrlParam(data), data);
+        alarmClient.updateAlarmRecord(getAlarmUrlParam(data), data);
     }
 
     /**
@@ -295,7 +316,7 @@ public abstract class NettyAlarmService {
             data.put("itemCode", jsonObject.getString("itemCode"));
             data.put("objId", jsonObject.getString("objId"));
             data.put("userId", "system");
-            DmpResult<JSONArray> alarmConfigQueryResult = alarmService.queryAlarmConfig(getAlarmUrlParam(data), getRequestBody(data));
+            DmpResult<JSONArray> alarmConfigQueryResult = alarmClient.queryAlarmConfig(getAlarmUrlParam(data), getRequestBody(data));
             JSONArray tmpAlarmConfigArr = alarmConfigQueryResult.getData();
             if (!CollectionUtils.isEmpty(tmpAlarmConfigArr)) {
                 // 报警定义中的信息点转换为表号、功能号