<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="io.metersphere.sdk.mapper.BaseUserMapper">
    <select id="selectById" resultType="io.metersphere.sdk.dto.UserDTO">
        SELECT *
        FROM user
                 LEFT JOIN user_extend ON user.id = user_extend.id
        WHERE user.id = #{id}
    </select>

    <select id="selectByEmail" resultType="io.metersphere.sdk.dto.UserDTO">
        SELECT *
        FROM user
                 LEFT JOIN user_extend ON user.id = user_extend.id
        WHERE user.email = #{email}
    </select>

    <select id="findAll" resultType="io.metersphere.system.domain.User">
        SELECT *
        FROM user
    </select>

    <insert id="batchSave">
        INSERT INTO user(id, name, email, password, status, create_time, update_time, language, last_organization_id,
                         phone,
                         source, last_project_id, create_user)
        VALUES
        <foreach collection="users" item="user" separator=",">
            (#{user.id}, #{user.name}, #{user.email}, #{user.password}, #{user.status}, #{user.createTime},
             #{user.updateTime}, #{user.language},
             #{user.lastOrganizationId}, #{user.phone}, #{user.source}, #{user.lastProjectId}, #{user.createUser})
        </foreach>
    </insert>

    <select id="isSuperUser" resultType="java.lang.Boolean">
        SELECT COUNT(*)
        FROM user_role_relation
        WHERE user_id = #{userId}
          AND role_id = 'admin'
    </select>
    <select id="selectEmailInDB" resultType="java.lang.String">
        SELECT u.email FROM user u
        WHERE u.email = #{email}
        <if test="id != null">
            AND u.id != #{id}
        </if>
    </select>
    <select id="selectUserIdByEmailList" resultType="io.metersphere.system.domain.User">
        SELECT u.id,u.email FROM user u
        WHERE u.email IN
        <foreach collection="emailList" item="email" open="(" separator="," close=")">
            #{email}
        </foreach>
    </select>
    <select id="selectByKeyword" resultType="io.metersphere.system.domain.User">
        SELECT *
        FROM user
        <if test="keyword != null and keyword != ''">
            WHERE name LIKE CONCAT('%', #{keyword}, '%')
            OR email LIKE CONCAT('%', #{keyword}, '%')
        </if>
    </select>
</mapper>