<?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.ruoyi.ems.mapper.ElecPvSupplyHMapper">
    <!-- 产能统计结果映射 -->
    <resultMap type="com.ruoyi.ems.model.PvSupplyVO" id="PvSupplyVOResult">
        <result property="areaCode" column="area_code"/>
        <result property="areaName" column="area_name"/>
        <result property="facsCode" column="facs_code"/>
        <result property="facsName" column="facs_name"/>
        <result property="statisticDate" column="statistic_date"/>
        <result property="statisticYear" column="statistic_year"/>
        <result property="statisticMonth" column="statistic_month"/>
        <result property="timeDimension" column="time_dimension"/>
        <result property="recordTime" column="record_time"/>
        <result property="date" column="date" jdbcType="DATE"/>
        <result property="time" column="time"/>
        <result property="timeIndex" column="time_index"/>
        <result property="upUnitPrice" column="up_unit_price"/>
        <result property="genElecQuantity" column="gen_elec_quantity"/>
        <result property="useElecQuantity" column="use_elec_quantity"/>
        <result property="upElecQuantity" column="up_elec_quantity"/>
        <result property="upElecEarn" column="up_elec_earn"/>
        <result property="createTime" column="create_time"/>
    </resultMap>

    <select id="selectPvSupplyHourlyList" parameterType="com.ruoyi.ems.model.QueryMeter" resultMap="PvSupplyVOResult">
        SELECT
        <choose>
            <when test="queryMeter.areaCode != null and queryMeter.areaCode != '' and queryMeter.areaCode != '-1'">
                pv.area_code,
                a.area_name,
            </when>
            <otherwise>
                '-1' as area_code,
                '全部' as area_name,
            </otherwise>
        </choose>
        pv.date as statistic_date,
        pv.time_index,
        CONCAT(LPAD(pv.time_index - 1, 2, '0'), ':00') as statistic_hour,
        'HOUR' as time_dimension,
        ROUND(SUM(COALESCE(pv.gen_elec_quantity, 0)), 2) as gen_elec_quantity,
        ROUND(SUM(COALESCE(pv.use_elec_quantity, 0)), 2) as use_elec_quantity,
        ROUND(SUM(COALESCE(pv.up_elec_quantity, 0)), 2) as up_elec_quantity,
        ROUND(SUM(COALESCE(pv.up_elec_earn, 0)), 2) as up_elec_earn,
        NOW() as create_time
        FROM adm_ems_pv_supply_h pv
        LEFT JOIN adm_area a ON pv.area_code = a.area_code
        <where>
            <if test="queryMeter.areaCode != null and queryMeter.areaCode != '' and queryMeter.areaCode != '-1'">
                AND pv.area_code = #{queryMeter.areaCode}
            </if>
            <if test="queryMeter.date != null and queryMeter.date != ''">
                AND pv.date = DATE(#{queryMeter.date})
            </if>
            <if test="queryMeter.startRecTime != null and queryMeter.startRecTime != ''">
                AND pv.date &gt;= DATE(#{queryMeter.startRecTime})
            </if>
            <if test="queryMeter.endRecTime != null and queryMeter.endRecTime != ''">
                AND pv.date &lt;= DATE(#{queryMeter.endRecTime})
            </if>
            AND pv.date IS NOT NULL
        </where>
        GROUP BY
        <if test="queryMeter.areaCode != null and queryMeter.areaCode != '' and queryMeter.areaCode != '-1'">
            pv.area_code, a.area_name,
        </if>
        pv.date, pv.time_index
        ORDER BY pv.date DESC, pv.time_index ${queryMeter.orderFlag}
    </select>

    <!-- ==================== 光伏产能按日汇总统计 ==================== -->
    <select id="selectPvSupplyDailyList" parameterType="com.ruoyi.ems.model.QueryMeter" resultMap="PvSupplyVOResult">
        SELECT
        <choose>
            <when test="queryMeter.areaCode != null and queryMeter.areaCode != '' and queryMeter.areaCode != '-1'">
                pv.area_code,
                a.area_name,
            </when>
            <when test="queryMeter.groupByArea != null and queryMeter.groupByArea == true">
                pv.area_code,
                a.area_name,
            </when>
            <otherwise>
                '-1' as area_code,
                '全部' as area_name,
            </otherwise>
        </choose>
        pv.date as statistic_date,
        'DAY' as time_dimension,
        ROUND(SUM(COALESCE(pv.gen_elec_quantity, 0)), 2) as gen_elec_quantity,
        ROUND(SUM(COALESCE(pv.use_elec_quantity, 0)), 2) as use_elec_quantity,
        ROUND(SUM(COALESCE(pv.up_elec_quantity, 0)), 2) as up_elec_quantity,
        ROUND(SUM(COALESCE(pv.up_elec_earn, 0)), 2) as up_elec_earn,
        NOW() as create_time
        FROM adm_ems_pv_supply_h pv
        LEFT JOIN adm_area a ON pv.area_code = a.area_code
        <where>
            <if test="queryMeter.areaCode != null and queryMeter.areaCode != '' and queryMeter.areaCode != '-1'">
                AND pv.area_code = #{queryMeter.areaCode}
            </if>
            <if test="queryMeter.startRecTime != null and queryMeter.startRecTime != ''">
                AND pv.date &gt;= DATE(#{queryMeter.startRecTime})
            </if>
            <if test="queryMeter.endRecTime != null and queryMeter.endRecTime != ''">
                AND pv.date &lt;= DATE(#{queryMeter.endRecTime})
            </if>
            AND pv.date IS NOT NULL
        </where>
        GROUP BY
        <choose>
            <when test="queryMeter.areaCode != null and queryMeter.areaCode != '' and queryMeter.areaCode != '-1'">
                pv.area_code, a.area_name,
            </when>
            <when test="queryMeter.groupByArea != null and queryMeter.groupByArea == true">
                pv.area_code, a.area_name,
            </when>
        </choose>
        pv.date
        ORDER BY
        <if test="queryMeter.groupByArea != null and queryMeter.groupByArea == true">
            a.area_name ASC,
        </if>
        pv.date ${queryMeter.orderFlag}
    </select>

    <!-- ==================== 光伏产能按月汇总统计 ==================== -->
    <select id="selectPvSupplyMonthlyList" parameterType="com.ruoyi.ems.model.QueryMeter" resultMap="PvSupplyVOResult">
        SELECT
        <choose>
            <!-- 指定了具体区域 -->
            <when test="queryMeter.areaCode != null and queryMeter.areaCode != '' and queryMeter.areaCode != '-1'">
                pv.area_code,
                a.area_name,
            </when>
            <!-- 全部区域 + 需要按区域分组（报表场景） -->
            <when test="queryMeter.groupByArea != null and queryMeter.groupByArea == true">
                pv.area_code,
                a.area_name,
            </when>
            <!-- 全部区域 + 不分组（分析场景，返回汇总） -->
            <otherwise>
                '-1' as area_code,
                '全部' as area_name,
            </otherwise>
        </choose>
        DATE(CONCAT(DATE_FORMAT(pv.date, '%Y-%m'), '-01')) as statistic_date,
        DATE_FORMAT(pv.date, '%Y-%m') as statistic_month,
        'MONTH' as time_dimension,
        ROUND(SUM(COALESCE(pv.gen_elec_quantity, 0)), 2) as gen_elec_quantity,
        ROUND(SUM(COALESCE(pv.use_elec_quantity, 0)), 2) as use_elec_quantity,
        ROUND(SUM(COALESCE(pv.up_elec_quantity, 0)), 2) as up_elec_quantity,
        ROUND(SUM(COALESCE(pv.up_elec_earn, 0)), 2) as up_elec_earn,
        NOW() as create_time
        FROM adm_ems_pv_supply_h pv
        LEFT JOIN adm_area a ON pv.area_code = a.area_code
        <where>
            <if test="queryMeter.areaCode != null and queryMeter.areaCode != '' and queryMeter.areaCode != '-1'">
                AND pv.area_code = #{queryMeter.areaCode}
            </if>
            <if test="queryMeter.startRecTime != null and queryMeter.startRecTime != ''">
                AND pv.date &gt;= DATE(#{queryMeter.startRecTime})
            </if>
            <if test="queryMeter.endRecTime != null and queryMeter.endRecTime != ''">
                AND pv.date &lt;= DATE(#{queryMeter.endRecTime})
            </if>
            AND pv.date IS NOT NULL
        </where>
        GROUP BY
        <choose>
            <!-- 指定了具体区域 -->
            <when test="queryMeter.areaCode != null and queryMeter.areaCode != '' and queryMeter.areaCode != '-1'">
                pv.area_code, a.area_name,
            </when>
            <!-- 全部区域 + 需要按区域分组（报表场景） -->
            <when test="queryMeter.groupByArea != null and queryMeter.groupByArea == true">
                pv.area_code, a.area_name,
            </when>
        </choose>
        DATE_FORMAT(pv.date, '%Y-%m')
        ORDER BY
        <if test="queryMeter.groupByArea != null and queryMeter.groupByArea == true">
            a.area_name ASC,
        </if>
        DATE_FORMAT(pv.date, '%Y-%m') ${queryMeter.orderFlag}
    </select>

    <!-- ==================== 光伏产能按年汇总统计 ==================== -->
    <select id="selectPvSupplyYearlyList" parameterType="com.ruoyi.ems.model.QueryMeter" resultMap="PvSupplyVOResult">
        SELECT
        <choose>
            <when test="queryMeter.areaCode != null and queryMeter.areaCode != '' and queryMeter.areaCode != '-1'">
                pv.area_code,
                a.area_name,
            </when>
            <when test="queryMeter.groupByArea != null and queryMeter.groupByArea == true">
                pv.area_code,
                a.area_name,
            </when>
            <otherwise>
                '-1' as area_code,
                '全部' as area_name,
            </otherwise>
        </choose>
        DATE(CONCAT(YEAR(pv.date), '-01-01')) as statistic_date,
        YEAR(pv.date) as statistic_year,
        'YEAR' as time_dimension,
        ROUND(SUM(COALESCE(pv.gen_elec_quantity, 0)), 2) as gen_elec_quantity,
        ROUND(SUM(COALESCE(pv.use_elec_quantity, 0)), 2) as use_elec_quantity,
        ROUND(SUM(COALESCE(pv.up_elec_quantity, 0)), 2) as up_elec_quantity,
        ROUND(SUM(COALESCE(pv.up_elec_earn, 0)), 2) as up_elec_earn,
        NOW() as create_time
        FROM adm_ems_pv_supply_h pv
        LEFT JOIN adm_area a ON pv.area_code = a.area_code
        <where>
            <if test="queryMeter.areaCode != null and queryMeter.areaCode != '' and queryMeter.areaCode != '-1'">
                AND pv.area_code = #{queryMeter.areaCode}
            </if>
            <if test="queryMeter.startRecTime != null and queryMeter.startRecTime != ''">
                AND pv.date &gt;= DATE(#{queryMeter.startRecTime})
            </if>
            <if test="queryMeter.endRecTime != null and queryMeter.endRecTime != ''">
                AND pv.date &lt;= DATE(#{queryMeter.endRecTime})
            </if>
            AND pv.date IS NOT NULL
        </where>
        GROUP BY
        <choose>
            <when test="queryMeter.areaCode != null and queryMeter.areaCode != '' and queryMeter.areaCode != '-1'">
                pv.area_code, a.area_name,
            </when>
            <when test="queryMeter.groupByArea != null and queryMeter.groupByArea == true">
                pv.area_code, a.area_name,
            </when>
        </choose>
        YEAR(pv.date)
        ORDER BY
        <if test="queryMeter.groupByArea != null and queryMeter.groupByArea == true">
            a.area_name ASC,
        </if>
        YEAR(pv.date) ${queryMeter.orderFlag}
    </select>

    <!-- ==================== 光伏产能汇总统计 ==================== -->
    <select id="selectPvSupplySummary" parameterType="com.ruoyi.ems.model.QueryMeter" resultMap="PvSupplyVOResult">
        SELECT
        COALESCE(#{queryMeter.areaCode}, '0') as area_code,
        '总计' as area_name,
        'SUMMARY' as time_dimension,
        ROUND(SUM(COALESCE(pv.gen_elec_quantity, 0)), 2) as gen_elec_quantity,
        ROUND(SUM(COALESCE(pv.use_elec_quantity, 0)), 2) as use_elec_quantity,
        ROUND(SUM(COALESCE(pv.up_elec_quantity, 0)), 2) as up_elec_quantity,
        ROUND(SUM(COALESCE(pv.up_elec_earn, 0)), 2) as up_elec_earn
        FROM adm_ems_pv_supply_h pv
        <where>
            <if test="queryMeter.areaCode != null and queryMeter.areaCode != '' and queryMeter.areaCode != '-1'">
                AND pv.area_code = #{queryMeter.areaCode}
            </if>
            <if test="queryMeter.startRecTime != null and queryMeter.startRecTime != ''">
                AND pv.date &gt;= DATE(#{queryMeter.startRecTime})
            </if>
            <if test="queryMeter.endRecTime != null and queryMeter.endRecTime != ''">
                AND pv.date &lt;= DATE(#{queryMeter.endRecTime})
            </if>
            AND pv.date IS NOT NULL
        </where>
    </select>

    <select id="calThisDayPv" resultType="Map">

        SELECT sum(
                       COALESCE(CAST(gen_elec_quantity AS DECIMAL(10, 2)), 0)) genQuantity,
               sum(
                       COALESCE(CAST(use_elec_quantity AS DECIMAL(10, 2)), 0)) useQuantity,
               sum(
                       COALESCE(CAST(up_elec_quantity AS DECIMAL(10, 2)), 0))  upQuantity,
               sum(
                       COALESCE(CAST(up_elec_earn AS DECIMAL(10, 2)), 0))      upEarn
        FROM adm_ems_pv_supply_h
        WHERE
            DATE = #{thisDay}
        GROUP BY
            DATE
    </select>

    <select id="calThisDayPvByH" resultType="Map">

        SELECT time_index                                                      timeIndex,
               sum(
                       COALESCE(CAST(gen_elec_quantity AS DECIMAL(10, 2)), 0)) genQuantity,
               sum(
                       COALESCE(CAST(use_elec_quantity AS DECIMAL(10, 2)), 0)) useQuantity,
               sum(
                       COALESCE(CAST(up_elec_quantity AS DECIMAL(10, 2)), 0))  upQuantity,
               sum(
                       COALESCE(CAST(up_elec_earn AS DECIMAL(10, 2)), 0))      upEarn
        FROM adm_ems_pv_supply_h
        WHERE
            DATE = #{thisDay}
        GROUP BY
            DATE, time_index;
    </select>

    <!-- 查询光伏产能按区域分布（用于饼图） -->
    <select id="selectPvSupplyDistribution" parameterType="com.ruoyi.ems.model.QueryMeter" resultMap="PvSupplyVOResult">
        SELECT
        pv.area_code,
        a.area_name,
        'DISTRIBUTION' as time_dimension,
        ROUND(SUM(COALESCE(pv.gen_elec_quantity, 0)), 2) as gen_elec_quantity,
        ROUND(SUM(COALESCE(pv.use_elec_quantity, 0)), 2) as use_elec_quantity,
        ROUND(SUM(COALESCE(pv.up_elec_quantity, 0)), 2) as up_elec_quantity,
        ROUND(SUM(COALESCE(pv.up_elec_earn, 0)), 2) as up_elec_earn,
        NOW() as create_time
        FROM adm_ems_pv_supply_h pv
        LEFT JOIN adm_area a ON pv.area_code = a.area_code
        <where>
            <!-- 关键修改：当areaCode为空或-1时，不加area_code条件，返回所有区域分组数据 -->
            <if test="queryMeter.areaCode != null and queryMeter.areaCode != '' and queryMeter.areaCode != '-1'">
                AND pv.area_code = #{queryMeter.areaCode}
            </if>
            <if test="queryMeter.startRecTime != null and queryMeter.startRecTime != ''">
                AND pv.date &gt;= DATE(#{queryMeter.startRecTime})
            </if>
            <if test="queryMeter.endRecTime != null and queryMeter.endRecTime != ''">
                AND pv.date &lt;= DATE(#{queryMeter.endRecTime})
            </if>
            AND pv.date IS NOT NULL
        </where>
        GROUP BY pv.area_code, a.area_name
        ORDER BY gen_elec_quantity DESC
    </select>

    <!-- 查询光伏产能分布明细（按区域和日期分组，用于对比柱状图） -->
    <select id="selectPvSupplyDistributionDetail" parameterType="com.ruoyi.ems.model.QueryMeter" resultMap="PvSupplyVOResult">
        SELECT
        pv.area_code,
        a.area_name,
        pv.date as statistic_date,
        'DISTRIBUTION_DETAIL' as time_dimension,
        ROUND(SUM(COALESCE(pv.gen_elec_quantity, 0)), 2) as gen_elec_quantity,
        ROUND(SUM(COALESCE(pv.use_elec_quantity, 0)), 2) as use_elec_quantity,
        ROUND(SUM(COALESCE(pv.up_elec_quantity, 0)), 2) as up_elec_quantity,
        ROUND(SUM(COALESCE(pv.up_elec_earn, 0)), 2) as up_elec_earn,
        NOW() as create_time
        FROM adm_ems_pv_supply_h pv
        LEFT JOIN adm_area a ON pv.area_code = a.area_code
        <where>
            <if test="queryMeter.areaCode != null and queryMeter.areaCode != '' and queryMeter.areaCode != '-1'">
                AND pv.area_code = #{queryMeter.areaCode}
            </if>
            <if test="queryMeter.startRecTime != null and queryMeter.startRecTime != ''">
                AND pv.date &gt;= DATE(#{queryMeter.startRecTime})
            </if>
            <if test="queryMeter.endRecTime != null and queryMeter.endRecTime != ''">
                AND pv.date &lt;= DATE(#{queryMeter.endRecTime})
            </if>
            AND pv.date IS NOT NULL
        </where>
        GROUP BY pv.area_code, a.area_name, pv.date
        ORDER BY pv.date ASC, pv.area_code ASC
    </select>

    <!-- 查询光伏产能按区域对比（堆叠柱状图数据） -->
    <select id="selectPvSupplyAreaCompare" parameterType="com.ruoyi.ems.model.QueryMeter" resultMap="PvSupplyVOResult">
        SELECT
        pv.area_code,
        a.area_name,
        'AREA_COMPARE' as time_dimension,
        ROUND(SUM(COALESCE(pv.gen_elec_quantity, 0)), 2) as gen_elec_quantity,
        ROUND(SUM(COALESCE(pv.use_elec_quantity, 0)), 2) as use_elec_quantity,
        ROUND(SUM(COALESCE(pv.up_elec_quantity, 0)), 2) as up_elec_quantity,
        ROUND(SUM(COALESCE(pv.up_elec_earn, 0)), 2) as up_elec_earn,
        NOW() as create_time
        FROM adm_ems_pv_supply_h pv
        LEFT JOIN adm_area a ON pv.area_code = a.area_code
        <where>
            <if test="queryMeter.areaCode != null and queryMeter.areaCode != '' and queryMeter.areaCode != '-1'">
                AND pv.area_code = #{queryMeter.areaCode}
            </if>
            <if test="queryMeter.startRecTime != null and queryMeter.startRecTime != ''">
                AND pv.date &gt;= DATE(#{queryMeter.startRecTime})
            </if>
            <if test="queryMeter.endRecTime != null and queryMeter.endRecTime != ''">
                AND pv.date &lt;= DATE(#{queryMeter.endRecTime})
            </if>
            AND pv.date IS NOT NULL
        </where>
        GROUP BY pv.area_code, a.area_name
        ORDER BY gen_elec_quantity DESC
    </select>

    <insert id="insertPvSupplyH" parameterType="com.ruoyi.ems.model.PvSupplyVO" useGeneratedKeys="true"
            keyProperty="id">
        insert into adm_ems_pv_supply_h
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="areaCode != null and areaCode != ''">area_code,</if>
            <if test="facsCode != null and facsCode != ''">facs_code,</if>
            <if test="recordTime != null">`record_time`,</if>
            <if test="date != null">`date`,</if>
            <if test="time != null">`time`,</if>
            <if test="timeIndex != null">time_index,</if>
            <if test="upUnitPrice != null">up_unit_price,</if>
            <if test="genElecQuantity != null">gen_elec_quantity,</if>
            <if test="useElecQuantity != null">use_elec_quantity,</if>
            <if test="upElecQuantity != null">up_elec_quantity,</if>
            <if test="upElecEarn != null">up_elec_earn,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="areaCode != null and areaCode != ''">#{areaCode},</if>
            <if test="facsCode != null and facsCode != ''">#{facsCode},</if>
            <if test="recordTime != null">#{recordTime},</if>
            <if test="date != null">#{date},</if>
            <if test="time != null">#{time},</if>
            <if test="timeIndex != null">#{timeIndex},</if>
            <if test="upUnitPrice != null">#{upUnitPrice},</if>
            <if test="genElecQuantity != null">#{genElecQuantity},</if>
            <if test="useElecQuantity != null">#{useElecQuantity},</if>
            <if test="upElecQuantity != null">#{upElecQuantity},</if>
            <if test="upElecEarn != null">#{upElecEarn},</if>
        </trim>
    </insert>

    <update id="updatePvSupplyH" parameterType="com.ruoyi.ems.model.PvSupplyVO">
        update adm_ems_pv_supply_h
        <trim prefix="SET" suffixOverrides=",">
            <if test="areaCode != null and areaCode != ''">area_code = #{areaCode},</if>
            <if test="facsCode != null and facsCode != ''">facs_code = #{facsCode},</if>
            <if test="recordTime != null and recordTime != ''">record_time = #{recordTime},</if>
            <if test="date != null">`date` = #{date},</if>
            <if test="time != null">`time` = #{time},</if>
            <if test="timeIndex != null">time_index = #{timeIndex},</if>
            <if test="upUnitPrice != null">up_unit_price = #{upUnitPrice},</if>
            <if test="genElecQuantity != null">gen_elec_quantity = #{genElecQuantity},</if>
            <if test="useElecQuantity != null">use_elec_quantity = #{useElecQuantity},</if>
            <if test="upElecQuantity != null">up_elec_quantity = #{upElecQuantity},</if>
            <if test="upElecEarn != null">up_elec_earn = #{upElecEarn},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deletePvSupplyHById" parameterType="Long">
        delete
        from adm_ems_pv_supply_h
        where id = #{id}
    </delete>

    <delete id="deletePvSupplyHByIds" parameterType="String">
        delete from adm_ems_pv_supply_h where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
</mapper>
