<?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.ProcessTaskRelationLogMapper">
    <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, operator, operate_time, create_time, update_time
    </sql>
    <select id="queryByProcessCodeAndVersion" resultType="org.apache.dolphinscheduler.dao.entity.ProcessTaskRelationLog">
        select
        <include refid="baseSql"/>
        from t_ds_process_task_relation_log
        WHERE process_definition_code = #{processCode}
        and process_definition_version = #{processVersion}
    </select>
    <insert id="batchInsert">
        insert into t_ds_process_task_relation_log (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, operator, operate_time,
        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.operator},#{relation.operateTime},
            #{relation.createTime},#{relation.updateTime})
        </foreach>
    </insert>
    <delete id="deleteByCode">
        delete from t_ds_process_task_relation_log
        WHERE process_definition_code = #{processCode}
        and process_definition_version = #{processVersion}
    </delete>
    <delete id="deleteRelation">
        delete from t_ds_process_task_relation_log
        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="queryRelationLogByRelation" resultType="org.apache.dolphinscheduler.dao.entity.ProcessTaskRelationLog">
        select
        <include refid="baseSql"/>
        from t_ds_process_task_relation_log
        WHERE project_code = #{processTaskRelation.projectCode}
        and process_definition_code = #{processTaskRelation.processDefinitionCode}
        and process_definition_version = #{processTaskRelation.processDefinitionVersion}
        and pre_task_code = #{processTaskRelation.preTaskCode}
        and pre_task_version = #{processTaskRelation.preTaskVersion}
        and post_task_code = #{processTaskRelation.postTaskCode}
        and post_task_version = #{processTaskRelation.postTaskVersion}
    </select>
</mapper>
