<?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.system.mapper.RegionMapper">
    
    <resultMap type="Region" id="RegionResult">
        <result property="code"    column="code"    />
        <result property="name"    column="name"    />
        <result property="parentCode"    column="parent_code"    />
        <result property="level"    column="level"    />
    </resultMap>

    <sql id="selectRegionVo">
        select code, name, parent_code, level from region
    </sql>

    <select id="selectRegionList" parameterType="Region" resultMap="RegionResult">
        <include refid="selectRegionVo"/>
        <where>  
            <if test="name != null  and name != ''"> and name like concat('%', #{name}, '%')</if>
            <if test="parentCode != null  and parentCode != ''"> and parent_code = #{parentCode}</if>
            <if test="level != null and level != ''"> and level = #{level}</if>
        </where>
    </select>
    
    <select id="selectRegionByCode" parameterType="String" resultMap="RegionResult">
        <include refid="selectRegionVo"/>
        where code = #{code}
    </select>

    <insert id="insertRegion" parameterType="Region">
        insert into region
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="code != null">code,</if>
            <if test="name != null and name != ''">name,</if>
            <if test="parentCode != null">parent_code,</if>
            <if test="level != null">level,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="code != null">#{code},</if>
            <if test="name != null and name != ''">#{name},</if>
            <if test="parentCode != null">#{parentCode},</if>
            <if test="level != null">#{level},</if>
         </trim>
    </insert>

    <update id="updateRegion" parameterType="Region">
        update region
        <trim prefix="SET" suffixOverrides=",">
            <if test="name != null and name != ''">name = #{name},</if>
            <if test="parentCode != null">parent_code = #{parentCode},</if>
            <if test="level != null">level = #{level},</if>
        </trim>
        where code = #{code}
    </update>

    <delete id="deleteRegionByCode" parameterType="String">
        delete from region where code = #{code}
    </delete>

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

    <insert id="batchInsert" parameterType="java.util.List">
        insert into region (code, name, parent_code, level) values
        <foreach collection="list" item="item" separator=",">
            (#{item.code}, #{item.name}, #{item.parentCode}, #{item.level})
        </foreach>
    </insert>
</mapper>