<?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.flossom.common.core.mapper.SysRegionMapper">
    
    <resultMap type="SysRegion" id="SysRegionResult">
        <result property="id"    column="id"    />
        <result property="name"    column="name"    />
        <result property="parentId"    column="parent_id"    />
        <result property="level"    column="level"    />
    </resultMap>

    <sql id="selectSysRegionVo">
        select id, name, parent_id, level from sys_region
    </sql>

    <select id="selectSysRegionList" parameterType="SysRegion" resultMap="SysRegionResult">
        <include refid="selectSysRegionVo"/>
        <where>  
            <if test="name != null  and name != ''"> and name like concat('%', #{name}, '%')</if>
            <if test="parentId != null "> and parent_id = #{parentId}</if>
            <if test="level != null "> and level = #{level}</if>
        </where>
    </select>
    
    <select id="selectSysRegionById" parameterType="Long" resultMap="SysRegionResult">
        <include refid="selectSysRegionVo"/>
        where id = #{id}
    </select>

    <select id="selectSysAreaByPid" resultMap="SysRegionResult">
        <include refid="selectSysRegionVo"/>
        WHERE
            parent_id = #{pid}
        ORDER BY
            id ASC
    </select>

    <insert id="insertSysRegion" parameterType="SysRegion" useGeneratedKeys="true" keyProperty="id">
        insert into sys_region
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="name != null and name != ''">name,</if>
            <if test="parentId != null">parent_id,</if>
            <if test="level != null">level,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="name != null and name != ''">#{name},</if>
            <if test="parentId != null">#{parentId},</if>
            <if test="level != null">#{level},</if>
         </trim>
    </insert>

    <update id="updateSysRegion" parameterType="SysRegion">
        update sys_region
        <trim prefix="SET" suffixOverrides=",">
            <if test="name != null and name != ''">name = #{name},</if>
            <if test="parentId != null">parent_id = #{parentId},</if>
            <if test="level != null">level = #{level},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteSysRegionById" parameterType="Long">
        delete from sys_region where id = #{id}
    </delete>

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