<?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.TaskInstanceMapper">
    <sql id="baseSql">
        id, name, task_type, process_instance_id, task_code, task_definition_version, state, submit_time,
        start_time, end_time, host, execute_path, log_path, alert_flag, retry_times, pid, app_link,
        flag, retry_interval, max_retry_times, task_instance_priority, worker_group,environment_code , executor_id,
        first_submit_time, delay_time, task_params, var_pool, dry_run
    </sql>
    <sql id="baseSqlV2">
        ${alias}.id, ${alias}.name, ${alias}.task_type, ${alias}.task_code, ${alias}.task_definition_version, ${alias}.process_instance_id, ${alias}.state, ${alias}.submit_time,
        ${alias}.start_time, ${alias}.end_time, ${alias}.host, ${alias}.execute_path, ${alias}.log_path, ${alias}.alert_flag, ${alias}.retry_times, ${alias}.pid, ${alias}.app_link,
        ${alias}.flag, ${alias}.retry_interval, ${alias}.max_retry_times, ${alias}.task_instance_priority, ${alias}.worker_group,${alias}.environment_code , ${alias}.executor_id,
        ${alias}.first_submit_time, ${alias}.delay_time, ${alias}.task_params, ${alias}.var_pool, ${alias}.dry_run
    </sql>
    <update id="setFailoverByHostAndStateArray">
        update t_ds_task_instance
        set state = #{destStatus}
        where host = #{host}
        and state in
        <foreach collection="states" index="index" item="i" open="(" separator="," close=")">
            #{i}
        </foreach>
    </update>
    <select id="queryTaskByProcessIdAndState" resultType="java.lang.Integer">
        select id
        from t_ds_task_instance
        WHERE  process_instance_id = #{processInstanceId}
        and state = #{state}
        and flag = 1
    </select>
    <select id="findValidTaskListByProcessId" resultType="org.apache.dolphinscheduler.dao.entity.TaskInstance">
        select
        <include refid="baseSql"/>
        from t_ds_task_instance
        WHERE process_instance_id = #{processInstanceId}
        and flag = #{flag}
        order by start_time desc
    </select>
    <select id="queryByHostAndStatus" resultType="org.apache.dolphinscheduler.dao.entity.TaskInstance">
        select
        <include refid="baseSql"/>
        from t_ds_task_instance
        where 1 = 1
        <if test="host != null and host != ''">
            and host = #{host}
        </if>
        <if test="states != null and states.length != 0">
            and state in
            <foreach collection="states" index="index" item="i" open="(" separator="," close=")">
                #{i}
            </foreach>
        </if>
    </select>
    <select id="countTaskInstanceStateByUser" resultType="org.apache.dolphinscheduler.dao.entity.ExecuteStatusCount">
        select state, count(0) as count
        from t_ds_task_instance t
        left join t_ds_task_definition_log d on d.code=t.task_code and d.version=t.task_definition_version
        left join t_ds_project p on p.code=d.project_code
        where 1=1
        <if test="projectCodes != null and projectCodes.length != 0">
            and d.project_code in
            <foreach collection="projectCodes" index="index" item="i" open="(" separator="," close=")">
                #{i}
            </foreach>
        </if>
        <if test="startTime != null">
            and t.start_time <![CDATA[ > ]]> #{startTime}
        </if>
        <if test="endTime != null">
            and t.start_time <![CDATA[ <= ]]> #{endTime}
        </if>
        group by t.state
    </select>
    <select id="queryByInstanceIdAndName" resultType="org.apache.dolphinscheduler.dao.entity.TaskInstance">
        select
        <include refid="baseSql"/>
        from t_ds_task_instance
        where process_instance_id = #{processInstanceId}
        and name = #{name}
        and flag = 1
        limit 1
    </select>
    <select id="queryByInstanceIdAndCode" resultType="org.apache.dolphinscheduler.dao.entity.TaskInstance">
        select
        <include refid="baseSql"/>
        from t_ds_task_instance
        where process_instance_id = #{processInstanceId}
        and task_code = #{taskCode}
        and flag = 1
        limit 1
    </select>
    <select id="countTask" resultType="java.lang.Integer">
        select count(1) as count
        from t_ds_task_instance task,t_ds_task_definition_log define
        where task.task_code=define.code
        and task.task_definition_version=define.version
        <if test="projectCodes != null and projectCodes.length != 0">
            and define.project_code in
            <foreach collection="projectCodes" index="index" item="i" open="(" separator="," close=")">
                #{i}
            </foreach>
        </if>
        <if test="taskIds != null and taskIds.length != 0">
            and task.id in
            <foreach collection="taskIds" index="index" item="i" open="(" separator="," close=")">
                #{i}
            </foreach>
        </if>
    </select>
    <select id="queryTaskInstanceListPaging" resultType="org.apache.dolphinscheduler.dao.entity.TaskInstance">
        select
        <include refid="baseSqlV2">
            <property name="alias" value="instance"/>
        </include>
        ,
        process.name as process_instance_name
        from t_ds_task_instance instance
        left join t_ds_task_definition_log define on define.code=instance.task_code and define.version=instance.task_definition_version
        left join t_ds_process_instance process on process.id=instance.process_instance_id
        where define.project_code = #{projectCode}
        <if test="startTime != null">
            and instance.start_time > #{startTime} and instance.start_time <![CDATA[ <=]]> #{endTime}
        </if>
        <if test="processInstanceId != 0">
            and instance.process_instance_id = #{processInstanceId}
        </if>
        <if test="searchVal != null and searchVal != ''">
            and instance.name like concat('%', #{searchVal}, '%')
        </if>
        <if test="taskName != null and taskName != ''">
            and instance.name=#{taskName}
        </if>
        <if test="states != null and states.length != 0">
            and instance.state in
            <foreach collection="states" index="index" item="i" open="(" separator="," close=")">
                #{i}
            </foreach>
        </if>
        <if test="host != null and host != ''">
            and instance.host like concat('%', #{host}, '%')
        </if>
        <if test="executorId != 0">
            and instance.executor_id = #{executorId}
        </if>
        <if test="processInstanceName != null and processInstanceName != ''">
            and process.name like concat('%', #{processInstanceName}, '%')
        </if>
        order by instance.start_time desc
    </select>
    <update id="updateHostAndSubmitTimeById">
        update t_ds_task_instance
        set host        = #{host},
            submit_time = #{submitTime}
        where id = #{id}
    </update>
    <select id="queryLastTaskInstance" resultType="org.apache.dolphinscheduler.dao.entity.TaskInstance">
        select
        <include refid="baseSql"/>
        from t_ds_task_instance
        where task_code=#{taskCode}
        <if test="startTime!=null and endTime != null">
            and start_time <![CDATA[ >= ]]> #{startTime} and start_time <![CDATA[ <= ]]> #{endTime}
        </if>
        order by end_time desc limit 1
    </select>
    <select id="queryLastTaskInstanceList" resultType="org.apache.dolphinscheduler.dao.entity.TaskInstance">
        select
        <include refid="baseSql"/>
        from t_ds_task_instance
        where 1=1
        <if test="taskCodes != null and taskCodes.size() != 0">
            and task_code in
            <foreach collection="taskCodes" index="index" item="i" open="(" separator="," close=")">
                #{i}
            </foreach>
        </if>
        <if test="startTime!=null and endTime != null">
            and start_time <![CDATA[ >= ]]> #{startTime} and start_time <![CDATA[ <= ]]> #{endTime}
        </if>
    </select>
    <select id="queryTaskInstanceListByIds" resultType="org.apache.dolphinscheduler.dao.entity.TaskInstance">
        select
        <include refid="baseSql"/>
        from t_ds_task_instance
        where 1=1
        <if test="ids != null and ids.size() != 0">
            and id in
            <foreach collection="ids" index="index" item="i" open="(" separator="," close=")">
                #{i}
            </foreach>
        </if>
    </select>
</mapper>
