Explorar o código

异步维护在线状态和在线时间

xucaiqin hai 1 ano
pai
achega
2fc3e0c578

+ 2 - 1
iot-module/iot-module-manage/iot-module-manage-biz/src/main/java/com/middle/platform/manage/biz/domain/vo/IotDeviceVo.java

@@ -6,6 +6,7 @@ import lombok.Getter;
 import lombok.Setter;
 
 import java.math.BigDecimal;
+import java.time.LocalDateTime;
 
 /**
  * 设备表
@@ -67,7 +68,7 @@ public class IotDeviceVo extends BaseVO {
      * 最后在线时间
      */
     @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
-    private Object onlineTime;
+    private LocalDateTime onlineTime;
     /**
      * 节点类型
      */

+ 2 - 0
iot-module/iot-module-manage/iot-module-manage-biz/src/main/java/com/middle/platform/manage/biz/mapper/IotDeviceMapper.java

@@ -61,4 +61,6 @@ public interface IotDeviceMapper extends BaseMapper<IotDevice> {
     DeviceVo deviceCache(@Param("guid") String guid);
 
     List<IotDeviceDownVo> list(@Param("productId") Long productId);
+
+    void updateStatus(IotDeviceVo iotDevice);
 }

+ 20 - 7
iot-module/iot-module-manage/iot-module-manage-biz/src/main/java/com/middle/platform/manage/biz/service/IotDeviceService.java

@@ -67,15 +67,21 @@ public class IotDeviceService {
         List<IotDeviceVo> iotProductVos = iotDeviceMapper.pageQuery(devicePage);
         PageInfo<IotDeviceVo> pageInfo = new PageInfo<>(iotProductVos);
         iotProductVos.forEach(a -> {
+            Optional.ofNullable(cacheService.getKey(String.format(CacheConstant.ONLINE_CACHE, a.getGuid()))).ifPresent(t -> {
+                a.setOnlineTime(LocalDateTimeUtil.parse((String) t));
+                a.setStatus(1);
+            });
             Optional.ofNullable(userApi.queryUser(a.getCreateBy())).ifPresent(userCache -> a.setCreateByLabel(userCache.getName()));
             Optional.ofNullable(userApi.queryUser(a.getUpdateBy())).ifPresent(userCache -> a.setUpdateByLabel(userCache.getName()));
             Optional.ofNullable(dictApi.query(DictType.NODE_TYPE, String.valueOf(a.getNodeType()))).ifPresent(dictCache -> a.setNodeTypeLabel(dictCache.getLabel()));
             Optional.ofNullable(dictApi.query(DictType.STATUS_TYPE, String.valueOf(a.getStatus()))).ifPresent(dictCache -> a.setStatusName(dictCache.getLabel()));
             Optional.ofNullable(dictApi.query(DictType.ENABLE_TYPE, String.valueOf(a.getEnableFlag()))).ifPresent(dictCache -> a.setEnableFlagName(dictCache.getLabel()));
-            Optional.ofNullable(cacheService.getKey(String.format(CacheConstant.ONLINE_CACHE, a.getGuid()))).ifPresent(t -> {
-                a.setOnlineTime(t);
-                a.setStatus(1);
-            });
+        });
+        //异步维护在线状态和在线时间
+        ThreadTask.addJob(() -> {
+            for (IotDeviceVo iotProductVo : iotProductVos) {
+                iotDeviceMapper.updateStatus(iotProductVo);
+            }
         });
         return new PageRes<>(pageInfo, iotProductVos);
     }
@@ -190,6 +196,10 @@ public class IotDeviceService {
         if (Objects.isNull(detail)) {
             throw new BusinessException("设备不存在");
         }
+        Optional.ofNullable(cacheService.getKey(String.format(CacheConstant.ONLINE_CACHE, detail.getGuid()))).ifPresent(t -> {
+            detail.setOnlineTime(LocalDateTimeUtil.parse((String) t));
+            detail.setStatus(1);
+        });
         Optional.ofNullable(userApi.queryUser(detail.getCreateBy())).ifPresent(userCache -> detail.setCreateByLabel(userCache.getName()));
         Optional.ofNullable(userApi.queryUser(detail.getUpdateBy())).ifPresent(userCache -> detail.setUpdateByLabel(userCache.getName()));
         Optional.ofNullable(dictApi.query(DictType.AUTH_TYPE, String.valueOf(detail.getAuthType()))).ifPresent(cache -> detail.setAuthTypeLabel(cache.getLabel()));
@@ -200,9 +210,12 @@ public class IotDeviceService {
         Optional.ofNullable(dictApi.query(DictType.REPORT_PROTOCOL_TYPE, String.valueOf(detail.getReportProtocol()))).ifPresent(cache -> detail.setReportProtocolLabel(cache.getLabel()));
         Optional.ofNullable(dictApi.query(DictType.VENDORS_TYPE, String.valueOf(detail.getVendors()))).ifPresent(cache -> detail.setVendorsLabel(cache.getLabel()));
         Optional.ofNullable(dictApi.query(DictType.DATA_FORMAT_TYPE, String.valueOf(detail.getDataFormat()))).ifPresent(cache -> detail.setDataFormatLabel(cache.getLabel()));
-        Optional.ofNullable(cacheService.getKey(String.format(CacheConstant.ONLINE_CACHE, detail.getGuid()))).ifPresent(t -> {
-            detail.setOnlineTime(LocalDateTimeUtil.parse((String) t));
-            detail.setStatus(1);
+
+        ThreadTask.addJob(() -> {
+            IotDeviceVo iotDevice = new IotDeviceVo();
+            iotDevice.setStatus(detail.getStatus());
+            iotDevice.setOnlineTime(detail.getOnlineTime());
+            iotDeviceMapper.updateStatus(iotDevice);
         });
         return detail;
     }

+ 7 - 0
iot-module/iot-module-manage/iot-module-manage-biz/src/main/resources/mapper/IotDeviceMapper.xml

@@ -205,4 +205,11 @@
             </if>
         </where>
     </select>
+
+    <update id="updateStatus">
+        UPDATE iot_device
+        SET `status`=#{status,jdbcType=INTEGER},
+            online_time= #{onlineTime,jdbcType=TIMESTAMP}
+        WHERE id = #{id,jdbcType=BIGINT}
+    </update>
 </mapper>