<?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="vip.xiaonuo.modular.energy.energyWater.mapper.EnergyWaterMapper">

    <select id="getWeekTotal" resultType="java.math.BigDecimal" parameterType="java.lang.String">
         SELECT
            COALESCE(ROUND(SUM(water_value), 2), 0) AS 'waterValue'
        FROM
            `energy_water`
        WHERE
            org_id = #{orgId}
            AND YEARWEEK(create_date, 1) = YEARWEEK(CURDATE(), 1)
    </select>

    <select id="getLastWeekTotal" resultType="java.math.BigDecimal" parameterType="java.lang.String">
        SELECT
            COALESCE(ROUND(SUM(water_value), 2), 0) AS 'waterValue'
        FROM
            `energy_water`
        WHERE
            org_id = #{orgId}
            AND create_date BETWEEN
                CURDATE() - INTERVAL 1 WEEK AND CURDATE() - INTERVAL 1 DAY;
    </select>

    <select id="getWeekTotalByType" resultType="java.math.BigDecimal">
        SELECT
            COALESCE(ROUND(SUM(w.water_value), 2), 0) AS 'waterValue'
        FROM
            `energy_water` w
        LEFT JOIN
            `base_pigpen` p
        ON
            w.unit_id = p.id
        WHERE
            w.org_id = #{orgId}
            p.stage = #{stage}
            AND YEARWEEK(e.create_date, 1) = YEARWEEK(CURDATE(), 1)
    </select>

    <select id="getMonthTotal" resultType="java.math.BigDecimal" parameterType="java.lang.String">
        SELECT
            COALESCE(ROUND(SUM(water_value), 2), 0) AS 'waterValue'
        FROM
            `energy_water`
        WHERE
            org_id = #{orgId}
            AND MONTH(create_date) = MONTH(CURDATE())
            AND YEAR(create_date) = YEAR(CURDATE())
    </select>

    <select id="getLastMonthTotal" resultType="java.math.BigDecimal" parameterType="java.lang.String">
        SELECT
            COALESCE(ROUND(SUM(water_value), 2), 0) AS 'waterValue'
        FROM
            `energy_water`
        WHERE
            org_id = #{orgId}
            AND MONTH(create_date) = MONTH(CURDATE() - INTERVAL 1 MONTH)
            AND YEAR(create_date) = YEAR(CURDATE() - INTERVAL 1 MONTH);
    </select>

    <select id="getMonthTotalByType" resultType="java.math.BigDecimal">
        SELECT
            COALESCE(ROUND(SUM(w.water_value), 2), 0) AS 'waterValue'
        FROM
            `energy_water` w
        LEFT JOIN
            `base_pigpen` p
        ON
            w.unit_id = p.id
        WHERE
            w.org_id = #{orgId}
            p.stage = #{stage}
            AND MONTH(e.create_date) = MONTH(CURDATE())
            AND YEAR(e.create_date) = YEAR(CURDATE())
    </select>
    <select id="listToday" resultType="vip.xiaonuo.modular.energy.energyWater.entity.vo.EnergyWaterListVo" parameterType="java.lang.String">
        SELECT
            water_value AS 'waterValue', create_date AS 'createTime'
        FROM
            `energy_water`
        WHERE
            unit_id = #{pigpenId}
            AND DATE(create_date) = CURRENT_DATE
        ORDER BY
         createTime ASC
    </select>

    <select id="listWeek" resultType="vip.xiaonuo.modular.energy.energyWater.entity.vo.EnergyWaterListVo" parameterType="java.lang.String">
        SELECT
            ROUND(AVG(water_value), 2) AS 'waterValue',
            DATE(create_date) AS 'createTime'
        FROM
            `energy_water`
        WHERE
            unit_id = #{pigpenId}
            AND YEARWEEK(create_date, 1) = YEARWEEK(CURDATE(), 1)
        GROUP BY
            DATE(create_date)
        ORDER BY
            DATE(create_date) ASC;
    </select>

    <select id="listMonth" resultType="vip.xiaonuo.modular.energy.energyWater.entity.vo.EnergyWaterListVo" parameterType="java.lang.String">
        SELECT
            ROUND(AVG(water_value), 2) AS 'waterValue',
            DATE(create_date) AS 'createTime'
        FROM
            `energy_water`
        WHERE
            unit_id = #{pigpenId}
            AND MONTH(create_date) = MONTH(CURDATE())
            AND YEAR(create_date) = YEAR(CURDATE())
        GROUP BY
            DATE(create_date)
        ORDER BY
            DATE(create_date) ASC;
    </select>
    <select id="listCustom" resultType="vip.xiaonuo.modular.energy.energyWater.entity.vo.EnergyWaterListVo">
        SELECT
            ROUND(AVG(water_value), 2) AS 'waterValue',
            DATE(create_date) AS 'createTime'
        FROM
            `energy_water`
        WHERE
            unit_id = #{pigpenId}
            AND create_date BETWEEN #{startDate} AND #{endDate}
        GROUP BY
            DATE(create_date)
        ORDER BY
            DATE(create_date) ASC;
    </select>
    <select id="listTodayTop" resultType="vip.xiaonuo.modular.energy.energyWater.entity.vo.EnergyWaterTopVo" parameterType="java.lang.String">
        SELECT
            p.full_name as 'location',
            ROUND(SUM(w.water_value), 2) as 'energyWaterValue'
        FROM
            `energy_water` w
        LEFT JOIN
            `base_pigpen` p
        ON
            w.unit_id = p.id
        WHERE
            w.org_id = #{orgId}
            AND DATE(w.create_date) = CURRENT_DATE
        GROUP BY
            w.unit_id
        ORDER BY
            energyWaterValue DESC
        LIMIT 6
    </select>

    <select id="listWeekTop" resultType="vip.xiaonuo.modular.energy.energyWater.entity.vo.EnergyWaterTopVo" parameterType="java.lang.String">
        SELECT
            p.full_name as 'location',
            ROUND(SUM(w.water_value), 2) as 'energyWaterValue'
        FROM
            `energy_water` w
        LEFT JOIN
            `base_pigpen` p
        ON
            w.unit_id = p.id
        WHERE
            w.org_id = #{orgId}
            AND YEARWEEK(w.create_date, 1) = YEARWEEK(CURDATE(), 1)
        GROUP BY
            w.unit_id
        ORDER BY
            energyWaterValue DESC
        LIMIT 6
    </select>

    <select id="listMonthTop" resultType="vip.xiaonuo.modular.energy.energyWater.entity.vo.EnergyWaterTopVo" parameterType="java.lang.String">
        SELECT
            p.full_name as 'location',
            ROUND(SUM(w.water_value), 2) as 'energyWaterValue'
        FROM
            `energy_water` w
        LEFT JOIN
            `base_pigpen` p
        ON
            w.unit_id = p.id
        WHERE
            w.org_id = #{orgId}
            AND MONTH(w.create_date) = MONTH(CURDATE())
            AND YEAR(w.create_date) = YEAR(CURDATE())
        GROUP BY
            w.unit_id
        ORDER BY
            energyWaterValue DESC
        LIMIT 6
    </select>

    <select id="listCustomTop" resultType="vip.xiaonuo.modular.energy.energyWater.entity.vo.EnergyWaterTopVo">
        SELECT
            p.full_name as 'location',
            ROUND(SUM(w.water_value), 2) as 'energyWaterValue''
        FROM
            `energy_water` w
        LEFT JOIN
            `base_pigpen` p
        ON
            w.unit_id = p.id
        WHERE
            w.org_id = #{orgId}
            AND w.create_date BETWEEN #{startDate} AND #{endDate}
        GROUP BY
            w.unit_id
        ORDER BY
            energyWaterValue DESC
        LIMIT 6
    </select>
</mapper>