Browse Source

修改数采定时任务

Zimo 1 tuần trước cách đây
mục cha
commit
d62349c375

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

@@ -6,7 +6,6 @@ import cn.iocoder.yudao.module.pms.controller.admin.vo.DeviceTdVO;
 import cn.iocoder.yudao.module.pms.controller.admin.vo.DeviceVO;
 import cn.iocoder.yudao.module.pms.dal.dataobject.TDDeviceDO;
 import cn.iocoder.yudao.module.pms.dal.dataobject.iotZHBD.TDLogDO;
-import cn.iocoder.yudao.module.pms.job.DeviceLastTimeVO;
 import com.baomidou.dynamic.datasource.annotation.DS;
 import com.baomidou.mybatisplus.annotation.InterceptorIgnore;
 import org.apache.ibatis.annotations.Insert;
@@ -37,16 +36,16 @@ public interface TDDeviceMapper extends BaseMapperX<TDDeviceDO> {
     List<DeviceVO> selectNew(@Param("deviceName") String tableName);
 
 
-//    @Select("SELECT LAST(ts) as ts FROM iot_log.device_${deviceName} where identity not in ('lng','lat','today_distance','distance','todayoil','totaloil','online','oil1','oil2','oil3'," +
+    //    @Select("SELECT LAST(ts) as ts FROM iot_log.device_${deviceName} where identity not in ('lng','lat','today_distance','distance','todayoil','totaloil','online','oil1','oil2','oil3'," +
 //            "'oil4','vehicle_name') ")
     @Select("SELECT ts FROM iot_log.device_${deviceName} " +
-        "where identity not in ('lng','lat','today_distance','distance','todayoil','totaloil','online','oil1','oil2','oil3','oil4','vehicle_name') " +
-        "ORDER BY ts DESC LIMIT 1")
+            "where identity not in ('lng','lat','today_distance','distance','todayoil','totaloil','online','oil1','oil2','oil3','oil4','vehicle_name') " +
+            "ORDER BY ts DESC LIMIT 1")
     @DS("tdengine")
     @TenantIgnore
     List<DeviceVO> selectLastTime(@Param("deviceName") String tableName);
 
-//    @Select("SELECT identity, LAST(log_value) as logValue FROM iot_log.device_${deviceName} where identity in ${params} group by identity")
+    //    @Select("SELECT identity, LAST(log_value) as logValue FROM iot_log.device_${deviceName} where identity in ${params} group by identity")
     @Select({
             "<script>",
             "SELECT",
@@ -167,15 +166,15 @@ public interface TDDeviceMapper extends BaseMapperX<TDDeviceDO> {
                     "        TAGS (${data.serialNumber}) VALUES ",
             "<foreach collection='data.list' item='device' separator=','>",
             "(now,"+
-            " #{device.logValue},"+
-            "#{device.isMonitor},"+
-            "#{device.logType},"+
-            "#{device.identity},"+
-            "#{device.mode},"+
-            "now,"+
-            "#{device.remark},"+
-            "#{device.batchId})"+
-            "</foreach>",
+                    " #{device.logValue},"+
+                    "#{device.isMonitor},"+
+                    "#{device.logType},"+
+                    "#{device.identity},"+
+                    "#{device.mode},"+
+                    "now,"+
+                    "#{device.remark},"+
+                    "#{device.batchId})"+
+                    "</foreach>",
             "</script>"
     })
     int batchInsert(@Param("data") TDLogDO data);
@@ -186,29 +185,5 @@ public interface TDDeviceMapper extends BaseMapperX<TDDeviceDO> {
     @TenantIgnore
     @InterceptorIgnore(tenantLine = "true")
     Integer selectRangeCount(@Param("deviceName") String tableName, @Param("identifier") String identifier,@Param("start") Timestamp start,@Param("end") Timestamp end,
-                                    @Param("max") String max,@Param("min") String min);
-
-    /**
-     * 批量获取 iot_log 库下所有 device_ 开头的表名
-     */
-    @Select("SELECT table_name FROM information_schema.ins_tables WHERE db_name = 'iot_log' AND table_name LIKE 'device_%'")
-    @DS("tdengine")
-    @TenantIgnore
-    List<String> selectAllDeviceTables();
-
-    /**
-     * 超级表批量查询所有子表的最新 ts(排除指定 identity)
-     * 注意:将 iot_log.你的超级表名 替换为实际超级表名
-     */
-
-    @Select("<script>" +
-            "SELECT tbname, LAST(ts) AS ts FROM iot_log.device_log " +
-            "WHERE tbname IN " +
-            "<foreach item='item' collection='tables' open='(' separator=',' close=')'>#{item}</foreach> " +
-            "AND identity NOT IN ('lng','lat','today_distance','distance','todayoil','totaloil','online','oil1','oil2','oil3','oil4','vehicle_name') " +
-            "GROUP BY tbname" +
-            "</script>")
-    @DS("tdengine")
-    @TenantIgnore
-    List<DeviceLastTimeVO> selectLastTimeBatch(@Param("tables") List<String> tables);
+                             @Param("max") String max,@Param("min") String min);
 }

