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

    <!-- 通用查询映射结果 -->
    <resultMap id="excelTabResultMap" type="org.springblade.manager.entity.ExcelTab">
        <result column="id" property="id"/>
        <result column="create_user" property="createUser"/>
        <result column="create_dept" property="createDept"/>
        <result column="create_time" property="createTime"/>
        <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="file_type" property="fileType"/>
        <result column="name" property="name"/>
        <result column="alias" property="alias"/>
        <result column="file_url" property="fileUrl"/>
        <result column="tab_cout" property="tabCout"/>
        <result column="extension" property="extension"/>
        <result column="attach_size" property="attachSize"/>
        <result column="tab_type" property="tabType"/>
    </resultMap>

    <resultMap id="treeNodeResultMap" type="org.springblade.manager.vo.ExceTabTreVO">
        <id column="id" property="id"/>
        <result column="parent_id" property="parentId"/>
        <result column="name" property="name"/>
        <result column="has_children" property="hasChildren"/>
    </resultMap>


    <resultMap id="wbsResultMap" type="org.springblade.manager.vo.ExcelTabWbsTypeVO">
        <id column="id" property="id"/>
        <id column="wbsName" property="wbsName"/>
        <result column="wbsType" property="wbsType"/>
    </resultMap>

    <resultMap id="treeNodeResultMap2" type="org.springblade.manager.vo.WbsTreeVO">
        <id column="id" property="id"/>
        <result column="parent_id" property="parentId"/>
        <result column="deptName" property="deptName"/>
        <result column="has_children" property="hasChildren"/>
        <result column="type" property="type"/>
    </resultMap>


    <select id="selectExcelTabPage" resultMap="excelTabResultMap">
        select * from m_excel_tab where is_deleted = 0
    </select>

    <select id="tabLazyTree" resultMap="treeNodeResultMap" >
        SELECT
        dept.id,
        dept.parent_id,
        dept.name,
        dept.file_type,
        (
        SELECT
        CASE WHEN count(1) > 0 THEN 1 ELSE 0 END
        FROM
        m_excel_tab
        WHERE
        parent_id = dept.id and is_deleted = 0
        ) AS "has_children"
        FROM
        m_excel_tab dept
        WHERE
         dept.is_deleted = 0
        <if test="param3!=null and param3!=''">
            AND dept.parent_id = #{param3}
        </if>
        <if test="param2!=null and param2!=''">
            and dept.alias like concat(concat('%', #{param2}),'%')
        </if>
        <if test="param1!=null and param1!=''">
            and dept.tenant_id = #{param1}
        </if>
        ORDER BY dept.create_time
    </select>


    <select id="getWbsTypeList" resultMap="wbsResultMap">
        SELECT id,wbs_name as wbsName,1 as wbsType from m_wbs_info where is_deleted=0
    </select>

    <select id="lazyTree" resultMap="treeNodeResultMap2">
        SELECT
        dept.type,
        dept.table_type,
        dept.id,
        dept.is_link_table,
        dept.parent_id,
        dept.dept_name as deptName,
        (
        SELECT
        CASE WHEN count(1) > 0 THEN 1 ELSE 0 END
        FROM
        m_wbs_tree
        WHERE
        parent_id = dept.id and is_deleted = 0 AND type = 1
        ) AS "has_children"
        FROM
        m_wbs_tree dept
        WHERE
        dept.parent_id = #{parentId} AND dept.is_deleted = 0
        <if test="tenantId!=null and tenantId!=''">
            and dept.tenant_id = #{tenantId}
        </if>
        AND dept.wbs_id = #{wbsId}
        AND dept.type = 1
        AND status = 1
        ORDER BY dept.sort
    </select>


    <select id="tabLazyTreeAll" resultMap="treeNodeResultMap" >
        SELECT
        dept.id,
        dept.parent_id,
        dept.name,
        dept.file_type,
        (
        SELECT
        CASE WHEN count(1) > 0 THEN 1 ELSE 0 END
        FROM
        m_excel_tab
        WHERE
        parent_id = dept.id and is_deleted = 0
        ) AS "has_children"
        FROM
        m_excel_tab dept
        WHERE
         dept.is_deleted = 0
        <if test="param1!=null and param1!=''">
            and dept.tenant_id = #{param1}
        </if>
        <if test="param2!=null and param2!=''">
            and dept.alias like concat(concat('%', #{param2}),'%')
        </if>

        <if test="param3!=null and param3!=''">
            and dept.name like concat(concat('%', #{param3}),'%')
        </if>
        ORDER BY dept.create_time
    </select>
</mapper>
