<?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.sckw.slope.detection.dao.tdengine.DevicesMapper">
    <resultMap id="BaseResultMap" type="com.sckw.slope.detection.model.dos.tdengine.Devices">
        <!--@mbg.generated-->
        <!--@Table devices-->
        <id column="ts" jdbcType="TIMESTAMP" property="ts"/>
        <result column="tslver_id" jdbcType="INTEGER" property="tslverId"/>
        <result column="line" jdbcType="VARCHAR" property="line"/>
        <result column="val" jdbcType="VARCHAR" property="val"/>
        <result column="msg_id" jdbcType="VARCHAR" property="msgId"/>
        <result column="raw_ts" jdbcType="TIMESTAMP" property="rawTs"/>
        <result column="guid" jdbcType="VARCHAR" property="guid"/>
    </resultMap>
    <sql id="Base_Column_List">
        <!--@mbg.generated-->
        ts,
        tslver_id,
        line,
        val,
        msg_id,
        raw_ts,
        guid
    </sql>
    <sql id="Base_List">
        ts,
        raw_ts    as rawId,
        tslver_id as tslverId,
        line,
        val
    </sql>
    <select id="selectLastData" resultType="com.sckw.slope.detection.model.dos.tdengine.Devices">
        SELECT
        <include refid="Base_List">
        </include>
        FROM devicesv2.device_${deviceCode}
        order by ts desc limit 1
    </select>

    <select id="selectLastDataByCodeAndItemName" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_List">
        </include>
        FROM devicesv2.device_${deviceCode}
        <where>
            <if test="itemName != null and itemName != ''">
                and line = #{itemName}
            </if>
        </where>
        order by ts desc limit 1
    </select>
</mapper>