Browse Source

添加消息

lihui001 3 years ago
parent
commit
ec7c8a7486

+ 46 - 0
user_center/src/main/java/com/zcxk/config/JacksonConfig.java

@@ -0,0 +1,46 @@
+package com.zcxk.config;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.module.SimpleModule;
+import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
+import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.context.annotation.Primary;
+import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
+
+import java.math.BigInteger;
+import java.time.LocalDateTime;
+import java.time.format.DateTimeFormatter;
+
+/**
+ * jackson配置
+ */
+@Configuration
+public class JacksonConfig {
+    @Bean
+    @Primary
+    @ConditionalOnMissingBean(ObjectMapper.class)
+    public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) {
+        ObjectMapper objectMapper = builder.createXmlMapper(false).build();
+
+        /**
+         * 序列换成json时,将所有的long变成string
+         * 因为js中得数字类型不能包含所有的java long值并且long值转json丢失精度
+         */
+        SimpleModule module = new SimpleModule();
+        module.addSerializer(Long.class, ToStringSerializer.instance);
+        module.addSerializer(Long.TYPE, ToStringSerializer.instance);
+        module.addSerializer(BigInteger.class, ToStringSerializer.instance);
+
+
+
+        //LocalDateTime
+        LocalDateTimeDeserializer localDateTimeDeserializer = new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
+        module.addDeserializer(LocalDateTime.class, localDateTimeDeserializer);
+        objectMapper.registerModule(module);
+
+        return objectMapper;
+    }
+}

+ 3 - 0
zoniot-common/zoniot-core-common/src/main/java/com/bz/zoneiot/core/common/pojo/AjaxMessage.java

