<?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, task_group_id
    </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, ${alias}.task_group_id
    </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="countTaskInstanceStateByProjectCodes" 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="countTaskInstanceStateByProjectCodesAndStatesBySubmitTime" 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="states != null and states.size != 0">
            and t.state in
            <foreach collection="states" index="index" item="state" open="(" separator="," close=")">
                #{state.code}
            </foreach>
        </if>
        <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.submit_time <![CDATA[ > ]]> #{startTime}
        </if>
        <if test="endTime != null">
            and t.submit_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>
    <select id="loadAllInfosNoRelease" resultType="org.apache.dolphinscheduler.dao.entity.TaskInstance">
        select
        <include refid="baseSqlV2">
            <property name="alias" value="instance"/>
        </include>
        from t_ds_task_instance instance
        left join t_ds_task_group_queue que on instance.id = que.task_id
        where instance.process_instance_id = #{processInstanceId}
        and que.status = #{status}
    </select>
</mapper>
