<?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.SlopeDataMapper">

    <resultMap id="BaseResultMap" type="com.sckw.slope.detection.model.dos.tdengine.SlopeData">
        <!--@mbg.generated-->
        <!--@Table slope_data-->
        <id column="ts" jdbcType="TIMESTAMP" property="ts"/>
        <result column="rawTs" jdbcType="TIMESTAMP" property="rawId"/>
        <result column="tslverId" jdbcType="TINYINT" property="tslverId"/>
        <result column="guid" jdbcType="VARCHAR" property="guid"/>
        <result column="line" jdbcType="VARCHAR" property="line"/>
        <result column="val" jdbcType="VARCHAR" property="val"/>
        <result column="createTime" jdbcType="TIMESTAMP" property="createTime"/>
<!--        <result column="msgId" jdbcType="VARCHAR" property="msgId"/>-->
<!--        <result column="name" jdbcType="TINYINT" property="name"/>-->
    </resultMap>

    <select id="selectList" resultType="com.sckw.slope.detection.model.vo.SlopeDataVo">
        <!--        ts, raw_id , device_id, item_name as itemName, item_value as itemValue, create_time as createTime-->
        SELECT ts,
               raw_ts      as rawId,
               tslver_id   as tslverId,
               guid,
               line,
               val,
               create_time as createTime,
               msg_id      as msgId,
               name,
               json_text   as jsonText
        FROM devicesv2.device_67
    </select>

    <sql id="Base_List">
        ts,
        raw_ts      as rawId,
        tslver_id   as tslverId,
        line,
        val
    </sql>

    <select id="selectListByLine" resultType="com.sckw.slope.detection.model.dos.tdengine.SlopeData">
        SELECT
        <include refid="Base_List">
        </include>
        FROM devicesv2.device_${snCode}
        where line = #{item} order by ts desc limit 1
    </select>

    <select id="selectListByTwoLine" resultType="com.sckw.slope.detection.model.dos.tdengine.SlopeData">
        SELECT
        <include refid="Base_List">
        </include>
        FROM devicesv2.device_#{snCode}
        where line = #{item}
        order by ts desc limit 2
    </select>

    <select id="selectListLineByMsgId" resultType="com.sckw.slope.detection.model.dos.tdengine.SlopeData">
        SELECT
        <include refid="Base_List">
        </include>
        FROM devicesv2.device_#{snCode}
        where line = #{item}
        and msg_id = #{msgId}
        order by ts desc limit 1
    </select>

    <select id="selectListByLineOrderByCreateTime" resultType="com.sckw.slope.detection.model.dos.tdengine.SlopeData">
        SELECT
        <include refid="Base_List">
        </include>
        FROM devicesv2.device_#{snCode}
        where line = #{item}
        order by ts desc limit 2
    </select>

    <insert id="insertData" parameterType="com.sckw.slope.detection.model.dos.tdengine.SlopeData">
        <!--        insert into devices.devicesv2202_2021-->
        insert into devicesv2.devices_#{slopeData.serialNumber} using devicesv2.devices (#{slopeData.tagsTitle}) tags (#{slopeData.tags})
            (ts, raw_ts, tslver_id, guid, line, val, create_time)
        VALUES (#{slopeData.ts}, #{slopeData.rawId},
        #{slopeData.tslverId}, #{slopeData.guid},
        #{slopeData.line}, #{slopeData.val}, #{slopeData.createTime})
        <!--        VALUES('2023-12-12 00:00:00.100', '2023-12-12 00:00:00.100', 111, '111', '1111', '111', '2023-12-12 00:00:00.100');-->
    </insert>

    <select id="selectLineList" resultType="com.sckw.slope.detection.model.dos.tdengine.SlopeData">
        SELECT
        <include refid="Base_List">
        </include>
        FROM devicesv2.device_${snCode}
        where line = #{item} and raw_ts &gt;= #{dateStart} and raw_ts &lt;= #{dateEnd} order by ts desc
    </select>

    <select id="selectLineListByArray" resultType="com.sckw.slope.detection.model.dos.tdengine.SlopeData">
        SELECT
        <include refid="Base_List">
        </include>
        FROM devicesv2.device_${snCode}
        where  raw_ts &gt;= #{dateStart} and raw_ts &lt;= #{dateEnd}

        <if test="parts != null">
            and line in
            <foreach collection="parts" index="index" separator="," open="(" close=")" item="parts">
                #{parts}
            </foreach>
        </if>

        order by ts desc limit 0,1000
    </select>

    <select id="selectLineListByList" resultType="com.sckw.slope.detection.model.dos.tdengine.SlopeData">
        SELECT
        line,
        val,
        msg_id      as msgId,
        ts,
        raw_ts      as rawId
        FROM devicesv2.device_#{snCode}
        where  raw_ts &gt;= #{dateStart} and raw_ts &lt;= #{dateEnd}

        <if test="parts != null">
            and line in
            <foreach collection="parts" index="index" separator="," open="(" close=")" item="parts">
                #{parts}
            </foreach>
        </if>

        order by ts desc limit 0,200
    </select>

    <select id="selectTableIsExit">
        select * from information_schema.ins_tables a where a.db_name = 'devices' and a.table_name = #{name}
    </select>
    <insert id="createTable">
        CREATE TABLE #{name} (
        ts TIMESTAMP,
        raw_id TIMESTAMP,
        tslver_id TINYINT(4),
        guid VARCHAR(64),
        line VARCHAR(16),
        val VARCHAR(16),
        create_time TIMESTAMP
        )
    </insert>

    <select id="selectListByMaster" resultType="com.sckw.slope.detection.model.vo.SlopeDataVo">
        SELECT
        <include refid="Base_List">
        </include>
        FROM devicesv2.devices
<!--        <if test="(page != null and page !='') and ( pageSize != null and pageSize != '')">-->
        <if test="page != null and pageSize != null ">
            limit #{page},#{pageSize}
        </if>

<!--        SELECT-->
<!--        t1.ts,-->
<!--        t1.raw_id      as rawId,-->
<!--        t1.tslver_id   as tslverId,-->
<!--        t1.guid,-->
<!--        t1.line,-->
<!--        t1.val,-->
<!--        t1.create_time as createTime,-->
<!--        t2.json_text as jsonText-->
<!--        FROM devices.slope_data AS t1-->
<!--        LEFT JOIN devices.slope_raw AS t2 ON t1.guid  = t2.guid-->
    </select>

    <select id="selectListByMasterCount" resultType="com.sckw.slope.detection.model.vo.SlopeDataVo">
        SELECT
        <include refid="Base_List">
        </include>
        FROM devicesv2.devices
    </select>

    <select id="selectLastData" resultType="com.sckw.slope.detection.model.dos.tdengine.SlopeData">
        SELECT
        <include refid="Base_List">
        </include>
        FROM devicesv2.device_${deviceCode}
        order by ts desc limit 1
    </select>
</mapper>