<?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.domain.ElecPvSupplyH" id="PvSupplyHResult">
        <result property="id" column="id"/>
        <result property="areaCode" column="area_code"/>
        <result property="areaName" column="area_name"/>
        <result property="areaShortName" column="area_short_name"/>
        <result property="facsCode" column="facs_code"/>
        <result property="facsName" column="facs_name"/>
        <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"/>
    </resultMap>

    <sql id="selectPvSupplyHVo">
        select pv.id,
               pv.area_code,
               a.area_name,
               a.short_name as area_short_name,
               pv.facs_code,
               f.facs_name,
               pv.`record_time`,
               pv.`date`,
               pv.`time`,
               pv.time_index,
               pv.up_unit_price,
               pv.gen_elec_quantity,
               pv.use_elec_quantity,
               pv.up_elec_quantity,
               pv.up_elec_earn
        from adm_ems_pv_supply_h pv
                 LEFT JOIN adm_area a ON pv.`area_code` = a.`area_code`
                 LEFT JOIN adm_ems_facs f ON pv.`facs_code` = f.`facs_code`
    </sql>

    <select id="selectPvSupplyHList" parameterType="com.ruoyi.ems.domain.ElecPvSupplyH" resultMap="PvSupplyHResult">
        <include refid="selectPvSupplyHVo"/>
        <where>
            <if test="areaCode != null  and areaCode != ''">and pv.area_code = #{areaCode}</if>
            <if test="facsCode != null  and facsCode != ''">and pv.facs_code = #{facsCode}</if>
            <if test="date != null ">and pv.`date` = #{date}</if>
            <if test="time != null ">and pv.`time` = #{time}</if>
            <if test="timeIndex != null ">and pv.time_index = #{timeIndex}</if>
            <if test="upUnitPrice != null ">and pv.up_unit_price = #{upUnitPrice}</if>
            <if test="genElecQuantity != null ">and pv.gen_elec_quantity = #{genElecQuantity}</if>
            <if test="useElecQuantity != null ">and pv.use_elec_quantity = #{useElecQuantity}</if>
            <if test="upElecQuantity != null ">and pv.up_elec_quantity = #{upElecQuantity}</if>
            <if test="upElecEarn != null ">and pv.up_elec_earn = #{upElecEarn}</if>
            <if test="startRecTime != null  and startRecTime != '' and endRecTime != null and endRecTime !=''">
                and pv.`record_time` &gt;= #{startRecTime} and pv.`record_time` &lt;= #{endRecTime}
            </if>
        </where>
        ORDER BY
        pv.`date` DESC,
        pv.`time` DESC,
        pv.`time_index`
    </select>

    <select id="selectPvSupplyHAll" parameterType="com.ruoyi.ems.domain.ElecPvSupplyH" resultMap="PvSupplyHResult">
        SELECT
            facCategory.`name` facs_name,
            pv.`record_time`,
            pv.`date`,
            pv.`time`,
            pv.time_index,
            pv.up_unit_price,
            sum(COALESCE(pv.gen_elec_quantity,0)) gen_elec_quantity,
            sum(COALESCE(pv.use_elec_quantity,0)) use_elec_quantity,
            sum(COALESCE(pv.up_elec_quantity,0)) up_elec_quantity,
            sum(COALESCE(pv.up_elec_earn,0)) up_elec_earn
        FROM
            adm_ems_pv_supply_h pv
                INNER JOIN adm_ems_facs f ON pv.`facs_code` = f.`facs_code`
                INNER JOIN dim_ems_facs_category facCategory ON f.facs_category = facCategory.`code`
        <where>
            <if test="startRecTime != null  and startRecTime != '' and endRecTime != null and endRecTime !=''">
                and pv.`record_time` &gt;= #{startRecTime} and pv.`record_time` &lt;= #{endRecTime}
            </if>
        </where>
        GROUP BY
            facCategory.`code`,
            pv.DATE,
            pv.time_index,
            pv.TIME
        ORDER BY
            pv.`date` DESC,
            pv.`time` DESC,
            pv.`time_index`
    </select>

    <select id="selectPvSupplyDayList" parameterType="com.ruoyi.ems.model.QueryIndex" resultMap="PvSupplyHResult">
        SELECT
            <if test="facsSubCategory != null and facsSubCategory !=''">
              sc.`name` as facs_name,
            </if>
            <if test="areaCode != null and areaCode != '' and areaCode != '-1'">
              pv.area_code,
            </if>
            pv.`date`,
            sum(COALESCE(pv.gen_elec_quantity,0)) gen_elec_quantity,
            sum(COALESCE(pv.use_elec_quantity,0)) use_elec_quantity,
            sum(COALESCE(pv.up_elec_quantity,0)) up_elec_quantity,
            sum(COALESCE(pv.up_elec_earn,0)) up_elec_earn
        FROM adm_ems_pv_supply_h pv
            <if test="facsSubCategory != null and facsSubCategory !=''">
              LEFT JOIN adm_ems_facs f ON pv.`facs_code` = f.`facs_code`
              LEFT JOIN dim_ems_facs_subcategory sc ON f.facs_subcategory = sc.`code`
            </if>
        <where>
            <if test="areaCode != null and areaCode != '' and areaCode != '-1'">
                and pv.area_code = #{areaCode}
            </if>
            <if test="startTime != null and startTime != '' and endTime != null and endTime !=''">
                and pv.`date` &gt;= #{startTime} and pv.`date` &lt;= #{endTime}
            </if>
            <if test="facsSubCategory != null and facsSubCategory !=''">
                and f.facs_subcategory = #{facsSubCategory}
            </if>
        </where>
        GROUP BY pv.`date`
        ORDER BY pv.`date`
    </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="selectPvSupplyHById" parameterType="Long" resultMap="PvSupplyHResult">
        <include refid="selectPvSupplyHVo"/>
        where id = #{id}
    </select>

    <insert id="insertPvSupplyH" parameterType="com.ruoyi.ems.domain.ElecPvSupplyH" 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.domain.ElecPvSupplyH">
        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>
