root
2023-09-01 00771647e19e18316e1a4a67a6fcb164274ae7d5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?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="com.ruoyi.iot.tdengine.dao.TDDeviceLogDAO">
 
    <resultMap type="com.ruoyi.iot.model.MonitorModel" id="MonitorResult">
        <result property="value"    column="log_value"    />
        <result property="time"    column="ts"    />
    </resultMap>
 
    <resultMap type="com.ruoyi.iot.domain.DeviceLog" id="DeviceLogResult">
        <result property="logType"    column="log_type"    />
        <result property="logValue"    column="log_value"    />
        <result property="mode"    column="mode"    />
        <result property="deviceId"    column="device_id"    />
        <result property="deviceName"    column="device_name"    />
        <result property="serialNumber"    column="serial_number"    />
        <result property="identity"    column="identity"    />
        <result property="createBy"    column="create_by"    />
        <result property="isMonitor"    column="is_monitor"    />
        <result property="createTime"    column="ts"    />
        <result property="userId"    column="user_id"    />
        <result property="userName"    column="user_name"    />
        <result property="tenantId"    column="tenant_id"    />
        <result property="tenantName"    column="tenant_name"    />
        <result property="remark"    column="remark"    />
    </resultMap>
 
    <update id="createDB">
        create database if not exists  ${database};
    </update>
 
    <update id="createSTable">
        create STABLE  if not exists ${database}.device_log
        (ts timestamp,
        `log_value` BINARY(100),
        is_monitor TINYINT,
        log_type TINYINT,
        `identity` BINARY(100),
        mode TINYINT,
        remark BINARY(500))
 
        TAGS(
        device_id BIGINT,
        serial_number BINARY(50),
        user_id BIGINT,
        tenant_Id BIGINT,
        user_name BINARY(100),
        tenant_name BINARY(100),
        device_name BINARY(100));
    </update>
 
    <insert id="save" parameterType="com.ruoyi.iot.domain.DeviceLog">
        INSERT INTO ${database}.device_${device.serialNumber} USING  device_log
        TAGS (#{device.deviceId},
            #{device.serialNumber},
            #{device.userId},
            #{device.tenantId},
            #{device.userName},
            #{device.tenantName},
            #{device.deviceName})
        VALUES (now,
            #{device.logValue},
            #{device.isMonitor},
            #{device.logType},
            #{device.identity},
            #{device.mode},
            #{device.remark});
    </insert>
 
    <delete id="deleteDeviceLogByDeviceNumber" parameterType="com.ruoyi.iot.domain.DeviceLog">
        DROP TABLE IF EXISTS ${database}.device_${serialNumber};
    </delete>
 
    <select id="selectPropertyLogCount" parameterType="com.ruoyi.iot.domain.Device" resultType="Long">
        select count(mode) as propertyCount
        from ${database}.device_log
        <where>
            <if test="1==1"> and log_type=1</if>
            <if test="device.userId != null and device.userId != 0"> and user_id = #{device.userId}</if>
            <if test="device.tenantId != null and device.tenantId != 0"> and tenant_id = #{device.tenantId}</if>
        </where>
    </select>
 
    <select id="selectFunctionLogCount" parameterType="com.ruoyi.iot.domain.Device" resultType="Long">
        select count(mode) as functionCount
        from ${database}.device_log
        <where>
            <if test="1==1"> and log_type=2</if>
            <if test="device.userId != null and device.userId != 0"> and user_id = #{device.userId}</if>
            <if test="device.tenantId != null and device.tenantId != 0"> and tenant_id = #{device.tenantId}</if>
        </where>
    </select>
 
    <select id="selectEventLogCount" parameterType="com.ruoyi.iot.domain.Device" resultType="Long">
        select count(mode) as eventCount
        from ${database}.device_log
        <where>
            <if test="1==1"> and log_type=3</if>
            <if test="device.userId != null and device.userId != 0"> and user_id = #{device.userId}</if>
            <if test="device.tenantId != null and device.tenantId != 0"> and tenant_id = #{device.tenantId}</if>
        </where>
    </select>
 
    <select id="selectMonitorLogCount" parameterType="com.ruoyi.iot.domain.Device" resultType="Long">
        select count(mode) as monitorCount
        from ${database}.device_log
        <where>
            <if test="1==1"> and log_type=1 and is_monitor=1</if>
            <if test="device.userId != null and device.userId != 0"> and user_id = #{device.userId}</if>
            <if test="device.tenantId != null and device.tenantId != 0"> and tenant_id = #{device.tenantId}</if>
        </where>
    </select>
 
    <select id="selectMonitorList" parameterType="com.ruoyi.iot.domain.DeviceLog" resultMap="MonitorResult">
        select log_value, ts from ${database}.device_log
        <where>
             is_monitor=1
            <if test="device.deviceId!=null"> and device_id = #{device.deviceId} </if>
            <if test="device.identity != null  and device.identity != ''"> and identity like  #{device.identity}</if>
            <if test="device.beginTime != null and device.beginTime != '' and device.endTime != null and device.endTime != ''"> and ts between #{device.beginTime} and #{device.endTime}</if>
        order by ts desc
        limit #{device.total}
        </where>
    </select>
 
    <select id="selectDeviceLogList" parameterType="com.ruoyi.iot.domain.DeviceLog" resultMap="DeviceLogResult">
        select * from ${database}.device_log
        <where>
            is_monitor!=1
            <if test="device.deviceId!=null"> and device_id = #{device.deviceId} </if>
            <if test="device.logType != null "> and log_type = #{device.logType}</if>
            <if test="device.identity != null  and device.identity != ''"> and identity like #{device.identity}</if>
        </where>
        order by ts desc
    </select>
 
</mapper>