<?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="listCount" 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 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-%d')
        ORDER BY create_date ASC
    </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 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-%d')
        ORDER BY create_date ASC
    </select>
    <select id="listYearWater" 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')
        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 ASC
    </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 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-%d')
        ORDER BY create_date ASC
    </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 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 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 &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 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 &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 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
FROM `energy_data` WHERE unit_id = ${unitId} AND farm_id =${farmId}
 </select>
</mapper>
