IotDeviceMessageMapper.xml 3.66 KB
<?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,
            request_id NCHAR(50),
            method NCHAR(100),
            params NCHAR(2048),
            data NCHAR(2048),
            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, request_id, method, params,
            data, code, msg
        )
        USING device_message
        TAGS (#{deviceId})
        VALUES (
            NOW, #{id}, #{reportTime}, #{tenantId},  #{serverId},
            #{upstream}, #{reply}, #{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, 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>
        </where>
        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="selectDeviceLogUpCountByHour" resultType="java.util.Map">
        SELECT
            TIMETRUNCATE(ts, 1h) as time,
            COUNT(*) as data
        FROM
        <choose>
            <when test="deviceId != null">
                device_message_${deviceId}
            </when>
            <otherwise>
                device_message
            </otherwise>
        </choose>
        <where>
            <if test="startTime != null">
                AND ts >= #{startTime}
            </if>
            <if test="endTime != null">
                AND ts &lt;= #{endTime}
            </if>
            AND upstream = true
        </where>
        GROUP BY TIMETRUNCATE(ts, 1h)
        ORDER BY time ASC
    </select>

    <select id="selectDeviceLogDownCountByHour" resultType="java.util.Map">
        SELECT
            TIMETRUNCATE(ts, 1h) as time,
            COUNT(*) as data
        FROM
        <choose>
            <when test="deviceId != null">
                device_message_${deviceId}
            </when>
            <otherwise>
                device_message
            </otherwise>
        </choose>
        <where>
            <if test="startTime != null">
                AND ts >= #{startTime}
            </if>
            <if test="endTime != null">
                AND ts &lt;= #{endTime}
            </if>
            AND upstream = false
        </where>
        GROUP BY TIMETRUNCATE(ts, 1h)
        ORDER BY time ASC
    </select>

</mapper>