<?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.WorkFlowLineageMapper">

    <select id="queryWorkFlowLineageByName" resultType="org.apache.dolphinscheduler.dao.entity.WorkFlowLineage">
        select tepd.code as work_flow_code,tepd.name as work_flow_name
        from t_ds_process_definition tepd
        left join t_ds_schedules tes on tepd.code = tes.process_definition_code
        where tepd.project_code = #{projectCode}
        <if test="workFlowName != null and workFlowName != ''">
            and tepd.name like concat('%', #{workFlowName}, '%')
        </if>
    </select>

    <select id="queryWorkFlowLineageByCode" resultType="org.apache.dolphinscheduler.dao.entity.WorkFlowLineage">
        select tepd.code as work_flow_code,tepd.name as work_flow_name,
               '' as source_work_flow_code,
                tepd.release_state as work_flow_publish_status,
                tes.start_time as schedule_start_time,
                tes.end_time as schedule_end_time,
                tes.crontab as crontab,
                tes.release_state as schedule_publish_status
        from t_ds_process_definition tepd
        left join t_ds_schedules tes on tepd.code = tes.process_definition_code
        where tepd.project_code = #{projectCode} and tepd.code = #{workFlowCode}
    </select>

    <select id="queryWorkFlowLineageByLineage" resultType="org.apache.dolphinscheduler.dao.entity.WorkFlowLineage">
        select tepd.code as work_flow_code,tepd.name as work_flow_name,
               '' as source_work_flow_code,
                tepd.release_state as work_flow_publish_status,
                tes.start_time as schedule_start_time,
                tes.end_time as schedule_end_time,
                tes.crontab as crontab,
                tes.release_state as schedule_publish_status
        from t_ds_process_definition tepd
        left join t_ds_schedules tes on tepd.code = tes.process_definition_code
        where 1=1
        <if test="processLineages != null and processLineages.size != 0">
            and
            <foreach collection="processLineages" index="index" item="item" open="(" separator=" or " close=")">
                (tepd.project_code = #{item.projectCode}
                and tepd.code = #{item.processDefinitionCode})
            </foreach>
        </if>
    </select>

    <select id="queryProcessLineage" resultType="org.apache.dolphinscheduler.dao.entity.ProcessLineage">
    select ptr.project_code,
        ptr.post_task_code,
        ptr.post_task_version,
        ptr.pre_task_code,
        ptr.pre_task_version,
        ptr.process_definition_code,
        ptr.process_definition_version
        from t_ds_process_definition pd
        join t_ds_process_task_relation ptr on pd.code = ptr.process_definition_code
        and pd.version = ptr.process_definition_version
        where pd.project_code = #{projectCode}
    </select>

    <select id="queryProcessLineageByCode" resultType="org.apache.dolphinscheduler.dao.entity.ProcessLineage">
        select project_code,
               post_task_code,
               post_task_version,
               pre_task_code,
               pre_task_version,
               process_definition_code,
               process_definition_version
         from t_ds_process_task_relation
         where project_code = #{projectCode}
                and process_definition_code = #{processDefinitionCode}
    </select>
</mapper>
