<?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="archiveTreeResultMap" type="org.springblade.manager.entity.ArchiveTree">
        <result column="id" property="id"/>
        <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="status" property="status"/>
        <result column="is_deleted" property="isDeleted"/>
        <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="node_type" property="nodeType"/>
        <result column="post_type" property="postType"/>
        <result column="association_type" property="associationType"/>
        <result column="major_data_type" property="majorDataType"/>
        <result column="display_hierarchy" property="displayHierarchy"/>
        <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"/>
        <result column="is_upload_file_display_configuration_tree" property="isUploadFileDisplayConfigurationTree"/>
    </resultMap>
    <resultMap id="treeNodeResultMap" type="org.springblade.manager.vo.TreeNodeVO2">
        <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"/>
        <result column="displayHierarchy" property="displayHierarchy"/>
        <result column="majorDataType" property="majorDataType"/>
    </resultMap>

    <update id="updateBatch">
        UPDATE m_archive_tree
        SET is_upload_file_display_configuration_tree = 1
        WHERE id IN
        <foreach item="diffRent" collection="diffRent" open="(" close=")" separator=",">
            #{diffRent}
        </foreach>
        AND is_deleted = 0
        AND status = 1
    </update>

    <update id="updateBatch2">
        UPDATE m_archive_tree
        SET is_upload_file_display_configuration_tree = 0
        WHERE id IN
        <foreach item="diffRent" collection="diffRent" open="(" close=")" separator=",">
            #{diffRent}
        </foreach>
        AND is_deleted = 0
        AND status = 1
    </update>

    <select id="lazyTree" resultMap="treeNodeResultMap">
        SELECT
        d.id,
        d.parent_id,
        d.node_name AS title,
        d.id AS "value",
        d.id AS "key",
        d.major_data_type AS "majorDataType",
        d.display_hierarchy AS "displayHierarchy",
        (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>
        ORDER BY d.sort
    </select>

    <select id="tree" resultMap="treeNodeResultMap">
        SELECT
        id,
        parent_id,
        node_name AS title,
        id AS "value",
        id AS "key",
        major_data_type AS "majorDataType",
        display_hierarchy AS "displayHierarchy"
        FROM m_archive_tree
        WHERE is_deleted = 0
        <if test=" tenantId!=null and tenantId!='' ">
            AND tenant_id = #{tenantId}
        </if>
        <if test=" disPlayTree!=null and disPlayTree!='' ">
            AND is_upload_file_display_configuration_tree = 1
        </if>
        <if test=" nodeType !=null and nodeType !='' ">
            /*根节点=0 关联电子原生文件=1 数字化上传文件=2*/
            AND node_type in (0,#{nodeType})
        </if>
        ORDER BY sort
    </select>

</mapper>
