<?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.yida.data.system.mapper.EduAreaMapper">
    <select id="findAreaByParentId" resultType="com.yida.data.common.core.entity.system.EduArea">
        select *
        from edu_area
        <where>
            <if test="parentId != null">
                and parent_id=#{parentId}
            </if>
        </where>
        <if test="parentId != null">
            union all
            select *
            from edu_area
            where parent_id in
            (
            select id
            from edu_area
            where parent_id=#{parentId})
        </if>
    </select>

    <select id="checkArea" resultType="com.yida.data.common.core.entity.system.EduArea">
        select *
        from edu_area
        <where>
            name = #{name}
            <if test="parentId != null">
                and parent_id = #{parentId}
            </if>
        </where>
    </select>

    <update id="updateParentName">
        update edu_area
        set parent_name=#{parentName}
        <where>
            id in
            <foreach collection="ids" close=")" item="id" open="(" separator=",">
                #{id}
            </foreach>
        </where>
    </update>

    <select id="findAreaById" resultType="com.yida.data.system.vo.AreaVO">
        SELECT
	        t4.name2 as county,
	        t4.id1 as village_id,
	        t4.id2 as countyId,
			id3 as cityId,
			id as provinceId,
	        t4.name3 as city,
	        t5.NAME as province
        FROM
	        (
	        SELECT
		    t1.id id1,
		    t1.NAME name1,
		    t1.parent_id parentid1,
		    t2.id id2,
		    t2.NAME name2,
		    t2.parent_id parent_id2,
		    t3.id id3,
		    t3.NAME name3,
		    t3.parent_id parent_id3
	        FROM
		    edu_area t1
		    LEFT JOIN edu_area t2 ON t2.id = t1.parent_id
		    LEFT JOIN edu_area t3 ON t3.id = t2.parent_id
	        WHERE
		    t1.id = #{id}
	        ) t4
	    LEFT JOIN edu_area t5 ON t5.id = t4.parent_id3
    </select>
</mapper>