<?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.ExtPanelGroupMapper">

    <resultMap id="BaseResultMapDTO" type="io.dataease.dto.panel.PanelGroupDTO"
               extends="io.dataease.base.mapper.PanelGroupMapper.BaseResultMap">
        <result column="label" jdbcType="VARCHAR" property="label"/>
        <result column="privileges" jdbcType="VARCHAR" property="privileges"/>
    </resultMap>

    <select id="panelGroup" resultMap="BaseResultMapDTO">
        select panel_group.*,panel_group.name as label from panel_group where id =#{id}
    </select>

    <select id="panelGroupList" resultMap="BaseResultMapDTO">
        SELECT
        id,
        `name`,
        pid,
        `level`,
        node_type,
        create_by,
        create_time,
        panel_type,
        `name` AS label,
        authInfo.`privileges`
        from (select GET_V_AUTH_MODEL_ID_P_USE (#{userId}, 'panel') cids) t,panel_group
        left join
        (SELECT
            `sys_auth`.`auth_source` AS `auth_source`,
            `sys_auth`.`auth_source_type` AS `auth_source_type`,
            group_concat( sys_auth_detail.privilege_extend) AS `privileges`
            FROM
            (
            `sys_auth`
            LEFT JOIN `sys_auth_detail` ON ((
            `sys_auth`.`id` = `sys_auth_detail`.`auth_id`
            )))
            where sys_auth_detail.privilege_value =1
            AND (
            (
            sys_auth.auth_target_type = 'dept'
            AND sys_auth.auth_target = ( SELECT dept_id FROM sys_user WHERE user_id = #{userId} )
            )
            OR (
            sys_auth.auth_target_type = 'user'
            AND sys_auth.auth_target = #{userId}
            )
            OR (
            sys_auth.auth_target_type = 'role'
            AND sys_auth.auth_target = ( SELECT role_id FROM sys_users_roles WHERE user_id = #{userId} )
            )
            )
            GROUP BY
            `sys_auth`.`auth_source`,
            `sys_auth`.`auth_source_type`) authInfo
        on panel_group.id = authInfo.auth_source and authInfo.auth_source_type='panel'
        <where>
            FIND_IN_SET(panel_group.id,cids)
            <if test="name != null">
                and panel_group.name like CONCAT('%', #{name},'%')
            </if>
            <if test="nodeType != null">
                and panel_group.node_type = #{nodeType}
            </if>
            <if test="panelType != null">
                and panel_group.panel_type = #{panelType}
            </if>
            <if test="id != null">
                and panel_group.id = #{id}
            </if>
            <if test="pid != null">
                and panel_group.pid = #{pid}
            </if>
            <if test="level != null">
                and panel_group.level = #{level}
            </if>
        </where>
        <if test="sort != null">
            order by ${sort}
        </if>
    </select>

    <delete id="deleteCircle">
        delete from panel_group where FIND_IN_SET(panel_group.id,GET_PANEL_GROUP_WITH_CHILDREN(#{pid}))
    </delete>
</mapper>
