<?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="panelGroupListDefault" resultMap="BaseResultMapDTO">
        SELECT
        id,
        `name`,
        pid,
        `level`,
        node_type,
        create_by,
        create_time,
        panel_type,
        `name` AS label
        from panel_group
        where panel_group.panel_type=#{panelType}
    </select>

    <select id="panelGroupList" resultMap="BaseResultMapDTO">
        SELECT
        id,
        `name`,
        pid,
        `level`,
        node_type,
        create_by,
        create_time,
        panel_type,
        `name` AS label,
        get_auths(id,'panel',#{userId}) as `privileges`
        from (select GET_V_AUTH_MODEL_ID_P_USE (#{userId}, 'panel') cids) t,panel_group
        <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>
        <if test="sort == null">
            order by panel_group.create_time desc
        </if>
    </select>

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