<?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>

    <sql id="Base_Column_List">
    panel_template.id, panel_template.`name`, panel_template.pid, panel_template.`level`, panel_template.node_type, panel_template.create_by, panel_template.create_time, panel_template.template_type
  </sql>
    <sql id="Blob_Column_List">
    panel_template.snapshot, panel_template.template_style, panel_template.template_data, panel_template.dynamic_data
  </sql>

    <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
        <include refid="Base_Column_List" />
        ,
        <if test='withBlobs == "Y"'>
            <include refid="Blob_Column_List" />
            ,
        </if>
        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>
        <if test="sort == null">
            order by panel_template.create_time desc
        </if>
    </select>

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