<?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.ruoyi.system.mapper.SysUserMapper">

	<resultMap id="SysUserResult" type="com.ruoyi.common.web.domain.entity.SysUser">
		<id     property="userId"       column="user_id"      />
		<result property="deptId"       column="dept_id"      />
		<result property="userName"     column="user_name"    />
		<result property="nickName"     column="nick_name"    />
		<result property="email"        column="email"        />
		<result property="phonenumber"  column="phonenumber"  />
		<result property="sex"          column="sex"          />
		<result property="avatar"       column="avatar"       />
		<result property="password"     column="password"     />
		<result property="status"       column="status"       />
		<result property="loginIp"      column="login_ip"     />
		<result property="loginDate"    column="login_date"   />
		<result property="createBy"     column="create_by"    />
		<result property="createTime"   column="create_time"  />
		<result property="updateBy"     column="update_by"    />
		<result property="updateTime"   column="update_time"  />
		<result property="delFlag"      column="del_flag"     />
		<result property="remark"       column="remark"       />
		<association property="dept" javaType="com.ruoyi.common.web.domain.entity.SysDept" resultMap="deptResult" />
		<collection  property="roles"   javaType="java.util.List"  resultMap="RoleResult" />
	</resultMap>

	<resultMap id="deptResult" type="com.ruoyi.common.web.domain.entity.SysDept">
		<id     property="deptId"    column="dept_id"     />
		<result property="parentId"  column="parent_id"   />
		<result property="deptName"  column="dept_name"   />
		<result property="ancestors" column="ancestors"   />
		<result property="orderNum"  column="order_num"   />
		<result property="leader"    column="leader"      />
		<result property="status"    column="dept_status" />
	</resultMap>

	<resultMap id="RoleResult" type="com.ruoyi.common.web.domain.entity.SysRole">
		<id     property="roleId"       column="role_id"        />
		<result property="roleName"     column="role_name"      />
		<result property="roleKey"      column="role_key"       />
		<result property="roleSort"     column="role_sort"      />
		<result property="dataScope"    column="data_scope"     />
		<result property="status"       column="role_status"    />
	</resultMap>

	<sql id="selectUserVo">
        SELECT u.user_id, u.dept_id, u.user_name, u.nick_name, u.email, u.avatar, u.phonenumber, u.password, u.sex, u.status, u.del_flag, u.login_ip, u.login_date, u.create_by, u.create_time, u.remark,
        d.dept_id, d.parent_id, d.ancestors, d.dept_name, d.order_num, d.leader, d.status AS dept_status,
        r.role_id, r.role_name, r.role_key, r.role_sort, r.data_scope, r.status AS role_status
        FROM sys_user u
		LEFT JOIN sys_dept d ON u.dept_id = d.dept_id
		LEFT JOIN sys_user_role ur ON u.user_id = ur.user_id
		LEFT JOIN sys_role r ON r.role_id = ur.role_id
    </sql>

	<select id="selectSysUserByUserName" resultMap="SysUserResult">
		<include refid="selectUserVo"/>
		WHERE u.user_name = #{userName}
		AND u.del_flag = #{delFlag}
	</select>

	<select id="selectUserById" parameterType="java.lang.Long" resultMap="SysUserResult">
		<include refid="selectUserVo"/>
		WHERE u.user_id = #{userId}
	</select>

	<select id="selectUserList" resultMap="SysUserResult">
		SELECT u.user_id, u.dept_id, u.user_name, u.nick_name, u.email, u.avatar, u.phonenumber, u.sex, u.status, u.del_flag, u.login_ip, u.login_date, u.create_by, u.create_time, u.remark, d.dept_name, d.leader
		FROM sys_user u
		LEFT JOIN sys_dept d ON u.dept_id = d.dept_id
		WHERE u.del_flag = #{delFlag}
		<if test="sysUser.userId != null and sysUser.userId != 0">
			AND u.user_id = #{sysUser.userId}
		</if>
		<if test="sysUser.userName != null and sysUser.userName != ''">
			AND u.user_name LIKE concat('%', #{sysUser.userName}, '%')
		</if>
		<if test="sysUser.status != null and sysUser.status != ''">
			AND u.status = #{sysUser.status}
		</if>
		<if test="sysUser.phonenumber != null and sysUser.phonenumber != ''">
			AND u.phonenumber LIKE concat('%', #{sysUser.phonenumber}, '%')
		</if>
		<if test="sysUser.deptId != null and sysUser.deptId != 0">
			AND (u.dept_id = #{sysUser.deptId} OR u.dept_id IN ( SELECT t.dept_id FROM sys_dept t WHERE find_in_set(#{sysUser.deptId}, ancestors) ))
		</if>
		<!-- 数据权限范围过滤 -->
		${sysUser.dataScopeSql}
	</select>

	<select id="selectAllocatedList" resultMap="SysUserResult">
		SELECT DISTINCT u.user_id, u.dept_id, u.user_name, u.nick_name, u.email, u.phonenumber, u.status, u.create_time
		FROM sys_user u
		LEFT JOIN sys_dept d ON u.dept_id = d.dept_id
		LEFT JOIN sys_user_role ur ON u.user_id = ur.user_id
		LEFT JOIN sys_role r ON r.role_id = ur.role_id
		WHERE u.del_flag = #{delFlag}
		AND r.role_id = #{sysUser.roleId}
		<if test="sysUser.userName != null and sysUser.userName != ''">
			AND u.user_name LIKE concat('%', #{sysUser.userName}, '%')
		</if>
		<if test="sysUser.phonenumber != null and sysUser.phonenumber != ''">
			AND u.phonenumber LIKE concat('%', #{sysUser.phonenumber}, '%')
		</if>
		<!-- 数据权限范围过滤 -->
		${sysUser.dataScopeSql}
	</select>

	<select id="selectUnallocatedList" resultMap="SysUserResult">
		SELECT DISTINCT u.user_id, u.dept_id, u.user_name, u.nick_name, u.email, u.phonenumber, u.status, u.create_time
		FROM sys_user u
		LEFT JOIN sys_dept d ON u.dept_id = d.dept_id
		LEFT JOIN sys_user_role ur ON u.user_id = ur.user_id
		LEFT JOIN sys_role r ON r.role_id = ur.role_id
		WHERE u.del_flag = #{delFlag}
		AND (r.role_id != #{sysUser.roleId} OR r.role_id IS NULL)
		AND u.user_id NOT IN
			(
				SELECT u.user_id
				FROM sys_user u INNER JOIN sys_user_role ur ON u.user_id = ur.user_id
				AND ur.role_id = #{sysUser.roleId}
			)
		<if test="sysUser.userName != null and sysUser.userName != ''">
			AND u.user_name LIKE concat('%', #{sysUser.userName}, '%')
		</if>
		<if test="sysUser.phonenumber != null and sysUser.phonenumber != ''">
			AND u.phonenumber LIKE concat('%', #{sysUser.phonenumber}, '%')
		</if>
		<!-- 数据权限范围过滤 -->
		${sysUser.dataScopeSql}
	</select>

</mapper>