<?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.environ.eco.mapper.EcoDeviceFlowMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.huimv.environ.eco.entity.EcoDeviceFlow">
        <id column="id" property="id" />
        <result column="device_code" property="deviceCode" />
        <result column="device_name" property="deviceName" />
        <result column="device_status" property="deviceStatus" />
        <result column="device_open" property="deviceOpen" />
        <result column="id1_name" property="id1Name" />
        <result column="id1_value" property="id1Value" />
        <result column="id1_alarm_status" property="id1AlarmStatus" />
        <result column="id1_alarm_msg" property="id1AlarmMsg" />
        <result column="id2_name" property="id2Name" />
        <result column="id2_value" property="id2Value" />
        <result column="id2_alarm_status" property="id2AlarmStatus" />
        <result column="id2_alarm_msg" property="id2AlarmMsg" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id, device_code, device_name, device_status, device_open, id1_name, id1_value, id1_alarm_status, id1_alarm_msg, id2_name, id2_value, id2_alarm_status, id2_alarm_msg
    </sql>


<!--    <select id="getLastFlow" resultType="com.huimv.environ.eco.entity.dto.LastFlowDto">-->
<!--       SELECT CONCAT(b.pigpen_name,a.pigpen_name) room,df.id1_value 'tem',df.id2_value 'hum'-->
<!--        FROM `base_pigpen` a INNER JOIN `base_pigpen` b ON a.parent_id =b.id AND a.farm_code = #{farmId}-->
<!--        LEFT JOIN `eco_device` d ON d.unit_id = a.id AND d.device_type ='1'-->
<!--        LEFT JOIN `eco_device_flow` df ON d.device_code = df.device_code-->
<!--        AND df.id IN (SELECT MAX(df.id) FROM `eco_device_flow` df GROUP BY device_code)-->
<!--        ORDER BY a.id DESC,a.parent_id DESC-->
<!--    </select>-->

    <select id="getLastFlow" resultType="com.huimv.environ.eco.entity.dto.LastFlowDto">
       SELECT CONCAT(b.pigpen_name,a.pigpen_name) room,a.id unit_id,df.id1_value 'tem',df.id2_value 'hum'
        FROM `base_pigpen` a INNER JOIN `base_pigpen` b ON a.parent_id =b.id AND a.farm_code = #{farmId}
        INNER JOIN `eco_device` d ON d.unit_id = a.id AND d.device_type ='1'
        LEFT JOIN `eco_device_flow` df ON d.device_code = df.device_code
        AND df.id IN (SELECT MAX(df.id) FROM `eco_device_flow` df GROUP BY device_code)
        ORDER BY a.id DESC,a.parent_id DESC
    </select>

    <select id="getUnitLastFlow" resultType="com.huimv.environ.eco.entity.dto.LastFlowDto">
        SELECT df.id1_value AS 'tem',df.id2_value AS 'hum' FROM `eco_device_flow` df
        WHERE df.device_code = #{deviceCode}
        ORDER BY df.id DESC
        LIMIT 0,1
    </select>

    <select id="getLastFlowByPigpenId" resultType="com.huimv.environ.eco.entity.dto.LastFlowDto">
     SELECT CONCAT(b.pigpen_name,a.pigpen_name) room,a.id unit_id,df.id1_value 'tem',df.id2_value 'hum'
    FROM `base_pigpen` a INNER JOIN `base_pigpen` b ON a.parent_id =b.id AND a.farm_code = #{farmId}
    LEFT JOIN `eco_device` d ON d.unit_id = a.id AND d.device_type ='1'
    LEFT JOIN `eco_device_flow` df ON d.device_code = df.device_code
    AND df.id IN (SELECT MAX(df.id) FROM `eco_device_flow` df GROUP BY device_code)
    WHERE a.parent_id=#{pigpenId}
    ORDER BY a.id DESC,a.parent_id DESC
    </select>

    <select id="listDevice" resultType="com.huimv.environ.eco.entity.dto.EcoDeviceDto">
       SELECT d.location 'roomName',ROUND(AVG(df.id1_value),2) 'tem',ROUND(AVG(df.id2_value),2) 'hum',DATE_FORMAT(df.add_time,'%Y-%m-%d') 'addTime' FROM `eco_device` d
