<?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.seatunnel.app.dal.mapper.SeatunnalTaskDefinitionMapper">
    <sql id="baseSqlV2">
        ${alias}.id, ${alias}.code, ${alias}.name, ${alias}.version, ${alias}.description, ${alias}.project_code, ${alias}.user_id,
        ${alias}.task_type, ${alias}.task_params, ${alias}.flag, ${alias}.task_priority, ${alias}.worker_group, ${alias}.environment_code,
        ${alias}.fail_retry_times, ${alias}.fail_retry_interval, ${alias}.timeout_flag, ${alias}.timeout_notify_strategy, ${alias}.timeout,
        ${alias}.delay_time, ${alias}.resource_ids, ${alias}.create_time, ${alias}.update_time, ${alias}.task_group_id, ${alias}.task_group_priority
    </sql>

    <sql id="selectNewMainInfoField">
        select
        td.name task_name,td.code task_code,td.version task_version,td.task_type,td.create_time task_create_time,td.update_time task_update_time,
        pd.code process_definition_code,pd.version process_definition_version,pd.name process_definition_name,pd.release_state process_release_state,
        pt.pre_task_code upstream_task_code, td.project_code, p.name project_name
        from t_ds_task_definition td
                 LEFT JOIN t_ds_process_task_relation pt ON td.code	= pt.post_task_code and td.version=pt.post_task_version
                 LEFT JOIN t_ds_process_definition pd ON pt.process_definition_code = pd.code and pt.process_definition_version=pd.version
                 LEFT JOIN t_ds_project p ON td.project_code = p.code
    </sql>

    <select id="queryProcessTaskType" resultType="org.apache.seatunnel.app.dal.entity.TaskDefinitionExpand">
        select
        <include refid="baseSqlV2">
            <property name="alias" value="td"/>
        </include>
        from t_ds_process_task_relation ptr
        join t_ds_task_definition td on td.code = ptr.post_task_code
        where ptr.process_definition_code = #{processCode}
          and td.task_type = #{taskType}
    </select>
    <select id="queryAllTaskProcessDefinition" resultType="org.apache.seatunnel.app.dal.entity.TaskDefinition">
        select
        <include refid="baseSqlV2">
            <property name="alias" value="td"/>
        </include>
        from t_ds_task_definition td
        where 1=1
        <if test="taskCodesList.size() > 0 and taskCodesList != null">
            and td.code  in
            <foreach item="code" index="index" collection="taskCodesList" open="(" separator="," close=")">
                #{code}
            </foreach>
        </if>
    </select>

    <select id="queryTaskDefinitionByProjectCodesAndTaskTypes" resultType="org.apache.seatunnel.app.dal.entity.TaskDefinition">
        select
        <include refid="baseSqlV2">
            <property name="alias" value="td"/>
        </include>
        from t_ds_process_task_relation ptr
        join t_ds_task_definition td on td.code = ptr.post_task_code
        where 1=1
        <if test="definitionCodes.size() > 0 and definitionCodes != null">
            and ptr.process_definition_code  in
            <foreach item="definitionCode" index="index" collection="definitionCodes" open="(" separator="," close=")">
                #{definitionCode}
            </foreach>
        </if>
        <if test="projectCodes.size() > 0 and projectCodes != null">
            and td.project_code  in
            <foreach item="projectCode" index="index" collection="projectCodes" open="(" separator="," close=")">
                #{projectCode}
            </foreach>
        </if>
        <if test="taskTypes.size() > 0 and taskTypes != null">
            and td.task_type  in
            <foreach item="taskType" index="index" collection="taskTypes" open="(" separator="," close=")">
                #{taskType}
            </foreach>
        </if>
    </select>
    <select id="queryTaskDefinitionBySubprocessTask" resultType="org.apache.seatunnel.app.dal.entity.TaskMainInfo">
        <include refid="selectNewMainInfoField"/>
        where td.task_type = 'SUB_PROCESS'
          and td.task_params like concat('%"processDefinitionCode":', #{processDefinitionCode}, '%')
    </select>
    <select id="queryTaskDefinitionByDependentTaskWithTaskCode" resultType="org.apache.seatunnel.app.dal.entity.TaskMainInfo">
        <include refid="selectNewMainInfoField"/>
        where td.task_type = 'DEPENDENT'
            and td.task_params like concat('%"depTaskCode":', #{taskCode}, '%')
    </select>

    <select id="queryTaskDefinitionByDependentTaskWithProcessCode" resultType="org.apache.seatunnel.app.dal.entity.TaskMainInfo">
        <include refid="selectNewMainInfoField"/>
        where td.task_type = 'DEPENDENT'
        and td.task_params like concat('%"definitionCode":', #{processDefinitionCode}, '%')
        and td.task_params like '%"depTaskCode":0%'
    </select>

</mapper>
