<?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.xinyilu.system.mapper.systemarea.SysAreaMapper">

    <resultMap type="SysArea" id="SysAreaResult">
        <result property="id" column="id"/>
        <result property="parentId" column="parent_id"/>
        <result property="name" column="name"/>
        <result property="code" column="code"/>
        <result property="level" column="level"/>
        <result property="sort" column="sort"/>
        <result property="delFlag" column="del_flag"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>

    <sql id="selectSysAreaVo">
        select id, parent_id, name, code, level, sort, del_flag, create_by, create_time, update_by, update_time from sys_area
    </sql>

    <select id="selectSysAreaList" parameterType="SysArea" resultMap="SysAreaResult">
        <include refid="selectSysAreaVo"/>
        <where>
            <if test="id != null ">
                and id = #{id}
            </if>
            <if test="parentId != null ">
                and parent_id = #{parentId}
            </if>
            <if test="name != null  and name != ''">
                and name like concat('%', #{name}, '%')
            </if>
            <if test="code != null  and code != ''">
                and code = #{code}
            </if>
            <if test="sort != null ">
                and sort = #{sort}
            </if>
            <if test="level != null ">
                and level = #{level}
            </if>
            and del_flag = 0
        </where>
        order by parent_id, sort asc
    </select>

    <select id="selectSysAreaById" parameterType="Long"
            resultMap="SysAreaResult">
        <include refid="selectSysAreaVo"/>
        where id = #{id}
    </select>

    <insert id="insertSysArea" parameterType="SysArea" useGeneratedKeys="true"
            keyProperty="id">
        insert into sys_area
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="parentId != null">parent_id,
            </if>
            <if test="name != null">name,
            </if>
            <if test="code != null">code,
            </if>
            <if test="level != null">level,
            </if>
            <if test="sort != null">sort,
            </if>
            <if test="delFlag != null">del_flag,
            </if>
            <if test="createBy != null">create_by,
            </if>
            <if test="createTime != null">create_time,
            </if>
            <if test="updateBy != null">update_by,
            </if>
            <if test="updateTime != null">update_time,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="parentId != null">#{parentId},
            </if>
            <if test="name != null">#{name},
            </if>
            <if test="code != null">#{code},
            </if>
            <if test="level != null">#{level},
            </if>
            <if test="sort != null">#{sort},
            </if>
            <if test="delFlag != null">#{delFlag},
            </if>
            <if test="createBy != null">#{createBy},
            </if>
            <if test="createTime != null">#{createTime},
            </if>
            <if test="updateBy != null">#{updateBy},
            </if>
            <if test="updateTime != null">#{updateTime},
            </if>
        </trim>
    </insert>

    <update id="updateSysArea" parameterType="SysArea">
        update sys_area
        <trim prefix="SET" suffixOverrides=",">
            <if test="parentId != null">parent_id =
                #{parentId},
            </if>
            <if test="name != null">name =
                #{name},
            </if>
            <if test="code != null">code =
                #{code},
            </if>
            <if test="level != null">level =
                #{level},
            </if>
            <if test="sort != null">sort =
                #{sort},
            </if>
            <if test="delFlag != null">del_flag =
                #{delFlag},
            </if>
            <if test="createBy != null">create_by =
                #{createBy},
            </if>
            <if test="createTime != null">create_time =
                #{createTime},
            </if>
            <if test="updateBy != null">update_by =
                #{updateBy},
            </if>
            <if test="updateTime != null">update_time =
                #{updateTime},
            </if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteSysAreaById" parameterType="Long">
        delete from sys_area where id = #{id}
    </delete>

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

    <select id="getSysAreaByCodeAndName" parameterType="java.lang.String" resultType="int">
        select count(1) from sys_area where code = #{code} and del_flag = 0
    </select>

    <select id="getCityAreaInfoList" parameterType="SysArea" resultMap="SysAreaResult">
        <include refid="selectSysAreaVo"/>
        <where>
            del_flag = 0
            <if test="parentId != null ">
                and id = (select id from sys_area where parent_id = #{parentId}) or parent_id = (select id from
                sys_area where parent_id = #{parentId})
            </if>
        </where>
        order by parent_id, sort asc
    </select>

    <select id="getThreeAreaInfo"
            resultType="com.xinyilu.system.domain.vo.sysarea.AreaInfoVO">
        SELECT
            province.id province_id,
            province.`code` province_code,
            province.`name` province_name,
            city.id city_id,
            city.`code` city_code,
            city.`name` city_name,
            region.id region_id,
            region.`code` region_code,
            region.`name` region_name
        FROM
            sys_area province,
            sys_area city,
            sys_area region
        WHERE
            city.parent_id = province.id
            AND region.parent_id = city.id
            AND region.id = #{id}
    </select>

    <select id="getSecondAreaInfo"
            resultType="com.xinyilu.system.domain.vo.sysarea.AreaInfoVO">
        SELECT
            province.id province_id,
            province.`code` province_code,
            province.`name` province_name,
            city.id city_id,
            city.`code` city_code,
            city.`name` city_name
        FROM
            sys_area province,
            sys_area city
        WHERE
            city.parent_id = province.id
            AND city.id = #{id}
    </select>

    <select id="getSysAreaIdList" parameterType="long" resultType="long">
        select id from sys_area where parent_id = #{parentId}
    </select>

    <select id="getSubordinateSysAreaIdList" resultType="com.xinyilu.system.domain.systemarea.SysArea">
        <include refid="selectSysAreaVo"/>
        <where>
            <if test="parentId != null ">
                and parent_id = #{parentId}
            </if>
            and del_flag = 0
        </where>
    </select>
</mapper>