<?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">
<!--  Copyright (c) 京北方信息技术股份有限公司 Corporation 2022 . All rights reserved. -->
<!--  Mybatis数据库持久层（业务层映射文件）：业务组件目录树 -->
<!--  version: 1.0  -->
<!--  author:  maven-cctp-plugin    -->
<!--  date:  2022-09-21 17:09:45  -->
<mapper namespace="net.northking.cctp.scriptcase.db.dao.BusComponentTreeDao">


  <!-- 请在本文件内添加自定义SQL语句 -->

  <resultMap id="DetailMap" type="net.northking.cctp.scriptcase.db.entity.BusComponentTree">
      <!-- 主键 -->
    <id column="id" jdbcType="VARCHAR" property="id"/>
                                          <!-- 树目录名称 -->
    <result column="tree_name" jdbcType="VARCHAR" property="treeName"/>
        <!-- 系统id -->
    <result column="project_id" jdbcType="VARCHAR" property="projectId"/>
        <!-- 父节点 -->
    <result column="parent_id" jdbcType="VARCHAR" property="parentId"/>
        <!-- 树节点等级 -->
    <result column="level_no" jdbcType="INTEGER" property="levelNo"/>
        <!-- id路径 -->
    <result column="id_path" jdbcType="VARCHAR" property="idPath"/>
        <!-- 是否为叶子节点 -->
    <result column="is_leaf" jdbcType="VARCHAR" property="isLeaf"/>
        <!-- 描述 -->
    <result column="memo" jdbcType="VARCHAR" property="memo"/>
        <!-- 中文名目录 -->
    <result column="name_path" jdbcType="VARCHAR" property="namePath"/>
        <!--  -->
    <result column="order_seq" jdbcType="INTEGER" property="orderSeq"/>
        <!-- 是否删除 -->
    <result column="is_deleted" jdbcType="INTEGER" property="isDeleted"/>
        <!-- 创建人 -->
    <result column="created_by" jdbcType="VARCHAR" property="createdBy"/>
        <!-- 创建时间 -->
    <result column="created_time" jdbcType="TIMESTAMP" property="createdTime"/>
        <!-- 修改人 -->
    <result column="updated_by" jdbcType="VARCHAR" property="updatedBy"/>
        <!-- 修改时间 -->
    <result column="updated_time" jdbcType="TIMESTAMP" property="updatedTime"/>
        <!-- 租户id -->
    <result column="tenant_id" jdbcType="VARCHAR" property="tenantId"/>
    </resultMap>

  <select id="pagingQuery" resultMap="DetailMap" parameterType="net.northking.cctp.scriptcase.db.entity.BusComponentTree">
    select
    id
    ,tree_name
    ,project_id
    ,parent_id
    ,level_no
    ,id_path
    ,is_leaf
    ,memo
    ,name_path
    ,order_seq
    ,is_deleted
    ,created_by
    ,created_time
    ,updated_by
    ,updated_time
    ,tenant_id
    from
    <include refid="Table_Name"/>
    <trim prefix="WHERE" prefixOverrides="AND" >
      <if test="id != null">
        AND id = #{id,jdbcType=VARCHAR}
      </if>
      <if test="treeName != null">
        AND tree_name = #{treeName,jdbcType=VARCHAR}
      </if>
      <if test="projectId != null">
        AND project_id = #{projectId,jdbcType=VARCHAR}
      </if>
      <if test="parentId != null">
        AND parent_id = #{parentId,jdbcType=VARCHAR}
      </if>
      <if test="levelNo != null">
        AND level_no = #{levelNo,jdbcType=INTEGER}
      </if>
      <if test="idPath != null">
        AND id_path = #{idPath,jdbcType=VARCHAR}
      </if>
      <if test="isLeaf != null">
        AND is_leaf = #{isLeaf,jdbcType=VARCHAR}
      </if>
      <if test="memo != null">
        AND memo = #{memo,jdbcType=VARCHAR}
      </if>
      <if test="namePath != null">
        AND name_path = #{namePath,jdbcType=VARCHAR}
      </if>
      <if test="orderSeq != null">
        AND order_seq = #{orderSeq,jdbcType=INTEGER}
      </if>
      <if test="isDeleted != null">
        AND is_deleted = #{isDeleted,jdbcType=INTEGER}
      </if>
      <if test="createdBy != null">
        AND created_by = #{createdBy,jdbcType=VARCHAR}
      </if>
      <if test="createdTime != null">
        AND created_time = #{createdTime,jdbcType=TIMESTAMP}
      </if>
      <if test="updatedBy != null">
        AND updated_by = #{updatedBy,jdbcType=VARCHAR}
      </if>
      <if test="updatedTime != null">
        AND updated_time = #{updatedTime,jdbcType=TIMESTAMP}
      </if>
      <if test="tenantId != null">
        AND tenant_id = #{tenantId,jdbcType=VARCHAR}
      </if>
    </trim>
  </select>


  <update id="updateChildNamePath" parameterType="net.northking.cctp.scriptcase.db.entity.BusComponentTree">
    update
    <include refid="Table_Name"/>
    <set>
      name_path = REPLACE(name_path,#{tree.namePath},#{newPath})
    </set>
    where project_id = #{tree.projectId,jdbcType=VARCHAR}
    and LOCATE(#{tree.namePath},name_path) > 0
    AND tenant_id = #{tree.tenantId,jdbcType=VARCHAR}
  </update>


  <select id="findChildTreeList" resultType="net.northking.cctp.scriptcase.dto.component.BusComponentTreeDetailDto"
          parameterType="net.northking.cctp.scriptcase.db.entity.BusComponentTree">
    select
    id
    ,tree_name
    ,project_id
    ,parent_id
    ,level_no
    ,is_leaf
    ,memo
    ,order_seq
    from
    <include refid="Table_Name"/>
    <trim prefix="WHERE" prefixOverrides="AND">
      <if test="projectId != null">
        AND project_id = #{projectId,jdbcType=VARCHAR}
      </if>
      <if test="parentId != null">
        AND parent_id = #{parentId,jdbcType=VARCHAR}
      </if>
      <if test="isDeleted != null">
        AND is_deleted = #{isDeleted,jdbcType=BIT}
      </if>
      <if test="idPath != null">
        AND LOCATE(#{idPath},id_Path)>0
      </if>
      <if test="tenantId != null">
        AND tenant_id = #{tenantId,jdbcType=VARCHAR}
      </if>
      <if test="treeName != null">
        AND LOCATE(#{treeName},tree_name)>0
      </if>
    </trim>
    order by order_seq
  </select>

  <select id="findChildTreeListMore" resultMap="DetailMap">
    select
    id
    ,tree_name
    ,project_id
    ,parent_id
    ,level_no
    ,id_path
    ,is_leaf
    ,memo
    ,name_path
    ,order_seq
    from
    <include refid="Table_Name"/>
    <trim prefix="WHERE" prefixOverrides="AND" >
      <if test="projectList != null">
        AND project_id in
        <foreach collection="projectList" item="idItem" open="(" separator="," close=")">
          #{idItem, jdbcType=VARCHAR}
        </foreach>
      </if>
      <if test="tree.projectId != null">
        AND project_id = #{tree.projectId,jdbcType=VARCHAR}
      </if>
      <if test="tree.parentId != null">
        AND parent_id = #{tree.parentId,jdbcType=VARCHAR}
      </if>
      <if test="tree.isDeleted != null">
        AND is_deleted = #{tree.isDeleted,jdbcType=BIT}
      </if>
      <if test="tree.idPath != null">
        AND LOCATE(#{tree.idPath},id_Path)>0
      </if>
      <if test="tree.tenantId != null">
        AND tenant_id = #{tree.tenantId,jdbcType=VARCHAR}
      </if>
      <if test="tree.treeName != null">
        AND LOCATE(#{tree.treeName},tree_name)>0
      </if>
    </trim>
    order by order_seq
  </select>

  <update id="updateChildIdPath" parameterType="net.northking.cctp.scriptcase.db.entity.BusComponentTree">
    update
    <include refid="Table_Name"/>
    <set>
      id_path = REPLACE(id_path,#{tree.idPath},#{newPath})
      <if test="tree.levelNo != null">
        , level_no = level_no +#{tree.levelNo}
      </if>
    </set>
    where tenant_id = #{tree.tenantId,jdbcType=VARCHAR}
    AND project_id = #{tree.projectId,jdbcType=VARCHAR}
    and LOCATE(#{tree.idPath},id_path) > 0
    and is_deleted = 0
  </update>


  <select id="getComTreePath" resultType="java.lang.String">
    SELECT concat('TOP/',id_path)
    FROM <include refid="Table_Name"/>
    WHERE
    tenant_id = #{tree.tenantId,jdbcType=VARCHAR}
    AND project_id = #{tree.projectId,jdbcType=VARCHAR}
    AND is_deleted=0
    <if test="tree.comName != null">
      AND LOCATE(#{tree.comName},tree_name)>0
    </if>
    AND id IN(
      SELECT DISTINCT tree_id
      FROM md_bus_component_info
      WHERE is_deleted=0
      AND project_id = #{tree.projectId,jdbcType=VARCHAR}
      AND tenant_id = #{tree.tenantId,jdbcType=VARCHAR}
      AND com_type IN
      <foreach collection="types" item="idItem" open="(" separator="," close=")">
        #{idItem, jdbcType=VARCHAR}
      </foreach>)
    </select>

  <select id="getComTreePathByMd" resultType="java.lang.String">
    SELECT concat('TOP/',id_path)
    FROM <include refid="Table_Name"/>
    WHERE
    tenant_id = #{tree.tenantId,jdbcType=VARCHAR}
    AND project_id = #{tree.projectId,jdbcType=VARCHAR}
    AND is_deleted=0
    <if test="tree.comName != null">
      AND LOCATE(#{tree.comName},tree_name)>0
    </if>
    AND id IN(
    SELECT DISTINCT tree_id
    FROM md_bus_component_info
    WHERE is_deleted=0
    AND project_id = #{tree.projectId,jdbcType=VARCHAR}
    AND tenant_id = #{tree.tenantId,jdbcType=VARCHAR}
    AND com_type IN
    <foreach collection="types" item="idItem" open="(" separator="," close=")">
      #{idItem, jdbcType=VARCHAR}
    </foreach>
    <if test="tree.isDeprecated != null">
      AND is_deprecated = #{tree.isDeprecated,jdbcType=INTEGER}
    </if>)
  </select>

  <select id="findComTreeListLike" resultType="net.northking.cctp.scriptcase.db.entity.BusComponentTree">
    SELECT
    id
    ,id_path
    FROM <include refid="Table_Name"/>
    WHERE
    tenant_id = #{tree.tenantId,jdbcType=VARCHAR}
    AND project_id = #{tree.projectId,jdbcType=VARCHAR}
    AND is_deleted=0
    <if test="tree.comName != null">
      AND LOCATE(#{tree.comName},tree_name)>0
    </if>
    AND id IN
    <foreach collection="ids" item="idItem" open="(" separator="," close=")">
      #{idItem, jdbcType=VARCHAR}
    </foreach>
  </select>

  <select id="getChildTreeIds" resultType="java.lang.String" parameterType="net.northking.cctp.scriptcase.db.entity.BusComponentTree">
    SELECT id FROM
    <include refid="Table_Name"/>
    WHERE tenant_id = #{tenantId,jdbcType=VARCHAR}
    and is_deleted = 0
    and LOCATE(#{id},id_path) > 0
  </select>
</mapper>