<?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="remoteSigns" column="remote_signs"/>
        <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,
               remote_signs,
               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="remoteSigns != null">
                and remote_signs = #{remoteSigns}
            </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="selectSysAreaById" resultType="com.xinelu.manage.domain.sysarea.SysArea"
            resultMap="SysAreaResult">
        <include refid="selectSysAreaVo"/>
        <where>
            <if test="id != null  ">
                and id = #{id}
            </if>
            <if test="areaCode != null  and areaCode != ''">
                and area_code = #{areaCode}
            </if>
        </where>
    </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="areaCode != null">area_code,
            </if>
            <if test="areaName != null">area_name,
            </if>
            <if test="areaLevel != null">area_level,
            </if>
            <if test="parentCode != null">parent_code,
            </if>
            <if test="remoteSigns != null">remote_signs,
            </if>
            <if test="sort != null">sort,
            </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="areaCode != null">#{areaCode},
            </if>
            <if test="areaName != null">#{areaName},
            </if>
            <if test="areaLevel != null">#{areaLevel},
            </if>
            <if test="parentCode != null">#{parentCode},
            </if>
            <if test="remoteSigns != null">#{remoteSigns},
            </if>
            <if test="sort != null">#{sort},
            </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="areaCode != null">area_code =
                #{areaCode},
            </if>
            <if test="areaName != null">area_name =
                #{areaName},
            </if>
            <if test="areaLevel != null">area_level =
                #{areaLevel},
            </if>
            <if test="parentCode != null">parent_code =
                #{parentCode},
            </if>
            <if test="remoteSigns != null">remote_signs =
                #{remoteSigns},
            </if>
            <if test="sort != null">sort =
                #{sort},
            </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="getCityInfoList"
            resultType="com.xinelu.manage.vo.sysarea.AreaInfoVO">
        SELECT id,
               parent_id,
               area_code,
               area_name,
               area_level,
               sort
        FROM sys_area
        WHERE parent_code = #{areaCode}
          AND remote_signs &lt;&gt; #{remoteSigns}
    </select>

    <select id="getStreetInfoList"
            resultType="com.xinelu.manage.vo.sysarea.AreaInfoVO">
        SELECT
        id,
        parent_id,
        area_code,
        area_name,
        area_level,
        sort
        FROM
        sys_area
        <where>
            <if test="parentIdList != null and parentIdList.size() > 0">
                parent_id IN
                <foreach item="parentId" collection="parentIdList" open="(" separator="," close=")">
                    #{parentId}
                </foreach>
            </if>
        </where>
    </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
        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}
        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_name,
               ''                 street_code
        FROM sys_area province,
             sys_area city,
             sys_area region
        WHERE city.parent_code = province.area_code
          AND region.parent_code = city.area_code
          AND region.area_code = #{areaCode} limit 1;
    </select>


    <select id="getNurseStationAreaByList" parameterType="com.xinelu.manage.vo.sysarea.SysAreaVO"
            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
        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
        <if test="areaCodeList != null and areaCodeList.size() > 0">
            and street.area_code in
            <foreach item="areaCode" collection="areaCodeList" open="(" separator="," close=")">
                #{areaCode}
            </foreach>
        </if>
    </select>

    <select id="getNurseStationGoodsAreaByList" parameterType="com.xinelu.manage.vo.sysarea.SysAreaVO"
            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_name,
        '' street_code
        FROM
        sys_area province,
        sys_area city,
        sys_area region
        WHERE
        city.parent_code = province.area_code
        AND region.parent_code = city.area_code
        <if test="areaCodeList != null and areaCodeList.size() > 0">
            and region.area_code in
            <foreach item="areaCode" collection="areaCodeList" open="(" separator="," close=")">
                #{areaCode}
            </foreach>
        </if>
    </select>


    <select id="getNurseStationAreaByCode" parameterType="com.xinelu.manage.vo.sysarea.SysAreaVO"
            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
        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}
        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_name,
               ''                 street_code
        FROM sys_area province,
             sys_area city,
             sys_area region
        WHERE city.parent_code = province.area_code
          AND region.parent_code = city.area_code
          AND region.area_code = #{areaCode} limit 1;
    </select>

    <select id="selectParentName" resultType="com.xinelu.manage.vo.sysarea.ParentAreaVO">
        SELECT sa.id,
               sa.parent_code,
               (select area_name FROM sys_area where area_code = sa.parent_code) parentName,
               sa.area_code,
               sa.area_name,
               sa.area_level,
               sa.sort,
               sa.remote_signs
        FROM sys_area sa
        WHERE sa.id = #{id}
    </select>
</mapper>
