<?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="vip.xiaonuo.modular.env.envdata.mapper.EnvDataMapper">

    <select id="listToday" resultType="vip.xiaonuo.modular.env.envdata.entity.vo.EnvDataListVo">
        SELECT tem_handle_value AS 'temp', humi_handle_value AS 'hum', update_time AS 'createTime'
        FROM `env_data`
        WHERE pigpen_id = #{pigpenId} AND DATE(update_time) = CURRENT_DATE AND is_show = 0
        ORDER BY update_time ASC
    </select>

    <select id="listWeek" resultType="vip.xiaonuo.modular.env.envdata.entity.vo.EnvDataListVo">
        SELECT
            DATE(update_time) AS 'createTime',
            ROUND(AVG(tem_handle_value), 2) AS 'temp',
            ROUND(AVG(humi_handle_value), 2) AS 'hum'
        FROM
            `env_data`
        WHERE
            pigpen_id = #{pigpenId}
            AND YEARWEEK(update_time, 1) = YEARWEEK(CURDATE(), 1)
            AND is_show = 0
        GROUP BY
            DATE(update_time)
        ORDER BY
            DATE(update_time) ASC;
    </select>

    <select id="listMonth" resultType="vip.xiaonuo.modular.env.envdata.entity.vo.EnvDataListVo">
        SELECT
            DATE(update_time) AS 'createTime',
            ROUND(AVG(tem_handle_value), 2) AS 'temp',
            ROUND(AVG(humi_handle_value), 2) AS 'hum'
        FROM
            `env_data`
        WHERE
            pigpen_id = #{pigpenId}
            AND MONTH(update_time) = MONTH(CURDATE())
            AND YEAR(update_time) = YEAR(CURDATE())
            AND is_show = 0
        GROUP BY
            DATE(update_time)
        ORDER BY
            DATE(update_time) ASC;
    </select>

    <select id="listCustom" resultType="vip.xiaonuo.modular.env.envdata.entity.vo.EnvDataListVo">
        SELECT
            DATE(update_time) AS 'createTime',
            ROUND(AVG(tem_handle_value), 2) AS 'temp',
            ROUND(AVG(humi_handle_value), 2) AS 'hum'

        FROM
            `env_data`
        WHERE
            pigpen_id = #{pigpenId}
            AND update_time BETWEEN #{startDate} AND #{endDate}
            AND is_show = 0
        GROUP BY
            DATE(update_time)
        ORDER BY
            DATE(update_time) ASC;
    </select>
    <select id="listData" resultType="vip.xiaonuo.modular.env.envdata.entity.vo.EnvDataCarouselVo">
        SELECT DISTINCT
            d.tem_handle_value AS 'temp',
            d.humi_handle_value AS 'hum',
            b.id AS 'unitId',
            b.full_name AS 'location'
        FROM `base_pigpen` b
        LEFT OUTER JOIN (
            SELECT
                pigpen_id,
                tem_handle_value,
                humi_handle_value,
                update_time
            FROM `env_data`
            WHERE (pigpen_id, update_time) IN (
                SELECT
                    pigpen_id,
                 MAX(update_time)
                FROM `env_data`
        GROUP BY pigpen_id
            )
        ) d ON b.id = d.pigpen_id
        WHERE b.type = 2 AND b.delete_flag = 'NOT_DELETE'  AND b.org_id = #{orgId}
        <if test="buildName != null and buildName !=''">
            AND b.full_name LIKE CONCAT('%', #{buildName}, '%')
        </if>
    </select>
</mapper>