<?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-31 11:18:16  -->
<mapper namespace="net.northking.cctp.scriptcase.db.dao.AtuInputSetDao">


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

  <resultMap id="DetailMap" type="net.northking.cctp.scriptcase.dto.inputSet.AtuInputSetDetailDto">
      <!-- 主键 -->
    <id column="id" jdbcType="VARCHAR" property="id"/>
                            <!-- 脚本id -->
    <result column="script_id" jdbcType="VARCHAR" property="scriptId"/>
        <!-- 环境id -->
    <result column="env_id" jdbcType="VARCHAR" property="envId"/>
        <!-- 版本 -->
    <result column="version" jdbcType="INTEGER" property="version"/>
        <!-- 数据文件地址 -->
    <result column="data_url" jdbcType="VARCHAR" property="dataUrl"/>
        <!-- 创建人 -->
    <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.AtuInputSet">
    select
    id
    ,script_id
    ,env_id
    ,version
    ,data_url
    ,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="scriptId != null">
        AND script_id = #{scriptId,jdbcType=VARCHAR}
      </if>
      <if test="envId != null">
        AND env_id = #{envId,jdbcType=VARCHAR}
      </if>
      <if test="version != null">
        AND version = #{version,jdbcType=INTEGER}
      </if>
      <if test="dataUrl != null">
        AND data_url = #{dataUrl,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>

  <select id="queryInputByEnvIdAndScriptId" resultMap="BaseResultMap">
    select
    id
    ,script_id
    ,env_id
    ,data_url
    from
    <include refid="Table_Name"/>
    where env_id in 
    <foreach collection="envIdList" item="envId" open="(" separator="," close=")">
      #{envId}
    </foreach>
    and script_id in
    <foreach collection="scriptIdList" item="scriptId" open="(" separator="," close=")">
      #{scriptId}
    </foreach>
  </select>

  <sql id="Link_Table_Name">
    ${defaultSchema}.atu_input_data_set
  </sql>

  <delete id="deleteDataSetLink" parameterType="net.northking.cctp.scriptcase.dto.inputSet.AtuInputDataSetDto">
    delete from
    <include refid="Link_Table_Name"/>
    where input_set_id = #{inputSetId,jdbcType=VARCHAR}
    and env_id = #{envId,jdbcType=VARCHAR}
    and source = #{source,jdbcType=VARCHAR}
  </delete>


 <insert id="insertDataSetLink" parameterType="java.util.List">
   insert into
   <include refid="Link_Table_Name"/>
   <trim prefix="(" suffix=")" suffixOverrides=",">
     id,
     input_set_id,
     env_id,
     data_set_id,
     source,
     set_column,
     row_num
   </trim>
   values
   <foreach collection="list" item="item" index="index" separator=",">
     <trim prefix="(" suffix=")" suffixOverrides=",">
       #{item.id, jdbcType=VARCHAR},
       #{item.inputSetId, jdbcType=VARCHAR},
       #{item.envId, jdbcType=VARCHAR},
       #{item.dataSetId, jdbcType=VARCHAR},
       #{item.source, jdbcType=VARCHAR},
       #{item.setColumn, jdbcType=VARCHAR},
       #{item.rowNum, jdbcType=INTEGER}
     </trim>
   </foreach>
 </insert>


  <select id="selectOneDataSetLink" parameterType="net.northking.cctp.scriptcase.dto.inputSet.AtuInputDataSetDto"
          resultType="integer">
    select
    count(*)
    from
    <include refid="Link_Table_Name"/>
    where data_set_id = #{dataSetId,jdbcType=VARCHAR}
    AND env_id = #{envId,jdbcType=VARCHAR}
    AND set_column = #{setColumn,jdbcType=VARCHAR}
  </select>


  <select id="selectDataSetLink" parameterType="String" resultType="net.northking.cctp.scriptcase.dto.inputSet.AtuInputDataSetResultDto">
    select
    count(*) as countNum,
    data_set_id as setId
    from
    <include refid="Link_Table_Name"/>
    where data_set_id in
    <foreach collection="list" item="setId" open="(" separator="," close=")">
      #{setId}
    </foreach>
    group by data_set_id
  </select>

</mapper>