<?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.UserMapper">
    <sql id="baseSql">
       id, user_name, user_password, user_type, email, phone, tenant_id, create_time, update_time, queue, state, time_zone
    </sql>
    <sql id="baseSqlV2">
       ${alias}.id, ${alias}.user_name, ${alias}.user_password, ${alias}.user_type, ${alias}.email, ${alias}.phone, ${alias}.tenant_id,
       ${alias}.create_time, ${alias}.update_time, ${alias}.queue, ${alias}.state, ${alias}.time_zone
    </sql>
    <select id="queryAllGeneralUser" resultType="org.apache.dolphinscheduler.dao.entity.User">
        select
        <include refid="baseSql"/>
        from t_ds_user
        where user_type=1;
    </select>
    <select id="queryByUserNameAccurately" resultType="org.apache.dolphinscheduler.dao.entity.User">
        select
        <include refid="baseSql"/>
        from t_ds_user
        where user_name=#{userName}
    </select>
    <select id="queryUserByNamePassword" resultType="org.apache.dolphinscheduler.dao.entity.User">
        select
        <include refid="baseSql"/>
        from t_ds_user
        where user_name=#{userName} and user_password = #{password}
    </select>
    <select id="queryUserPaging" resultType="org.apache.dolphinscheduler.dao.entity.User">
        select u.id,u.user_name,u.user_password,u.user_type,u.email,u.phone,u.tenant_id,u.create_time,
        u.update_time,t.tenant_code,u.state,
        case when u.queue  <![CDATA[ <> ]]> '' then u.queue else q.queue_name end as queue, q.queue_name
        from t_ds_user u
        left join t_ds_tenant t on u.tenant_id=t.id
        left join t_ds_queue q on t.queue_id = q.id
        where 1=1
        <if test="userName!=null and userName != ''">
            and u.user_name like concat ('%', #{userName}, '%')
        </if>
        order by u.update_time desc
    </select>
    <select id="queryDetailsById" resultType="org.apache.dolphinscheduler.dao.entity.User">
        select
        <include refid="baseSqlV2">
            <property name="alias" value="u"/>
        </include>
        ,t.tenant_code,
        case when u.queue <![CDATA[ <> ]]>  '' then u.queue else q.queue_name end as queue_name
        from t_ds_user u
        left join t_ds_tenant t on u.tenant_id=t.id
        left join t_ds_queue q on t.queue_id = q.id
        WHERE u.id = #{userId}
    </select>
    <select id="queryUserListByAlertGroupId" resultType="org.apache.dolphinscheduler.dao.entity.User">
        select
        <include refid="baseSqlV2">
            <property name="alias" value="u"/>
        </include>
        from t_ds_user u, t_ds_relation_user_alertgroup rel
        where u.id = rel.user_id AND rel.alertgroup_id = #{alertgroupId}
    </select>
    <select id="queryUserListByTenant" resultType="org.apache.dolphinscheduler.dao.entity.User">
        select
        <include refid="baseSql"/>
        from t_ds_user
        where tenant_id = #{tenantId}
    </select>
    <select id="queryTenantCodeByUserId" resultType="org.apache.dolphinscheduler.dao.entity.User">
        SELECT
        <include refid="baseSqlV2">
            <property name="alias" value="u"/>
        </include>
        , t.tenant_code
        FROM t_ds_user u, t_ds_tenant t
        WHERE u.tenant_id = t.id AND u.id = #{userId}
    </select>
    <select id="queryUserByToken" resultType="org.apache.dolphinscheduler.dao.entity.User">
        select
        <include refid="baseSqlV2">
            <property name="alias" value="u"/>
        </include>
        from t_ds_user u ,t_ds_access_token t
        where u.id = t.user_id and token=#{token} and t.expire_time > #{now}
    </select>
    <select id="queryUserListByQueue" resultType="org.apache.dolphinscheduler.dao.entity.User">
        select
        <include refid="baseSql"/>
        from t_ds_user
        where queue = #{queue}
    </select>
    <select id="existUser" resultType="java.lang.Boolean">
        select 1
        from t_ds_user
        where queue = #{queue} limit 1
    </select>
    <update id="updateUserQueue" parameterType="java.lang.String">
        update t_ds_user
        set queue = #{newQueue}
        where queue = #{oldQueue}
    </update>
    <select id="selectByIds" resultType="org.apache.dolphinscheduler.dao.entity.User">
        select *
        from t_ds_user
        <if test="ids != null and ids.size() != 0">
            where id in
            <foreach item="id" index="index" collection="ids" open="(" separator="," close=")">
                #{id}
            </foreach>
        </if>
    </select>
    <select id="queryAuthedUserListByProjectId" resultType="org.apache.dolphinscheduler.dao.entity.User">
        select
        <include refid="baseSqlV2">
            <property name="alias" value="u"/>
        </include>
        from t_ds_user u, t_ds_relation_project_user rel
        where u.id = rel.user_id and rel.project_id = #{projectId}
    </select>
    <select id="queryEnabledUsers" resultType="org.apache.dolphinscheduler.dao.entity.User">
        select
        <include refid="baseSql"/>
        from t_ds_user
        where state = 1
    </select>
</mapper>
