lihui001 3 years ago
parent
commit
4537f99992

+ 1 - 1
user_center/src/main/java/com/zcxk/UserCenterApplication.java

@@ -15,7 +15,7 @@ import java.net.InetAddress;
 import java.net.UnknownHostException;
 
 @EnableDiscoveryClient
-@SpringBootApplication
+@SpringBootApplication(scanBasePackages={"com.bz.zoneiot","com.zcxk"})
 @EnableSwagger2
 @Slf4j
 public class UserCenterApplication {

+ 0 - 1
zoniot-water/zoniot-water-api/src/main/java/com/bz/zoneiot/water/api/dto/alarm/DataAlarmDto.java

@@ -19,7 +19,6 @@ import java.io.Serializable;
 public class DataAlarmDto implements Serializable {
 
     @ApiModelProperty(value = "场景类型")
-    @NotNull
     private Integer sceneType;
 
     @ApiModelProperty(value = "名称")

+ 3 - 0
zoniot-water/zoniot-water-api/src/main/java/com/bz/zoneiot/water/api/vo/DeviceSceneVo.java

@@ -31,6 +31,9 @@ public class DeviceSceneVo {
     @ApiModelProperty(value = "场景名字")
     private String sceneName;
 
+    @ApiModelProperty(value = "设备名字")
+    private String deviceName;
+
     @ApiModelProperty(value = "父场景名字")
     private String parentSceneName;
 

+ 4 - 1
zoniot-water/zoniot-water-core/src/main/java/com/bz/zoneiot/water/core/mapper/DataAlarmDetailMapper.xml

@@ -35,7 +35,7 @@
             INNER JOIN sms_device_attribute t2 on m.to_attribute_id=t2.id  and t2.status =1
         ) mm on mm.id = a.id
       <where>
-          a.tenant_id = #{dto.tenantId} and a.scene_type =#{dto.sceneType}
+          a.tenant_id = #{dto.tenantId}
           <if test="dto.userType != null and dto.userType != -999 and dto.userType!=-9999 and  dto.programItems != null and dto.programItems.size() > 0">
               <if test="dto.permissonType == 5 or dto.permissonType == 2">
                   and ( a.DEPT_ORG_ID in
@@ -63,6 +63,9 @@
                   and (a.DEPT_ORG_ID is null or a.DEPT_ORG_ID =0)
               </if>
           </if>
+        <if test="dto.sceneType != null and dto.sceneType != ''">
+          and a.scene_type = #{dto.sceneType}
+        </if>
         <if test="dto.alarmName != null and dto.alarmName != ''">
             and a.alarm_name like CONCAT('%',#{dto.alarmName,jdbcType=VARCHAR},'%')
         </if>

+ 1 - 1
zoniot-water/zoniot-water-core/src/main/java/com/bz/zoneiot/water/core/mapper/DeviceSceneMapper.xml

@@ -76,7 +76,7 @@
     <select id="findSceneByDeviceId" resultType="com.bz.zoneiot.water.api.vo.DeviceSceneVo">
         SELECT
 
-        s.COMPANY_ORG_ID, s.TENANT_ID, s.DEPT_ORG_ID, sds.SCENE_ID, sds.PARENT_SCENE_ID, ss1.SCENE_NAME, ss2.SCENE_NAME as parent_scene_name
+        s.COMPANY_ORG_ID, s.DEVICE_NAME, s.TENANT_ID, s.DEPT_ORG_ID, sds.SCENE_ID, sds.PARENT_SCENE_ID, ss1.SCENE_NAME, ss2.SCENE_NAME as parent_scene_name
 
         from sms_device s
 

+ 9 - 4
zoniot-water/zoniot-water-web/src/main/java/com/bz/zoneiot/water/web/config/RabbitConfig.java

@@ -1,3 +1,4 @@
+/*
 package com.bz.zoneiot.water.web.config;
 
 import org.slf4j.Logger;
@@ -18,6 +19,7 @@ import org.springframework.context.annotation.Configuration;
 import org.springframework.context.annotation.Primary;
 import org.springframework.context.annotation.Scope;
 
+*/
 /**
  Broker:它提供一种传输服务,它的角色就是维护一条从生产者到消费者的路线,保证数据能按照指定的方式进行传输,
  Exchange:消息交换机,它指定消息按什么规则,路由到哪个队列。
@@ -29,7 +31,8 @@ import org.springframework.context.annotation.Scope;
  Consumer:消息消费者,就是接受消息的程序.
  Channel:消息通道,在客户端的每个连接里,可建立多个channel.
  * @author Andy
- **/
+ **//*
+
 @Configuration
 public class RabbitConfig {
 
@@ -180,10 +183,12 @@ public class RabbitConfig {
         return template;
     }
 
-    /**
+    */
+/**
      * 配置消费者监听的容器
      * @return
-     */
+     *//*
+
     @Bean(name = "receiveDataContainerFactory")
     public SimpleRabbitListenerContainerFactory receiveDatarabbitListenerContainerFactory() {
         SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
@@ -199,4 +204,4 @@ public class RabbitConfig {
         return factory;
     }
 
-}
+}*/

+ 3 - 3
zoniot-water/zoniot-water-web/src/main/java/com/bz/zoneiot/water/web/service/impl/AlarmDetailsServiceImpl.java

@@ -36,8 +36,8 @@ public class AlarmDetailsServiceImpl implements AlarmDetailsService {
     @Resource
     private AlarmDetailMapper alarmDetailMapper;
 
-    @Resource(name = "messageRabbitTemplate")
-    private AmqpTemplate messageRabbitTemplate;
+    @Resource
+    private AmqpTemplate amqpTemplate;
 
     @Value("${receive.exchange.name}")
     private  String receiveExchangeName;
@@ -48,7 +48,7 @@ public class AlarmDetailsServiceImpl implements AlarmDetailsService {
 
     private void send(Message message){
         log.debug("消息发送 exchange={}  routingkey={} 用户id={}",receiveExchangeName,dispathRoutingKey,message.getUserId());
-        messageRabbitTemplate.convertAndSend(receiveExchangeName,dispathRoutingKey, JSONObject.toJSONString(message));
+        amqpTemplate.convertAndSend(receiveExchangeName,dispathRoutingKey, JSONObject.toJSONString(message));
     }
 
     @Override

+ 62 - 3
zoniot-water/zoniot-water-web/src/main/java/com/bz/zoneiot/water/web/service/impl/AlarmTypeDetailsServiceImpl.java

@@ -1,10 +1,10 @@
 package com.bz.zoneiot.water.web.service.impl;
 
+import com.alibaba.fastjson.JSONObject;
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.bz.zoneiot.core.common.enums.StatusEnum;
-import com.bz.zoneiot.core.common.util.BeanCopyUtils;
+import com.bz.zoneiot.core.common.pojo.Message;
 import com.bz.zoneiot.core.oauth2.util.UserUtil;
-import com.bz.zoneiot.core.utils.BigDecimalUtils;
 import com.bz.zoneiot.water.api.dto.AlarmDetailsAddDto;
 import com.bz.zoneiot.water.api.dto.AlarmTypeDetailsDto;
 import com.bz.zoneiot.water.api.enums.AlarmStateEnum;
@@ -15,10 +15,13 @@ import com.bz.zoneiot.water.core.dao.DeviceSceneMapper;
 import com.bz.zoneiot.water.core.entity.AlarmDetailsEntity;
 import com.bz.zoneiot.water.core.entity.AlarmType;
 import com.bz.zoneiot.water.core.entity.DeviceAttributeEntity;
+import com.bz.zoneiot.water.web.client.UserCenterClient;
 import com.bz.zoneiot.water.web.service.AlarmTypeDetailsService;
 import com.bz.zoneiot.water.web.service.AlarmTypeService;
-import io.swagger.models.auth.In;
 import lombok.extern.log4j.Log4j2;
+import org.springframework.amqp.core.AmqpTemplate;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
 import org.springframework.util.CollectionUtils;
 
@@ -27,6 +30,7 @@ import java.math.BigDecimal;
 import java.util.ArrayList;
 import java.util.Date;
 import java.util.List;
+import java.util.UUID;
 
 /**
  * @description
@@ -37,6 +41,18 @@ import java.util.List;
 @Log4j2
 public class AlarmTypeDetailsServiceImpl implements AlarmTypeDetailsService {
 
+    @Resource
+    private AmqpTemplate amqpTemplate;
+
+    @Autowired
+    private UserCenterClient userCenterClient;
+
+    @Value("${receive.exchange.name}")
+    private String receiveExchangeName;
+
+    @Value("${dispath.routing.key}")
+    private String dispathRoutingKey;
+
     @Resource
     private AlarmTypeDetailMapper alarmTypeDetailMapper;
 
@@ -122,6 +138,8 @@ public class AlarmTypeDetailsServiceImpl implements AlarmTypeDetailsService {
         }
         result = new ArrayList<>();
         BigDecimal value;
+        String attrName = null;
+        String deviceName = null;
         // 离线告警
         if (alarmType.getAlarmCategory() == 2 || alarmType.getAttributeId() == null) {
             value = new BigDecimal("0");
@@ -131,6 +149,7 @@ public class AlarmTypeDetailsServiceImpl implements AlarmTypeDetailsService {
                 log.error("【接收告警信息失败】:属性为空 ,请求参数:{}", dto.toString());
                 return null;
             }
+            attrName = attributeEntity.getName();
             value = new BigDecimal(dto.getData().get(attributeEntity.getIdentifier()).toString());
         }
         List<DeviceSceneVo> sceneVos = deviceSceneMapper.findSceneByDeviceId(alarmType.getDeviceId());
@@ -161,9 +180,49 @@ public class AlarmTypeDetailsServiceImpl implements AlarmTypeDetailsService {
             alarmDetailsEntity.setStatus(StatusEnum.OK.getCode());
             alarmDetailsEntity.setState(AlarmStateEnum.REALTIME_ALARM.getCode());
             alarmDetailsEntity.setDateCreate(new Date());
+            deviceName = sceneVo.getDeviceName();
+            sendMessage(alarmDetailsEntity, attrName, deviceName);
             result.add(alarmDetailsEntity);
         }
+
         return result;
     }
 
+    private void sendMessage(AlarmDetailsEntity alarmDetailsEntity, String attrName, String deviceName){
+        Message message = new Message();
+        message.setStatus(1);
+        message.setTenantId(alarmDetailsEntity.getTenantId());
+        message.setMessageId(UUID.randomUUID().toString());
+        JSONObject jsonContent = new JSONObject();
+        // ${场景名称}${设备名称}【${报警字段}】异常报警
+        jsonContent.put("场景名称",alarmDetailsEntity.getSceneName());
+        jsonContent.put("设备名称",deviceName);
+        jsonContent.put("报警字段", attrName);
+        // 消息内容,如果需要动态使用,配合模板使用{key:value}
+        message.setMessageContent(jsonContent.toJSONString());
+        // 消息类型、模板id、渠道
+        message.setMessageType(1);
+        message.setMessageTemplateId(1);
+        message.setChannel(0);
+        Integer companyOrgId = alarmDetailsEntity.getCompanyOrgId();
+        Integer departmentOrgId = alarmDetailsEntity.getDeptOrgId();
+        try{
+            List<Integer> taskUsers = userCenterClient.findUserIdsByPermissonOrg(alarmDetailsEntity.getTenantId(),companyOrgId,departmentOrgId);
+            if (taskUsers == null) {
+                return;
+            }
+            taskUsers.forEach(id -> {
+                message.setUserId(id);
+                this.send(message);
+            });
+        } catch (Exception e) {
+            log.error ("推送报警消息失败:{}",e);
+        }
+    }
+
+    private void send(Message message){
+        log.debug("消息发送 exchange={}  routingkey={} 用户id={}",receiveExchangeName,dispathRoutingKey,message.getUserId());
+        amqpTemplate.convertAndSend(receiveExchangeName, dispathRoutingKey, JSONObject.toJSONString(message));
+    }
+
 }