<?xml version="1.0" encoding="UTF-8" ?>
<!--
  ~ Licensed to the Apache Software Foundation (ASF) under one or more
  ~ contributor license agreements.  See the NOTICE file distributed with
  ~ this work for additional information regarding copyright ownership.
  ~ The ASF licenses this file to You under the Apache License, Version 2.0
  ~ (the "License"); you may not use this file except in compliance with
  ~ the License.  You may obtain a copy of the License at
  ~
  ~     http://www.apache.org/licenses/LICENSE-2.0
  ~
  ~ Unless required by applicable law or agreed to in writing, software
  ~ distributed under the License is distributed on an "AS IS" BASIS,
  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  ~ See the License for the specific language governing permissions and
  ~ limitations under the License.
  -->

<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="org.apache.dolphinscheduler.dao.mapper.TaskDefinitionLogMapper">
    <sql id="baseSql">
        id, code, name, version, description, project_code, user_id, task_type, task_params, flag, task_priority,
        worker_group, environment_code, fail_retry_times, fail_retry_interval, timeout_flag, timeout_notify_strategy, timeout, delay_time,
        resource_ids, operator, operate_time, create_time, update_time
    </sql>
    <select id="queryMaxVersionForDefinition" resultType="java.lang.Integer">
        select max(version)
        from t_ds_task_definition_log
        WHERE code = #{code}
    </select>
    <select id="queryByDefinitionCodeAndVersion" resultType="org.apache.dolphinscheduler.dao.entity.TaskDefinitionLog">
        select
        <include refid="baseSql"/>
        from t_ds_task_definition_log
        WHERE code = #{code}
        and version = #{version}
    </select>
    <select id="queryByTaskDefinitions" resultType="org.apache.dolphinscheduler.dao.entity.TaskDefinitionLog">
        select
        <include refid="baseSql"/>
        from t_ds_task_definition_log
        WHERE  1 = 1
        <if test="taskDefinitions != null and taskDefinitions.size != 0">
            and
            <foreach collection="taskDefinitions" index="index" item="item" open="(" separator=" or " close=")">
                (code = #{item.code}
                and version = #{item.version})
            </foreach>
        </if>
    </select>
    <insert id="batchInsert">
        insert into t_ds_task_definition_log (code, name, version, description, project_code, user_id,
        task_type, task_params, flag, task_priority, worker_group, environment_code, fail_retry_times, fail_retry_interval,
        timeout_flag, timeout_notify_strategy, timeout, delay_time, resource_ids, operator, operate_time, create_time, update_time)
        values
        <foreach collection="taskDefinitionLogs" item="taskDefinitionLog" separator=",">
            (#{taskDefinitionLog.code},#{taskDefinitionLog.name},#{taskDefinitionLog.version},#{taskDefinitionLog.description},
            #{taskDefinitionLog.projectCode},#{taskDefinitionLog.userId},#{taskDefinitionLog.taskType},#{taskDefinitionLog.taskParams},
            #{taskDefinitionLog.flag},#{taskDefinitionLog.taskPriority},#{taskDefinitionLog.workerGroup},#{taskDefinitionLog.environmentCode},
            #{taskDefinitionLog.failRetryTimes},#{taskDefinitionLog.failRetryInterval},#{taskDefinitionLog.timeoutFlag},#{taskDefinitionLog.timeoutNotifyStrategy},
            #{taskDefinitionLog.timeout},#{taskDefinitionLog.delayTime},#{taskDefinitionLog.resourceIds},#{taskDefinitionLog.operator},#{taskDefinitionLog.operateTime},
            #{taskDefinitionLog.createTime},#{taskDefinitionLog.updateTime})
        </foreach>
    </insert>
    <delete id="deleteByCodeAndVersion">
        delete
        from t_ds_task_definition_log
        where code = #{code}
          and version = #{version}
    </delete>
    <select id="queryTaskDefinitionVersionsPaging" resultType="org.apache.dolphinscheduler.dao.entity.TaskDefinitionLog">
        select
        <include refid="baseSql"/>
        from t_ds_task_definition_log
        where code = #{code}
        <if test="projectCode != 0">
            and project_code = #{projectCode}
        </if>
        order by version desc
    </select>
</mapper>
