<?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.energyElectricity.mapper.EnergyElectricityMapper">

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


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

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

    <select id="getLastMonthTotal" resultType="java.math.BigDecimal">
        SELECT
            COALESCE(ROUND(SUM(electricity_value), 2), 0) AS 'electricityValue'
        FROM
            `energy_electricity`
        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="getWeekTotalByType" resultType="java.math.BigDecimal">
        SELECT
            COALESCE(ROUND(SUM(e.electricity_value), 2), 0) AS 'electricityValue'
        FROM
            `energy_electricity` e
        LEFT JOIN
            `base_pigpen` p
        ON
            e.unit_id = p.id
        WHERE
            e.org_id = #{orgId}
            AND p.stage = #{stage}
            AND YEARWEEK(e.create_date, 1) = YEARWEEK(CURDATE(), 1)
    </select>
    <select id="getMonthTotalByType" resultType="java.math.BigDecimal">
        SELECT
            COALESCE(ROUND(SUM(e.electricity_value), 2), 0) AS 'electricityValue'
        FROM
            `energy_electricity` e
        LEFT JOIN
            `base_pigpen` p
        ON
            e.unit_id = p.id
        WHERE
            e.org_id = #{orgId}
            AND 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.energyElectricity.entity.vo.EnergyElectricityListVo">
        SELECT
            electricity_value AS 'electricityValue', create_date AS 'createTime'
        FROM
            `energy_electricity`
        WHERE
            DATE(create_date) = CURRENT_DATE
            AND org_id = #{orgId}
            <if test="pigpenList != null and pigpenList.size > 0">
                AND unit_id IN
                <foreach collection="pigpenList" item="pigpenId" index="i" open="(" close=")" separator=",">
                    #{pigpenId}
                </foreach>
            </if>
        ORDER BY
         createTime ASC
    </select>

    <select id="listWeek" resultType="vip.xiaonuo.modular.energy.energyElectricity.entity.vo.EnergyElectricityListVo">
        SELECT
            ROUND(SUM(electricity_value), 2) AS 'electricityValue',
            DATE(create_date) AS 'createTime'
        FROM
            `energy_electricity`
        WHERE
            YEARWEEK(create_date, 1) = YEARWEEK(CURDATE(), 1)
            AND org_id = #{orgId}
            <if test="pigpenList != null and pigpenList.size > 0">
                AND unit_id IN
                <foreach collection="pigpenList" item="pigpenId" index="i" open="(" close=")" separator=",">
                    #{pigpenId}
                </foreach>
            </if>
        GROUP BY
            DATE(create_date)
        ORDER BY
            DATE(create_date) ASC;
    </select>
    <select id="listMonth" resultType="vip.xiaonuo.modular.energy.energyElectricity.entity.vo.EnergyElectricityListVo">
        SELECT
            ROUND(SUM(electricity_value), 2) AS 'electricityValue',
            DATE(create_date) AS 'createTime'
        FROM
            `energy_electricity`
        WHERE
            MONTH(create_date) = MONTH(CURDATE())
            AND YEAR(create_date) = YEAR(CURDATE())
            AND org_id = #{orgId}
            <if test="pigpenList != null and pigpenList.size > 0">
                AND unit_id IN
                <foreach collection="pigpenList" item="pigpenId" index="i" open="(" close=")" separator=",">
                    #{pigpenId}
                </foreach>
            </if>
        GROUP BY
            DATE(create_date)
        ORDER BY
            DATE(create_date) ASC;
    </select>

    <select id="listCustom" resultType="vip.xiaonuo.modular.energy.energyElectricity.entity.vo.EnergyElectricityListVo">
        SELECT
            ROUND(SUM(electricity_value), 2) AS 'electricityValue',
            DATE(create_date) AS 'createTime'
        FROM
            `energy_electricity`
        WHERE
            create_date BETWEEN #{startDate} AND #{endDate}
            AND org_id = #{orgId}
            <if test="pigpenList != null and pigpenList.size > 0">
                AND unit_id IN
                <foreach collection="pigpenList" item="pigpenId" index="i" open="(" close=")" separator=",">
                    #{pigpenId}
                </foreach>
            </if>
        GROUP BY
            DATE(create_date)
        ORDER BY
            DATE(create_date) ASC;
    </select>

    <select id="listTodayTop" resultType="vip.xiaonuo.modular.energy.energyElectricity.entity.vo.EnergyElectricityTopVo">
        SELECT
            p.full_name as 'location',
            ROUND(SUM(electricity_value), 2) as 'energyElectricityValue'
        FROM
            `energy_electricity` e
        LEFT JOIN
            `base_pigpen` p
        ON
            e.unit_id = p.id
        WHERE
            e.org_id = #{orgId}
            AND DATE(create_date) = CURRENT_DATE
        GROUP BY
            e.unit_id
        ORDER BY
            energyElectricityValue DESC
        LIMIT 6
    </select>

    <select id="listWeekTop" resultType="vip.xiaonuo.modular.energy.energyElectricity.entity.vo.EnergyElectricityTopVo">
        SELECT
            p.full_name as 'location',
            ROUND(SUM(electricity_value), 2) as 'energyElectricityValue'
        FROM
            `energy_electricity` e
        LEFT JOIN
            `base_pigpen` p
        ON
            e.unit_id = p.id
        WHERE
            e.org_id = #{orgId}
            AND YEARWEEK(e.create_date, 1) = YEARWEEK(CURDATE(), 1)
        GROUP BY
            e.unit_id
        ORDER BY
            energyElectricityValue DESC
        LIMIT 6
    </select>

    <select id="listMonthTop" resultType="vip.xiaonuo.modular.energy.energyElectricity.entity.vo.EnergyElectricityTopVo">
        SELECT
            p.full_name as 'location',
            ROUND(SUM(electricity_value), 2) as 'energyElectricityValue'
        FROM
            `energy_electricity` e
        LEFT JOIN
            `base_pigpen` p
        ON
            e.unit_id = p.id
        WHERE
            e.org_id = #{orgId}
            AND MONTH(e.create_date) = MONTH(CURDATE())
            AND YEAR(e.create_date) = YEAR(CURDATE())
        GROUP BY
            e.unit_id
        ORDER BY
            energyElectricityValue DESC
        LIMIT 6
    </select>

    <select id="listCustomTop" resultType="vip.xiaonuo.modular.energy.energyElectricity.entity.vo.EnergyElectricityTopVo">
        SELECT
            p.full_name as 'location',
            ROUND(SUM(electricity_value), 2) as 'energyElectricityValue'
        FROM
            `energy_electricity` e
        LEFT JOIN
            `base_pigpen` p
        ON
            e.unit_id = p.id
        WHERE
            e.org_id = #{orgId}
            AND e.create_date BETWEEN '${startDate}' AND '${endDate}'
        GROUP BY
            e.unit_id
        ORDER BY
            energyElectricityValue DESC
        LIMIT 6
    </select>

    <select id="listSevenDay"
            resultType="vip.xiaonuo.modular.energy.energyElectricity.entity.vo.EnergyElectricityListVo">
        SELECT
            ROUND(AVG(electricity_value), 2) AS 'electricityValue',
            DATE(create_date) AS 'createTime'
        FROM
            `energy_electricity`
        WHERE
            create_date BETWEEN CURDATE() - INTERVAL 6 DAY AND NOW()
            AND org_id = #{orgId}
            <if test="pigpenList != null and pigpenList.size > 0">
                AND unit_id IN
                <foreach collection="pigpenList" item="pigpenId" index="i" open="(" close=")" separator=",">
                    #{pigpenId}
                </foreach>
            </if>
        GROUP BY
            DATE(create_date)
        ORDER BY
            DATE(create_date) ASC;
    </select>
    <select id="listScreenEnergy" resultType="vip.xiaonuo.modular.energy.energyElectricity.entity.vo.EnergyScreenVo">
        SELECT
            m.month,
            COALESCE(ROUND(w.waterValue, 2), 0) AS waterValue,
            COALESCE(ROUND(e.electricityValue, 2), 0) AS electricityValue,
            COALESCE(ROUND(f.feedValue, 1), 0) AS feedValue
        FROM months m
        LEFT JOIN (
            SELECT
                MONTH(create_date) AS MONTH,
                SUM(water_value) AS waterValue
            FROM energy_water
            WHERE YEAR(create_date) = YEAR(CURDATE())
            AND org_id = #{orgId}
            GROUP BY MONTH(create_date)
        ) w ON m.month = w.month
        LEFT JOIN (
         SELECT
                MONTH(create_date) AS MONTH,
                SUM(electricity_value) AS electricityValue
            FROM energy_electricity
            WHERE YEAR(create_date) = YEAR(CURDATE())
            AND org_id = #{orgId}
            GROUP BY MONTH(create_date)
        ) e ON m.month = e.month
        LEFT JOIN (
            SELECT
                MONTH(create_time) AS MONTH,
                ROUND(SUM(feed_value)/1000,1) AS feedValue
            FROM feeding_fat
            WHERE YEAR(create_time) = YEAR(CURDATE())
            AND org_id = #{orgId}
            GROUP BY MONTH(create_time)
        ) f ON m.month = f.month
        ORDER BY m.month
   </select>
    <select id="getToday" resultType="java.math.BigDecimal">
        SELECT
            ROUND(SUM(electricity_value), 2)
        FROM
        `energy_electricity`
        WHERE
            DATE(create_date) = CURRENT_DATE
            AND org_id = #{orgId}
            <if test="pigpenList != null and pigpenList.size > 0">
                AND unit_id IN
                <foreach collection="pigpenList" item="pigpenId" index="i" open="(" close=")" separator=",">
                    #{pigpenId}
                </foreach>
            </if>
    </select>
</mapper>