<?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="io.dataease.base.mapper.ext.ExtVAuthModelMapper">

    <resultMap id="BaseResultMapDTO" type="io.dataease.dto.VAuthModelDTO"
               extends="io.dataease.base.mapper.VAuthModelMapper.BaseResultMap">
        <result column="children_count" property="childrenCount"/>
        <result column="leaf" property="leaf"/>
    </resultMap>

<!--    函数嵌套会导致循环调用函数 导致查询速度变慢 所有预先经需要嵌套查询的函数分解查询-->
    <select id="searchTree" resultMap="BaseResultMapDTO">
        SELECT
        auth.*,
        authCount.children_count AS children_count,
        IF
        (( authCount.children_count > 0 ), 0, 1 ) AS leaf
        FROM (select get_grant_auths (#{modelType},#{createBy}) c_auth_ids) t1,
        ( SELECT * FROM (select GET_V_AUTH_MODEL_WITH_PARENT(get_grant_auths (#{modelType},#{createBy}),#{modelType}) c_auth_parent_ids) t2,
        <if test="withExtend == 'parent' and id != null">
        (select GET_V_AUTH_MODEL_WITH_PARENT(#{id},#{modelType}) c_model_parent_ids) tmp,
        </if>
        <if test="withExtend == 'children' and id != null">
            (select GET_V_AUTH_MODEL_WITH_CHILDREN(#{id},#{modelType}) c_model_children_ids) tmc,
        </if>
        <if test="name != null and name !='' and withExtend == 'parent'">
            (select GET_V_AUTH_MODEL_WITH_PARENT ( (select GROUP_CONCAT(id) from
            v_auth_model where model_type = #{modelType} and `name` like CONCAT('%', #{name},'%')) ,#{modelType}) c_model_parent_seartch_ids) tmsc,
        </if>
         v_auth_model
        <where>
            model_type = #{modelType}
            <if test="1== withAuth">
                and FIND_IN_SET(v_auth_model.id,c_auth_parent_ids)
            </if>
            <if test="pid !=null">
                and v_auth_model.pid = #{pid}
            </if>

            <if test="withExtend == null and id != null">
                and v_auth_model.id = #{id}
            </if>
            <if test="withExtend == 'parent' and id != null">
                and FIND_IN_SET(v_auth_model.id,c_model_parent_ids)
            </if>
            <if test="withExtend == 'children' and id != null">
                and FIND_IN_SET(v_auth_model.id,c_model_children_ids)
            </if>

            <if test="name != null and name !='' and withExtend == 'parent'">
                and FIND_IN_SET(v_auth_model.id,c_model_parent_seartch_ids)
            </if>

            <if test="name != null and name =='' and withExtend == 'parent'">
                and v_auth_model.pid = '0'
            </if>

        </where>
        ) auth
        LEFT JOIN (
        SELECT
        count( 1 ) AS `children_count`,
        `authTemp`.`pid` AS `pid`
        FROM
        ( SELECT * FROM (select GET_V_AUTH_MODEL_WITH_PARENT(get_grant_auths (#{modelType},#{createBy}),#{modelType}) cids3) t3,v_auth_model
        <where>
            model_type = #{modelType}
            <if test="1== withAuth">
                and FIND_IN_SET(v_auth_model.id,cids3)
            </if>
        </where>
        ) authTemp
        GROUP BY
        authTemp.pid
        ) authCount ON
        auth.id = authCount.pid
        <where>
            <if test="1== withAuth">
                (authCount.children_count>0 or FIND_IN_SET(auth.id,c_auth_ids) )
            </if>
        </where>

    </select>

</mapper>
