<?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="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 t1.id,t1.build_name,t4.env_temp,t4.env_hum,t4.create_time
        FROM `base_pigpen` t1
        INNER JOIN
        (
            SELECT t2.id,t2.unit_id,t2.env_temp,t2.env_hum,t2.create_time FROM `env_data` t2 INNER JOIN (
             (
                SELECT MAX(ed.id),ed.unit_id,MAX(ed.create_time)  AS create_time
                FROM `env_data` ed
                GROUP BY ed.unit_id
                ORDER BY ed.id
            )t3
            ) ON t2.unit_id = t3.unit_id WHERE t2.create_time = t3.create_time) t4
              ON t1.id = t4.unit_id AND t1.farm_id = #{farmId} AND t1.f_type = 3
    </select>
</mapper>
