<?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.ProcessTaskRelationMapper">
    <sql id="baseSql">
        id, name, process_definition_version, project_code, process_definition_code, pre_task_code, pre_task_version,
        post_task_code, post_task_version, condition_type, condition_params, create_time, update_time
    </sql>
    <select id="queryByProcessCode" resultType="org.apache.dolphinscheduler.dao.entity.ProcessTaskRelation">
        select
        <include refid="baseSql"/>
        from t_ds_process_task_relation
        WHERE project_code = #{projectCode}
        and process_definition_code = #{processCode}
    </select>
    <select id="queryByTaskCode" resultType="org.apache.dolphinscheduler.dao.entity.ProcessTaskRelation">
        select
        <include refid="baseSql"/>
        from t_ds_process_task_relation
        WHERE pre_task_code = #{taskCode}
        <if test="taskCode != 0">
            or post_task_code = #{taskCode}
        </if>
    </select>
    <select id="queryByTaskCodes" resultType="org.apache.dolphinscheduler.dao.entity.ProcessTaskRelation">
        select
        <include refid="baseSql"/>
        from t_ds_process_task_relation
        WHERE 1 = 1
        <if test="taskCodes != null and taskCodes.length != 0">
            and pre_task_code in
            <foreach collection="taskCodes" index="index" item="i" open="(" separator="," close=")">
                #{i}
            </foreach>
            or post_task_code in
            <foreach collection="taskCodes" index="index" item="i" open="(" separator="," close=")">
                #{i}
            </foreach>
        </if>
    </select>
    <delete id="deleteByCode">
        delete from t_ds_process_task_relation
        WHERE project_code = #{projectCode}
        and process_definition_code = #{processCode}
    </delete>
    <insert id="batchInsert">
        insert into t_ds_process_task_relation (name, process_definition_version, project_code, process_definition_code,
        pre_task_code, pre_task_version, post_task_code, post_task_version, condition_type, condition_params, create_time, update_time)
        values
        <foreach collection="taskRelationList" item="relation" separator=",">
            (#{relation.name},#{relation.processDefinitionVersion},#{relation.projectCode},#{relation.processDefinitionCode},
            #{relation.preTaskCode},#{relation.preTaskVersion},#{relation.postTaskCode},#{relation.postTaskVersion},
            #{relation.conditionType},#{relation.conditionParams},#{relation.createTime},#{relation.updateTime})
        </foreach>
    </insert>
    <select id="queryDownstreamByProcessDefinitionCode" resultType="org.apache.dolphinscheduler.dao.entity.ProcessTaskRelation">
        select
        <include refid="baseSql"/>
        from t_ds_process_task_relation
        where process_definition_code = #{processDefinitionCode}
        and pre_task_version >= 1
    </select>
    <select id="queryDownstreamByTaskCode" resultType="org.apache.dolphinscheduler.dao.entity.ProcessTaskRelation">
        select
        <include refid="baseSql"/>
        from t_ds_process_task_relation
        WHERE pre_task_code = #{taskCode}
    </select>

    <select id="queryDownstreamByCode" resultType="org.apache.dolphinscheduler.dao.entity.ProcessTaskRelation">
        select
        <include refid="baseSql"/>
        from t_ds_process_task_relation
        WHERE project_code = #{projectCode}
        and pre_task_code = #{taskCode}
    </select>

    <select id="queryUpstreamByCode" resultType="org.apache.dolphinscheduler.dao.entity.ProcessTaskRelation">
        select
        <include refid="baseSql"/>
        from t_ds_process_task_relation
        WHERE project_code = #{projectCode}
        and post_task_code = #{taskCode}
    </select>

    <select id="queryUpstreamByCodes" resultType="org.apache.dolphinscheduler.dao.entity.ProcessTaskRelation">
        select
        <include refid="baseSql"/>
        from t_ds_process_task_relation
        WHERE project_code = #{projectCode}
        and post_task_code = #{taskCode}
        <if test="preTaskCodes != null and preTaskCodes.length != 0">
            and pre_task_code in
            <foreach collection="preTaskCodes" index="index" item="i" open="(" separator="," close=")">
                #{i}
            </foreach>
        </if>
    </select>

    <select id="countUpstreamByCodeGroupByProcessDefinitionCode" resultType="java.util.HashMap">
        select process_definition_code as processDefinitionCode,
        count(0) as countValue
        from t_ds_process_task_relation
        WHERE project_code = #{projectCode}
        and post_task_code = #{taskCode}
        <if test="processDefinitionCodes != null and processDefinitionCodes.length != 0">
            and process_definition_code in
            <foreach collection="processDefinitionCodes" index="index" item="i" open="(" separator="," close=")">
                #{i}
            </foreach>
        </if>
        group by process_definition_code
    </select>

    <select id="queryProcessTaskRelationsByProcessDefinitionCode" resultType="org.apache.dolphinscheduler.dao.entity.ProcessTaskRelation">
        SELECT
        <include refid="baseSql"/>
        FROM
        t_ds_process_task_relation
        WHERE 1=1
        AND process_definition_code = #{processDefinitionCode}
        AND process_definition_version = #{processDefinitionVersion}
    </select>

    <update id="batchUpdateProcessTaskRelationPreTask" parameterType="java.util.List">
        <foreach collection="processTaskRelationList" item="processTaskRelation" index="index" open="" close="" separator=";">
            update t_ds_process_task_relation
            <set>
                pre_task_code=#{processTaskRelation.preTaskCode},
                pre_task_version=#{processTaskRelation.preTaskVersion}
            </set>
            <where>
                WHERE id = #{processTaskRelation.id}
            </where>
        </foreach>
    </update>

    <select id="queryByCode" resultType="org.apache.dolphinscheduler.dao.entity.ProcessTaskRelation">
        select
        <include refid="baseSql"/>
        from t_ds_process_task_relation
        WHERE 1=1
        <if test="projectCode != 0">
            and project_code = #{projectCode}
        </if>
        <if test="processDefinitionCode != 0">
            and process_definition_code = #{processDefinitionCode}
        </if>
        <if test="preTaskCode != 0">
            and pre_task_code = #{preTaskCode}
        </if>
        <if test="postTaskCode != 0">
            and post_task_code = #{postTaskCode}
        </if>
    </select>

    <delete id="deleteRelation">
        delete from t_ds_process_task_relation
        WHERE project_code = #{processTaskRelationLog.projectCode}
          and process_definition_code = #{processTaskRelationLog.processDefinitionCode}
          and process_definition_version = #{processTaskRelationLog.processDefinitionVersion}
        <if test="processTaskRelationLog.preTaskCode !=0 and processTaskRelationLog.preTaskVersion != 0">
            and pre_task_code = #{processTaskRelationLog.preTaskCode}
            and pre_task_version = #{processTaskRelationLog.preTaskVersion}
        </if>
          and post_task_code = #{processTaskRelationLog.postTaskCode}
          and post_task_version = #{processTaskRelationLog.postTaskVersion}
    </delete>

    <select id="countByCode" resultType="int">
        select count(0)
        from t_ds_process_task_relation
        WHERE 1=1
        <if test="projectCode != 0">
            and project_code = #{projectCode}
        </if>
        <if test="processDefinitionCode != 0">
            and process_definition_code = #{processDefinitionCode}
        </if>
        <if test="preTaskCode != 0">
            and pre_task_code = #{preTaskCode}
        </if>
        <if test="postTaskCode != 0">
            and post_task_code = #{postTaskCode}
        </if>
    </select>

    <select id="filterProcessTaskRelation"
            parameterType="org.apache.dolphinscheduler.dao.entity.ProcessTaskRelation"
            resultType="org.apache.dolphinscheduler.dao.entity.ProcessTaskRelation">
        SELECT
        <include refid="baseSql"/>
        FROM t_ds_process_task_relation
        <where>
            <if test=" relation.projectCode != 0">
                and project_code = #{relation.projectCode}
            </if>
            <if test=" relation.processDefinitionCode != 0">
                and process_definition_code = #{relation.processDefinitionCode}
            </if>
            <if test=" relation.preTaskCode != 0">
                and pre_task_code = #{relation.preTaskCode}
            </if>
            <if test=" relation.postTaskCode != 0">
                and post_task_code = #{relation.postTaskCode}
            </if>
        </where>
        order by update_time desc, id asc
    </select>
    <update id="updateProcessTaskRelationTaskVersion">
            update t_ds_process_task_relation
            set
                pre_task_version=#{processTaskRelation.preTaskVersion},
                post_task_version=#{processTaskRelation.postTaskVersion}
            where
                id = #{processTaskRelation.id}
    </update>
</mapper>
