<?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.PowerBoxMapper">

    <resultMap type="com.ruoyi.ems.domain.PowerBox" id="powerBoxResult">
        <result property="id"    column="id"    />
        <result property="boxCode"    column="box_code"    />
        <result property="boxType"    column="box_type"    />
        <result property="areaCode"    column="area_code"    />
        <result property="areaName"    column="area_name"    />
        <result property="areaShortName"    column="area_short_name"    />
        <result property="location"    column="location"    />
        <result property="boxSize"    column="box_size"    />
        <result property="gatewayAddr"    column="gateway_addr"    />
        <result property="gatewayPort"    column="gateway_port"    />
    </resultMap>

    <sql id="selectPowerBoxVo">
        select
            pb.id, pb.box_code, pb.box_type, pb.area_code, sa.area_name, sa.short_name as area_short_name, pb.location, pb.box_size, pb.gateway_addr, pb.gateway_port
        from adm_power_box pb
          LEFT JOIN adm_service_area sa on pb.area_code = sa.area_code
    </sql>

    <select id="selectPowerBoxList" parameterType="com.ruoyi.ems.domain.PowerBox" resultMap="powerBoxResult">
        <include refid="selectPowerBoxVo"/>
        <where>
            <if test="boxCode != null  and boxCode != ''"> and pb.box_code = #{boxCode}</if>
            <if test="boxType != null "> and pb.box_type = #{boxType}</if>
            <if test="areaCode != null  and areaCode != ''"> and pb.area_code = #{areaCode}</if>
            <if test="location != null  and location != ''"> and pb.location = #{location}</if>
            <if test="boxSize != null  and boxSize != ''"> and pb.box_size = #{boxSize}</if>
            <if test="gatewayAddr != null  and gatewayAddr != ''"> and pb.gateway_addr = #{gatewayAddr}</if>
            <if test="gatewayPort != null "> and pb.gateway_port = #{gatewayPort}</if>
        </where>
    </select>

    <select id="selectPowerBoxById" parameterType="Long" resultMap="powerBoxResult">
        <include refid="selectPowerBoxVo"/>
        where pb.id = #{id}
    </select>

    <select id="selectPowerBoxByCode" parameterType="String" resultMap="powerBoxResult">
        <include refid="selectPowerBoxVo"/>
        where pb.box_code = #{boxCode}
    </select>

    <insert id="insertPowerBox" parameterType="com.ruoyi.ems.domain.PowerBox" useGeneratedKeys="true" keyProperty="id">
        insert into adm_power_box
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="boxCode != null and boxCode != ''">box_code,</if>
            <if test="boxType != null">box_type,</if>
            <if test="areaCode != null">area_code,</if>
            <if test="location != null">location,</if>
            <if test="boxSize != null">box_size,</if>
            <if test="gatewayAddr != null">gateway_addr,</if>
            <if test="gatewayPort != null">gateway_port,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="boxCode != null and boxCode != ''">#{boxCode},</if>
            <if test="boxType != null">#{boxType},</if>
            <if test="areaCode != null">#{areaCode},</if>
            <if test="location != null">#{location},</if>
            <if test="boxSize != null">#{boxSize},</if>
            <if test="gatewayAddr != null">#{gatewayAddr},</if>
            <if test="gatewayPort != null">#{gatewayPort},</if>
         </trim>
    </insert>

    <update id="updatePowerBox" parameterType="com.ruoyi.ems.domain.PowerBox">
        update adm_power_box
        <trim prefix="SET" suffixOverrides=",">
            <if test="boxCode != null and boxCode != ''">box_code = #{boxCode},</if>
            <if test="boxType != null">box_type = #{boxType},</if>
            <if test="areaCode != null">area_code = #{areaCode},</if>
            <if test="location != null">location = #{location},</if>
            <if test="boxSize != null">box_size = #{boxSize},</if>
            <if test="gatewayAddr != null">gateway_addr = #{gatewayAddr},</if>
            <if test="gatewayPort != null">gateway_port = #{gatewayPort},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deletePowerBoxById" parameterType="Long">
        delete from adm_power_box where id = #{id}
    </delete>

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