浏览代码

提一版

lipenghui 3 月之前
父节点
当前提交
1c495ca6a3

+ 27 - 19
yudao-module-pms/yudao-module-pms-biz/src/main/java/cn/iocoder/yudao/module/pms/controller/admin/IotDeviceController.java

@@ -248,10 +248,10 @@ public class IotDeviceController {
     @PreAuthorize("@ss.hasPermission('rq:iot-device:query')")
     public CommonResult<PageResult<IotDeviceRespVO>> getIotDeviceTdPage(@Valid IotDevicePageReqVO pageReqVO) {
         List<String> codes = new ArrayList<>();
-        List<YfDeviceDO> allDevice = yfDeviceService.getAllDevice();
-        allDevice.forEach(d -> {
-            codes.add(d.getSerialNumber());
-        });
+//        List<YfDeviceDO> allDevice = yfDeviceService.getAllDevice();
+//        allDevice.forEach(d -> {
+//            codes.add(d.getSerialNumber());
+//        });
         PageResult<IotDeviceDO> pageResult = iotDeviceService.getIotDeviceTdPage(pageReqVO, codes);
         if (CollUtil.isEmpty(pageResult.getList())) {
             return success(new PageResult<>(pageResult.getTotal()));
@@ -263,25 +263,33 @@ public class IotDeviceController {
         for (IotDeviceRespVO iotDeviceRespVO : result.getList()) {
             IotProductClassifyDO iotProductClassify = iotProductClassifyService.getIotProductClassify(iotDeviceRespVO.getAssetClass());
             iotDeviceRespVO.setAssetClassName(iotProductClassify.getName());
-            String deviceCode = iotDeviceRespVO.getDeviceCode();
-            allDevice.stream().filter(e -> e.getSerialNumber().equals(deviceCode)).findFirst().ifPresent(e -> {
-                iotDeviceRespVO.setIfInline(e.getStatus());
-                Integer i = deviceMapper.tableIfExist(e.getSerialNumber().toLowerCase());
-                if (i==1) {
-                    List<DeviceVO> deviceVOS = deviceMapper.selectLastTime(e.getSerialNumber());
-                    if (CollUtil.isNotEmpty(deviceVOS)) {
-                        Timestamp ts = deviceVOS.get(0).getTs();
-                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
-                        String format = sdf.format(ts);
-                        iotDeviceRespVO.setLastInlineTime(format);
-                    }
-                }
-            });
-
         }
+//        for (IotDeviceRespVO iotDeviceRespVO : result.getList()) {
+//            IotProductClassifyDO iotProductClassify = iotProductClassifyService.getIotProductClassify(iotDeviceRespVO.getAssetClass());
+//            iotDeviceRespVO.setAssetClassName(iotProductClassify.getName());
+//            String deviceCode = iotDeviceRespVO.getDeviceCode();
+//            allDevice.stream().filter(e -> e.getSerialNumber().equals(deviceCode)).findFirst().ifPresent(e -> {
+//                iotDeviceRespVO.setIfInline(e.getStatus());
+//                Integer i = deviceMapper.tableIfExist(e.getSerialNumber().toLowerCase());
+//                if (i==1) {
+//                    List<DeviceVO> deviceVOS = deviceMapper.selectLastTime(e.getSerialNumber());
+//                    if (CollUtil.isNotEmpty(deviceVOS)) {
+//                        Timestamp ts = deviceVOS.get(0).getTs();
+//                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+//                        String format = sdf.format(ts);
+//                        iotDeviceRespVO.setLastInlineTime(format);
+//                    }
+//                }
+//            });
+//
+//        }
+
         return success(result);
     }
 
+
+
+
     @GetMapping("/td/test")
     @Operation(summary = "获得设备台账包含数采信息")
     @PermitAll

+ 2 - 2
yudao-module-pms/yudao-module-pms-biz/src/main/java/cn/iocoder/yudao/module/pms/controller/admin/stat/IotStaticController.java

@@ -332,9 +332,9 @@ public class IotStaticController {
             return null;
         }
         long current = System.currentTimeMillis();
-        Timestamp start = new Timestamp(current - 120 * 60 * 1000);
+        Timestamp start = new Timestamp(current - 1440 * 60 * 1000);
         Timestamp end = new Timestamp(current);
-        List<DeviceVO> deviceVOS = tdDeviceMapper.selectAllBtTimeAndIdentifier(deviceName.toLowerCase(), identifier, start, end);
+        List<DeviceVO> deviceVOS = tdDeviceMapper.selectAllBtTimeAndIdentifier(deviceName.toLowerCase(), identifier, start, end, "1m");
         deviceVOS.forEach(deviceVO->{
             deviceVO.setTimestamp(deviceVO.getTs().getTime());
             deviceVO.setValue(Double.valueOf(deviceVO.getLogValue()));

+ 5 - 0
yudao-module-pms/yudao-module-pms-biz/src/main/java/cn/iocoder/yudao/module/pms/dal/dataobject/IotDeviceDO.java

@@ -4,6 +4,7 @@ import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO;
 import com.baomidou.mybatisplus.annotation.KeySequence;
 import com.baomidou.mybatisplus.annotation.TableId;
 import com.baomidou.mybatisplus.annotation.TableName;
+import io.swagger.v3.oas.annotations.media.Schema;
 import lombok.*;
 
 import java.time.LocalDateTime;
@@ -146,4 +147,8 @@ public class IotDeviceDO extends BaseDO {
      * 设备分类BOM同步状态 1已同步 2未同步
      */
     private Integer bomSyncStatus;
+
+    private Integer ifInline;
+
+    private String lastInlineTime;
 }

+ 10 - 3
yudao-module-pms/yudao-module-pms-biz/src/main/java/cn/iocoder/yudao/module/pms/dal/mysql/IotDeviceMapper.java

@@ -4,6 +4,7 @@ import cn.iocoder.yudao.framework.common.pojo.PageResult;
 import cn.iocoder.yudao.framework.common.pojo.SortablePageParam;
 import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
 import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
+import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
 import cn.iocoder.yudao.module.pms.controller.admin.vo.IotDevicePageReqVO;
 import cn.iocoder.yudao.module.pms.controller.admin.vo.IotDeviceRespVO;
 import cn.iocoder.yudao.module.pms.dal.dataobject.IotDeviceDO;
@@ -64,9 +65,9 @@ public interface IotDeviceMapper extends BaseMapperX<IotDeviceDO> {
                 .inIfPresent(IotDeviceDO::getDeptId, deptIds));
     }
 
-    default PageResult<IotDeviceDO> selectTdPage(IotDevicePageReqVO reqVO, Collection<Long> deptIds, Collection<String> codes) {
+    default PageResult<IotDeviceDO> selectTdPage(IotDevicePageReqVO reqVO, Collection<Long> deptIds) {
         return selectPage(reqVO, new LambdaQueryWrapperX<IotDeviceDO>()
-                .inIfPresent(IotDeviceDO::getDeviceCode, codes)
+//                .inIfPresent(IotDeviceDO::getDeviceCode, codes)
                 .likeIfPresent(IotDeviceDO::getDeviceName, reqVO.getDeviceName())
                 .eqIfPresent(IotDeviceDO::getBrand, reqVO.getBrand())
                 .eqIfPresent(IotDeviceDO::getModel, reqVO.getModel())
@@ -97,7 +98,13 @@ public interface IotDeviceMapper extends BaseMapperX<IotDeviceDO> {
                 .eqIfPresent(IotDeviceDO::getTemplateJson, reqVO.getTemplateJson())
                 .eqIfPresent(IotDeviceDO::getDeviceCode, reqVO.getDeviceCode())
                 .eqIfPresent(IotDeviceDO::getBomSyncStatus, reqVO.getBomSyncStatus())
-                .orderByDesc(IotDeviceDO::getId));
+                .isNotNull(IotDeviceDO::getIfInline)
+                .orderByAsc(IotDeviceDO::getIfInline).orderByDesc(IotDeviceDO::getLastInlineTime));
+    }
+
+    default List<IotDeviceDO> selectByCodeIn(Collection<String> codes) {
+        return selectList(new LambdaQueryWrapperX<IotDeviceDO>()
+                .in(IotDeviceDO::getDeviceCode, codes));
     }
 
     /* default PageResult<IotDeviceDO> deviceAssociateBomPage(IotDevicePageReqVO reqVO, Collection<Long> deptIds) {

+ 4 - 6
yudao-module-pms/yudao-module-pms-biz/src/main/java/cn/iocoder/yudao/module/pms/dal/mysql/TDDeviceMapper.java

@@ -82,11 +82,9 @@ public interface TDDeviceMapper extends BaseMapperX<TDDeviceDO> {
     DeviceVO getYesInfo(@Param("deviceName") String tableName, @Param("colName") String colName, @Param("ts") Timestamp ts,@Param("ts1")Timestamp ts1);
 
 
-    @Select("<script>" +
-            "SELECT * FROM iot_log.device_${deviceName} " +
-            "WHERE ts between #{start} and #{end} and identity = #{identifier}" +
-            "</script>")
+//    @Select("<![CDATA[ SELECT   _WSTART AS ts, AVG(CAST(log_value AS FLOAT)) as log_value   FROM iot_log.device_${deviceName} " +
+//            "WHERE ts between #{start} and #{end} and identity = #{identifier} <![CDATA[ INTERVAL(1m) ]]>" )
     @DS("tdengine")
-    @TenantIgnore
-    List<DeviceVO> selectAllBtTimeAndIdentifier(@Param("deviceName") String tableName, @Param("identifier") String identifier,@Param("start") Timestamp start,@Param("end") Timestamp end);
+//    @TenantIgnore
+    List<DeviceVO> selectAllBtTimeAndIdentifier(@Param("deviceName") String tableName, @Param("identifier") String identifier,@Param("start") Timestamp start,@Param("end") Timestamp end, @Param(("intervalDuration")) String intervalDuration);
 }

+ 60 - 0
yudao-module-pms/yudao-module-pms-biz/src/main/java/cn/iocoder/yudao/module/pms/job/TdCronJob.java

@@ -0,0 +1,60 @@
+package cn.iocoder.yudao.module.pms.job;
+
+import cn.hutool.core.collection.CollUtil;
+import cn.iocoder.yudao.framework.quartz.core.handler.JobHandler;
+import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
+import cn.iocoder.yudao.module.pms.controller.admin.vo.DeviceVO;
+import cn.iocoder.yudao.module.pms.dal.dataobject.IotDeviceDO;
+import cn.iocoder.yudao.module.pms.dal.dataobject.yanfan.YfDeviceDO;
+import cn.iocoder.yudao.module.pms.dal.mysql.IotDeviceMapper;
+import cn.iocoder.yudao.module.pms.dal.mysql.TDDeviceMapper;
+import cn.iocoder.yudao.module.pms.service.yanfan.YfDeviceService;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+import javax.annotation.Resource;
+import java.sql.Timestamp;
+import java.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.List;
+
+@Component
+@Slf4j
+public class TdCronJob implements JobHandler {
+    @Autowired
+    private YfDeviceService yfDeviceService;
+    @Resource
+    private IotDeviceMapper iotDeviceMapper;
+    @Autowired
+    private TDDeviceMapper deviceMapper;
+
+    @Override
+    @TenantIgnore
+    public String execute(String param) throws Exception {
+        List<String> codes = new ArrayList<>();
+        List<YfDeviceDO> allDevice = yfDeviceService.getAllDevice();
+        allDevice.forEach(d -> {
+            codes.add(d.getSerialNumber());
+        });
+        List<IotDeviceDO> devices = iotDeviceMapper.selectByCodeIn(codes);
+        for (IotDeviceDO device : devices) {
+            String deviceCode = device.getDeviceCode();
+            allDevice.stream().filter(e -> e.getSerialNumber().equals(deviceCode)).findFirst().ifPresent(e -> {
+                device.setIfInline(e.getStatus());
+                Integer i = deviceMapper.tableIfExist(e.getSerialNumber().toLowerCase());
+                if (i==1) {
+                    List<DeviceVO> deviceVOS = deviceMapper.selectLastTime(e.getSerialNumber());
+                    if (CollUtil.isNotEmpty(deviceVOS)) {
+                        Timestamp ts = deviceVOS.get(0).getTs();
+                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+                        String format = sdf.format(ts);
+                        device.setLastInlineTime(format);
+                    }
+                }
+            });
+        }
+        iotDeviceMapper.updateBatch(devices);
+        return "";
+    }
+}

+ 1 - 1
yudao-module-pms/yudao-module-pms-biz/src/main/java/cn/iocoder/yudao/module/pms/service/IotDeviceServiceImpl.java

@@ -230,7 +230,7 @@ public class IotDeviceServiceImpl implements IotDeviceService {
             ids = deptService.getChildDeptIdListFromCache(pageReqVO.getDeptId());
             ids.add(pageReqVO.getDeptId());
         }
-        return iotDeviceMapper.selectTdPage(pageReqVO,ids, codes);
+        return iotDeviceMapper.selectTdPage(pageReqVO,ids);
     }
 
     /* @Override

+ 19 - 0
yudao-module-pms/yudao-module-pms-biz/src/main/resources/mapper/static/TdDeviceMapper.xml

@@ -0,0 +1,19 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+<mapper namespace="cn.iocoder.yudao.module.pms.dal.mysql.TDDeviceMapper">
+
+    <!--
+        一般情况下,尽可能使用 Mapper 进行 CRUD 增删改查即可。
+        无法满足的场景,例如说多表关联查询,才使用 XML 编写 SQL。
+        代码生成器暂时只生成 Mapper XML 文件本身,更多推荐 MybatisX 快速开发插件来生成查询。
+        文档可见:https://www.iocoder.cn/MyBatis/x-plugins/
+     -->
+    <select id="selectAllBtTimeAndIdentifier"
+            resultType="cn.iocoder.yudao.module.pms.controller.admin.vo.DeviceVO">
+        <![CDATA[  SELECT _WSTART AS ts, AVG(CAST(log_value AS FLOAT)) AS log_value
+            FROM iot_log.device_${deviceName}
+            WHERE ts BETWEEN #{start} AND #{end} AND identity = #{identifier}
+            INTERVAL(${intervalDuration})
+        ]]>
+    </select>
+</mapper>