<?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.agri.system.mapper.SysDtuDataMapper">
    
    <resultMap type="SysDtuData" id="SysDtuDataResult">
        <result property="id"    column="id"    />
        <result property="imei"    column="imei"    />
        <result property="ts"    column="ts"    />
        <result property="time"    column="time"    />
        <result property="temp1"    column="temp1"    />
        <result property="humi1"    column="humi1"    />
        <result property="temp2"    column="temp2"    />
        <result property="humi2"    column="humi2"    />
        <result property="temp3"    column="temp3"    />
        <result property="humi3"    column="humi3"    />
        <result property="temp4"    column="temp4"    />
        <result property="humi4"    column="humi4"    />
        <result property="raw"    column="raw"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
    </resultMap>

    <sql id="selectSysDtuDataVo">
        select id, imei, ts, time, temp1, humi1, temp2, humi2, temp3, humi3, temp4, humi4, raw, create_by, create_time from sys_dtu_data
    </sql>

    <select id="selectSysDtuDataList" parameterType="SysDtuData" resultMap="SysDtuDataResult">
        <include refid="selectSysDtuDataVo"/>
        <where>  
            <if test="imei != null  and imei != ''"> and imei = #{imei}</if>
            <if test="ts != null "> and ts = #{ts}</if>
            <if test="time != null "> and time = #{time}</if>
            <if test="temp1 != null "> and temp1 = #{temp1}</if>
            <if test="humi1 != null "> and humi1 = #{humi1}</if>
            <if test="temp2 != null "> and temp2 = #{temp2}</if>
            <if test="humi2 != null "> and humi2 = #{humi2}</if>
            <if test="temp3 != null "> and temp3 = #{temp3}</if>
            <if test="humi3 != null "> and humi3 = #{humi3}</if>
            <if test="temp4 != null "> and temp4 = #{temp4}</if>
            <if test="humi4 != null "> and humi4 = #{humi4}</if>
            <if test="raw != null  and raw != ''"> and raw = #{raw}</if>
        </where>
    </select>
    
    <select id="selectSysDtuDataById" parameterType="Long" resultMap="SysDtuDataResult">
        <include refid="selectSysDtuDataVo"/>
        where id = #{id}
    </select>

    <insert id="insertSysDtuData" parameterType="SysDtuData" useGeneratedKeys="true" keyProperty="id">
        insert into sys_dtu_data
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="imei != null and imei != ''">imei,</if>
            <if test="ts != null">ts,</if>
            <if test="time != null">time,</if>
            <if test="temp1 != null">temp1,</if>
            <if test="humi1 != null">humi1,</if>
            <if test="temp2 != null">temp2,</if>
            <if test="humi2 != null">humi2,</if>
            <if test="temp3 != null">temp3,</if>
            <if test="humi3 != null">humi3,</if>
            <if test="temp4 != null">temp4,</if>
            <if test="humi4 != null">humi4,</if>
            <if test="raw != null">raw,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="imei != null and imei != ''">#{imei},</if>
            <if test="ts != null">#{ts},</if>
            <if test="time != null">#{time},</if>
            <if test="temp1 != null">#{temp1},</if>
            <if test="humi1 != null">#{humi1},</if>
            <if test="temp2 != null">#{temp2},</if>
            <if test="humi2 != null">#{humi2},</if>
            <if test="temp3 != null">#{temp3},</if>
            <if test="humi3 != null">#{humi3},</if>
            <if test="temp4 != null">#{temp4},</if>
            <if test="humi4 != null">#{humi4},</if>
            <if test="raw != null">#{raw},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
         </trim>
    </insert>

    <update id="updateSysDtuData" parameterType="SysDtuData">
        update sys_dtu_data
        <trim prefix="SET" suffixOverrides=",">
            <if test="imei != null and imei != ''">imei = #{imei},</if>
            <if test="ts != null">ts = #{ts},</if>
            <if test="time != null">time = #{time},</if>
            <if test="temp1 != null">temp1 = #{temp1},</if>
            <if test="humi1 != null">humi1 = #{humi1},</if>
            <if test="temp2 != null">temp2 = #{temp2},</if>
            <if test="humi2 != null">humi2 = #{humi2},</if>
            <if test="temp3 != null">temp3 = #{temp3},</if>
            <if test="humi3 != null">humi3 = #{humi3},</if>
            <if test="temp4 != null">temp4 = #{temp4},</if>
            <if test="humi4 != null">humi4 = #{humi4},</if>
            <if test="raw != null">raw = #{raw},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteSysDtuDataById" parameterType="Long">
        delete from sys_dtu_data where id = #{id}
    </delete>

    <delete id="deleteSysDtuDataByIds" parameterType="String">
        delete from sys_dtu_data where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>


    <select id="findDtuDataByInfo" parameterType="SysDtuData" resultMap="SysDtuDataResult">
        <include refid="selectSysDtuDataVo"/>
        <where>
            <if test="imei != null  and imei != ''"> and imei = #{imei}</if>
            <if test="ts != null "> and ts = #{ts}</if>
            <if test="time != null "> and time = #{time}</if>
            <if test="temp1 != null "> and temp1 = #{temp1}</if>
            <if test="humi1 != null "> and humi1 = #{humi1}</if>
            <if test="temp2 != null "> and temp2 = #{temp2}</if>
            <if test="humi2 != null "> and humi2 = #{humi2}</if>
            <if test="temp3 != null "> and temp3 = #{temp3}</if>
            <if test="humi3 != null "> and humi3 = #{humi3}</if>
            <if test="temp4 != null "> and temp4 = #{temp4}</if>
            <if test="humi4 != null "> and humi4 = #{humi4}</if>
            <if test="raw != null  and raw != ''"> and raw = #{raw}</if>
        </where>
        order by `time` desc limit 1
    </select>
    <select id="getLastDtuDataByImeiList" resultType="java.util.Map">
        SELECT
            id, imei, temp1, temp2, temp3, temp4, time
        FROM
            sys_dtu_data
        WHERE id IN (
            SELECT MAX(id) AS id
            FROM sys_dtu_data
            WHERE imei IN
            <foreach collection="imeiList" item="imei" open="(" separator="," close=")">
                #{imei}
            </foreach>
            AND time >= DATE_SUB(NOW(), INTERVAL 90 SECOND)
            GROUP BY imei
        )
    </select>
    <select id="getHistoryData" resultType="com.agri.system.domain.SysDtuData">
        SELECT
            t.*
        FROM (
                 SELECT
                     `imei`,
                     `time`,
                     temp1,
                     humi1,
                     temp2,
                     humi2,
                     temp3,
                     humi3,
                     temp4,
                     humi4,
                     ROW_NUMBER() OVER (
				PARTITION BY DATE_FORMAT(`time`, '%Y-%m-%d %H:%i:00') - INTERVAL (MINUTE(`time`) % 5) MINUTE
				ORDER BY `time` DESC
				) AS rn
                 FROM
                     sys_dtu_data
                 WHERE
                     imei = #{imei}
                   AND
                     `time` &gt;= #{startTime}
                   AND
                     `time` &lt;= #{endTime}
             ) t
        WHERE t.rn = 1
        ORDER BY t.time ASC
    </select>
</mapper>