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

    <!-- 通用查询映射结果 -->
    <resultMap id="ResultMap" type="org.springblade.manager.entity.WbsTreeContract">
        <result column="id" property="id"/>
        <result column="p_key_id" property="pKeyId"/>
        <result column="wbs_id" property="wbsId"/>
        <result column="project_id" property="projectId"/>
        <result column="contract_id" property="contractId"/>
        <result column="contract_type" property="contractType"/>
        <result column="contract_id_relation" property="contractIdRelation"/>
        <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"/>
        <result column="unique_code" property="uniqueCode"/>
        <result column="partition_code" property="partitionCode"/>
        <result column="is_concrete" property="isConcrete"/>
        <result column="is_expernode" property="isExpernode"/>
        <result column="table_owner" property="tableOwner"/>
        <result column="major_data_type" property="majorDataType"/>
        <result column="init_table_name" property="initTableName"/>
        <result column="is_link_table" property="isLinkTable"/>
        <result column="wbs_type" property="wbsType"/>
    </resultMap>

    <resultMap id="resultMap2" type="org.springblade.manager.vo.WbsTreeContractVO">
        <result column="id" property="id"/>
        <result column="wbs_id" property="wbsId"/>
        <result column="project_id" property="projectId"/>
        <result column="project_name" property="projectName"/>
        <result column="contract_id" property="contractId"/>
    </resultMap>

    <resultMap id="treeNodeResultMap" type="org.springblade.manager.vo.TreeNodeVO">
        <id column="id" property="id"/>
        <result column="parent_id" property="parentId"/>
        <result column="fullName" property="title"/>
        <result column="has_children" property="hasChildren"/>
        <result column="type" property="type"/>
        <result column="deptCategory" property="deptCategory"/>
        <result column="wbsType" property="wbsType"/>
        <result column="contractType" property="contractType"/>
        <result column="contractId" property="contractId"/>
        <result column="primaryKeyId" property="primaryKeyId"/>
        <result column="tenantId" property="tenantId"/>
        <result column="key" property="key"/>
        <result column="value" property="value"/>
        <result column="contractIdRelation" property="contractIdRelation"/>
    </resultMap>


    <insert id="insertWbsTreeContract1">
        INSERT INTO m_wbs_tree_contract(
        p_key_id,id,wbs_id,wbs_type,project_id,contract_id,contract_type,tenant_id,parent_id,ancestors,dept_category,dept_name,full_name,sort,remark,`type`,
        table_type,create_time,create_user,create_dept,update_user,update_time,`STATUS`,is_deleted,
        unique_code,partition_code,is_expernode,is_concrete,table_owner,major_data_type,init_table_name,is_link_table)
        VALUES(
        #{snowId},#{wbsTree.id},#{wbsTree.wbsId},#{wbsType},#{wbsTree.projectId},#{contractId},1,#{wbsTree.tenantId},#{wbsTree.parentId},#{wbsTree.ancestors},
        #{wbsTree.deptCategory},#{wbsTree.deptName},#{wbsTree.fullName},#{wbsTree.sort},#{wbsTree.remark},#{wbsTree.type},
        #{wbsTree.tableType},#{wbsTree.createTime},#{wbsTree.createUser},#{wbsTree.createDept},#{wbsTree.updateUser},#{wbsTree.updateTime},
        #{wbsTree.status},#{wbsTree.isDeleted},#{wbsTree.uniqueCode},#{wbsTree.partitionCode},#{wbsTree.isExpernode},#{wbsTree.isConcrete},
        #{wbsTree.tableOwner},#{wbsTree.majorDataType},#{wbsTree.initTableName},#{wbsTree.isLinkTable})
    </insert>

    <insert id="insertByCondition">
        INSERT INTO m_wbs_tree_contract(
        p_key_id,id,wbs_id,wbs_type,project_id,contract_id,contract_type,tenant_id,parent_id,ancestors,dept_category,dept_name,full_name,sort,remark,`type`,
        table_type,create_time,create_user,create_dept,update_user,update_time,`STATUS`,is_deleted,
        unique_code,partition_code,is_expernode,is_concrete,table_owner,major_data_type,init_table_name,is_link_table)
        VALUES(
        #{pKeyId},#{wbsTree.id},#{wbsTree.wbsId},#{wbsType},#{wbsTree.projectId},#{contractId},1,#{wbsTree.tenantId},#{wbsTree.parentId},#{wbsTree.ancestors},
        #{wbsTree.deptCategory},#{wbsTree.deptName},#{wbsTree.fullName},#{wbsTree.sort},#{wbsTree.remark},#{wbsTree.type},
        #{wbsTree.tableType},#{wbsTree.createTime},#{wbsTree.createUser},#{wbsTree.createDept},#{wbsTree.updateUser},#{wbsTree.updateTime},
        #{wbsTree.status},#{wbsTree.isDeleted},#{wbsTree.uniqueCode},#{wbsTree.partitionCode},#{wbsTree.isExpernode},#{wbsTree.isConcrete},
        #{wbsTree.tableOwner},#{wbsTree.majorDataType},#{wbsTree.initTableName},#{wbsTree.isLinkTable})
    </insert>

    <update id="deleteBatch">
        UPDATE m_wbs_tree_contract
        SET is_deleted = 1
        WHERE id IN
        <foreach item="ids" collection="ids" open="(" close=")" separator=",">
            #{ids}
        </foreach>
        AND wbs_id = #{wbsId}
        AND project_id = #{projectId}
        AND contract_id = #{contractId}
    </update>

    <update id="updateByCondition">
        UPDATE m_wbs_tree_contract
        SET is_deleted = 0
        WHERE id = #{id}
        AND wbs_id = #{wbsId}
        AND project_id =#{projectId}
        AND contract_id = #{contractId}
    </update>

    <update id="updateByCondition1">
        UPDATE m_wbs_tree_contract
        SET dept_category = #{wbsTP.deptCategory},
            dept_name = #{wbsTP.deptName},
            unique_code = #{wbsTP.uniqueCode},
            partition_code = #{wbsTP.partitionCode},
            major_data_type = #{wbsTP.majorDataType}
        WHERE
            project_id = #{wbsTP.projectId}
        AND id = #{wbsTP.id}
        AND wbs_id = #{wbsTP.wbsId}
    </update>

    <update id="updateByCondition2">
        UPDATE m_wbs_tree_contract
        SET is_deleted = 0
        WHERE contract_id = #{contractId}
        AND contract_id_relation = #{contractIdRelation}
        AND status = 1
    </update>


    <select id="selectAll" resultMap="resultMap2">
        SELECT
            wtc.id,
            wtc.wbs_id,
            wtc.project_id,
            ( SELECT project_name FROM m_project_info pi WHERE wtc.project_id = pi.id ) AS project_name,
            contract_id
        FROM
            m_wbs_tree_contract wtc
        WHERE
            is_deleted = 0
            AND STATUS = 1
        GROUP BY
            project_id
    </select>

    <select id="selectByCondition" resultType="org.springblade.manager.entity.WbsTreeContract">
        SELECT
            *
        FROM
            m_wbs_tree_contract
        WHERE
            id = #{id}
        AND project_id = #{projectId}
        AND contract_id = #{contractId}
        AND wbs_id = #{wbsId}
        AND STATUS = 1
        AND is_deleted = 0
    </select>

    <select id="selectByCondition2" resultType="org.springblade.manager.entity.WbsTreeContract">
        SELECT
            *
        FROM
            m_wbs_tree_contract
        WHERE
            id = #{id}
        AND project_id = #{projectId}
        AND contract_id = #{contractId}
        AND wbs_id = #{wbsId}
        AND STATUS = 1
    </select>

    <select id="findAllNodeList2" resultType="org.springblade.manager.entity.WbsTreeContract">
        SELECT
            *
        FROM
            m_wbs_tree_contract
        WHERE
            project_id = #{projectId}
        AND contract_id = #{contractId}
        AND wbs_id = #{wbsId}
        AND STATUS = 1
        AND is_deleted = 0
    </select>

    <select id="selectParent" resultType="org.springblade.manager.entity.WbsTreePrivate">
        SELECT p_key_id,dept_name,full_name FROM m_wbs_tree_private
        WHERE
            project_id = #{projectId}
        AND wbs_id = #{wbsId}
        AND id = #{parentId}
    </select>

    <select id="queryCurrentContractLogList" resultMap="ResultMap">
        select
            p_key_id,
            dept_name,
            full_name,
            dept_category
        from
          m_wbs_tree_contract
        where
          is_deleted = 0
        and parent_id in(select id from m_wbs_tree_contract where is_deleted = 0 and contract_id = #{contractId} and parent_id = 0)
        and major_data_type = 5
        and contract_id = #{contractId}
    </select>

    <select id="selectListByCondition" resultType="org.springblade.manager.vo.WbsTreeContractVO2">
        SELECT
            p_key_id AS "pKeyId",
            id AS "id",
            dept_name AS "deptName",
            full_name AS "fullName",
            wbs_type AS "wbsType",
            wbs_id AS "wbsId",
            project_id AS "projectId",
            (select project_name from m_project_info where id =#{projectId}) AS "projectName",
            contract_id AS "contractId",
            contract_type AS "contractType",
            (select contract_name from m_contract_info ci where ci.id = t.contract_id) AS "contractName"
        FROM
            m_wbs_tree_contract t
        WHERE project_id = #{projectId}
        AND contract_type = 1
        AND parent_id = 0
        AND is_deleted = 0
        AND status = 1
    </select>

    <select id="selectCondition" resultType="org.springblade.manager.entity.WbsTreeContract">
        SELECT
            id,
            p_key_id,
            wbs_id,
            wbs_type,
            project_id,
            contract_id,
            contract_type,
            contract_id_relation,
            tenant_id,
            parent_id,
            dept_category,
            type,
            dept_name,
            full_name,
            table_type,
            sort,
            ancestors,
            remark,
            unique_code,
            partition_code,
            is_expernode,
            is_concrete,
            table_owner,
            major_data_type,
            init_table_name,
            is_link_table,
            create_user,
            create_dept,
            create_time,
            update_user,
            update_time,
            STATUS,
            is_deleted
        FROM
            m_wbs_tree_contract
        WHERE
            is_deleted = 1
        AND parent_id = 0
        AND contract_id = #{getContractId}
        AND id = #{id}
        AND contract_id_relation = #{getContractIdRelation}
        AND contract_type = #{contractType}
    </select>

    <select id="lazyTreeTwo" resultMap="treeNodeResultMap">
        SELECT
        dept.type AS "type",
        dept.wbs_type AS "wbsType",
        dept.contract_type AS "contractType",
        dept.contract_id AS "contractId",
        dept.p_key_id AS "id",
        dept.dept_category AS "deptCategory",
        dept.id AS "primaryKeyId",
        dept.id AS "value",
        dept.id AS "key" ,
        dept.parent_id,
        dept.full_name AS "fullName",
        dept.tenant_id AS "tenantId",
        dept.contract_id_relation AS "contractIdRelation",
        (SELECT CASE WHEN count(1) > 0 THEN 1 ELSE 0 END FROM m_wbs_tree_contract WHERE parent_id = dept.id and
        is_deleted = 0 ) AS "has_children"
        FROM
        m_wbs_tree_contract dept
        WHERE
        dept.is_deleted = 0
        <if test="contractIdRelation != null and contractIdRelation != ''">
            AND contract_id_relation = #{contractIdRelation}
        </if>
        AND dept.contract_id = #{contractId}
        AND dept.contract_type = #{contractType}
        AND parent_id = #{parentId}
        AND dept.type = 1
        AND status = 1
        ORDER BY dept.sort
    </select>

</mapper>
