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

    <!-- 通用查询映射结果 -->
    <resultMap id="wbsTreeResultMap" type="org.springblade.manager.entity.WbsTree">
        <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="type" property="type"/>
        <result column="table_type" property="tableType"/>
        <result column="tenant_id" property="tenantId"/>
        <result column="parent_id" property="parentId"/>
        <result column="ancestors" property="ancestors"/>
        <result column="dept_category" property="deptCategory"/>
        <result column="dept_name" property="deptName"/>
        <result column="full_name" property="fullName"/>
        <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>

    <resultMap id="selectByNodeTableMap" type="org.springblade.manager.vo.WbsNodeTableVO">
        <id column="id" property="id"/>
        <result column="tableName" property="tableName"/>
        <result column="tableType" property="tableType"/>
        <result column="isCreateTable" property="isCreateTable"/>
        <result column="elementTotal" property="elementTotal"/>
    </resultMap>

    <resultMap id="wbsFormElementMap" type="org.springblade.manager.entity.WbsFormElement">
        <id column="id" property="id"/>
        <result column="fId" property="fId"/>
        <result column="e_name" property="eName"/>
        <result column="e_type" property="eType"/>
        <result column="e_length" property="eLength"/>
        <result column="e_allow_deviation" property="eAllowDeviation"/>
        <result column="e_inspection_method" property="eInspectionMethod"/>
    </resultMap>

    <insert id="addFormElement" parameterType="org.springblade.manager.dto.WbsFormElementDTO">
        insert into m_wbs_form_element(f_id,e_name,e_type,e_length)
        values (#{f_id},#{e_name},#{e_type},#{e_length})
    </insert>


    <update id="removeTableById">
        UPDATE m_wbs_tree AS wt SET is_deleted = 1 WHERE wt.type = 2 AND wt.id = #{id}
    </update>


    <select id="selectWbsTreePage" resultMap="wbsTreeResultMap">
        select * from m_wbs_tree where is_deleted = 0
    </select>

    <select id="tree" resultMap="treeNodeResultMap">
        select id, parent_id, dept_name as title, id as "value", id as "key" from m_wbs_tree where is_deleted = 0
        <if test="_parameter!=null and _parameter!=''">
            and tenant_id = #{_parameter}
        </if>
        ORDER BY sort
    </select>

    <select id="lazyTree" resultMap="treeNodeResultMap">
        SELECT
        dept.type,
        dept.table_type,
        dept.id,
        dept.parent_id,
        dept.dept_name AS title,
        dept.id AS "value",
        dept.id AS "key",
        (
        SELECT
        CASE WHEN count(1) > 0 THEN 1 ELSE 0 END
        FROM
        m_wbs_tree
        WHERE
        parent_id = dept.id and is_deleted = 0
        ) AS "has_children"
        FROM
        m_wbs_tree dept
        WHERE
        dept.parent_id = #{param2} AND dept.is_deleted = 0
        <if test="param1!=null and param1!=''">
            and dept.tenant_id = #{param1}
        </if>
        ORDER BY dept.sort
    </select>

    <select id="selectByNodeTable" resultMap="selectByNodeTableMap">
        SELECT
            wt.id AS id,
            wt.dept_name AS tableName,
            wt.table_type AS tableType,
            wt.`status` AS isCreateTable ,
            (SELECT count(*) FROM m_wbs_form_element AS wfe WHERE wfe.f_id=wt.id) AS elementTotal
        FROM
            m_wbs_tree AS wt
        WHERE
            wt.type = 2
        AND wt.is_deleted = 0
        AND wt.parent_id = #{id}
    </select>

    <select id="selectByElementsTotal" resultType="java.lang.String">
        SELECT f_id,count(*) as elementTotal FROM m_wbs_form_element where f_id in #{ids} and is_deleted=0 GROUP BY f_id
    </select>


    <select id="selectFormElements" resultMap="wbsFormElementMap">
        SELECT
            w.f_id as fId,w.id,w.e_name,e_type,e_length,e_allow_deviation,e_Inspection_method
        FROM
            m_wbs_form_element w
        WHERE
            is_deleted = 0 and f_id = #{id}
    </select>



</mapper>
