<?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.ProcessDefinitionMapper">
    <sql id="baseSql">
        id, name, version, release_state, project_id, user_id, process_definition_json, description,
        global_params, flag, locations, connects, receivers, receivers_cc, create_time, timeout,
        tenant_id, update_time, modify_by, resource_ids
    </sql>
    <select id="verifyByDefineName" resultType="org.apache.dolphinscheduler.dao.entity.ProcessDefinition">
        select pd.id, pd.name, pd.version, pd.release_state, pd.project_id, pd.user_id, pd.process_definition_json, pd.description,
        pd.global_params, pd.flag, pd.locations, pd.connects, pd.receivers, pd.receivers_cc, pd.create_time, pd.timeout,
        pd.tenant_id, pd.update_time, pd.modify_by, pd.resource_ids
        from t_ds_process_definition pd
        WHERE pd.project_id = #{projectId}
        and pd.name = #{processDefinitionName}
    </select>
    <select id="queryByDefineName" resultType="org.apache.dolphinscheduler.dao.entity.ProcessDefinition">
        select pd.id, pd.name, pd.version, pd.release_state, pd.project_id, pd.user_id, pd.process_definition_json, pd.description,
        pd.global_params, pd.flag, pd.locations, pd.connects, pd.receivers, pd.receivers_cc, pd.create_time, pd.timeout,
        pd.tenant_id, pd.update_time, pd.modify_by, pd.resource_ids,
        u.user_name,p.name as project_name,t.tenant_code,q.queue,q.queue_name
        from t_ds_process_definition pd
        JOIN t_ds_user u ON pd.user_id = u.id
        JOIN  t_ds_project p ON pd.project_id = p.id
        JOIN  t_ds_tenant t ON t.id = u.tenant_id
        JOIN t_ds_queue q ON t.queue_id = q.id
        WHERE p.id = #{projectId}
        and pd.name = #{processDefinitionName}
    </select>
    <select id="queryDefineListPaging" resultType="org.apache.dolphinscheduler.dao.entity.ProcessDefinition">
        SELECT td.id, td.name, td.version, td.release_state, td.project_id, td.user_id, td.description,
        td.global_params,
        td.flag, td.receivers, td.receivers_cc, td.timeout, td.tenant_id, td.modify_by, td.update_time, td.create_time,
        sc.schedule_release_state, tu.user_name
        FROM t_ds_process_definition td
        left join (select process_definition_id,release_state as schedule_release_state from t_ds_schedules group by
        process_definition_id,release_state) sc on sc.process_definition_id = td.id
        left join t_ds_user tu on td.user_id = tu.id
        where td.project_id = #{projectId}
        <if test=" searchVal != null and searchVal != ''">
            and td.name like concat('%', #{searchVal}, '%')
        </if>
        <if test=" userId != 0">
            and td.user_id = #{userId}
        </if>
        order by sc.schedule_release_state desc,td.update_time desc
    </select>

    <select id="queryAllDefinitionList" resultType="org.apache.dolphinscheduler.dao.entity.ProcessDefinition">
        select
        <include refid="baseSql"/>
        from t_ds_process_definition
        where project_id = #{projectId}
        order by create_time desc
    </select>
    <select id="queryDefinitionListByTenant" resultType="org.apache.dolphinscheduler.dao.entity.ProcessDefinition">
        select
        <include refid="baseSql"/>
        from t_ds_process_definition
        where tenant_id = #{tenantId}
    </select>
    <select id="queryDefinitionListByIdList" resultType="org.apache.dolphinscheduler.dao.entity.ProcessDefinition">
        select
        <include refid="baseSql"/>
        from t_ds_process_definition
        where id in
        <foreach collection="ids" index="index" item="i" open="(" separator="," close=")">
            #{i}
        </foreach>
    </select>
    <select id="countDefinitionGroupByUser" resultType="org.apache.dolphinscheduler.dao.entity.DefinitionGroupByUser">
        SELECT td.user_id as user_id, tu.user_name as user_name, count(0) as count
        FROM t_ds_process_definition td
        JOIN t_ds_user tu on tu.id=td.user_id
        where 1 = 1
        <if test="projectIds != null and projectIds.length != 0">
            and td.project_id in
            <foreach collection="projectIds" index="index" item="i" open="(" separator="," close=")">
                #{i}
            </foreach>
        </if>
        group by td.user_id,tu.user_name
    </select>
    <select id="queryByDefineId" resultType="org.apache.dolphinscheduler.dao.entity.ProcessDefinition">
        SELECT
            pd.id, pd.name, pd.version, pd.release_state, pd.project_id, pd.user_id, pd.process_definition_json, pd.description,
            pd.global_params, pd.flag, pd.locations, pd.connects, pd.receivers, pd.receivers_cc, pd.create_time, pd.timeout,
            pd.tenant_id, pd.update_time, pd.modify_by, pd.resource_ids,
            u.user_name,
            p.name AS project_name
        FROM
            t_ds_process_definition pd,
            t_ds_user u,
            t_ds_project p
        WHERE
            pd.user_id = u.id AND pd.project_id = p.id
        AND pd.id = #{processDefineId}
    </select>


    <select id="listResources" resultType="java.util.HashMap">
        SELECT id,resource_ids
        FROM t_ds_process_definition
        WHERE release_state = 1 and resource_ids is not null and resource_ids != ''
    </select>

    <select id="listResourcesByUser" resultType="java.util.HashMap">
        SELECT id,resource_ids
        FROM t_ds_process_definition
        WHERE user_id = #{userId} and release_state = 1 and resource_ids is not null and resource_ids != ''
    </select>

    <update id="updateVersionByProcessDefinitionId">
        update  t_ds_process_definition
        set version = #{version}
        where id = #{processDefinitionId}
    </update>
</mapper>