LEFT JOIN `eco_device_flow` df ON df.device_code = d.device_code
WHERE d.farm_id = ${farmId} AND unit_id = ${unitId}
AND add_time BETWEEN '${startDate}' AND '${endDate}'
GROUP BY DATE_FORMAT(df.add_time,"%Y-%m-%d")
    </select>

    <select id="listDayDevice" resultType="com.huimv.environ.eco.entity.dto.EcoDeviceDto">
   SELECT d.location 'roomName',df.id1_value 'tem',df.id2_value 'hum',df.add_time FROM `eco_device` d
LEFT JOIN `eco_device_flow` df ON df.device_code = d.device_code
WHERE d.farm_id = ${farmId} AND unit_id = ${unitId}
AND TO_DAYS(add_time) = TO_DAYS(NOW());
    </select>

    <select id="listWeekDevice" resultType="com.huimv.environ.eco.entity.dto.EcoDeviceDto">
        SELECT d.location 'roomName',ROUND(AVG(df.id1_value),2) 'tem',ROUND(AVG(df.id2_value)) 'hum',DATE_FORMAT(df.add_time,'%Y-%m-%d') add_time FROM `eco_device` d
LEFT JOIN `eco_device_flow` df ON df.device_code = d.device_code
WHERE d.farm_id = ${farmId} AND unit_id = ${unitId}
AND add_time BETWEEN (SELECT DATE_SUB(CURDATE(),INTERVAL WEEKDAY(CURDATE()) DAY)) AND NOW()
GROUP BY DATE_FORMAT(df.add_time,"%Y-%m-%d")
    </select>

    <select id="listMonthDevice" resultType="com.huimv.environ.eco.entity.dto.EcoDeviceDto">
        SELECT d.location 'roomName',ROUND(AVG(df.id1_value),2) 'tem',ROUND(AVG(df.id2_value)) 'hum',DATE_FORMAT(df.add_time,'%Y-%m-%d') add_time FROM `eco_device` d
LEFT JOIN `eco_device_flow` df ON df.device_code = d.device_code
WHERE d.farm_id = ${farmId} AND unit_id = ${unitId}
AND add_time BETWEEN (SELECT DATE_ADD(CURDATE(),INTERVAL -DAY(CURDATE())+1 DAY)) AND NOW()
GROUP BY DATE_FORMAT(df.add_time,"%Y-%m-%d")
    </select>

    <select id="getLastFlowByUnitId" resultType="com.huimv.environ.eco.entity.dto.LastFlowDto">
        SELECT  d.location 'room',df.id1_value 'tem',df.id2_value 'hum' FROM `eco_device` d
LEFT JOIN `eco_device_flow` df ON d.device_code = df.device_code
AND df.id IN (SELECT MAX(df.id) FROM `eco_device_flow` df GROUP BY device_code)
AND d.device_type = 1
WHERE d.unit_id = #{unitId}
    </select>

    <!--读取最后一条环境温度记录-->
    <select id="getLastDryDeviceFlow" resultType="com.huimv.environ.eco.entity.EcoDeviceFlow">
        select * from eco_device_flow WHERE device_code=#{dryDeviceCode} AND add_time BETWEEN #{startTime} AND #{endTime} ORDER BY add_time DESC
    </select>


    <select id="listData" resultType="com.huimv.environ.eco.entity.dto.LastFlowDto">
        SELECT  d.location 'room',df.id1_value 'tem',df.id2_value 'hum' FROM `eco_device` d
LEFT JOIN `eco_device_flow` df ON d.device_code = df.device_code
AND df.id IN (SELECT MAX(df.id) FROM `eco_device_flow` df GROUP BY device_code)
AND d.device_type = 1
WHERE d.unit_id = #{unitId}
    </select>
</mapper>