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

    <sql id="baseSql">
       id, code, name, version, description, project_code,
        release_state, user_id,global_params, flag, locations,
         warning_group_id, timeout, tenant_id,operator,execution_type, operate_time, create_time,
          update_time
    </sql>
    <select id="queryByDefinitionName" resultType="org.apache.dolphinscheduler.dao.entity.ProcessDefinitionLog">
        select pd.id, pd.code, pd.name, pd.version, pd.description, pd.project_code,
        pd.release_state, pd.user_id,pd.global_params, pd.flag, pd.locations,
        pd.warning_group_id, pd.timeout, pd.tenant_id,pd.operator, pd.operate_time, pd.create_time,
        pd.update_time, u.user_name,p.name as project_name ,pd.execution_type
        from t_ds_process_definition_log pd
        JOIN t_ds_user u ON pd.user_id = u.id
        JOIN  t_ds_project p ON pd.project_code = p.code
        WHERE p.code = #{projectCode}
        and pd.name = #{name}
    </select>
    <select id="queryByDefinitionCode" resultType="org.apache.dolphinscheduler.dao.entity.ProcessDefinitionLog">
        select
        <include refid="baseSql"/>
        from t_ds_process_definition_log
        WHERE code = #{code}
    </select>
    <select id="queryByDefinitionCodeAndVersion"
            resultType="org.apache.dolphinscheduler.dao.entity.ProcessDefinitionLog">
        select
        <include refid="baseSql"/>
        from t_ds_process_definition_log
        where code = #{code}
        and version = #{version}
    </select>
    <select id="queryMaxVersionForDefinition" resultType="java.lang.Integer">
        select max(version)
        from t_ds_process_definition_log
        where code = #{code}
    </select>

    <select id="queryMaxVersionDefinitionLog" resultType="org.apache.dolphinscheduler.dao.entity.ProcessDefinitionLog">

        select
        <include refid="baseSql"/>
        from t_ds_process_definition_log
        where code = #{code} order by version desc limit 1
    </select>

    <select id="queryProcessDefinitionVersionsPaging"
            resultType="org.apache.dolphinscheduler.dao.entity.ProcessDefinitionLog">
        select
        <include refid="baseSql"/>
        from t_ds_process_definition_log
        where code = #{code}
        <if test="projectCode != 0">
            and project_code = #{projectCode}
        </if>
        order by version desc
    </select>

    <delete id="deleteByProcessDefinitionCodeAndVersion">
        delete
        from t_ds_process_definition_log
        where code = #{code}
          and version = #{version}
    </delete>
</mapper>
