<?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="com.zxdmy.excite.system.mapper.SysUserMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.zxdmy.excite.system.entity.SysUser">
        <id column="id" property="id"/>
        <result column="username" property="username"/>
        <result column="avatar" property="avatar"/>
        <result column="sex" property="sex"/>
        <result column="login_name" property="loginName"/>
        <result column="phone" property="phone"/>
        <result column="password" property="password"/>
        <result column="remark" property="remark"/>
        <result column="status" property="status"/>
        <result column="login_ip" property="loginIp"/>
        <result column="login_date" property="loginDate"/>
        <result column="is_delete" property="isDelete"/>
        <result column="create_time" property="createTime"/>
        <result column="update_time" property="updateTime"/>
        <result column="delete_time" property="deleteTime"/>
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id
        , username, avatar, sex, login_name, phone, password, remark, status, login_ip, login_date, is_delete, create_time, update_time, delete_time
    </sql>

    <!-- 通过角色ID查询用户列表（即某个角色，分配的用户信息） -->
    <select id="selectUsersByRoleId" parameterType="int" resultType="com.zxdmy.excite.system.entity.SysUser">
        SELECT u.*
        FROM sys_user_role ur,
             sys_user u
        WHERE ur.role_id = #{roleId}
          AND ur.user_id = u.id
          AND u.is_delete = 0
          AND u.id != 10000
        ORDER BY u.id
    </select>

    <!-- 通过角色ID查询不拥有此角色的用户列表 -->
    <select id="selectUsersByRoleIdNotIn" parameterType="int" resultType="com.zxdmy.excite.system.entity.SysUser">
        SELECT u.*
        FROM sys_user u
                 LEFT JOIN sys_user_role ur ON u.id = ur.user_id
            AND ur.role_id = #{roleId}
        WHERE ur.user_id IS NULL
          AND u.id != 10000
          AND u.is_delete = 0
        ORDER BY u.id
    </select>

    <!-- 通过角色ID查询用户列表（即某个角色，分配的用户信息） -->
    <select id="selectUsersByCloudUploadRoleId" resultType="com.zxdmy.excite.system.entity.SysUser">
        SELECT u.*
        FROM cloud_role_user ur,
             sys_user u
        WHERE ur.role_id = #{roleId}
          AND ur.user_id = u.id
          AND u.is_delete = 0
          AND u.id != 10000
        ORDER BY u.id
    </select>

    <!-- 通过角色ID查询不拥有此角色的用户列表 -->
    <select id="selectUsersByCloudUploadRoleIdNotIn" resultType="com.zxdmy.excite.system.entity.SysUser">
        SELECT u.*
        FROM sys_user u
                 LEFT JOIN cloud_role_user ur ON u.id = ur.user_id
            AND ur.role_id = #{roleId}
        WHERE ur.user_id IS NULL
          AND u.is_delete = 0
          AND u.id != 10000
        ORDER BY u.id
    </select>


    <select id="queryByPage" resultType="com.zxdmy.excite.system.entity.SysUser" >
        SELECT u.* from sys_user u
        LEFT JOIN cloud_department_user du on du.user_id = u.id
        <trim prefix="WHERE" prefixOverrides="AND">
            u.delete_time is NULL  AND u.is_delete = 0 AND u.id != 10000
            <if test="departmentId != null and departmentId != ''">
                AND du.department_id = #{departmentId}
            </if>
            <if test="username != null and username != ''">
                AND u.username LIKE CONCAT('%' ,#{username}, '%')
            </if>
            <if test="phone != null and phone != ''">
                AND u.phone = #{phone}
            </if>
        </trim>
        ORDER BY create_time DESC
        LIMIT #{page},#{size};
    </select>

    <select id="queryByPageCount" resultType="java.lang.Integer">
        select count(*)
        from sys_user u
        LEFT JOIN cloud_department_user du on du.user_id = u.id
        <trim prefix="WHERE" prefixOverrides="AND">
            u.delete_time is NULL  AND u.is_delete = 0 AND u.id != '10000'
            <if test="departmentId != null and departmentId != ''">
                AND du.department_id = #{departmentId}
            </if>
            <if test="username != null and username != ''">
                AND u.username LIKE CONCAT('%' ,#{username}, '%')
            </if>
            <if test="phone != null and phone != ''">
                AND u.phone = #{phone}
            </if>
        </trim>
    </select>



    <select id="selectByIds" resultType="com.zxdmy.excite.system.entity.SysUser" >
        SELECT * FROM sys_user WHERE id IN
        <foreach collection='ids' item='id' open='(' separator=',' close=')'>
        #{id}
        </foreach>
    </select>

</mapper>
