<?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.lyms.platform.permission.dao.slave.RegionsMapper">

    <resultMap id="RegionsResultMap" type="com.lyms.platform.permission.model.Regions">
        <id column="id" property="id" jdbcType="INTEGER"/>
        <result column="parent_id" property="parentId" jdbcType="INTEGER"/>
        <result column="region_name" property="regionName" jdbcType="VARCHAR"/>
        <result column="level" property="level" jdbcType="INTEGER"/>
    </resultMap>


    <insert id="addRegions" parameterType="com.lyms.platform.permission.model.Regions">
        <selectKey order="AFTER" keyProperty="id" resultType="java.lang.Integer">SELECT LAST_INSERT_ID()</selectKey>
        insert into regions (parent_id,region_name,level
        <if test="id != null and id >= 0">
            ,id
        </if>
        ) values (#{parentId},#{regionName},#{level}
        <if test="id != null and id >= 0">
        ,#{id}
        </if>
        )
    </insert>


    <update id="updateRegions" parameterType="com.lyms.platform.permission.model.Regions">
        update regions
        <set>
            <if test="parentId != null and parentId >= 0">
                parent_id = #{parentId,jdbcType=INTEGER},
            </if>
            <if test="regionName != null and regionName != ''">
                region_name = #{regionName,jdbcType=VARCHAR},
            </if>
            <if test="level != null and level >= 0">
                level = #{level,jdbcType=INTEGER},
            </if>
        </set>
        where id = #{id,jdbcType=INTEGER}
    </update>


    <delete id="deleteRegions" parameterType="java.lang.Integer">
delete from regions where id = #{id,jdbcType=INTEGER}
</delete>


    <select id="getRegions" resultMap="RegionsResultMap" parameterType="java.lang.Integer">
select id,parent_id,region_name,level
 from regions where id = #{id,jdbcType=INTEGER}
</select>


    <sql id="orderAndLimit">
        <if test="sort != null and sort != '' ">
            order by ${sort}
            <if test="need != null">
                limit #{offset, jdbcType=INTEGER} , #{limit, jdbcType=INTEGER}
            </if>
        </if>
    </sql>


    <sql id="RegionsCondition">
        <where>
            1 = 1
            <if test="id != null and id >= 0">
                and id = #{id,jdbcType=INTEGER}
            </if>
            <if test="parentId != null and parentId >= 0">
                and parent_id = #{parentId,jdbcType=INTEGER}
            </if>
            <if test="regionName != null and regionName != ''">
                and region_name = #{regionName,jdbcType=VARCHAR}
            </if>
            <if test="level != null and level >= 0">
                and level = #{level,jdbcType=INTEGER}
            </if>
        </where>
    </sql>


    <select id="queryRegions" resultMap="RegionsResultMap"
            parameterType="com.lyms.platform.permission.model.RegionsQuery">
        select id,parent_id,region_name,level
        from regions
        <include refid="RegionsCondition"/>
        <include refid="orderAndLimit"/>
    </select>


    <select id="queryRegionsCount" resultType="int" parameterType="com.lyms.platform.permission.model.RegionsQuery">
        select count(1) from regions
        <include refid="RegionsCondition"/>
    </select>


</mapper>