<?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.ElecGwPriceConfigMapper">
    
    <resultMap type="com.ruoyi.ems.domain.ElecGwPriceConfig" id="elecPriceConfigResult">
        <result property="id"    column="id"    />
        <result property="cfgCode"    column="cfg_code"    />
        <result property="elecType"    column="elec_type"    />
        <result property="elecTypeName"    column="elec_type_name"    />
        <result property="voltageLevel"    column="voltage_level"    />
        <result property="opPrice"    column="op_price"    />
        <result property="cpPrice"    column="cp_price"    />
        <result property="pPrice"    column="p_price"    />
        <result property="vPrice"    column="v_Price"    />
        <result property="dvPrice"    column="dv_Price"    />
        <result property="maxReqPrice"    column="max_req_price"    />
        <result property="transCapacityPrice"    column="trans_capacity_price"    />
    </resultMap>

    <sql id="selectElecPriceConfigVo">
        select
            v.id, v.cfg_code, v.elec_type, t.`name` as elec_type_name, v.voltage_level, v.op_price, v.cp_price, v.p_price, v.v_Price, v.dv_Price, v.max_req_price, v.trans_capacity_price
        from adm_gw_elecprice_config v
            LEFT JOIN dim_ems_elecprice_type t ON v.elec_type = t.`code`
    </sql>

    <select id="selectElecPriceConfigList" parameterType="com.ruoyi.ems.domain.ElecGwPriceConfig" resultMap="elecPriceConfigResult">
        <include refid="selectElecPriceConfigVo"/>
        <where>  
            <if test="cfgCode != null  and cfgCode != ''"> and v.cfg_code = #{cfgCode}</if>
            <if test="elecType != null "> and v.elec_type = #{elecType}</if>
        </where>
    </select>
    
    <select id="selectElecPriceConfigById" parameterType="Long" resultMap="elecPriceConfigResult">
        <include refid="selectElecPriceConfigVo"/>
        where v.id = #{id}
    </select>

    <select id="selectElecPriceConfigByCode" parameterType="java.lang.String" resultMap="elecPriceConfigResult">
        <include refid="selectElecPriceConfigVo"/>
        where v.cfg_code = #{cfgCode}
    </select>
        
    <insert id="insertElecPriceConfig" parameterType="com.ruoyi.ems.domain.ElecGwPriceConfig" useGeneratedKeys="true" keyProperty="id">
        insert into adm_gw_elecprice_config
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="cfgCode != null and cfgCode != ''">cfg_code,</if>
            <if test="elecType != null">elec_type,</if>
            <if test="voltageLevel != null and voltageLevel != ''">voltage_level,</if>
            <if test="opPrice != null">op_price,</if>
            <if test="cpPrice != null">cp_price,</if>
            <if test="pPrice != null">p_price,</if>
            <if test="vPrice != null">v_price,</if>
            <if test="dvPrice != null">dv_price,</if>
            <if test="maxReqPrice != null">max_req_price,</if>
            <if test="transCapacityPrice != null">trans_capacity_price,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="cfgCode != null and cfgCode != ''">#{cfgCode},</if>
            <if test="elecType != null">#{elecType},</if>
            <if test="voltageLevel != null and voltageLevel != ''">#{voltageLevel},</if>
            <if test="opPrice != null">#{opPrice},</if>
            <if test="cpPrice != null">#{cpPrice},</if>
            <if test="pPrice != null">#{pPrice},</if>
            <if test="vPrice != null">#{vPrice},</if>
            <if test="dvPrice != null">#{dvPrice},</if>
            <if test="maxReqPrice != null">#{maxReqPrice},</if>
            <if test="transCapacityPrice != null">#{transCapacityPrice},</if>
         </trim>
    </insert>

    <update id="updateElecPriceConfig" parameterType="com.ruoyi.ems.domain.ElecGwPriceConfig">
        update adm_gw_elecprice_config
        <set>
            <if test="opPrice != null">op_price = #{opPrice},</if>
            <if test="opPrice == null">op_price = NULL,</if>
            <if test="cpPrice != null">cp_price = #{cpPrice},</if>
            <if test="cpPrice == null">cp_price = NULL,</if>
            <if test="pPrice != null">p_price = #{pPrice},</if>
            <if test="pPrice == null">p_price = NULL,</if>
            <if test="vPrice != null">v_price = #{vPrice},</if>
            <if test="vPrice == null">v_price = NULL,</if>
            <if test="dvPrice != null">dv_price = #{dvPrice},</if>
            <if test="dvPrice == null">dv_price = NULL,</if>
        </set>
        where id = #{id}
    </update>

    <delete id="deleteElecPriceConfigById" parameterType="Long">
        delete from adm_gw_elecprice_config where id = #{id}
    </delete>

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