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

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

    <select id="panelTemplate" resultMap="BaseResultMapDTO">
        SELECT
            panel_template.*,
            panel_template.NAME AS label,
            ifnull(tempCountInfo.childrenCount,0) as childrenCount
        FROM
            panel_template
            LEFT JOIN (
            SELECT
                pid,
                count( 1 ) AS childrenCount
            FROM
                panel_template
            WHERE
                pid IS NOT NULL
            GROUP BY
                pid
            ) tempCountInfo ON panel_template.id = tempCountInfo.pid
        WHERE
            id = #{id}
    </select>

    <select id="panelTemplateList" resultMap="BaseResultMapDTO">
        SELECT
        panel_template.*,
        panel_template.NAME AS label,
        ifnull(tempCountInfo.childrenCount,0) as childrenCount
        FROM
        panel_template
        LEFT JOIN (
        SELECT
        pid,
        count( 1 ) AS childrenCount
        FROM
        panel_template
        WHERE
        pid IS NOT NULL
        GROUP BY
        pid
        ) tempCountInfo ON panel_template.id = tempCountInfo.pid
        <where>
            <if test="name != null">
                and panel_template.name like CONCAT('%', #{name},'%')
            </if>
            <if test="nodeType != null">
                and panel_template.node_type = #{nodeType}
            </if>
            <if test="templateType != null">
                and panel_template.template_type = #{templateType}
            </if>
            <if test="id != null">
                and panel_template.id = #{id}
            </if>
            <if test="pid != null">
                and panel_template.pid = #{pid}
            </if>
            <if test="level != null">
                and panel_template.level = #{level}
            </if>
        </where>
        <if test="sort != null">
            order by ${sort}
        </if>
    </select>

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