<?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.changhu.mapper.AdministrativeDivisionMapper">
    <select id="treeList" resultType="com.changhu.common.pojo.vo.TreeNodeVo">
        select
        code as 'value',
        name as 'label',
        parent_code as 'parentValue'
        from
        administrative_division
        where delete_flag = 0
        <if test="level!=null">and level &lt;= #{level}</if>
        order by code
    </select>
    <resultMap id="administrativeDivisionByParentCodeVoResultMap" type="com.changhu.common.pojo.vo.TreeNodeVo">
        <result
                column="extData"
                typeHandler="com.baomidou.mybatisplus.extension.handlers.Fastjson2TypeHandler"
                property="extData"/>
    </resultMap>
    <select id="administrativeDivisionByParentCode" resultMap="administrativeDivisionByParentCodeVoResultMap">
        select ad1.code                                                 as 'value',
               ad1.name                                                 as 'label',
               ad1.parent_code                                          as 'parentValue',
               any_value(if(count(ad2.snow_flake_id) > 0, false, true)) as 'isLeaf'
        from administrative_division ad1
                 left join administrative_division ad2 on ad1.code = ad2.parent_code
            and ad2.delete_flag = 0
        where ad1.delete_flag = 0
          and ad1.parent_code = #{parentCode}
        group by ad1.code
    </select>
</mapper>