<?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.ssm.manage.mapper.RegionListMapper">

    <resultMap id="BaseResultMap" type="com.ssm.manage.entity.RegionList">
        <result property="id" column="id"/>
        <result property="regionName" column="region_name"/>
        <result property="regionAddress" column="region_address"/>
        <result property="inputId" column="input_id"/>
        <result property="inputName" column="input_name"/>
        <result property="inputDate" column="input_date"/>
        <result property="updateId" column="update_id"/>
        <result property="updateName" column="update_name"/>
        <result property="updateDate" column="update_date"/>
        <result property="storeId" column="store_id"/>
        <result property="manageId" column="manage_id"/>
        <result property="manageName" column="manage_name"/>
        <result property="mobilePhone" column="mobile_phone"/>
        <result property="regionNum" column="region_num"/>
        <result property="parentId" column="parent_id"/>
        <result property="beisenId" column="beisen_id"/>
    </resultMap>

    <sql id="Base_Column_List">
        id
        , region_name, region_address, input_id, input_name, input_date, update_id,
            update_name, update_date ,store_id ,manage_id ,manage_name ,mobile_phone,
            region_num,parent_id,beisen_id
    </sql>

    <select id="queryAll" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"></include>
        from region_list
        <where>
            <if test="id != null">
                and id = #{id}
            </if>
            <if test="regionName != null and regionName != ''">
                and region_name = #{regionName}
            </if>
            <if test="regionAddress != null and regionAddress != ''">
                and region_address = #{regionAddress}
            </if>
            <if test="inputId != null">
                and input_id = #{inputId}
            </if>
            <if test="inputName != null and inputName != ''">
                and input_name = #{inputName}
            </if>
            <if test="inputDate != null">
                and input_date = #{inputDate}
            </if>
            <if test="updateId != null">
                and update_id = #{updateId}
            </if>
            <if test="updateName != null and updateName != ''">
                and update_name = #{updateName}
            </if>
            <if test="updateDate != null">
                and update_date = #{updateDate}
            </if>
            <if test="storeId != null">
                and store_id = #{storeId}
            </if>
        </where>
    </select>
    <select id="selectByChildren" resultType="java.lang.Integer">
        SELECT id from region_list where id = #{zhongkongDeptId}
        union all
        SELECT id from region_list where parent_id = #{zhongkongDeptId}
        union all
        SELECT id from region_list where parent_id in (SELECT id from region_list where parent_id = #{zhongkongDeptId} )
    </select>

    <select id="selectByChildrenByDeptName" resultType="java.lang.String">
        SELECT region_num from region_list where region_num = #{deptNum}
        union all
        SELECT region_num from region_list where parent_id = (SELECT id from region_list where region_num = #{deptNum})
        union all
        SELECT region_num from region_list where parent_id in (SELECT id from region_list where parent_id = (SELECT id from region_list where region_num = #{deptNum} ) )
    </select>

    <resultMap id="RegionThreeLevelMap" type="com.ssm.manage.entity.RegionThreeLevel">
        <result property="threeDeptId" column="threeDeptId"/>
        <result property="threeDeptName" column="threeDeptName"/>
        <result property="threeDeptNum" column="threeDeptNum"/>
        <result property="secondDeptId" column="secondDeptId"/>
        <result property="secondDeptName" column="secondDeptName"/>
        <result property="secondDeptNum" column="secondDeptNum"/>
        <result property="firstDeptId" column="firstDeptId"/>
        <result property="firstDeptName" column="firstDeptName"/>
        <result property="firstDeptNum" column="firstDeptNum"/>
    </resultMap>
<!--    <select id="selectParentByIds" resultType="com.ssm.manage.entity.RegionThreeLevel">-->
    <select id="selectParentByIds" resultMap="RegionThreeLevelMap">
        SELECT
            `t`.`id`            AS `threeDeptId`,
            `t`.`region_name`   AS `threeDeptName`,
            `t`.`region_num`    AS `threeDeptNum`,
            `s`.`id`            AS `secondDeptId`,
            `s`.`region_name`   AS `secondDeptName`,
            `s`.`region_num`    AS `secondDeptNum`,
            `f`.`id`            AS `firstDeptId`,
            `f`.`region_name`   AS `firstDeptName`,
            `f`.`region_num`    AS `firstDeptNum`
        FROM `region_list` `f`
        RIGHT JOIN `region_list` `s`
        ON `f`.`id` = `s`.`parent_id`
        RIGHT JOIN `region_list` `t`
        ON `s`.`id` = `t`.`parent_id`
        <where>
            <if test="ids != null and ids.size() > 0">
                t.`id` IN
                <foreach collection="ids" item="id" open="(" close=")" separator=",">
                    #{id}
                </foreach>
            </if>
        </where>
    </select>

    <select id="selectParentById" resultMap="RegionThreeLevelMap">
        SELECT
            `t`.`id`            AS `threeDeptId`,
            `t`.`region_name`   AS `threeDeptName`,
            `t`.`region_num`    AS `threeDeptNum`,
            `s`.`id`            AS `secondDeptId`,
            `s`.`region_name`   AS `secondDeptName`,
            `s`.`region_num`    AS `secondDeptNum`,
            `f`.`id`            AS `firstDeptId`,
            `f`.`region_name`   AS `firstDeptName`,
            `f`.`region_num`    AS `firstDeptNum`
        FROM `region_list` `f`
        RIGHT JOIN `region_list` `s`
        ON `f`.`id` = `s`.`parent_id`
        RIGHT JOIN `region_list` `t`
        ON `s`.`id` = `t`.`parent_id`
        <where>
            t.`id` = #{id}
        </where>
    </select>

    <select id="selectParentByRegionNum" resultMap="RegionThreeLevelMap">
        SELECT
        `t`.`id`            AS `threeDeptId`,
        `t`.`region_name`   AS `threeDeptName`,
        `t`.`region_num`    AS `threeDeptNum`,
        `s`.`id`            AS `secondDeptId`,
        `s`.`region_name`   AS `secondDeptName`,
        `s`.`region_num`    AS `secondDeptNum`,
        `f`.`id`            AS `firstDeptId`,
        `f`.`region_name`   AS `firstDeptName`,
        `f`.`region_num`    AS `firstDeptNum`
        FROM `region_list` `f`
        RIGHT JOIN `region_list` `s`
        ON `f`.`id` = `s`.`parent_id`
        RIGHT JOIN `region_list` `t`
        ON `s`.`id` = `t`.`parent_id`
        <where>
            <if test="regionNums != null and regionNums.size() > 0">
                t.`region_num` IN
                <foreach collection="regionNums" item="num" open="(" close=")" separator=",">
                    #{num}
                </foreach>
            </if>
        </where>
    </select>

</mapper>
