Bläddra i källkod

推送命令结果

lin 3 år sedan
förälder
incheckning
85f72e1990

+ 4 - 0
smart-city-intf/src/main/java/com/zcxk/smartcity/data/access/dao/DeviceCommandMapper.java

@@ -1,5 +1,7 @@
 package com.zcxk.smartcity.data.access.dao;
 
+import com.zcxk.smartcity.data.access.entity.Device;
+import com.zcxk.smartcity.data.access.entity.DeviceCommand;
 import org.apache.ibatis.annotations.Mapper;
 import org.apache.ibatis.annotations.Param;
 
@@ -9,4 +11,6 @@ public interface DeviceCommandMapper {
 
 
     int updateCommandStatus(@Param("commandId") String commandId, @Param("status") Integer status, @Param("message") String message);
+
+    DeviceCommand getDeviceCommand(@Param("commandId") String commandId);
 }

+ 18 - 0
smart-city-intf/src/main/java/com/zcxk/smartcity/data/access/entity/DeviceCommand.java

@@ -0,0 +1,18 @@
+package com.zcxk.smartcity.data.access.entity;
+
+import io.swagger.annotations.ApiModelProperty;
+import lombok.Data;
+
+@Data
+public class DeviceCommand {
+    @ApiModelProperty(value = "编号", position = 1)
+    private Long id;
+
+    @ApiModelProperty(value = "设备编号", position = 2)
+    private String deviceNo;
+
+    @ApiModelProperty(value = "客户id", position = 16)
+    private Integer customerId;
+
+    private String relationId;
+}

+ 29 - 0
smart-city-intf/src/main/java/com/zcxk/smartcity/data/access/rabbitmq/model/RabbitCommandResult.java

@@ -0,0 +1,29 @@
+package com.zcxk.smartcity.data.access.rabbitmq.model;
+
+import com.alibaba.fastjson.annotation.JSONField;
+import com.fasterxml.jackson.annotation.JsonFormat;
+import io.swagger.annotations.ApiModelProperty;
+import lombok.Data;
+
+import java.time.LocalDateTime;
+
+@Data
+public class RabbitCommandResult {
+
+    private Integer customerId;
+
+    private String commandId;
+
+    @ApiModelProperty(value="指令状态 0:发送、1:超时、2:成功、3:失败")
+    private Integer commandStatus;
+
+    private String relationId;
+
+    @ApiModelProperty(value="备注")
+    private String remark;
+
+    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
+    @JSONField(format="yyyy-MM-dd HH:mm:ss")
+    @ApiModelProperty(value="完成时间")
+    private LocalDateTime finishDate;
+}

+ 55 - 3
smart-city-intf/src/main/java/com/zcxk/smartcity/data/access/service/impl/DeviceValveRecordServiceImpl.java

@@ -1,16 +1,26 @@
 package com.zcxk.smartcity.data.access.service.impl;
 
+import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONObject;
 import com.zcxk.smartcity.data.access.dao.DeviceCommandMapper;
 import com.zcxk.smartcity.data.access.dao.DeviceValveRecordMapper;
+import com.zcxk.smartcity.data.access.dto.ConfigDataDto;
+import com.zcxk.smartcity.data.access.entity.Device;
+import com.zcxk.smartcity.data.access.entity.DeviceCommand;
 import com.zcxk.smartcity.data.access.entity.DeviceValveRecord;
+import com.zcxk.smartcity.data.access.rabbitmq.model.RabbitCommandResult;
+import com.zcxk.smartcity.data.access.service.ConfigService;
 import com.zcxk.smartcity.data.access.service.DeviceValveRecordService;
 import org.apache.commons.lang3.StringUtils;
+import org.springframework.amqp.rabbit.connection.SimpleResourceHolder;
+import org.springframework.amqp.rabbit.core.RabbitTemplate;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
 import javax.annotation.Resource;
 import java.time.LocalDateTime;
