<?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.huimv.admin.mapper.EnvDataMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.huimv.admin.entity.EnvData">
        <id column="id" property="id"/>
        <result column="device_id" property="deviceId"/>
        <result column="env_temp" property="envTemp"/>
        <result column="env_hum" property="envHum"/>
        <result column="original_data" property="originalData"/>
        <result column="unit_id" property="unitId"/>
        <result column="farm_id" property="farmId"/>
        <result column="other1" property="other1"/>
        <result column="other2" property="other2"/>
        <result column="other3" property="other3"/>
        <result column="create_time" property="createTime"/>
        <result column="receive_date" property="receiveDate"/>
    </resultMap>
    <select id="listDay" resultType="com.huimv.admin.entity.EnvData">
    SELECT Convert((IFNULL(AVG(env_temp),'0')),DECIMAL(10,2)) 'envTemp',Convert((IFNULL(AVG(env_hum),'0')),DECIMAL(10,2)) 'envHum',create_time FROM env_data
    ${ew.customSqlSegment}
    group BY DATE_FORMAT(create_time,'%Y-%m-%d')
    ORDER BY create_time ASC
    </select>

    <select id="listDay2" resultType="com.huimv.admin.entity.EnvData">
    SELECT Convert((IFNULL(AVG(env_temp),'0')),DECIMAL(10,2)) 'envTemp',Convert((IFNULL(AVG(env_hum),'0')),DECIMAL(10,2)) 'envHum',create_time FROM env_data
    ${ew.customSqlSegment}
    group BY DATE_FORMAT(create_time,'%Y-%m-%d %H')
    ORDER BY create_time ASC
    </select>

    <select id="listNow" resultType="com.huimv.admin.entity.EnvData">
    SELECT Convert((IFNULL(env_temp,'0')),DECIMAL(10,2)) 'envTemp',Convert((IFNULL(env_hum,'0')),DECIMAL(10,2)) 'envHum',create_time FROM env_data
    ${ew.customSqlSegment}
    ORDER BY create_time ASC
    </select>

    <select id="listPigpenAll" resultType="com.huimv.admin.entity.vo.EnvDataVo">
 SELECT
    bp.build_name location,
    COALESCE(ed.env_temp,0) temp,
     COALESCE(ed.env_hum,0) hum,
     COALESCE(ed.unit_id,0) unitId,
     COALESCE(en.nh3_n,0) aq
FROM
    `base_pigpen` bp
LEFT JOIN (
    SELECT
        ed1.*
    FROM
        `env_data` ed1
    INNER JOIN (
        SELECT
            unit_id,
            MAX(id) AS max_id
        FROM
            `env_data`
        GROUP BY
            unit_id
    ) ed2 ON ed1.unit_id = ed2.unit_id AND ed1.id = ed2.max_id
) ed ON ed.unit_id = bp.id


LEFT JOIN (
    SELECT
        en1.*
    FROM
        `env_nh3n` en1
    INNER JOIN (
        SELECT
            unit_id,
            MAX(id) AS max_id
        FROM
            `env_nh3n`
        GROUP BY
            unit_id
    ) en2 ON en1.unit_id = en2.unit_id AND en1.id = en2.max_id
) en ON en.unit_id = bp.id



WHERE
    bp.farm_id = #{farmId}
    AND bp.f_type = 3
    AND bp.other3 = 0;
    </select>
</mapper>