@@ -31,6 +31,9 @@ public class AjaxMessage<T> implements Serializable {
     public AjaxMessage() {
     }
 
+    public AjaxMessage(String msg) {
+        this.msg = msg;
+    }
 
     public AjaxMessage(RespCode respCode) {
         this.status = respCode.getStatus();

+ 9 - 0
zoniot-common/zoniot-core-common/src/main/java/com/bz/zoneiot/core/common/pojo/Message.java

@@ -17,6 +17,7 @@ import java.util.Date;
 @Data
 @ApiModel
 public class Message implements Serializable {
+
     private static final long serialVersionUID = 218863842329351978L;
     /**
      * 主键
@@ -113,7 +114,15 @@ public class Message implements Serializable {
     private String shortName;
     @ApiModelProperty(value = "消息跳转绝对路径")
     private String path;
+
     @ApiModelProperty(value = "消息类型id")
     private Integer typeId;
+
     private Integer appId;
+
+    @ApiModelProperty(value = "公司ID")
+    private Integer companyOrgId;
+
+    @ApiModelProperty(value = "部门ID")
+    private Integer departmentOrgId;
 }

+ 1 - 1
zoniot-common/zoniot-core-mq/src/main/java/com/bz/zoneiot/core/utils/rabbit/RabbitServiceImpl.java

@@ -12,7 +12,7 @@ import org.springframework.stereotype.Service;
  * @author Andy
  * @date 2021-07-22 11:03
  */
-@Service
+@Service("rabbitService")
 @Slf4j
 public class RabbitServiceImpl implements MqService {
 

+ 4 - 0
zoniot-water/zoniot-water-web/pom.xml

@@ -72,6 +72,10 @@
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-amqp</artifactId>
         </dependency>
+        <dependency>
+            <groupId>com.zcxk</groupId>
+            <artifactId>zoniot-core-mq</artifactId>
+        </dependency>
 
     </dependencies>
 

+ 79 - 0
zoniot-water/zoniot-water-web/src/main/java/com/bz/zoneiot/water/web/component/MessageComponent.java

@@ -0,0 +1,79 @@
+package com.bz.zoneiot.water.web.component;
+
+import com.alibaba.fastjson.JSONObject;
+import com.bz.zoneiot.core.common.pojo.Message;
+import com.bz.zoneiot.core.utils.MqService;
+import com.bz.zoneiot.water.web.client.UserCenterClient;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.amqp.core.AmqpTemplate;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+import org.springframework.util.Assert;
+
+import javax.annotation.Resource;
+import java.util.List;
+import java.util.UUID;
+
+/**
+ * @author Andy
+ * @version V1.0
+ * @description: TODO
+ * @date 2021/10/25
+ **/
+@Component
+@Slf4j
+public class MessageComponent  {
+
+    @Resource
+    private MqService rabbitService;
+
+    @Autowired
+    private UserCenterClient userCenterClient;
+
+    /**
+    * 推送消息给租户所有的用户
+    * @author Andy
+    * @date 11:51 2021/11/11
+    * @param message:
+    * @param receiveExchangeName:
+    * @param dispatchRoutingKey:
+    * @return void
+    **/
+    public void sendMessageToAllTenantUsers(Message message, String receiveExchangeName, String dispatchRoutingKey){
+        Assert.notNull(message.getTenantId(), "租户ID为空");
+        Assert.notNull(message.getCompanyOrgId(), "公司ID为空");
+        try{
+            List<Integer> taskUsers = userCenterClient.findUserIdsByPermissonOrg(message.getTenantId() , message.getCompanyOrgId(), message.getCompanyOrgId());
+            if (taskUsers == null) {
+                return;
+            }
+            taskUsers.forEach(id -> {
+                message.setUserId(id);
+                this.send(message, receiveExchangeName, dispatchRoutingKey);
+            });
+        } catch (Exception e) {
+            log.error ("推送报警消息失败:{}",e);
+        }
+    }
+
+    /**
+    * 推送单个
+    * @author Andy
+    * @date 11:52 2021/11/11
+    * @param message:
+    * @param receiveExchangeName:
+    * @param dispatchRoutingKey:
+    * @return void
+    **/
+    public void sendMessageToUser(Message message, String receiveExchangeName, String dispatchRoutingKey){
+        Assert.notNull(message.getUserId(), "用户ID为空");
+        this.send(message, receiveExchangeName, dispatchRoutingKey);
+    }
+
+    private void send(Message message, String receiveExchangeName, String dispatchRoutingKey){
+        log.debug("消息发送 exchange={}  routingkey={} 用户id={}",receiveExchangeName, dispatchRoutingKey, message.getUserId());
+        rabbitService.send(receiveExchangeName, dispatchRoutingKey, JSONObject.toJSONString(message));
+    }
+
+
+}

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

@@ -3,6 +3,7 @@ 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.pojo.AjaxMessage;
 import com.bz.zoneiot.core.common.pojo.Message;
 import com.bz.zoneiot.core.oauth2.util.UserUtil;
 import com.bz.zoneiot.water.api.dto.AlarmDetailsAddDto;
@@ -15,7 +16,9 @@ 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.core.entity.Org;
 import com.bz.zoneiot.water.web.client.UserCenterClient;
+import com.bz.zoneiot.water.web.component.MessageComponent;
 import com.bz.zoneiot.water.web.service.AlarmTypeDetailsService;
 import com.bz.zoneiot.water.web.service.AlarmTypeService;
 import lombok.extern.log4j.Log4j2;
@@ -41,18 +44,15 @@ import java.util.UUID;
 @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 MessageComponent messageComponent;
+
     @Resource
     private AlarmTypeDetailMapper alarmTypeDetailMapper;
 
@@ -181,48 +181,37 @@ public class AlarmTypeDetailsServiceImpl implements AlarmTypeDetailsService {
             alarmDetailsEntity.setState(AlarmStateEnum.REALTIME_ALARM.getCode());
             alarmDetailsEntity.setDateCreate(new Date());
             deviceName = sceneVo.getDeviceName();
-            sendMessage(alarmDetailsEntity, attrName, deviceName);
+            sendMessage(alarmDetailsEntity, alarmType.getAlarmCategory(), attrName, deviceName);
             result.add(alarmDetailsEntity);
         }
 
         return result;
     }
 
-    private void sendMessage(AlarmDetailsEntity alarmDetailsEntity, String attrName, String deviceName){
+    private void sendMessage(AlarmDetailsEntity alarmDetailsEntity, int category , 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);
+        if (category == 1) {
+            jsonContent.put("报警字段", attrName);
+        } else {
+            jsonContent.put("状态", "离线");
+        }
         // 消息内容,如果需要动态使用,配合模板使用{key:value}
         message.setMessageContent(jsonContent.toJSONString());
         // 消息类型、模板id、渠道
         message.setMessageType(1);
-        message.setMessageTemplateId(1);
+        message.setMessageTemplateId(category == 1 ? 1 : 2);
         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);
-        }
+        message.setCompanyOrgId(alarmDetailsEntity.getCompanyOrgId());
+        message.setDepartmentOrgId(alarmDetailsEntity.getDeptOrgId());
+        messageComponent.sendMessageToAllTenantUsers(message, receiveExchangeName, dispathRoutingKey);
     }
 
-    private void send(Message message){
-        log.debug("消息发送 exchange={}  routingkey={} 用户id={}",receiveExchangeName,dispathRoutingKey,message.getUserId());
-        amqpTemplate.convertAndSend(receiveExchangeName, dispathRoutingKey, JSONObject.toJSONString(message));
-    }
 
 }