<?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.xinelu.manage.mapper.sysarea.SysAreaMapper">

    <resultMap type="SysArea" id="SysAreaResult">
        <result property="id" column="id"/>
        <result property="parentId" column="parent_id"/>
        <result property="areaCode" column="area_code"/>
        <result property="areaName" column="area_name"/>
        <result property="areaLevel" column="area_level"/>
        <result property="parentCode" column="parent_code"/>
        <result property="sort" column="sort"/>
        <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,
               area_code,
               area_name,
               area_level,
               parent_code,
               sort,
               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="parentId != null ">
                and parent_id = #{parentId}
            </if>
            <if test="areaCode != null  and areaCode != ''">
                and area_code = #{areaCode}
            </if>
            <if test="areaName != null  and areaName != ''">
                and area_name like concat( #{areaName}, '%')
            </if>
            <if test="parentCode != null  and parentCode != ''">
                and parent_code = #{parentCode}
            </if>
            <if test="areaLevel != null ">
                and area_level = #{areaLevel}
            </if>
            <if test="sort != null ">
                and sort = #{sort}
            </if>
        </where>
        order by sort
    </select>

    <select id="getSubordinateRegionsFindSuperiorRegions" parameterType="String"
            resultType="com.xinelu.manage.vo.sysarea.SysAreaVO">
        SELECT province.area_name  province_name,
               province.area_code  province_code,
               city.area_name      city_name,
               city.area_code      city_code,
               region.area_name    region_name,
               region.area_code    region_code,
               street.area_name    street_name,
               street.area_code    street_code,
               community.area_name community_name,
               community.area_code community_code
        FROM sys_area province,
             sys_area city,
             sys_area region,
             sys_area street,
             sys_area community
        WHERE city.parent_code = province.area_code
          AND region.parent_code = city.area_code
          AND street.parent_code = region.area_code
          AND community.parent_code = street.area_code
          AND community.area_code = #{areaCode}
        UNION ALL
        SELECT province.area_name province_name,
               province.area_code province_code,
               city.area_name     city_name,
               city.area_code     city_code,
               region.area_name   region_name,
               region.area_code   region_code,
               street.area_name   street_name,
               street.area_code   street_code,
               ''                 community_name,
               ''                 community_code
        FROM sys_area province,
             sys_area city,
             sys_area region,
             sys_area street
        WHERE city.parent_code = province.area_code
          AND region.parent_code = city.area_code
          AND street.parent_code = region.area_code
          and street.area_code = #{areaCode} limit 1;
    </select>
</mapper>
