<?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.usky.backend.mapper.DataRealTimeMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.usky.backend.domain.DataRealTime">
        <id column="id" property="id" />
        <result column="device_id" property="deviceId" />
        <result column="product_code" property="productCode" />
        <result column="device_type" property="deviceType" />
        <result column="attribute_name" property="attributeName" />
        <result column="attribute_data" property="attributeData" />
        <result column="data_time" property="dataTime" />
        <result column="insert_time" property="insertTime" />
    </resultMap>
<!--    <insert id="insertHistoryTable" >-->
<!--        insert into ${tableName}   ( device_id, device_type,  attribute_name, attribute_data, data_time, insert_time)-->
<!--        values (#{dataInfo.deviceId}, #{dataInfo.deviceType},  #{dataInfo.name},  #{dataInfo.value},  #{dataInfo.timestamp},  #{curTime})-->
<!--    </insert>-->
    <select id="QueryHistoryData" resultType="com.usky.backend.domain.MetricItemVo">
        SELECT
        attribute_data as value,data_time as timestamp
        FROM
        ${tableName}
        <where>
            <if test="deviceId != null">
                and device_id = #{deviceId}
            </if>
            <if test="metric != null">
                and attribute_name = #{metric}
            </if>
            <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
                and  data_time BETWEEN  #{startTime} AND  #{endTime}
            </if>
        </where>
    </select>

    <select id="QueryDeviceTypeAbbrevia" resultType="java.lang.String">
        select
            type_abbrevia
        from data_config
        <where>
            <if test="1 == 1">
                and type_code = #{deviceType}
            </if>
        </where>
    </select>
    <select id="QueryEachHistoryTotalData" resultType="com.usky.backend.domain.DataHistoryTotalVO">
        select device_id as deviceId,attribute_name as attributeName,sum(attribute_data) as totalValue
        FROM ${tableName}
        WHERE data_time  BETWEEN  #{startTime} AND  #{endTime} AND product_code = #{productCode}
        <if test="deviceIdList != null and deviceIdList.size() > 0">
            AND device_id in
            <foreach item="item" collection="deviceIdList" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="attributeNameList != null and attributeNameList.size() > 0">
            AND attribute_name in
            <foreach item="item" collection="attributeNameList" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        group by device_id,attribute_name
    </select>
    <select id="QueryTotalHistoryTotalData" resultType="com.usky.backend.domain.DataHistoryTotalVO">
        select "all" as deviceId,attribute_name as attributeName,sum(attribute_data) as totalValue
        FROM ${tableName}
        WHERE data_time  BETWEEN  #{startTime} AND  #{endTime} AND product_code = #{productCode}
        <if test="attributeNameList != null and attributeNameList.size() > 0">
            AND attribute_name in
            <foreach item="item" collection="attributeNameList" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        group by attribute_name
    </select>
    <select id="QueryEachHistoryAvrgData" resultType="com.usky.backend.domain.DataHistoryAvergerVO">
        SELECT a.data_date as dataDate,a.device_id as deviceId, a.attribute_name as attributeName ,avg(a.attribute_data) as avrg
        from (select product_code,device_id,attribute_name,attribute_data,left(data_time,#{leftLen}) as data_date FROM ${tableName} WHERE data_time  BETWEEN  #{startTime} AND  #{endTime}
        <if test="productCode != null">
            AND product_code = #{productCode}
        </if>
        <if test="deviceIdList != null and deviceIdList.size() > 0">
            AND device_id in
            <foreach item="item" collection="deviceIdList" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="attributeNameList != null and attributeNameList.size() > 0">
            AND attribute_name in
            <foreach item="item" collection="attributeNameList" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>) a
        GROUP BY a.data_date,a.device_id,a.attribute_name;
    </select>
    <select id="QueryTotalOptHistoryAvrgData" resultType="com.usky.backend.domain.DataHistoryAvergerVO">
        SELECT a.data_date as dataDate,'所选房间' as deviceId,a.attribute_name as attributeName ,avg(a.attribute_data) as avrg
        FROM (SELECT product_code,attribute_name,attribute_data,LEFT(data_time,#{leftLen}) AS data_date FROM ${tableName} WHERE data_time  BETWEEN  #{startTime} AND  #{endTime} AND product_code = #{productCode}
        AND device_id != 'weather0001'
        <if test="deviceIdList != null and deviceIdList.size() > 0">
            AND device_id in
            <foreach item="item" collection="deviceIdList" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="attributeNameList != null and attributeNameList.size() > 0">
            AND attribute_name in
            <foreach item="item" collection="attributeNameList" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>) a
        GROUP BY a.data_date,a.attribute_name;
    </select>
    <select id="QueryTotalHistoryAvrgData" resultType="com.usky.backend.domain.DataHistoryAvergerVO">
        SELECT a.data_date as dataDate,'所有房间' as deviceId,a.attribute_name as attributeName ,avg(a.attribute_data) as avrg
        FROM (SELECT product_code,attribute_name,attribute_data,LEFT(data_time,#{leftLen}) AS data_date FROM ${tableName} WHERE data_time  BETWEEN  #{startTime} AND  #{endTime} AND product_code = #{productCode}
        AND device_id != 'weather0001'
        <if test="attributeNameList != null and attributeNameList.size() > 0">
            AND attribute_name in
            <foreach item="item" collection="attributeNameList" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>) a
        GROUP BY a.data_date,a.attribute_name;
    </select>

</mapper>
