<?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.TaskDefinitionMapper">
    <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, create_time, update_time
    </sql>
    <select id="queryByName" resultType="org.apache.dolphinscheduler.dao.entity.TaskDefinition">
        select
        <include refid="baseSql"/>
        from t_ds_task_definition
        WHERE project_code = #{projectCode}
        and name = #{name}
    </select>
    <select id="queryAllDefinitionList" resultType="org.apache.dolphinscheduler.dao.entity.TaskDefinition">
        select
        <include refid="baseSql"/>
        from t_ds_task_definition
        where project_code = #{projectCode}
        order by create_time desc
    </select>

    <select id="countDefinitionGroupByUser" resultType="org.apache.dolphinscheduler.dao.entity.DefinitionGroupByUser">
        SELECT td.user_id as user_id, tu.user_name as user_name, count(0) as count
        FROM t_ds_task_definition td
        JOIN t_ds_user tu on tu.id=td.user_id
        where 1 = 1
        <if test="projectCodes != null and projectCodes.length != 0">
            and td.project_code in
            <foreach collection="projectCodes" index="index" item="i" open="(" separator="," close=")">
                #{i}
            </foreach>
        </if>
        group by td.user_id,tu.user_name
    </select>

    <select id="queryByCode" resultType="org.apache.dolphinscheduler.dao.entity.TaskDefinition">
        select
        <include refid="baseSql"/>
        from t_ds_task_definition
        where code = #{code}
    </select>
    <select id="listResources" resultType="java.util.HashMap">
        SELECT id,resource_ids
        FROM t_ds_task_definition
        WHERE  resource_ids is not null and resource_ids != ''
    </select>

    <select id="listResourcesByUser" resultType="java.util.HashMap">
        SELECT id,resource_ids
        FROM t_ds_task_definition
        WHERE user_id = #{userId} and resource_ids is not null and resource_ids != ''
    </select>
    <delete id="deleteByCode">
        delete from t_ds_task_definition
        where code = #{code}
    </delete>
    <insert id="batchInsert">
        insert into t_ds_task_definition (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, create_time, update_time)
        values
        <foreach collection="taskDefinitions" item="taskDefinition" separator=",">
            (#{taskDefinition.code},#{taskDefinition.name},#{taskDefinition.version},#{taskDefinition.description},
            #{taskDefinition.projectCode},#{taskDefinition.userId},#{taskDefinition.taskType},#{taskDefinition.taskParams},#{taskDefinition.flag},
            #{taskDefinition.taskPriority},#{taskDefinition.workerGroup},#{taskDefinition.environmentCode},#{taskDefinition.failRetryTimes},
            #{taskDefinition.failRetryInterval},#{taskDefinition.timeoutFlag},#{taskDefinition.timeoutNotifyStrategy},#{taskDefinition.timeout},
            #{taskDefinition.delayTime},#{taskDefinition.resourceIds},#{taskDefinition.createTime},#{taskDefinition.updateTime})
        </foreach>
    </insert>
    <select id="queryDefineListPaging" resultType="org.apache.dolphinscheduler.dao.entity.TaskDefinition">
        select td.id, td.code, td.name, td.version, td.description, td.project_code, td.user_id, td.task_type, td.task_params, td.flag,
        td.task_priority, td.worker_group, td.environment_code, td.fail_retry_times, td.fail_retry_interval, td.timeout_flag, td.timeout_notify_strategy,
        td.timeout, td.delay_time, td.resource_ids, td.create_time, td.update_time, u.user_name,p.name as project_name
        from t_ds_task_definition td
        JOIN t_ds_user u ON td.user_id = u.id
        JOIN t_ds_project p ON td.project_code = p.code
        where td.project_code = #{projectCode}
        <if test=" taskType != null and taskType != ''">
            and td.task_type = #{taskType}
        </if>
        <if test=" searchVal != null and searchVal != ''">
            and td.name like concat('%', #{searchVal}, '%')
        </if>
        <if test=" userId != 0">
            and td.user_id = #{userId}
        </if>
        order by td.update_time desc
    </select>
</mapper>
