<?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-30 10:30:29  -->
<mapper namespace="net.northking.cctp.executePlan.db.dao.AtuPlanEnvLinkDao">


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

  <resultMap id="DetailMap" type="net.northking.cctp.executePlan.dto.planLink.AtuPlanEnvLinkDetailDto">
      <!-- 主键 -->
    <id column="id" jdbcType="BIGINT" property="id"/>
                  <!-- 计划id -->
    <result column="plan_id" jdbcType="VARCHAR" property="planId"/>
        <!-- 环境id -->
    <result column="env_id" jdbcType="VARCHAR" property="envId"/>
        <!-- 排序值 -->
    <result column="sort" jdbcType="INTEGER" property="sort"/>
    </resultMap>

  <select id="pagingQuery" resultMap="DetailMap" parameterType="net.northking.cctp.executePlan.db.entity.AtuPlanEnvLink">
    select
    id
    ,plan_id
    ,env_id
    ,sort
    from
    <include refid="Table_Name"/>
    <trim prefix="WHERE" prefixOverrides="AND" >
      <if test="id != null">
        AND id = #{id,jdbcType=BIGINT}
      </if>
      <if test="planId != null">
        AND plan_id = #{planId,jdbcType=VARCHAR}
      </if>
      <if test="envId != null">
        AND env_id = #{envId,jdbcType=VARCHAR}
      </if>
      <if test="sort != null">
        AND sort = #{sort,jdbcType=INTEGER}
      </if>
    </trim>
  </select>

  <select id="queryEnvIdByPlanIdOderBySort" resultType="java.lang.String">
    select env_id from <include refid="Table_Name"/> where plan_id = #{planId} order by sort desc
  </select>

    <insert id="insertLinkList" parameterType="java.util.List">
    insert into
    <include refid="Table_Name"/>
    <trim prefix="(" suffix=")" suffixOverrides=",">
      plan_id,
      env_id,
      sort,
    </trim>
    values
    <foreach collection="list" item="item" index="index" separator=",">
      <trim prefix="(" suffix=")" suffixOverrides=",">
        #{item.planId, jdbcType=VARCHAR},
        #{item.envId, jdbcType=VARCHAR},
        #{item.sort, jdbcType=INTEGER},
      </trim>
    </foreach>
  </insert>

</mapper>