<?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.ElecAttrMapper">
    
    <resultMap type="com.ruoyi.ems.domain.ElecAttr" id="ElecAttrResult">
        <result property="id"    column="id"    />
        <result property="areaCode"    column="area_code"    />
        <result property="areaName"    column="area_name"    />
        <result property="elecType"    column="elec_type"    />
        <result property="elecTypeName"    column="elec_type_name"    />
        <result property="priceCode"    column="price_code"    />
        <result property="voltageLevel"    column="voltage_level"    />
        <result property="reqCapacityFlag"    column="req_capacity_flag"    />
        <result property="transCapacity"    column="trans_capacity"    />
        <result property="reqQuantity"    column="req_quantity"    />
    </resultMap>

    <sql id="selectElecAttrVo">
        SELECT attr.id, attr.area_code, area.area_name, price.elec_type, attr.price_code, t.`name` as elec_type_name, price.voltage_level, attr.req_capacity_flag, attr.trans_capacity, attr.req_quantity
          FROM adm_area_elec_attr attr
            LEFT JOIN adm_area area ON attr.area_code = area.area_code
            LEFT JOIN adm_gw_elecprice_config price ON attr.price_code = price.cfg_code
            LEFT JOIN dim_ems_elecprice_type t ON t.`code` = price.elec_type
    </sql>

    <select id="selectElecAttrList" parameterType="com.ruoyi.ems.domain.ElecAttr" resultMap="ElecAttrResult">
        <include refid="selectElecAttrVo"/>
        <where>  
            <if test="areaCode != null  and areaCode != ''"> and attr.area_code = #{areaCode}</if>
            <if test="priceCode != null  and priceCode != ''"> and attr.price_code = #{priceCode}</if>
            <if test="reqCapacityFlag != null "> and attr.req_capacity_flag = #{reqCapacityFlag}</if>
        </where>
    </select>
    
    <select id="selectElecAttrById" parameterType="Long" resultMap="ElecAttrResult">
        <include refid="selectElecAttrVo"/>
        where attr.id = #{id}
    </select>
        
    <insert id="insertElecAttr" parameterType="com.ruoyi.ems.domain.ElecAttr" useGeneratedKeys="true" keyProperty="id">
        insert into adm_area_elec_attr
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="areaCode != null and areaCode != ''">area_code,</if>
            <if test="priceCode != null and priceCode != ''">price_code,</if>
            <if test="reqCapacityFlag != null">req_capacity_flag,</if>
            <if test="transCapacity != null">trans_capacity,</if>
            <if test="reqQuantity != null">req_quantity,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="areaCode != null and areaCode != ''">#{areaCode},</if>
            <if test="priceCode != null and priceCode != ''">#{priceCode},</if>
            <if test="reqCapacityFlag != null">#{reqCapacityFlag},</if>
            <if test="transCapacity != null">#{transCapacity},</if>
            <if test="reqQuantity != null">#{reqQuantity},</if>
         </trim>
    </insert>

    <update id="updateElecAttr" parameterType="com.ruoyi.ems.domain.ElecAttr">
        update adm_area_elec_attr
        <trim prefix="SET" suffixOverrides=",">
            area_code = #{areaCode},
            price_code = #{priceCode},
            req_capacity_flag = #{reqCapacityFlag},
            trans_capacity = #{transCapacity},
            req_quantity = #{reqQuantity},
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteElecAttrById" parameterType="Long">
        delete from adm_area_elec_attr where id = #{id}
    </delete>

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