<?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-11-25 11:15:19  -->
<mapper namespace="net.northking.cctp.dw.db.dao.DwAtuPlanTaskDao">
    <resultMap id="BaseResultMap" type="net.northking.cctp.dw.db.entity.DwAtuPlanTask">
        <!--  -->
        <id column="id" jdbcType="VARCHAR" property="id"/>
        <!--  -->
        <result column="batch_id" jdbcType="VARCHAR" property="batchId"/>
        <!--  -->
        <result column="env_id" jdbcType="VARCHAR" property="envId"/>

        <result column="script_id" jdbcType="VARCHAR" property="scriptId"/>

        <result column="start_time" jdbcType="DATE" property="startTime"/>

        <result column="end_time" jdbcType="DATE" property="endTime"/>

        <result column="script_name" jdbcType="VARCHAR" property="scriptName"/>

        <result column="status" jdbcType="INTEGER" property="status"/>

    </resultMap>

    <sql id="Base_Column_List">
        id
        ,batch_id
        ,env_id
        ,script_id
        ,start_time
        ,end_time
        ,script_name
        ,status
    </sql>

    <sql id="Table_Name">
        ${defaultSchema}.dw_atu_plan_task
    </sql>

    <select id="findByPrimaryKey" parameterType="String" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from
        <include refid="Table_Name"/>
        where id = #{id,jdbcType=VARCHAR}
    </select>

    <select id="findByPrimaryKeys" parameterType="java.util.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>

    <delete id="deleteByPrimaryKey" parameterType="String">
        delete from
        <include refid="Table_Name"/>
        where id = #{id,jdbcType=VARCHAR}
    </delete>

    <delete id="deleteByPrimaryKeys" parameterType="String">
        delete from
        <include refid="Table_Name"/>
        where id in
        <foreach collection="list" item="idItem" open="(" separator="," close=")">
            #{idItem, jdbcType=VARCHAR}
        </foreach>
    </delete>

    <delete id="deleteByExample" parameterType="net.northking.cctp.dw.db.entity.DwAtuPlanTask">
        delete from
        <include refid="Table_Name"/>
        <trim prefix="WHERE" prefixOverrides="AND" >
            <choose>
                <when test="id != null">
                    id = #{id,jdbcType=VARCHAR}
                </when>
            </choose>
            <if test="id != null">
                AND id=#{id,jdbcType=VARCHAR}
            </if>
            <if test="batchId != null">
                AND batch_id=#{batchId,jdbcType=VARCHAR}
            </if>
            <if test="envId != null">
                AND env_id=#{envId,jdbcType=VARCHAR}
            </if>
            <if test="scriptId != null">
                AND script_id=#{scriptId,jdbcType=DATE}
            </if>
            <if test="scriptName != null">
                AND script_name=#{scriptName,jdbcType=VARCHAR}
            </if>
            <if test="status != null">
                AND status=#{status,jdbcType=VARCHAR}
            </if>
            <if test="status != null">
                AND start_time=#{startTime,jdbcType=DATE}
            </if>
            <if test="endTime != null">
                AND end_time=#{endTime,jdbcType=DATE}
            </if>
        </trim>
    </delete>

    <insert id="insert" parameterType="net.northking.cctp.dw.db.entity.DwAtuPlanTask">
        insert into
        <include refid="Table_Name"/>
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">
                id,
            </if>
            <if test="batchId != null">
                batch_id,
            </if>
            <if test="envId != null">
                env_id,
            </if>
            <if test="scriptId != null">
                script_id,
            </if>
            <if test="startTime != null">
                start_time,
            </if>
            <if test="endTime != null">
                end_time,
            </if>
            <if test="status != null">
                status,
            </if>
            <if test="scriptName!=null">
                script_name,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">
                #{id, jdbcType=VARCHAR},
            </if>
            <if test="batchId != null">
                #{batchId, jdbcType=VARCHAR},
            </if>
            <if test="envId != null">
                #{envId, jdbcType=VARCHAR},
            </if>
            <if test="scriptId != null">
                #{scriptId, jdbcType=VARCHAR},
            </if>
            <if test="startTime != null">
                #{startTime, jdbcType=TIMESTAMP},
            </if>
            <if test="endTime != null">
                #{endTime, jdbcType=TIMESTAMP},
            </if>
            <if test="status != null">
                #{status, jdbcType=VARCHAR},
            </if>
            <if test="scriptName!=null">
                #{scriptName, jdbcType=VARCHAR},
            </if>
        </trim>
    </insert>

    <insert id="insertByBatch" parameterType="java.util.List">
        insert into
        <include refid="Table_Name"/>
        <trim prefix="(" suffix=")" suffixOverrides=",">
            id
            ,batch_id
            ,env_id
            ,script_id
            ,start_time
            ,end_time
            ,script_name
            ,status,
        </trim>
        values
        <foreach collection="list" item="item" index="index" separator=",">
            <trim prefix="(" suffix=")" suffixOverrides=",">
                #{item.id, jdbcType=VARCHAR},
                #{item.batchId, jdbcType=VARCHAR},
                #{item.envId, jdbcType=VARCHAR},
                #{item.scriptId, jdbcType=VARCHAR},
                #{item.startTime, jdbcType=DATE},
                #{item.endTime, jdbcType=DATE},
                #{item.scriptName, jdbcType=VARCHAR},
                #{item.status, jdbcType=VARCHAR},
            </trim>
        </foreach>
    </insert>

    <update id="updateByPrimaryKey" parameterType="net.northking.cctp.dw.db.entity.DwAtuPlanTask">
        update
        <include refid="Table_Name"/>
        <set>
            <if test="batchId != null">
                batch_id=#{batchId,jdbcType=VARCHAR},
            </if>
            <if test="envId != null">
                env_id=#{envId,jdbcType=VARCHAR},
            </if>
            <if test="scriptId != null">
                script_id=#{scriptId,jdbcType=DATE},
            </if>
            <if test="scriptName != null">
                script_name=#{scriptName,jdbcType=VARCHAR},
            </if>
            <if test="status != null">
                status=#{status,jdbcType=VARCHAR},
            </if>
            <if test="status != null">
                start_time=#{startTime,jdbcType=DATE},
            </if>
            <if test="endTime != null">
                end_time=#{endTime,jdbcType=DATE},
            </if>
        </set>
        where id = #{id,jdbcType=VARCHAR}
    </update>

    <select id="query" resultMap="BaseResultMap" parameterType="net.northking.cctp.dw.db.entity.DwAtuPlanTask">
        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="batchId != null">
                AND batch_id=#{batchId,jdbcType=VARCHAR}
            </if>
            <if test="envId != null">
                AND env_id=#{envId,jdbcType=VARCHAR}
            </if>
            <if test="scriptId != null">
                AND script_id=#{scriptId,jdbcType=DATE}
            </if>
            <if test="scriptName != null">
                AND script_name=#{scriptName,jdbcType=VARCHAR}
            </if>
            <if test="status != null">
                AND status=#{status,jdbcType=VARCHAR}
            </if>
            <if test="status != null">
                AND start_time=#{startTime,jdbcType=DATE}
            </if>
            <if test="endTime != null">
                AND end_time=#{endTime,jdbcType=DATE}
            </if>
        </trim>
    </select>

    <select id="queryCount" resultType="java.lang.Long" parameterType="net.northking.cctp.dw.db.entity.DwAtuCaseDesign">
        select count(*) from
        <include refid="Table_Name"/>
        <trim prefix="WHERE" prefixOverrides="AND" >
            <if test="id != null">
                AND id=#{id,jdbcType=VARCHAR}
            </if>
            <if test="batchId != null">
                AND batch_id=#{batchId,jdbcType=VARCHAR}
            </if>
            <if test="envId != null">
                AND env_id=#{envId,jdbcType=VARCHAR}
            </if>
            <if test="scriptId != null">
                AND script_id=#{scriptId,jdbcType=DATE}
            </if>
            <if test="scriptName != null">
                AND script_name=#{scriptName,jdbcType=VARCHAR}
            </if>
            <if test="status != null">
                AND status=#{status,jdbcType=VARCHAR}
            </if>
            <if test="status != null">
                AND start_time=#{startTime,jdbcType=DATE}
            </if>
            <if test="endTime != null">
                AND end_time=#{endTime,jdbcType=DATE}
            </if>
        </trim>
    </select>
    <select id="getFirstExecuteTime" resultType="net.northking.cctp.dw.dto.FirstExecuteInfoDto">
        select
            start_time as startTime,
            batch_id as batchId
        from
            dw_atu_plan_task
        where
            script_id = #{scriptId}
            and status = 2
        order by start_time
    </select>
    <select id="getFirstExecuteTimeByScriptIds" resultType="net.northking.cctp.dw.dto.FirstExecuteInfoDto">
        select
            dapt.batch_id as batchId,
            dapt.script_id scriptId,
            dapi.id planId,
            dapi.plan_name planName,
            MIN(dapt.start_time) as startTime
        from
            dw_atu_plan_task dapt
            LEFT JOIN dw_atu_plan_batch dapb ON dapt.batch_id = dapb.id
            LEFT JOIN dw_atu_plan_info dapi ON dapb.plan_id = dapi.id
        where
            dapi.id is not NULL AND
            dapt.script_id in
            <foreach collection="scriptIds" item="item" open="(" separator="," close=")">
                #{item, jdbcType=VARCHAR}
            </foreach>
            and dapt.status = 2
        GROUP BY script_id
    </select>

</mapper>