+import java.util.List;
 
 @Service
 public class DeviceValveRecordServiceImpl implements DeviceValveRecordService {
@@ -19,6 +29,10 @@ public class DeviceValveRecordServiceImpl implements DeviceValveRecordService {
     private DeviceValveRecordMapper deviceValveRecordMapper;
     @Resource
     private DeviceCommandMapper deviceCommandMapper;
+    @Autowired
+    private ConfigService configService;
+    @Autowired
+    private RabbitTemplate rabbitTemplate;
 
     @Override
     public int insertSelective(DeviceValveRecord record) {
@@ -37,11 +51,22 @@ public class DeviceValveRecordServiceImpl implements DeviceValveRecordService {
     @Transactional
     public void updateCommandStatus(JSONObject jsonObject) {
         String commandId =  jsonObject.getString("relationId");
-        String commandStatus =  jsonObject.getString("status");
+        String status =  jsonObject.getString("status");
         String message =  jsonObject.getString("message");
-        deviceValveRecordMapper.updateCommandStatus(commandId,commandStatus,message);
+        deviceValveRecordMapper.updateCommandStatus(commandId,status,message);
+
+        Integer commandStatus =  convertStatus(status);
+        int result = deviceCommandMapper.updateCommandStatus(commandId,commandStatus,message);
+
+        if(commandStatus !=null && commandStatus >0 && result > 0){
+            DeviceCommand deviceCommand = deviceCommandMapper.getDeviceCommand(commandId);
+            if (deviceCommand != null) {
+                if(this.matches(deviceCommand.getCustomerId())){
+                    sendMsg(deviceCommand,commandId,commandStatus,message);
+                }
+            }
+        }
 
-        deviceCommandMapper.updateCommandStatus(commandId,convertStatus(commandStatus),message);
     }
 
     private Integer convertStatus(String commandStatus){
@@ -65,4 +90,31 @@ public class DeviceValveRecordServiceImpl implements DeviceValveRecordService {
         if(StringUtils.equalsIgnoreCase("SENT",commandStatus))return 0;
         return null;
     }
+
+    private boolean matches(Integer customerId) {
+        List<ConfigDataDto> configDataList = configService.getConfigData("push_command_result_config");
+        if (configDataList != null && configDataList.size() > 0) {
+            for (ConfigDataDto configDataDto : configDataList) {
+                if(StringUtils.equals(configDataDto.getValue(),customerId.toString())){
+                    return true;
+                }
+            }
+        }
+        return false;
+    }
+
+    private void sendMsg(DeviceCommand deviceCommand,String commandId,Integer commandStatus,String message) {
+        RabbitCommandResult result = new RabbitCommandResult();
+        result.setRelationId(deviceCommand.getRelationId());
+        result.setCustomerId(deviceCommand.getCustomerId());
+        result.setCommandId(commandId);
+        result.setCommandStatus(commandStatus);
+        result.setRemark(message);
+        result.setFinishDate(LocalDateTime.now());
+
+
+        String jsonString = JSON.toJSONString(result);
+
+        rabbitTemplate.convertAndSend("command_result_exchange","command_result_queue", jsonString);
+    }
 }

+ 6 - 0
smart-city-intf/src/main/resources/mapper/DeviceCommandMapper.xml

@@ -5,4 +5,10 @@
     <update id="updateCommandStatus">
         update sc_device_command set command_status = #{status},remark=#{message},finish_date=NOW(),date_update=NOW() where command_id = #{commandId}
     </update>
+
+    <select id="getDeviceCommand" resultType="com.zcxk.smartcity.data.access.entity.DeviceCommand">
+        select sd.id,sd.device_no,sd.customer_id,sdc.relation_id  from sc_device_command sdc
+        left join sc_device sd on(sd.id = sdc.device_id)
+        where sd.status = 1 and sdc.command_id = #{commandId}
+    </select>
 </mapper>