<?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.BaseRoomMapper">
    <update id="setS">

        SET sql_mode =
      "STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
    </update>


    <select id="getAllRoomEnvironment" resultType="com.huimv.environ.eco.entity.dto.AllRoomEnvironment">

  SELECT CONCAT(br.alias, br.room_name )room,br.id room_id  , tem.value temperature, hum.value humidity ,br.status status
        FROM
        `base_room` br
        LEFT JOIN  (
          SELECT  a.tem_id ,a.room_id , a.value `value` FROM  `sys_temperature`  a RIGHT  JOIN
                (
                SELECT MAX(tem_id) AS tem_id, room_id , `value` FROM `sys_temperature`
                WHERE TO_DAYS(create_time) = TO_DAYS(NOW()) AND `value` &lt;= 50 AND `value` &gt;= -50
                 GROUP BY room_id
                ) b
                ON  a.tem_id=  b.tem_id

         )  tem ON  tem.room_id = br.id


         LEFT JOIN  (
          SELECT  a.hum_id ,a.room_id , a.value `value` FROM  `sys_humidity`  a RIGHT  JOIN
                (
                SELECT MAX(hum_id) AS hum_id, room_id , `value` FROM `sys_humidity`
                 WHERE  TO_DAYS(create_time) = TO_DAYS(NOW()) AND  `value` &lt;= 100 AND`value` &gt;= 0
                 GROUP BY room_id
                ) b
                ON  a.hum_id=  b.hum_id

         )  hum ON  hum.room_id = br.id  WHERE br.farm_id =#{farmId}



    </select>
    <select id="getAllDayWater" resultType="com.huimv.environ.eco.entity.dto.AllDayWater">
        SELECT CONCAT(br.alias ,br.room_name) room,  ROUND (water.`value`,2) water
        FROM `base_room` br
         LEFT JOIN (
            SELECT SUM(increase_value)`value` ,room_id  FROM `sys_month_water`  WHERE TO_DAYS(create_time) = TO_DAYS(NOW()) GROUP BY room_id
         )water ON water.room_id = br.id
         WHERE br.farm_id = #{farmId}
    </select>
    <select id="listAllWaterEnvPowerApp" resultType="com.huimv.environ.eco.entity.dto.AllWaterEnvPowerApp">
        SELECT CONCAT(br.alias ,br.room_name) room_name,br.id room_id ,  ROUND(tem.value ,2)tem,  ROUND(hum.value,2)hum , ROUND(water.`value` ,2) water,bf.id floor_id ,br.box_id box_id
        FROM
        `base_room` br
        LEFT JOIN  (
          SELECT  a.tem_id ,a.room_id , a.value `value` FROM  `sys_temperature`  a RIGHT  JOIN
                (
                SELECT MAX(tem_id) AS tem_id, room_id , `value` FROM `sys_temperature`
                WHERE   `value` &lt;= 50 AND`value` &gt;= -50
                 GROUP BY room_id
                ) b
                ON  a.tem_id=  b.tem_id

         )  tem ON  tem.room_id = br.id


         LEFT JOIN  (
          SELECT  a.hum_id ,a.room_id , a.value `value` FROM  `sys_humidity`  a RIGHT  JOIN
                (
                SELECT MAX(hum_id) AS hum_id, room_id , `value` FROM `sys_humidity`
                 WHERE  `value` &lt;= 100 AND`value` &gt;= 0
                  GROUP BY room_id
                ) b
                ON  a.hum_id=  b.hum_id

         )  hum ON  hum.room_id = br.id


         LEFT JOIN (
            SELECT SUM(increase_value)`value` ,room_id  FROM `sys_month_water`  WHERE  TO_DAYS(create_time) = TO_DAYS(NOW())  GROUP BY room_id
         )water ON water.room_id = br.id

            LEFT JOIN `base_box` bb  ON bb.uid = br.box_id
            LEFT JOIN `base_floor`  bf ON bb.floor_id =  bf.id

        ${ew.customSqlSegment}


    </select>
</mapper>