+ 64 - 152
yudao-module-pms/yudao-module-pms-biz/src/main/java/cn/iocoder/yudao/module/pms/job/TdCronJob.java

@@ -4,12 +4,14 @@ import cn.hutool.core.collection.CollUtil;
 import cn.iocoder.yudao.framework.common.util.date.DateUtils;
 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 com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONObject;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -19,9 +21,9 @@ import org.springframework.stereotype.Component;
 import javax.annotation.Resource;
 import java.sql.Timestamp;
 import java.text.SimpleDateFormat;
-import java.util.*;
-import java.util.function.Function;
-import java.util.stream.Collectors;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Objects;
 
 @Component
 @Slf4j
@@ -38,166 +40,76 @@ public class TdCronJob implements JobHandler {
     @Override
     @TenantIgnore
     public String execute(String param) throws Exception {
+        List<String> codes = new ArrayList<>();
         List<YfDeviceDO> allDevice = yfDeviceService.getAllDevice();
-
-        // ① serialNumber(小写) -> YfDeviceDO,替代循环内 stream filter
-        Map<String, YfDeviceDO> yfDeviceMap = allDevice.stream()
-                .collect(Collectors.toMap(
-                        d -> d.getSerialNumber().toLowerCase(),
-                        Function.identity(),
-                        (a, b) -> a   // 重复时保留前者
-                ));
-
-        List<String> codes = new ArrayList<>(yfDeviceMap.keySet());
+        allDevice.forEach(d -> {
+            codes.add(d.getSerialNumber());
+        });
         List<IotDeviceDO> devices = iotDeviceMapper.selectByCodeIn(codes);
-
-        // ② 预取所有存在的 device 表名(一次查询,放入 Set)
-        Set<String> existTables = deviceMapper.selectAllDeviceTables().stream()
-                .map(String::toLowerCase)
-                .collect(Collectors.toSet());
-
-        // ③ 筛选出表存在的设备,组装子表名列表
-        List<String> targetTableNames = allDevice.stream()
-                .map(d -> "device_" + d.getSerialNumber().toLowerCase())
-                .filter(existTables::contains)
-                .collect(Collectors.toList());
-
-        // ④ 超级表一次查询全部设备的最新 ts → Map<serialNumber小写, Timestamp>
-        Map<String, Timestamp> lastTimeMap = new HashMap<>();
-        if (CollUtil.isNotEmpty(targetTableNames)) {
-            List<DeviceLastTimeVO> lastTimeList = deviceMapper.selectLastTimeBatch(targetTableNames);
-            for (DeviceLastTimeVO vo : lastTimeList) {
-                // tbname 格式为 device_xxx,去掉前缀得到 serialNumber
-                String serialNumber = vo.getTbname().toLowerCase().replace("device_", "");
-                lastTimeMap.put(serialNumber, vo.getTs());
-            }
-        }
-
-        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
         List<IotDeviceDO> deviceDOS = new ArrayList<>();
-
-        // ⑤ 循环内纯内存操作,零数据库查询
         for (IotDeviceDO device : devices) {
             String deviceCode = device.getDeviceCode();
-            YfDeviceDO e = yfDeviceMap.get(deviceCode.toLowerCase());
-            if (e == null) {
-                continue;
-            }
-
-            device.setYfDeviceId(e.getDeviceId());
-
-            // 从 Map 取最新时间,替代循环内 selectLastTime
-            Timestamp ts = lastTimeMap.get(e.getSerialNumber().toLowerCase());
-            if (ts != null) {
-                device.setLastInlineTime(sdf.format(ts));
-            }
-
-            if (e.getStatus() == 1) { // 未激活状态
-                if (StringUtils.isNotBlank(device.getLastInlineTime())) {
-                    device.setIfInline(DateUtils.checkIfFullDayDifference(device.getLastInlineTime()) ? 4 : 3);
-                } else {
-                    device.setIfInline(4);
-                }
-
-                // Redis 一次 hgetAll 替代三次 hget
-                Map<Object, Object> tslv = redisTemplate.opsForHash().entries("TSLV:" + device.getDeviceCode());
-                Object lat = tslv.get("lat");
-                Object lng = tslv.get("lng");
-                Object online = tslv.get("online");
-
-                if (Objects.nonNull(lat)) {
-                    device.setLat(Double.valueOf((String) JSON.parseObject(lat.toString()).get("value")));
+            allDevice.stream().filter(e -> e.getSerialNumber().equals(deviceCode)).findFirst().ifPresent(e -> {
+                device.setYfDeviceId(e.getDeviceId());
+                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);
+                    }
                 }
-                if (Objects.nonNull(lng)) {
-                    device.setLng(Double.valueOf((String) JSON.parseObject(lng.toString()).get("value")));
-                }
-                if (Objects.nonNull(online)) {
-                    String value = String.valueOf(JSON.parseObject(online.toString()).get("value"));
-                    device.setIfInline("true".equals(value) ? 3 : 4);
-                }
-            } else {
-                if (StringUtils.isNotBlank(device.getLastInlineTime())) {
-                    device.setIfInline(DateUtils.checkIfFullDayDifference(device.getLastInlineTime()) ? 4 : e.getStatus());
+                if (e.getStatus()==1) {//未激活状态
+
+                    if (StringUtils.isNotBlank(device.getLastInlineTime())) {
+                        try {
+                            if (DateUtils.checkIfFullDayDifference(device.getLastInlineTime())) {
+                                device.setIfInline(4);
+                            } else {
+                                device.setIfInline(3);
+                            }
+                        } catch (Exception ex) {
+                            throw new RuntimeException(ex.getMessage());
+                        }
+                    } else {
+                        device.setIfInline(4);
+                    }
+
+                    Object lat = redisTemplate.opsForHash().get("TSLV:" + device.getDeviceCode(), "lat");
+                    if (Objects.nonNull(lat)) {
+                        JSONObject jsonObject = JSON.parseObject(lat.toString());
+                        device.setLat(Double.valueOf((String) jsonObject.get("value")));
+                    }
+                    Object lng = redisTemplate.opsForHash().get("TSLV:" + device.getDeviceCode(), "lng");
+                    if (Objects.nonNull(lng)) {
+                        JSONObject jsonObject = JSON.parseObject(lng.toString());
+                        device.setLng(Double.valueOf((String) jsonObject.get("value")));
+                    }
+                    Object online = redisTemplate.opsForHash().get("TSLV:" + device.getDeviceCode(), "online");
+                    if (Objects.nonNull(online)) {
+                        JSONObject jsonObject = JSON.parseObject(online.toString());
+                        String value = String.valueOf(jsonObject.get("value"));
+                        device.setIfInline("true".equals(value)?3:4);
+                    }
                 } else {
-                    device.setIfInline(4);
+                    if (StringUtils.isNotBlank(device.getLastInlineTime())) {
+                        if (DateUtils.checkIfFullDayDifference(device.getLastInlineTime())) {
+                            device.setIfInline(4);
+                        } else {
+                            device.setIfInline(e.getStatus());
+                        }
+                    } else {
+                        device.setIfInline(4);
+                    }
                 }
-            }
-
-            deviceDOS.add(device);
+//                iotDeviceMapper.updateTdCron(device);
+                deviceDOS.add(device);
+            });
         }
-
         iotDeviceMapper.updateBatch(deviceDOS);
         return "";
-//        List<String> codes = new ArrayList<>();
-//        List<YfDeviceDO> allDevice = yfDeviceService.getAllDevice();
-//        allDevice.forEach(d -> {
-//            codes.add(d.getSerialNumber());
-//        });
-//        List<IotDeviceDO> devices = iotDeviceMapper.selectByCodeIn(codes);
-//        List<IotDeviceDO> deviceDOS = new ArrayList<>();
-//        for (IotDeviceDO device : devices) {
-//            String deviceCode = device.getDeviceCode();
-//            allDevice.stream().filter(e -> e.getSerialNumber().equals(deviceCode)).findFirst().ifPresent(e -> {
-//                device.setYfDeviceId(e.getDeviceId());
-//                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);
-//                    }
-//                }
-//                if (e.getStatus()==1) {//未激活状态
-//
-//                    if (StringUtils.isNotBlank(device.getLastInlineTime())) {
-//                        try {
-//                            if (DateUtils.checkIfFullDayDifference(device.getLastInlineTime())) {
-//                                device.setIfInline(4);
-//                            } else {
-//                                device.setIfInline(3);
-//                            }
-//                        } catch (Exception ex) {
-//                            throw new RuntimeException(ex.getMessage());
-//                        }
-//                    } else {
-//                        device.setIfInline(4);
-//                    }
-//
-//                    Object lat = redisTemplate.opsForHash().get("TSLV:" + device.getDeviceCode(), "lat");
-//                    if (Objects.nonNull(lat)) {
-//                        JSONObject jsonObject = JSON.parseObject(lat.toString());
-//                        device.setLat(Double.valueOf((String) jsonObject.get("value")));
-//                    }
-//                    Object lng = redisTemplate.opsForHash().get("TSLV:" + device.getDeviceCode(), "lng");
-//                    if (Objects.nonNull(lng)) {
-//                        JSONObject jsonObject = JSON.parseObject(lng.toString());
-//                        device.setLng(Double.valueOf((String) jsonObject.get("value")));
-//                    }
-//                    Object online = redisTemplate.opsForHash().get("TSLV:" + device.getDeviceCode(), "online");
-//                    if (Objects.nonNull(online)) {
-//                        JSONObject jsonObject = JSON.parseObject(online.toString());
-//                        String value = String.valueOf(jsonObject.get("value"));
-//                        device.setIfInline("true".equals(value)?3:4);
-//                    }
-//                } else {
-//                        if (StringUtils.isNotBlank(device.getLastInlineTime())) {
-//                            if (DateUtils.checkIfFullDayDifference(device.getLastInlineTime())) {
-//                                device.setIfInline(4);
-//                            } else {
-//                                device.setIfInline(e.getStatus());
-//                            }
-//                        } else {
-//                            device.setIfInline(4);
-//                        }
-//                    }
-////                iotDeviceMapper.updateTdCron(device);
-//                deviceDOS.add(device);
-//            });
-//        }
-//        iotDeviceMapper.updateBatch(deviceDOS);
-//        return "";
     }