2026-06-26 20b96473f2520590a0dca6b775b81e3ea06a77a0
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
<?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.iot.dal.tdengine.IotDeviceMessageMapper">
 
    <update id="createSTable">
        CREATE STABLE IF NOT EXISTS device_message (
            ts TIMESTAMP,
            id NCHAR(50),
            report_time TIMESTAMP,
            tenant_id BIGINT,
            server_id NCHAR(50),
            upstream BOOL,
            reply BOOL,
            identifier NCHAR(100),
            request_id NCHAR(50),
            method NCHAR(100),
            params VARCHAR(8192),
            data VARCHAR(8192),
            code INT,
            msg NCHAR(256)
        ) TAGS (
            device_id BIGINT
        )
    </update>
 
    <select id="showSTable" resultType="String">
        SHOW STABLES LIKE 'device_message'
    </select>
 
    <insert id="insert">
        INSERT INTO device_message_${deviceId} (
            ts, id, report_time, tenant_id, server_id,
            upstream, reply, identifier, request_id, method,
            params, data, code, msg
        )
        USING device_message
        TAGS (#{deviceId})
        VALUES (
            #{ts}, #{id}, #{reportTime}, #{tenantId},  #{serverId},
            #{upstream}, #{reply}, #{identifier}, #{requestId}, #{method}, 
            #{params}, #{data}, #{code}, #{msg}
        )
    </insert>
 
    <select id="selectPage" resultType="cn.iocoder.yudao.module.iot.dal.dataobject.device.IotDeviceMessageDO">
        SELECT ts, id, report_time, tenant_id, server_id,
               upstream, reply, identifier, request_id, method, 
               params, data, code, msg
        FROM device_message_${reqVO.deviceId}
        <where>
            <if test="reqVO.method != null and reqVO.method != ''">
                AND method = #{reqVO.method}
            </if>
            <if test="reqVO.upstream != null">
                AND upstream = #{reqVO.upstream}
            </if>
            <if test="reqVO.reply != null">
                AND reply = #{reqVO.reply}
            </if>
            <if test="reqVO.identifier != null and reqVO.identifier != ''">
                AND identifier = #{reqVO.identifier}
            </if>
        </where>
        ORDER BY ts DESC
    </select>
 
    <select id="selectListByRequestIdsAndReply" resultType="cn.iocoder.yudao.module.iot.dal.dataobject.device.IotDeviceMessageDO">
        SELECT ts, id, report_time, tenant_id, server_id,
               upstream, reply, identifier, request_id, method, 
               params, data, code, msg
        FROM device_message_${deviceId}
        WHERE reply = #{reply}
        AND request_id IN
        <foreach collection="requestIds" item="requestId" open="(" close=")" separator=",">
            #{requestId}
        </foreach>
        ORDER BY ts DESC
    </select>
 
    <select id="selectCountByCreateTime" resultType="Long">
        SELECT COUNT(*)
        FROM device_message
        <where>
            <if test="createTime != null">
                AND ts >= #{createTime}
            </if>
        </where>
    </select>
 
    <select id="selectDeviceMessageCountGroupByDate" resultType="java.util.Map">
        SELECT
            TIMETRUNCATE(ts, 1h) AS time,
            SUM(CASE WHEN upstream = true THEN 1 ELSE 0 END) AS upstream_count,
            SUM(CASE WHEN upstream = false THEN 1 ELSE 0 END) AS downstream_count
        FROM device_message
        <where>
            <if test="startTime != null">
                AND ts >= #{startTime}
            </if>
            <if test="endTime != null">
                AND ts &lt;= #{endTime}
            </if>
        </where>
        GROUP BY TIMETRUNCATE(ts, 1h)
    </select>
 
</mapper>