<?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 2023 . All rights reserved. -->
<!--  Mybatis数据库持久层（业务层映射文件）：用例集目录表 -->
<!--  version: 1.0  -->
<!--  author:  maven-cctp-plugin    -->
<!--  date:  2023-08-26 14:52:39  -->
<mapper namespace="net.northking.cctp.scriptcase.db.dao.AtuCaseDataSetDao">


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

  <resultMap id="DetailMap" type="net.northking.cctp.scriptcase.dto.caseDataSet.AtuCaseDataSetDetailDto">
      <!-- 主键 -->
    <id column="id" jdbcType="VARCHAR" property="id"/>
                                  <!-- 目录名称 -->
    <result column="set_name" jdbcType="VARCHAR" property="setName"/>
        <!-- 系统id -->
    <result column="project_id" jdbcType="VARCHAR" property="projectId"/>
        <!-- 脚本id -->
    <result column="script_id" jdbcType="VARCHAR" property="scriptId"/>
        <!-- 路径 -->
    <result column="path" jdbcType="VARCHAR" property="path"/>
        <!-- 是否叶子节点 -->
    <result column="is_leaf" jdbcType="TINYINT" property="leaf"/>
        <!-- 父节点 -->
    <result column="parent_id" jdbcType="VARCHAR" property="parentId"/>
        <!-- 创建人 -->
    <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.AtuCaseDataSet">
    select
    id
    ,set_name
    ,project_id
    ,script_id
    ,path
    ,is_leaf
    ,parent_id
    ,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="setName != null">
        AND set_name = #{setName,jdbcType=VARCHAR}
      </if>
      <if test="projectId != null">
        AND project_id = #{projectId,jdbcType=VARCHAR}
      </if>
      <if test="scriptId != null">
        AND script_id = #{scriptId,jdbcType=VARCHAR}
      </if>
      <if test="path != null">
        AND path = #{path,jdbcType=VARCHAR}
      </if>
      <if test="leaf != null">
        AND is_leaf = #{leaf,jdbcType=TINYINT}
      </if>
      <if test="parentId != null">
        AND parent_id = #{parentId,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="tenantId != null">
        AND tenant_id = #{tenantId,jdbcType=VARCHAR}
      </if>
    </trim>
  </select>

  <select id="querySetByName" resultMap="BaseResultMap" parameterType="net.northking.cctp.scriptcase.db.entity.AtuCaseDataSet">
    select
    <include refid="Base_Column_List" />
    from
    <include refid="Table_Name"/>
    <trim prefix="WHERE" prefixOverrides="AND" >
      <if test="id != null">
        AND id = #{id,jdbcType=VARCHAR}
      </if>
      <if test="setName != null">
        AND LOCATE(#{setName,jdbcType=VARCHAR},set_name) > 0
      </if>
      <if test="projectId != null">
        AND project_id = #{projectId,jdbcType=VARCHAR}
      </if>
      <if test="scriptId != null">
        AND script_id = #{scriptId,jdbcType=VARCHAR}
      </if>
      <if test="path != null">
        AND path = #{path,jdbcType=VARCHAR}
      </if>
      <if test="leaf != null">
        AND is_leaf = #{leaf,jdbcType=TINYINT}
      </if>
      <if test="parentId != null">
        AND parent_id = #{parentId,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="tenantId != null">
        AND tenant_id = #{tenantId,jdbcType=VARCHAR}
      </if>
    </trim>
  </select>


  <select id="queryTreeNode" resultMap="BaseResultMap" parameterType="java.util.List">
    select
    <include refid="Base_Column_List" />
    from
    <include refid="Table_Name"/>
    where
    is_leaf = 0 and
    id in
    <foreach collection="list" item="idItem" open="(" separator="," close=")">
      #{idItem, jdbcType=VARCHAR}
    </foreach>
  </select>

  <insert id="replaceTreeSet" parameterType="java.util.List">
    replace into
    <include refid="Table_Name"/>
    <trim prefix="(" suffix=")" suffixOverrides=",">
      id,
      set_name,
      project_id,
      script_id,
      path,
      is_leaf,
      level_no,
      version,
      idx,
      parent_id,
      created_by,
      created_time,
      updated_by,
      updated_time,
      tenant_id,
      principal_id,
      script_type,
    </trim>
    values
    <foreach collection="list" item="item" index="index" separator=",">
      <trim prefix="(" suffix=")" suffixOverrides=",">
        #{item.id, jdbcType=VARCHAR},
        #{item.setName, jdbcType=VARCHAR},
        #{item.projectId, jdbcType=VARCHAR},
        #{item.scriptId, jdbcType=VARCHAR},
        #{item.path, jdbcType=VARCHAR},
        #{item.leaf, jdbcType=TINYINT},
        #{item.levelNo, jdbcType=INTEGER},
        #{item.version, jdbcType=VARCHAR},
        #{item.idx,jdbcType=INTEGER},
        #{item.parentId, jdbcType=VARCHAR},
        #{item.createdBy, jdbcType=VARCHAR},
        #{item.createdTime, jdbcType=TIMESTAMP},
        #{item.updatedBy, jdbcType=VARCHAR},
        #{item.updatedTime, jdbcType=TIMESTAMP},
        #{item.tenantId, jdbcType=VARCHAR},
        #{item.principalId, jdbcType=VARCHAR},
        #{item.scriptType, jdbcType=VARCHAR},
      </trim>
    </foreach>
  </insert>

  <select id="queryScriptByPath" resultType="net.northking.cctp.scriptcase.db.entity.AtuScriptInfo" parameterType="string">
    select
    id
    ,group_id
    ,script_name
    ,script_type
    ,latest_version_id
    ,script_url
    ,data_url
    ,is_deleted
    ,project_id
    ,tenant_id
    ,created_by
    ,created_time
    ,updated_by
    ,updated_time
    ,app_id
    ,app_package
    from atu_script_info
    where id in
    (select id from atu_case_data_set where LOCATE(#{path, jdbcType=VARCHAR},path)>0)
    and is_deleted = false
  </select>

  <select id="queryByPath" resultType="String" parameterType="string">
    select
    path
    from
    atu_case_data_set
    where LOCATE(#{path, jdbcType=VARCHAR},path)>0 and is_leaf = 1
  </select>

  <sql id="commonNode">
    select
    dataSet.id
    ,dataSet.set_name
    ,dataSet.project_id
    ,dataSet.script_id
    ,dataSet.version
    ,dataSet.path
    ,dataSet.parent_id
    ,dataSet.created_by as createdBy
    ,dataSet.created_time as createdTime
    ,dataSet.updated_by as updatedBy
    ,dataSet.updated_time as updatedTime
    ,dataSet.principal_id
    ,info.script_type
    ,info.script_code
    from atu_script_info info left join
    atu_case_data_set dataSet on info.id = dataSet.script_id
    where
    dataSet.is_leaf = 1
    and info.is_deleted = false
    <if test="proId != null">
      and dataSet.project_id = #{proId,jdbcType=VARCHAR}
    </if>
    <if test="nodeId != null">
      and LOCATE(#{nodeId, jdbcType=VARCHAR},dataSet.path)>0
    </if>
    <if test="scriptType != null">
      AND info.script_type = #{scriptType,jdbcType=VARCHAR}
    </if>
    <if test="scriptName != null">
      AND LOCATE(#{scriptName,jdbcType=VARCHAR},dataSet.set_name) > 0
    </if>
    <if test="scriptList != null and scriptList.size() > 0">
      AND info.id not in
      <foreach collection="scriptList" item="idItem" open="(" separator="," close=")">
        #{idItem, jdbcType=VARCHAR}
      </foreach>
    </if>
    <if test="scriptCode != null">
      AND LOCATE(#{scriptCode,jdbcType=VARCHAR},info.script_code) > 0
    </if>

    <if test="startCreatedTime!=null">
      and info.created_time <![CDATA[>=]]> #{startCreatedTime}
    </if>
    <if test="endCreatedTime!=null">
      and info.created_time <![CDATA[<=]]> #{endCreatedTime}
    </if>
    <if test="startUpdatedTime!=null">
      and info.updated_time <![CDATA[>=]]> #{startUpdatedTime}
    </if>
    <if test="endUpdatedTime!=null">
      and info.updated_time <![CDATA[<=]]> #{endUpdatedTime}
    </if>
    <if test="principalIds != null and principalIds.size > 0">
      AND dataSet.principal_id IN
      <foreach collection="principalIds" open="(" index="index" item="item" close=")" separator=",">
        #{item,jdbcType=VARCHAR}
      </foreach>
    </if>

    <if test="principalId != null and principalId!=''">
      AND dataSet.principal_id =#{principalId}
    </if>

    <if test="updatedBys != null and updatedBys.size > 0">
      AND dataSet.updated_by IN
      <foreach collection="updatedBys" open="(" index="index" item="item" close=")" separator=",">
        #{item,jdbcType=VARCHAR}
      </foreach>
    </if>

    <if test="updateBy != null and updateBy!=''">
      AND dataSet.updated_by =#{updateBy}
    </if>

    <if test="createBys != null and createBys.size > 0">
      AND dataSet.created_by IN
      <foreach collection="createBys" open="(" index="index" item="item" close=")" separator=",">
        #{item,jdbcType=VARCHAR}
      </foreach>
    </if>

    <if test="creatorId != null and creatorId!=''">
      AND dataSet.created_by =#{creatorId}
    </if>
    <if test="tenantId != null">
      AND dataSet.tenant_id = #{tenantId,jdbcType=VARCHAR}
    </if>
    order by dataSet.created_time desc
  </sql>

  <select id="queryCommonNode" resultType="net.northking.cctp.scriptcase.dto.caseDataSet.AtuCaseScriptResultDto"
          parameterType="net.northking.cctp.scriptcase.dto.caseDataSet.AtuCaseScriptQueryDto">
    <include refid="commonNode"/>
  </select>

  <sql id="sceneNode">
    select
    dataSet.id
    ,dataSet.set_name
    ,dataSet.project_id
    ,dataSet.script_id
    ,dataSet.version
    ,dataSet.path
    ,dataSet.parent_id
    ,dataSet.created_by as createdBy
    ,dataSet.created_time as createdTime
    ,dataSet.updated_by as updatedBy
    ,dataSet.updated_time as updatedTime
    ,dataSet.principal_id
    ,"5" as script_type
    ,info.script_code
    from atu_scene_info info left join
    atu_case_data_set dataSet on info.id = dataSet.script_id
    where
    dataSet.is_leaf = 1
    and info.is_deleted = false
    <if test="proId != null">
      and dataSet.project_id = #{proId,jdbcType=VARCHAR}
    </if>
    <if test="nodeId != null">
      and LOCATE(#{nodeId, jdbcType=VARCHAR},dataSet.path)>0
    </if>
    <if test="scriptName != null">
      AND LOCATE(#{scriptName,jdbcType=VARCHAR},dataSet.set_name) > 0
    </if>
    <if test="scriptList != null and scriptList.size() > 0">
      AND info.id not in
      <foreach collection="scriptList" item="idItem" open="(" separator="," close=")">
        #{idItem, jdbcType=VARCHAR}
      </foreach>
    </if>
    <if test="scriptCode != null">
      AND LOCATE(#{scriptCode,jdbcType=VARCHAR},info.script_code) > 0
    </if>
    <if test="startCreatedTime!=null">
      and info.created_time <![CDATA[>=]]> #{startCreatedTime}
    </if>
    <if test="endCreatedTime!=null">
      and info.created_time <![CDATA[<=]]> #{endCreatedTime}
    </if>
    <if test="startUpdatedTime!=null">
      and info.updated_time <![CDATA[>=]]> #{startUpdatedTime}
    </if>
    <if test="endUpdatedTime!=null">
      and info.updated_time <![CDATA[<=]]> #{endUpdatedTime}
    </if>
    <if test="principalIds != null and principalIds.size > 0">
      AND dataSet.principal_id IN
      <foreach collection="principalIds" open="(" index="index" item="item" close=")" separator=",">
        #{item,jdbcType=VARCHAR}
      </foreach>
    </if>

    <if test="principalId != null and principalId!=''">
      AND dataSet.principal_id =#{principalId}
    </if>

    <if test="updatedBys != null and updatedBys.size > 0">
      AND dataSet.updated_by IN
      <foreach collection="updatedBys" open="(" index="index" item="item" close=")" separator=",">
        #{item,jdbcType=VARCHAR}
      </foreach>
    </if>

    <if test="updateBy != null and updateBy!=''">
      AND dataSet.updated_by =#{updateBy}
    </if>

    <if test="createBys != null and createBys.size > 0">
      AND dataSet.created_by IN
      <foreach collection="createBys" open="(" index="index" item="item" close=")" separator=",">
        #{item,jdbcType=VARCHAR}
      </foreach>
    </if>

    <if test="creatorId != null and creatorId!=''">
      AND dataSet.created_by =#{creatorId}
    </if>
    <if test="tenantId != null">
      AND dataSet.tenant_id = #{tenantId,jdbcType=VARCHAR}
    </if>
    order by dataSet.created_time desc
  </sql>
  <select id="querySceneNode" resultType="net.northking.cctp.scriptcase.dto.caseDataSet.AtuCaseScriptResultDto"
          parameterType="net.northking.cctp.scriptcase.dto.caseDataSet.AtuCaseScriptQueryDto">
    <include refid="sceneNode"/>
  </select>

  <update id="updatePrincipal" parameterType="net.northking.cctp.scriptcase.dto.atuScript.atuScriptInfo.PrincipalUpdateDto">
    update
    <include refid="Table_Name"/>
    <set>
      principal_id = #{principalId, jdbcType=VARCHAR}
      <if test="updatedBy != null">
        , updated_by = #{updatedBy}
      </if>
      <if test="updatedTime != null">
        , updated_time = #{updatedTime,jdbcType=TIMESTAMP}
      </if>
    </set>
    where id in
    <foreach collection="scriptList" item="idItem" open="(" separator="," close=")">
      #{idItem, jdbcType=VARCHAR}
    </foreach>
  </update>

  <select id="queryCommonList" resultType="net.northking.cctp.scriptcase.dto.caseDataSet.AtuCaseScriptResultDto"
          parameterType="net.northking.cctp.scriptcase.dto.caseDataSet.AtuCaseScriptQueryDto">
    select
    dataSet.id
    ,dataSet.set_name
    ,dataSet.project_id
    ,dataSet.script_id
    ,dataSet.version
    ,dataSet.path
    ,dataSet.parent_id
    ,dataSet.created_by as createdBy
    ,dataSet.created_time as createdTime
    ,dataSet.updated_by as updatedBy
    ,dataSet.updated_time as updatedTime
    ,dataSet.principal_id
    ,info.script_type
    ,info.script_code
    from atu_script_info info left join
    atu_case_data_set dataSet on info.id = dataSet.script_id
    where dataSet.id in
    <foreach collection="list" item="idItem" open="(" separator="," close=")">
      #{idItem, jdbcType=VARCHAR}
    </foreach>
    <if test="scriptName != null">
      AND LOCATE(#{scriptName,jdbcType=VARCHAR},dataSet.set_name) > 0
    </if>
    order by dataSet.created_time desc
  </select>

  <select id="querySceneList" resultType="net.northking.cctp.scriptcase.dto.caseDataSet.AtuCaseScriptResultDto"
          parameterType="net.northking.cctp.scriptcase.dto.caseDataSet.AtuCaseScriptQueryDto">
    select
    dataSet.id
    ,dataSet.set_name
    ,dataSet.project_id
    ,dataSet.script_id
    ,dataSet.version
    ,dataSet.path
    ,dataSet.parent_id
    ,dataSet.created_by as createdBy
    ,dataSet.created_time as createdTime
    ,dataSet.updated_by as updatedBy
    ,dataSet.updated_time as updatedTime
    ,dataSet.principal_id
    ,'5' script_type
    ,info.script_code
    from atu_scene_info info left join
    atu_case_data_set dataSet on info.id = dataSet.script_id
    where info.id in
    <foreach collection="list" item="idItem" open="(" separator="," close=")">
      #{idItem, jdbcType=VARCHAR}
    </foreach>
    <if test="scriptName != null">
      AND LOCATE(#{scriptName,jdbcType=VARCHAR},dataSet.set_name) > 0
    </if>
    order by dataSet.created_time desc
  </select>

  <select id="queryPlanScriptByPath" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List" />
    from
    <include refid="Table_Name"/>
    where project_id = #{proId,jdbcType=VARCHAR}
    and LOCATE(#{path, jdbcType=VARCHAR},path)>0
    and is_leaf = 1
  </select>

  <select id="querySetByList" resultMap="BaseResultMap">
    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>
    and is_leaf = 1
  </select>
  <select id="queryAtuCaseDataSetByScriptId" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List"/>
    from
    <include refid="Table_Name"/>
    where script_id = #{scriptId,jdbcType=VARCHAR} and is_leaf = 1
  </select>
  <select id="queryScriptIdBySetIdList" resultType="java.lang.String">
    select DISTINCT script_id from
    <include refid="Table_Name"/>
    where id in
    <foreach collection="list" item="idItem" open="(" separator="," close=")">
      #{idItem, jdbcType=VARCHAR}
    </foreach>
  </select>
  <!--  查询所有用例 一般用力和场景用例-->
  <select id="queryCommonNodeAndSceneNode"
          resultType="net.northking.cctp.scriptcase.dto.caseDataSet.AtuCaseScriptResultDto">
    <if test="scriptType == null || scriptType == ''">
      (
      <include refid="commonNode"/>
      )
      union
      (
      <include refid="sceneNode"/>
      )
    </if>

    <if test="scriptType != null and scriptType != '' and (scriptType != 5 and scriptType != '5')">
      (
      <include refid="commonNode"/>
      )
    </if>
    <if test="scriptType != null and scriptType != '' and (scriptType == 5 or scriptType == '5')">
      (
      <include refid="sceneNode"/>
      )
    </if>
  </select>

  <select id="picusQueryDeletedByScriptIds" resultType="net.northking.cctp.scriptcase.dto.IdAndStatusDto">
    (select
    dataSet.id
    ,info.is_deleted deleted
    from atu_script_info info left join
    atu_case_data_set dataSet on info.id = dataSet.script_id
    where
    dataSet.is_leaf = 1
    and dataSet.script_id in
    <foreach collection="ids" item="id" open="(" separator="," close=")">
      #{id, jdbcType=VARCHAR}
    </foreach>
    )
    union
    (
    select
    dataSet.id
    ,info.is_deleted deleted
    from atu_scene_info info left join
    atu_case_data_set dataSet on info.id = dataSet.script_id
    where
    dataSet.is_leaf = 1
    and dataSet.script_id in
    <foreach collection="ids" item="id" open="(" separator="," close=")">
      #{id, jdbcType=VARCHAR}
    </foreach>
    )
  </select>
    <select id="selectManagerByProject" resultType="java.lang.String">
      SELECT principal_id FROM atu_case_data_set
      WHERE is_leaf = 1 AND
      project_id = #{projectId}
      GROUP BY principal_id;
    </select>
    <select id="selectCreatorByProject" resultType="java.lang.String">
      SELECT created_by FROM atu_case_data_set
      WHERE is_leaf = 1 AND
      project_id = #{projectId}
      GROUP BY created_by;
    </select>
    <select id="selectUpdaterByProject" resultType="java.lang.String">
      SELECT updated_by FROM atu_case_data_set
      WHERE is_leaf = 1
      AND project_id = #{projectId}
      GROUP BY updated_by;
    </select>

</mapper>