<?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="org.springblade.manager.mapper.ArchiveTreeMapper">


    <!-- 通用查询映射结果 -->
    <resultMap id="contractInfoResultMap" type="org.springblade.manager.entity.ArchiveTree">
        <result column="id" property="id"/>
        <result column="is_deleted" property="isDeleted"/>
        <result column="status" property="status"/>
        <result column="create_time" property="createTime"/>
        <result column="create_user" property="createUser"/>
        <result column="create_dept" property="createDept"/>
        <result column="update_user" property="updateUser"/>
        <result column="update_time" property="updateTime"/>
        <result column="tenant_id" property="tenantId"/>
        <result column="parent_id" property="parentId"/>
        <result column="ancestors" property="ancestors"/>
        <result column="node_name" property="nodeName"/>
        <result column="full_name" property="fullName"/>
        <result column="role_type" property="roleType"/>
        <result column="node_type" property="nodeType"/>
        <result column="is_storage_node" property="isStorageNode"/>
        <result column="is_built_drawing" property="isBuiltDrawing"/>
        <result column="is_interface_node" property="isInterfaceNode"/>
        <result column="interface_type" property="interfaceType"/>
        <result column="sort" property="sort"/>
        <result column="remark" property="remark"/>
    </resultMap>

    <resultMap id="treeNodeResultMap" type="org.springblade.core.tool.node.TreeNode">
        <id column="id" property="id"/>
        <result column="parent_id" property="parentId"/>
        <result column="title" property="title"/>
        <result column="value" property="value"/>
        <result column="key" property="key"/>
        <result column="has_children" property="hasChildren"/>
    </resultMap>

    <select id="lazyTree" resultMap="treeNodeResultMap">
        SELECT
        d.id,
        d.parent_id,
        d.node_name AS title,
        d.id AS "value",
        d.id AS "key",
        (
        SELECT
        CASE WHEN count(1) > 0 THEN 1 ELSE 0 END
        FROM
        m_archive_tree
        WHERE
        parent_id = d.id and is_deleted = 0
        ) AS "has_children"
        FROM
        m_archive_tree d
        WHERE
        d.parent_id = #{parentId} AND d.is_deleted = 0
        <if test="tenantId!=null and tenantId!=''">
            and d.tenant_id = #{tenantId}
        </if>
        <if test="roleTypeNum!=null and roleTypeNum!=''">
            AND d.role_type = #{roleTypeNum}
        </if>
        ORDER BY d.sort
    </select>
    <select id="tree" resultMap="treeNodeResultMap">
        select id, parent_id,node_name as title, id as "value", id as "key" from m_archive_tree where is_deleted = 0
        <if test="tenantId!=null and tenantId!=''">
            and tenant_id = #{tenantId}
        </if>
        <if test="roleTypeNum!=null and roleTypeNum!=''">
            and `role_type` = #{roleTypeNum}
        </if>
        ORDER BY sort
    </select>


</mapper>
