<?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-18 17:29:31  -->
<mapper namespace="net.northking.cctp.scriptcase.db.dao.AtuScriptGroupDao">


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

  <resultMap id="DetailMap" type="net.northking.cctp.scriptcase.dto.atuScript.atuScriptGroup.AtuScriptGroupDetailDto">
      <!-- 目录ID -->
    <id column="id" jdbcType="VARCHAR" property="id"/>
                                        <!-- 目录父ID -->
    <result column="pid" jdbcType="VARCHAR" property="pid"/>
        <!-- 目录名称 -->
    <result column="group_name" jdbcType="VARCHAR" property="groupName"/>
        <!-- 目录描述 -->
    <result column="description" jdbcType="VARCHAR" property="description"/>
        <!-- 目录层级 -->
    <result column="level_no" jdbcType="INTEGER" property="levelNo"/>
        <!-- 目录次序 -->
    <result column="seq" jdbcType="VARCHAR" property="seq"/>
        <!-- 是否为叶子节点 -->
    <result column="is_leaf" jdbcType="VARCHAR" property="isLeaf"/>
        <!-- ID路径 -->
    <result column="id_path" jdbcType="VARCHAR" property="idPath"/>
        <!-- 名称路径 -->
    <result column="name_path" jdbcType="VARCHAR" property="namePath"/>
        <!-- 系统ID -->
    <result column="project_id" jdbcType="VARCHAR" property="projectId"/>
        <!-- 租户号 -->
    <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"/>
    </resultMap>

  <select id="pagingQuery" resultMap="DetailMap" parameterType="net.northking.cctp.scriptcase.db.entity.AtuScriptGroup">
    select
    id
    ,pid
    ,group_name
    ,description
    ,level_no
    ,seq
    ,is_leaf
    ,id_path
    ,name_path
    ,project_id
    ,tenant_id
    ,created_by
    ,created_time
    ,updated_by
    ,updated_time
    from
    <include refid="Table_Name"/>
    <trim prefix="WHERE" prefixOverrides="AND" >
      <if test="id != null">
        AND id = #{id,jdbcType=VARCHAR}
      </if>
      <if test="pid != null">
        AND pid = #{pid,jdbcType=VARCHAR}
      </if>
      <if test="groupName != null">
        AND group_name = #{groupName,jdbcType=VARCHAR}
      </if>
      <if test="description != null">
        AND description = #{description,jdbcType=VARCHAR}
      </if>
      <if test="levelNo != null">
        AND level_no = #{levelNo,jdbcType=INTEGER}
      </if>
      <if test="seq != null">
        AND seq = #{seq,jdbcType=VARCHAR}
      </if>
      <if test="isLeaf != null">
        AND is_leaf = #{isLeaf,jdbcType=VARCHAR}
      </if>
      <if test="idPath != null">
        AND id_path = #{idPath,jdbcType=VARCHAR}
      </if>
      <if test="namePath != null">
        AND name_path = #{namePath,jdbcType=VARCHAR}
      </if>
      <if test="projectId != null">
        AND project_id = #{projectId,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>
    </trim>
  </select>
  <update id="updateChildNamePath" parameterType="net.northking.cctp.scriptcase.db.entity.AtuScriptGroup">
    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
  </update>
  <select id="selectScriptTree" resultMap="DetailMap"
          parameterType="net.northking.cctp.scriptcase.dto.atuScript.atuScriptGroup.AtuScriptGroupQueryDto">
    select
    <include refid="Base_Column_List" />
    from
    <include refid="Table_Name"/>
    <trim prefix="WHERE" prefixOverrides="AND" >
      <if test="projectIds != null and projectIds.size() > 0">
        AND project_id in
        <foreach collection="projectIds" item="idItem" open="(" separator="," close=")">
          #{idItem, jdbcType=VARCHAR}
        </foreach>
      </if>
      <if test="projectId != null">
        AND project_id = #{projectId,jdbcType=VARCHAR}
      </if>
      <if test="pid != null">
        AND pid = #{pid,jdbcType=VARCHAR}
      </if>
      <if test="idPath != null">
        AND LOCATE(#{idPath},id_Path)>0
      </if>
      <if test="groupName != null">
        AND LOCATE(#{groupName},group_name)>0
      </if>
      <if test="tenantId != null">
        AND tenant_id = #{tenantId,jdbcType=VARCHAR}
      </if>
    </trim>
    order by seq
  </select>
  <select id="countMaxLevelNo" resultType="java.lang.Integer" parameterType="net.northking.cctp.scriptcase.db.entity.AtuScriptGroup">
    select max(level_no) from
    <include refid="Table_Name"/>
    where project_id = #{projectId,jdbcType=VARCHAR}
    and LOCATE(#{id},id_path)>0
  </select>
  <update id="updateSeqByMove" parameterType="net.northking.cctp.scriptcase.dto.atuScript.atuScriptGroup.AtuScriptGroupMoveDto">
    update
    <include refid="Table_Name"/>
    <set>
      seq = seq + 1
    </set>
    where project_id = #{projectId,jdbcType=VARCHAR}
    and seq > #{orderSeq}
  </update>
  <update id="updateChildIdPath" parameterType="net.northking.cctp.scriptcase.db.entity.AtuScriptGroup">
    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 project_id = #{tree.projectId,jdbcType=VARCHAR}
    and LOCATE(#{tree.idPath},id_path) > 0
  </update>
  <select id="queryByIds" resultType="net.northking.cctp.scriptcase.db.entity.AtuScriptGroup" parameterType="list">
    select
    <include refid="Base_Column_List" />
    from
    <include refid="Table_Name"/>
    where id in
    <foreach collection="list" item="idItem" open="(" separator="," close=")">
      #{idItem, jdbcType=VARCHAR}
    </foreach>
  </select>
  <select id="queryChildrenTreeList" resultType="net.northking.cctp.scriptcase.db.entity.AtuScriptGroup" parameterType="string">
    select
    <include refid="Base_Column_List" />
    from
    <include refid="Table_Name"/>
    where LOCATE(#{groupId},id_path) > 0
  </select>
  <select id="getAllIdPath" resultType="java.lang.String">
    SELECT id_path
    FROM <include refid="Table_Name"/>
    WHERE project_id = #{projectId,jdbcType=VARCHAR}
  </select>
  <select id="findTreeListLike" parameterType="net.northking.cctp.scriptcase.dto.atuScript.atuScriptGroup.PublishScriptQueryDto" resultType="net.northking.cctp.scriptcase.db.entity.AtuScriptGroup">
    SELECT
    id
    ,id_path
    FROM <include refid="Table_Name"/>
    WHERE project_id = #{projectId,jdbcType=VARCHAR}
    <if test="treeName != null and treeName != ''">
      AND LOCATE(#{treeName,jdbcType=VARCHAR},group_name) > 0
    </if>
    AND id IN
    <foreach collection="groupIdList" item="idItem" open="(" separator="," close=")">
      #{idItem, jdbcType=VARCHAR}
    </foreach>
  </select>
</mapper>