<?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.FdEnergyPriceConfigMapper">
    
    <resultMap type="FdEnergyPriceConfig" id="FdEnergyPriceConfigResult">
        <result property="id"    column="id"    />
        <result property="cfgCode"    column="cfg_code"    />
        <result property="areaCode"    column="area_code"    />
        <result property="areaName"    column="area_name"    />
        <result property="meterCls"    column="meter_cls"    />
        <result property="meterClsName"    column="meter_cls_name"    />
        <result property="unitPrice"    column="unit_price"    />
        <result property="meterUnit"    column="meter_unit"    />
    </resultMap>

    <sql id="selectConfigVo">
        select id, cfg_code, area_code, meter_cls, unit_price, meter_unit from adm_fd_energyprice_config
    </sql>

    <select id="selectList" parameterType="FdEnergyPriceConfig" resultMap="FdEnergyPriceConfigResult">
        select
            c.id, c.cfg_code, c.area_code, a.area_name, c.meter_cls, ec.`name` as meter_cls_name, c.unit_price, c.meter_unit from adm_fd_energyprice_config c
          left join adm_area a on c.area_code = a.area_code
          left join dim_gb_ems_cls ec on c.meter_cls = ec.`code`
        <where>  
            <if test="cfgCode != null  and cfgCode != ''"> and c.cfg_code = #{cfgCode}</if>
            <if test="areaCode != null  and areaCode != ''"> and c.area_code = #{areaCode}</if>
            <if test="meterCls != null "> and c.meter_cls = #{meterCls}</if>
        </where>
    </select>
    
    <select id="selectConfigById" parameterType="Long" resultMap="FdEnergyPriceConfigResult">
        <include refid="selectConfigVo"/>
        where id = #{id}
    </select>
        
    <insert id="insertConfig" parameterType="FdEnergyPriceConfig" useGeneratedKeys="true" keyProperty="id">
        insert into adm_fd_energyprice_config
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="cfgCode != null and cfgCode != ''">cfg_code,</if>
            <if test="areaCode != null">area_code,</if>
            <if test="meterCls != null">meter_cls,</if>
            <if test="unitPrice != null">unit_price,</if>
            <if test="meterUnit != null">meter_unit,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="cfgCode != null and cfgCode != ''">#{cfgCode},</if>
            <if test="areaCode != null">#{areaCode},</if>
            <if test="meterCls != null">#{meterCls},</if>
            <if test="unitPrice != null">#{unitPrice},</if>
            <if test="meterUnit != null">#{meterUnit},</if>
         </trim>
    </insert>

    <update id="updateConfig" parameterType="FdEnergyPriceConfig">
        update adm_fd_energyprice_config
        <trim prefix="SET" suffixOverrides=",">
            <if test="cfgCode != null and cfgCode != ''">cfg_code = #{cfgCode},</if>
            <if test="areaCode != null">area_code = #{areaCode},</if>
            <if test="meterCls != null">meter_cls = #{meterCls},</if>
            <if test="unitPrice != null">unit_price = #{unitPrice},</if>
            <if test="meterUnit != null">meter_unit = #{meterUnit},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteById" parameterType="Long">
        delete from adm_fd_energyprice_config where id = #{id}
    </delete>
</mapper>