<?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-10-24 16:58:38  -->
<mapper namespace="net.northking.cctp.dataPool.db.dao.DataSetTreeDao">


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

  <resultMap id="DetailMap" type="net.northking.cctp.dataPool.dto.dataSets.DataSetTreeDetailDto">
      <!-- id -->
    <id column="id" jdbcType="VARCHAR" property="id"/>
                                      <!-- 节点名称 -->
    <result column="name" jdbcType="VARCHAR" property="name"/>
        <!-- 父节点id -->
    <result column="parent_id" jdbcType="VARCHAR" property="parentId"/>
        <!-- 层级 -->
    <result column="level_no" jdbcType="INTEGER" property="levelNo"/>
        <!-- 序号 -->
    <result column="index_no" jdbcType="INTEGER" property="indexNo"/>
        <!-- 是否叶子节点 -->
    <result column="is_leaf" jdbcType="VARCHAR" property="isLeaf"/>
        <!-- 路径 -->
    <result column="path" jdbcType="VARCHAR" property="path"/>
        <!-- 完整路径 -->
    <result column="full_name" jdbcType="VARCHAR" property="fullName"/>
        <!-- 系统id -->
    <result column="pro_id" jdbcType="VARCHAR" property="proId"/>
        <!-- 租户id -->
    <result column="tenant_id" jdbcType="VARCHAR" property="tenantId"/>
        <!-- 创建人 -->
    <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"/>
        <!-- 描述 -->
    <result column="description" jdbcType="VARCHAR" property="description"/>
    </resultMap>

  <select id="pagingQuery" resultMap="DetailMap" parameterType="net.northking.cctp.dataPool.db.entity.DataSetTree">
    select
    id
    ,name
    ,parent_id
    ,level_no
    ,index_no
    ,is_leaf
    ,path
    ,full_name
    ,pro_id
    ,tenant_id
    ,created_by
    ,created_time
    ,updated_by
    ,updated_time
    ,description
    from
    <include refid="Table_Name"/>
    <trim prefix="WHERE" prefixOverrides="AND" >
      <if test="id != null">
        AND id = #{id,jdbcType=VARCHAR}
      </if>
      <if test="name != null">
        AND name = #{name,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="indexNo != null">
        AND index_no = #{indexNo,jdbcType=INTEGER}
      </if>
      <if test="isLeaf != null">
        AND is_leaf = #{isLeaf,jdbcType=VARCHAR}
      </if>
      <if test="path != null">
        AND path = #{path,jdbcType=VARCHAR}
      </if>
      <if test="fullName != null">
        AND full_name = #{fullName,jdbcType=VARCHAR}
      </if>
      <if test="proId != null">
        AND pro_id = #{proId,jdbcType=VARCHAR}
      </if>
      <if test="tenantId != null">
        AND tenant_id = #{tenantId,jdbcType=VARCHAR}
      </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="description != null">
        AND description=#{description,jdbcType=VARCHAR}
      </if>
    </trim>
  </select>
  <update id="updateChildNamePath" parameterType="net.northking.cctp.dataPool.db.entity.DataSetTree">
    update
    <include refid="Table_Name"/>
    <set>
      full_name = REPLACE(full_name,#{tree.fullName},#{newPath})
    </set>
    where pro_id = #{tree.proId,jdbcType=VARCHAR}
    and LOCATE(#{tree.fullName},full_name) > 0
  </update>
  <select id="queryChildrenTreeList" parameterType="string" resultMap="DetailMap">
    select
    <include refid="Base_Column_List" />
    from
    <include refid="Table_Name"/>
    where LOCATE(#{treeId},path) > 0
  </select>
  <select id="queryByIds" parameterType="list" resultMap="DetailMap">
    select
    <include refid="Base_Column_List" />
    from
    <include refid="Table_Name"/>
    <trim prefix="WHERE" prefixOverrides="AND" >
      <if test="list != null and list.size() > 0">
        AND id in
        <foreach collection="list" item="idItem" open="(" separator="," close=")">
          #{idItem, jdbcType=VARCHAR}
        </foreach>
      </if>
    </trim>
    order by index_no
  </select>
  <update id="updateSeqByMove" parameterType="net.northking.cctp.dataPool.dto.dataSets.DataSetTreeMoveDto">
    update
    <include refid="Table_Name"/>
    <set>
      index_no = index_no + 1
    </set>
    where pro_id = #{proId,jdbcType=VARCHAR}
    and index_no > #{orderSeq}
  </update>
  <update id="updateChildIdPath" parameterType="net.northking.cctp.dataPool.db.entity.DataSetTree">
    update
    <include refid="Table_Name"/>
    <set>
      path = REPLACE(path,#{tree.path},#{newPath})
      <if test="tree.levelNo != null">
        , level_no = level_no +#{tree.levelNo}
      </if>
    </set>
    where pro_id = #{tree.proId,jdbcType=VARCHAR}
    and LOCATE(#{tree.path},path) > 0
  </update>
  <select id="countMaxLevelNo" resultType="java.lang.Integer" parameterType="net.northking.cctp.dataPool.db.entity.DataSetTree">
    select max(level_no) from
    <include refid="Table_Name"/>
    where pro_id = #{proId,jdbcType=VARCHAR}
    and LOCATE(#{id},path)>0
  </select>
  <select id="selectDataSetTree" resultMap="DetailMap"
          parameterType="net.northking.cctp.dataPool.dto.dataSets.DataSetTreeQueryDto">
    select
    <include refid="Base_Column_List" />
    from
    <include refid="Table_Name"/>
    <trim prefix="WHERE" prefixOverrides="AND" >
      <if test="proIds != null and proIds.size() > 0">
        AND pro_id in
        <foreach collection="proIds" item="idItem" open="(" separator="," close=")">
          #{idItem, jdbcType=VARCHAR}
        </foreach>
      </if>
      <if test="parentId != null and parentId != ''">
        AND parent_id = #{parentId,jdbcType=VARCHAR}
      </if>
      <if test="tenantId != null and tenantId != ''">
        AND tenant_id = #{tenantId,jdbcType=VARCHAR}
      </if>
      <if test="path != null and path != ''">
        AND LOCATE(#{path},path)>0
      </if>
      <if test="name != null and name != ''">
        AND LOCATE(#{name},name)>0
      </if>
    </trim>
    order by index_no
  </select>
</mapper>