<?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.EnergyDataMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.huimv.admin.entity.EnergyData">
        <id column="id" property="id" />
        <result column="feed_value" property="feedValue" />
        <result column="original_feed_value" property="originalFeedValue" />
        <result column="water_value" property="waterValue" />
        <result column="original_water_value" property="originalWaterValue" />
        <result column="gas_varlue" property="gasValue" />
        <result column="original_gas_varlue" property="originalGasValue" />
        <result column="electricity_value" property="electricityValue" />
        <result column="original_electricity_value" property="originalElectricityValue" />
        <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_date" property="createDate"/>
    </resultMap>
    <select id="listYear" resultType="com.huimv.admin.entity.vo.EnergyGasDataVo">
        SELECT Convert((IFNUll(sum(gas_value),'0')),DECIMAL(10,2)) 'gasValue',create_date 'createDate' FROM `energy_data`
         ${ew.customSqlSegment}
        GROUP BY DATE_FORMAT(create_date,'%Y-%m')
        ORDER BY create_date ASC
    </select>
    <select id="listDay" resultType="com.huimv.admin.entity.vo.EnergyGasDataVo">
        SELECT Convert((IFNUll(sum(gas_value),'0')),DECIMAL(10,2)) 'gasValue',create_date 'createDate' FROM `energy_data`
         ${ew.customSqlSegment}
        GROUP BY DATE_FORMAT(create_date,'%Y-%m-%d')
        ORDER BY create_date ASC
    </select>

    <select id="listYear2" resultType="com.huimv.admin.entity.vo.EnergyFeedDataVo">
        SELECT Convert((IFNUll(sum(feed_value),'0')),DECIMAL(10,2)) 'feedValue',create_date 'createDate' FROM `energy_data`
         ${ew.customSqlSegment}
        GROUP BY DATE_FORMAT(create_date,'%Y-%m')
        ORDER BY create_date ASC
    </select>
    <select id="listDay2" resultType="com.huimv.admin.entity.vo.EnergyFeedDataVo">
        SELECT Convert((IFNUll(sum(feed_value),'0')),DECIMAL(10,2)) 'feedValue',create_date 'createDate' FROM `energy_data`
         ${ew.customSqlSegment}
        GROUP BY DATE_FORMAT(create_date,'%Y-%m-%d')
        ORDER BY create_date ASC
    </select>
    <select id="listYear3" resultType="com.huimv.admin.entity.vo.EnergyWaterDataVo">
        SELECT Convert((IFNUll(sum(water_value),'0')),DECIMAL(10,2)) 'waterValue',create_date 'createDate' FROM `energy_data`
         ${ew.customSqlSegment}
        GROUP BY DATE_FORMAT(create_date,'%Y-%m')
        ORDER BY create_date ASC
    </select>
    <select id="listDay3" resultType="com.huimv.admin.entity.vo.EnergyWaterDataVo">
        SELECT Convert((IFNUll(sum(water_value),'0')),DECIMAL(10,2)) 'waterValue',create_date 'createDate' FROM `energy_data`
         ${ew.customSqlSegment}
        GROUP BY DATE_FORMAT(create_date,'%Y-%m-%d')
        ORDER BY create_date ASC
    </select>
    <select id="listYear4" resultType="com.huimv.admin.entity.vo.EnergyElectricityDataVo">
        SELECT Convert((IFNUll(sum(electricity_value),'0')),DECIMAL(10,2)) 'electricityValue',create_date 'createDate' FROM `energy_data`
         ${ew.customSqlSegment}
        GROUP BY DATE_FORMAT(create_date,'%Y-%m')
        ORDER BY create_date ASC
    </select>
    <select id="listDay4" resultType="com.huimv.admin.entity.vo.EnergyElectricityDataVo">
        SELECT Convert((IFNUll(sum(electricity_value),'0')),DECIMAL(10,2)) 'electricityValue',create_date 'createDate' FROM `energy_data`
         ${ew.customSqlSegment}
        GROUP BY DATE_FORMAT(create_date,'%Y-%m-%d')
        ORDER BY create_date ASC
    </select>
    <select id="listWaterCount" resultType="com.huimv.admin.entity.vo.EnergyDataVo">
        select Convert((IFNUll(sum(water_value),'0')),DECIMAL(10,2)) 'waterValue',
        now() 'nowTime'
         from energy_data
        ${ew.customSqlSegment}
    </select>

    <select id="listElectricityCount" resultType="java.lang.String">
        select Convert((IFNUll(sum(electricity_value),'0')),DECIMAL(10,2)) 'electricityValue'
         from energy_data
        ${ew.customSqlSegment}
    </select>


    <select id="listFeedCount" resultType="java.lang.String">
        select Convert((IFNUll(sum(feed_value),'0')),DECIMAL(10,2)) 'feedValue'
         from energy_data
        ${ew.customSqlSegment}
    </select>



    <select id="listGasCount" resultType="java.lang.String">
        select Convert((IFNUll(sum(gas_value),'0')),DECIMAL(10,2)) 'gasValue'
         from energy_data
        ${ew.customSqlSegment}
    </select>

    <select id="listAllCount" resultType="com.huimv.admin.entity.vo.EnergyDataVo">
        select Convert((IFNUll(sum(water_value),'0')),DECIMAL(10,2)) 'waterValue',Convert((IFNUll(sum(electricity_value),'0')),DECIMAL(10,2)) 'electricityValue',
        Convert((IFNUll(sum(feed_value),'0')),DECIMAL(10,2)) 'feedValue',Convert((IFNUll(sum(gas_value),'0')),DECIMAL(10,2)) 'gasValue',
        now() 'nowTime'
         from energy_data
        ${ew.customSqlSegment}
    </select>



    <!--为的是给前端不改变值的写法-->
    <select id="listYearGas" resultType="com.huimv.admin.entity.vo.EnergyAllDataVo">
        SELECT Convert((IFNUll(sum(gas_value),'0')),DECIMAL(10,2)) 'value',create_date 'createDate' FROM `energy_data`
         ${ew.customSqlSegment}
        GROUP BY DATE_FORMAT(create_date,'%Y-%m')
        ORDER BY create_date ASC
    </select>
    <select id="listDayGas" resultType="com.huimv.admin.entity.vo.EnergyAllDataVo">
        SELECT ROUND(COALESCE(SUM(gas_value), 0),2) 'value',create_date 'createDate' FROM `energy_data`
         ${ew.customSqlSegment}
        GROUP BY DATE_FORMAT(create_date,'%Y-%m-%d')
        ORDER BY create_date DESC
        LIMIT 30
    </select>

    <select id="listYearFeed" resultType="com.huimv.admin.entity.vo.EnergyAllDataVo">
        SELECT Convert((IFNUll(sum(feed_value),'0')),DECIMAL(10,2)) 'value',create_date 'createDate' FROM `energy_data`
         ${ew.customSqlSegment}
        GROUP BY DATE_FORMAT(create_date,'%Y-%m')
        ORDER BY create_date ASC
    </select>
    <select id="listDayFeed" resultType="com.huimv.admin.entity.vo.EnergyAllDataVo">
        SELECT ROUND(COALESCE(SUM(feed_value), 0),2) 'value',create_date 'createDate' FROM `energy_data`
         ${ew.customSqlSegment}
        GROUP BY DATE_FORMAT(create_date,'%Y-%m-%d')
        ORDER BY create_date DESC
        LIMIT 30
    </select>
    <select id="listYearWater" resultType="com.huimv.admin.entity.vo.EnergyAllDataVo">
        SELECT ROUND(COALESCE(SUM(water_value), 0),2) AS 'value',create_date 'createDate' FROM `energy_data`
         ${ew.customSqlSegment}
        GROUP BY DATE_FORMAT(create_date,'%Y-%m')
        ORDER BY create_date ASC
    </select>
    <select id="listDayWater" resultType="com.huimv.admin.entity.vo.EnergyAllDataVo">
        SELECT Convert((IFNUll(sum(water_value),'0')),DECIMAL(10,2)) 'value',create_date 'createDate' FROM `energy_data`
         ${ew.customSqlSegment}
        GROUP BY DATE_FORMAT(create_date,'%Y-%m-%d')
        ORDER BY create_date DESC
        LIMIT 30
    </select>
    <select id="listYearElectricity" resultType="com.huimv.admin.entity.vo.EnergyAllDataVo">
        SELECT Convert((IFNUll(sum(electricity_value),'0')),DECIMAL(10,2)) 'value',create_date 'createDate' FROM `energy_data`
         ${ew.customSqlSegment}
        GROUP BY DATE_FORMAT(create_date,'%Y-%m')
        ORDER BY create_date ASC
    </select>
    <select id="listDayElectricity" resultType="com.huimv.admin.entity.vo.EnergyAllDataVo">
        SELECT ROUND(COALESCE(SUM(electricity_value), 0),2) 'value',create_date 'createDate' FROM `energy_data`
         ${ew.customSqlSegment}
        GROUP BY DATE_FORMAT(create_date,'%Y-%m-%d')
        ORDER BY create_date DESC
        LIMIT 30
    </select>

    <select id="listDataCount" resultType="com.huimv.admin.entity.EnergyData">
        select Convert(IFNULL(SUM(water_value),0),DECIMAL(10,2)) 'waterValue',Convert(IFNULL(SUM(electricity_value),0),DECIMAL(10,2)) 'electricityValue',
