<?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}) cids1) t1,
        ( SELECT * FROM (select get_grant_auths (#{modelType},#{createBy}) cids2) t2 ,v_auth_model
        <where>
            model_type = #{modelType}
            <if test="1== withAuth">
               and  FIND_IN_SET(v_auth_model.id,GET_V_AUTH_MODEL_WITH_PARENT ( cids2 ,#{modelType}))
            </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,GET_V_AUTH_MODEL_WITH_PARENT(#{id},#{modelType}))
            </if>
            <if test="withExtend == 'children' and id != null">
                and FIND_IN_SET(v_auth_model.id,GET_V_AUTH_MODEL_WITH_CHILDREN(#{id},#{modelType}))
            </if>

            <if test="name != null and name !='' and withExtend == 'parent'">
               and  FIND_IN_SET(v_auth_model.id,GET_V_AUTH_MODEL_WITH_PARENT ( (select GROUP_CONCAT(id) from v_auth_model where model_type = #{modelType} and `name`  like CONCAT('%', #{name},'%')) ,#{modelType}))
            </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_grant_auths (#{modelType},#{createBy}) cids3) t3,v_auth_model
        <where>
            model_type = #{modelType}
            <if test="1== withAuth">
                and   FIND_IN_SET(v_auth_model.id,GET_V_AUTH_MODEL_WITH_PARENT ( cids3 ,#{modelType}))
            </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,cids1) )
            </if>
        </where>

    </select>
</mapper>
