<?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.PvSupplyHMapper">
    <resultMap type="com.ruoyi.ems.domain.PvSupplyH" 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="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.`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_service_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.PvSupplyH" 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>
        </where>
    </select>

    <select id="selectAreaSupplyH" parameterType="com.ruoyi.ems.domain.PvSupplyH" resultMap="PvSupplyHResult">
        SELECT area_code, `date`, time_index,
               SUM( gen_elec_quantity ) as gen_elec_quantity,
               SUM( use_elec_quantity ) as use_elec_quantity,
               SUM( up_elec_quantity ) as up_elec_quantity,
               SUM( up_elec_earn ) as up_elec_earn
        FROM
            adm_ems_pv_supply_h
        WHERE
            area_code = #{areaCode}
          AND DATE = #{date,jdbcType=DATE}
        GROUP BY
            area_code,
            time_index
        ORDER BY
            time_index ASC
    </select>

    <select id="selectPvSupplyHById" parameterType="Long" resultMap="PvSupplyHResult">
        <include refid="selectPvSupplyHVo"/>
        where id = #{id}
    </select>

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