Convert(IFNULL(SUM(feed_value),0),DECIMAL(10,2)) 'feedValue',Convert(IFNULL(SUM(gas_value),0),DECIMAL(10,2)) 'gasValue' FROM energy_data
 ${ew.customSqlSegment}
    </select>

    <select id="listDataAll" resultType="com.huimv.admin.entity.vo.EnergyDataScreenVo">
SELECT
    -- 当日读数
    CONVERT(IFNULL(SUM(CASE WHEN 1 = 1 THEN water_value ELSE 0 END), '0'), DECIMAL(10, 2)) AS waterValueToday,
    CONVERT(IFNULL(SUM(CASE WHEN 1 = 1 THEN electricity_value ELSE 0 END), '0'), DECIMAL(10, 2)) AS electricityValueToday,
    CONVERT(IFNULL(SUM(CASE WHEN 1 = 1 THEN feed_value ELSE 0 END), '0'), DECIMAL(10, 2)) AS feedValueToday,

    -- 当日用量
    CONVERT(IFNULL(SUM(CASE WHEN create_date &gt;= #{today} THEN water_value ELSE 0 END), '0'), DECIMAL(10, 2)) AS waterValueTodayUsage,
    CONVERT(IFNULL(SUM(CASE WHEN create_date &gt;= #{today} THEN electricity_value ELSE 0 END), '0'), DECIMAL(10, 2)) AS electricityValueTodayUsage,
    CONVERT(IFNULL(SUM(CASE WHEN create_date &gt;= #{today} THEN feed_value ELSE 0 END), '0'), DECIMAL(10, 2)) AS feedValueTodayUsage,

    -- 昨日用量
    CONVERT(IFNULL(SUM(CASE WHEN create_date BETWEEN #{yesterdayStart} AND #{yesterdayEnd} THEN water_value ELSE 0 END), '0'), DECIMAL(10, 2)) AS waterValueYesterdayUsage,
    CONVERT(IFNULL(SUM(CASE WHEN create_date BETWEEN #{yesterdayStart} AND #{yesterdayEnd} THEN electricity_value ELSE 0 END), '0'), DECIMAL(10, 2)) AS electricityValueYesterdayUsage,
    CONVERT(IFNULL(SUM(CASE WHEN create_date BETWEEN #{yesterdayStart} AND #{yesterdayEnd} THEN feed_value ELSE 0 END), '0'), DECIMAL(10, 2)) AS feedValueYesterdayUsage,
    -- 本周用量
    CONVERT(IFNULL(SUM(CASE WHEN create_date &gt;= #{week} THEN water_value ELSE 0 END), '0'), DECIMAL(10, 2)) AS waterValueThisWeekUsage,
    CONVERT(IFNULL(SUM(CASE WHEN create_date &gt;= #{week} THEN electricity_value ELSE 0 END), '0'), DECIMAL(10, 2)) AS electricityValueThisWeekUsage,
    CONVERT(IFNULL(SUM(CASE WHEN create_date &gt;= #{week} THEN feed_value ELSE 0 END), '0'), DECIMAL(10, 2)) AS feedValueThisWeekUsage,

    -- 上周用量
    CONVERT(IFNULL(SUM(CASE WHEN create_date BETWEEN #{weekStart} AND #{weekEnd} THEN water_value ELSE 0 END), '0'), DECIMAL(10, 2)) AS waterValueLastWeekUsage,
    CONVERT(IFNULL(SUM(CASE WHEN create_date BETWEEN #{weekStart} AND #{weekEnd} THEN electricity_value ELSE 0 END), '0'), DECIMAL(10, 2)) AS electricityValueLastWeekUsage,
    CONVERT(IFNULL(SUM(CASE WHEN create_date BETWEEN #{weekStart} AND #{weekEnd} THEN feed_value ELSE 0 END), '0'), DECIMAL(10, 2)) AS feedValueLastWeekUsage,

    -- 本月用量
    CONVERT(IFNULL(SUM(CASE WHEN create_date &gt;= #{month} THEN water_value ELSE 0 END), '0'), DECIMAL(10, 2)) AS waterValueThisMonthUsage,
    CONVERT(IFNULL(SUM(CASE WHEN create_date &gt;= #{month} THEN electricity_value ELSE 0 END), '0'), DECIMAL(10, 2)) AS electricityValueThisMonthUsage,
    CONVERT(IFNULL(SUM(CASE WHEN create_date &gt;= #{month} THEN feed_value ELSE 0 END), '0'), DECIMAL(10, 2)) AS feedValueThisMonthUsage,

    -- 上月用量
    CONVERT(IFNULL(SUM(CASE WHEN create_date BETWEEN #{monthStart} AND #{monthEnd} THEN water_value ELSE 0 END), '0'), DECIMAL(10, 2)) AS waterValueLastMonthUsage,
    CONVERT(IFNULL(SUM(CASE WHEN create_date BETWEEN #{monthStart} AND #{monthEnd} THEN electricity_value ELSE 0 END), '0'), DECIMAL(10, 2)) AS electricityValueLastMonthUsage,
    CONVERT(IFNULL(SUM(CASE WHEN create_date BETWEEN #{monthStart} AND #{monthEnd} THEN feed_value ELSE 0 END), '0'), DECIMAL(10, 2)) AS feedValueLastMonthUsage
FROM `energy_data` WHERE unit_id = ${unitId} AND farm_id =${farmId}
 </select>


    <select id="listSevenDayElectricity" resultType="com.huimv.admin.entity.vo.EnergyVo">
        SELECT
            calendar.date,
            ROUND(COALESCE(SUM(energy_data.electricity_value), 0), 2) AS 'value'
        FROM (
            SELECT CURDATE() AS DATE
            UNION ALL SELECT DATE_SUB(CURDATE(), INTERVAL 1 DAY)
            UNION ALL SELECT DATE_SUB(CURDATE(), INTERVAL 2 DAY)
            UNION ALL SELECT DATE_SUB(CURDATE(), INTERVAL 3 DAY)
            UNION ALL SELECT DATE_SUB(CURDATE(), INTERVAL 4 DAY)
            UNION ALL SELECT DATE_SUB(CURDATE(), INTERVAL 5 DAY)
            UNION ALL SELECT DATE_SUB(CURDATE(), INTERVAL 6 DAY)
        )AS calendar
        LEFT JOIN
            `energy_data`
        ON
            calendar.date = DATE(energy_data.create_date)
            AND energy_data.farm_id = #{farmId}
            AND energy_data.display_type = #{displayType}
            AND energy_data.unit_id = #{unitId}
        GROUP BY
            calendar.date;
    </select>

    <select id="listSixMonthElectricity" resultType="com.huimv.admin.entity.vo.EnergyVo">
        SELECT
            calendar.date,
            ROUND(COALESCE(SUM(energy_data.electricity_value), 0), 2) AS 'value'
        FROM (
            SELECT DATE_FORMAT(NOW(), '%Y-%m') AS date
            UNION ALL SELECT DATE_FORMAT(DATE_SUB(NOW(), INTERVAL 1 MONTH), '%Y-%m')
            UNION ALL SELECT DATE_FORMAT(DATE_SUB(NOW(), INTERVAL 2 MONTH), '%Y-%m')
            UNION ALL SELECT DATE_FORMAT(DATE_SUB(NOW(), INTERVAL 3 MONTH), '%Y-%m')
            UNION ALL SELECT DATE_FORMAT(DATE_SUB(NOW(), INTERVAL 4 MONTH), '%Y-%m')
            UNION ALL SELECT DATE_FORMAT(DATE_SUB(NOW(), INTERVAL 5 MONTH), '%Y-%m')
        ) AS calendar
        LEFT JOIN
            `energy_data`
        ON
            calendar.date = DATE_FORMAT(energy_data.create_date,"%Y-%m")
            AND energy_data.farm_id = #{farmId}
            AND energy_data.display_type = #{displayType}
            AND energy_data.unit_id = #{unitId}
        GROUP BY
            calendar.date
        order by
            calendar.date
    </select>
    <select id="getElectricity" resultType="java.lang.Double">
        SELECT
            ROUND(COALESCE(SUM(energy_data.electricity_value), 0), 2) AS 'value'
        FROM
            `energy_data`
        WHERE
            create_date BETWEEN #{startDate} AND #{endDate}
            AND energy_data.farm_id = #{farmId}
            AND energy_data.display_type = #{displayType}
            AND energy_data.unit_id = #{unitId}
    </select>
    <select id="listSevenDayWater" resultType="com.huimv.admin.entity.vo.EnergyVo">
        SELECT
            calendar.date,
            ROUND(COALESCE(SUM(energy_data.water_value), 0), 2) AS 'value'
        FROM (
            SELECT CURDATE() AS DATE
            UNION ALL SELECT DATE_SUB(CURDATE(), INTERVAL 1 DAY)
            UNION ALL SELECT DATE_SUB(CURDATE(), INTERVAL 2 DAY)
            UNION ALL SELECT DATE_SUB(CURDATE(), INTERVAL 3 DAY)
            UNION ALL SELECT DATE_SUB(CURDATE(), INTERVAL 4 DAY)
            UNION ALL SELECT DATE_SUB(CURDATE(), INTERVAL 5 DAY)
            UNION ALL SELECT DATE_SUB(CURDATE(), INTERVAL 6 DAY)
        )AS calendar
        LEFT JOIN
            `energy_data`
        ON
            calendar.date = DATE(energy_data.create_date)
            AND energy_data.farm_id = #{farmId}
            AND energy_data.display_type = #{displayType}
            AND energy_data.unit_id = #{unitId}
        GROUP BY
            calendar.date;
    </select>

    <select id="getWater" resultType="java.lang.Double">
        SELECT
            ROUND(COALESCE(SUM(energy_data.water_value), 0), 2) AS 'value'
        FROM
            `energy_data`
        WHERE
            create_date BETWEEN #{startDate} AND #{endDate}
            AND energy_data.farm_id = #{farmId}
            AND energy_data.display_type = #{displayType}
            AND energy_data.unit_id = #{unitId}
    </select>

    <select id="listSixMonthWater" resultType="com.huimv.admin.entity.vo.EnergyVo">
        SELECT
            calendar.date,
            ROUND(COALESCE(SUM(energy_data.water_value), 0), 2) AS 'value'
        FROM (
            SELECT DATE_FORMAT(NOW(), '%Y-%m') AS date
            UNION ALL SELECT DATE_FORMAT(DATE_SUB(NOW(), INTERVAL 1 MONTH), '%Y-%m')
            UNION ALL SELECT DATE_FORMAT(DATE_SUB(NOW(), INTERVAL 2 MONTH), '%Y-%m')
            UNION ALL SELECT DATE_FORMAT(DATE_SUB(NOW(), INTERVAL 3 MONTH), '%Y-%m')
            UNION ALL SELECT DATE_FORMAT(DATE_SUB(NOW(), INTERVAL 4 MONTH), '%Y-%m')
            UNION ALL SELECT DATE_FORMAT(DATE_SUB(NOW(), INTERVAL 5 MONTH), '%Y-%m')
        ) AS calendar
        LEFT JOIN
            `energy_data`
        ON
            calendar.date = DATE_FORMAT(energy_data.create_date,"%Y-%m")
            AND energy_data.farm_id = #{farmId}
            AND energy_data.display_type = #{displayType}
            AND energy_data.unit_id = #{unitId}
        GROUP BY
            calendar.date
        order by
            calendar.date
    </select>
    <select id="getTodayElectricity" resultType="java.lang.Double">
        SELECT ROUND(COALESCE(SUM(electricity_value),0),2) AS 'value'
        FROM `energy_data`
        WHERE unit_id = #{unitId} AND display_type = #{displayType} and Date(create_date) = CURDATE()
    </select>
    <select id="getTodayWater" resultType="java.lang.Double">
        SELECT ROUND(COALESCE(SUM(water_value),0),2) AS 'value'
        FROM `energy_data`
        WHERE unit_id = #{unitId} AND display_type = #{displayType} and Date(create_date) = CURDATE()
    </select>

    <select id="listRankWater" resultType="com.huimv.admin.entity.dto.ListRankVo">
        select ROUND(IFNUll(SUM(water_value),0),2) value,unit_id unitId from energy_data
         ${ew.customSqlSegment}
        GROUP BY unit_id
        ORDER BY value DESC
        limit 5
    </select>
    <select id="listRankElectricity" resultType="com.huimv.admin.entity.dto.ListRankVo">
        select ROUND(IFNUll(SUM(electricity_value),0),2) value,unit_id unitId from energy_data
         ${ew.customSqlSegment}
        GROUP BY unit_id
        ORDER BY value DESC
         limit 5
    </select>
    <select id="listRankGas" resultType="com.huimv.admin.entity.dto.ListRankVo">
        select ROUND(IFNUll(SUM(gas_value),0),2) value,unit_id unitId from energy_data
         ${ew.customSqlSegment}
        GROUP BY unit_id
        ORDER BY value DESC
           limit 5
    </select>
    <select id="listRankFeed" resultType="com.huimv.admin.entity.dto.ListRankVo">
        select ROUND(IFNUll(SUM(feed_value),0),2) value,unit_id unitId from energy_data
         ${ew.customSqlSegment}
        GROUP BY unit_id
        ORDER BY value DESC
           limit 5
    </select>
</mapper>
