<?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 type="com.ruoyi.common.core.domain.saas.vo.SysUserVO" id="SysUserResult">
		<id     property="id"        column="user_id"         />
		<result property="deptId"        column="dept_id"         />
		<result property="loginName"     column="login_name"      />
		<result property="userName"      column="user_name"       />
		<result property="userType"      column="user_type"       />
		<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="salt"          column="salt"            />
		<result property="status"        column="status"          />
		<result property="delFlag"       column="del_flag"        />
		<result property="loginIp"       column="login_ip"        />
		<result property="loginDate"     column="login_date"      />
		<result property="pwdUpdateDate" column="pwd_update_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="remark"        column="remark"          />
		<result property="tenantId"        column="tenant_id"          />
		<association property="dept"     column="dept_id" javaType="com.ruoyi.common.core.domain.saas.vo.SysDeptVO" resultMap="deptResult" />
		<collection  property="roles"   javaType="java.util.List"        resultMap="RoleResult" />
	</resultMap>

	<resultMap id="deptResult" type="com.ruoyi.common.core.domain.saas.vo.SysDeptVO">
		<id     property="id"   column="dept_id"     />
		<result property="parentId" column="parent_id"   />
		<result property="deptName" column="dept_name"   />
		<result property="orderNum" column="order_num"   />
		<result property="leader"   column="leader"      />
		<result property="status"   column="dept_status" />
		<result property="tenantId"        column="tenant_id"          />
	</resultMap>

	<resultMap id="RoleResult" type="com.ruoyi.common.core.domain.saas.vo.SysRoleVO">
		<id     property="id"       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"    />
		<result property="tenantId"        column="tenant_id"          />
	</resultMap>

	<sql id="selectUserVo">
		select  u.id AS user_id, u.dept_id, u.login_name, u.user_name, u.user_type, u.email, u.avatar, u.phonenumber,
		       u.sex, u.password, u.salt, u.status, u.del_flag, u.login_ip, u.login_date, u.pwd_update_date,
		       u.create_time, u.remark,u.tenant_id,
				d.id AS dept_id, d.parent_id, d.dept_name, d.order_num, d.leader, d.status as dept_status,
				r.id AS role_id, r.role_name, r.role_key, r.role_sort, r.data_scope, r.status as role_status
		from trs_sys_user u
				 left join sys_dept d on u.dept_id = d.id
				 left join sys_user_role ur on u.id = ur.user_id
				 left join trs_sys_role r on r.id = ur.role_id
	</sql>

	<select id="selectUserList" parameterType="com.ruoyi.common.core.domain.saas.dto.SysUserDTO" resultMap="SysUserResult">
		select u.id AS user_id, u.dept_id, u.login_name, u.user_name, u.user_type, u.email, u.avatar, u.phonenumber,
		u.password, u.sex, u.salt, 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 trs_sys_user u
		left join sys_dept d on u.dept_id = d.id
		where u.del_flag = '0'
		<if test="loginName != null and loginName != ''">
			AND u.login_name like concat('%', #{loginName}, '%')
		</if>
		<if test="status != null and status != ''">
			AND u.status = #{status}
		</if>
		<if test="phonenumber != null and phonenumber != ''">
			AND u.phonenumber like concat('%', #{phonenumber}, '%')
		</if>
		<if test="params.beginTime != null and params.beginTime != ''"><!-- 开始时间检索 -->
			AND date_format(u.create_time,'%y%m%d') &gt;= date_format(#{params.beginTime},'%y%m%d')
		</if>
		<if test="params.endTime != null and params.endTime != ''"><!-- 结束时间检索 -->
			AND date_format(u.create_time,'%y%m%d') &lt;= date_format(#{params.endTime},'%y%m%d')
		</if>
		<if test="deptId != null and deptId != ''">
			AND (u.dept_id = #{deptId} OR u.dept_id = ANY (
			SELECT t.id
			FROM sys_dept t
			WHERE t.ancestors LIKE '%' || #{deptId} || '%'
			OR t.ancestors LIKE #{deptId} || '%'
			OR t.ancestors LIKE '%' || #{deptId}
			OR t.ancestors = #{deptId}
			))
		</if>
		<!-- 数据范围过滤 -->
		${params.dataScope}
	</select>

	<select id="selectAllocatedList" parameterType="com.ruoyi.common.core.domain.saas.dto.SysUserDTO" resultMap="SysUserResult">
		select distinct u.id AS user_id, u.dept_id, u.login_name, u.user_name, u.user_type, u.email, u.avatar,
		                u.phonenumber, u.status, u.create_time
		from trs_sys_user u
		left join sys_dept d on u.dept_id = d.id
		left join sys_user_role ur on u.id = ur.user_id
		left join trs_sys_role r on r.id = ur.role_id
		where u.del_flag = '0' and r.id = #{roleId}
		<if test="loginName != null and loginName != ''">
			AND u.login_name like concat('%', #{loginName}, '%')
		</if>
		<if test="phonenumber != null and phonenumber != ''">
			AND u.phonenumber like concat('%', #{phonenumber}, '%')
		</if>
		<!-- 数据范围过滤 -->
		${params.dataScope}
	</select>

	<select id="selectUnallocatedList" parameterType="com.ruoyi.common.core.domain.saas.dto.SysUserDTO" resultMap="SysUserResult">
		select distinct u.id AS user_id, u.dept_id, u.login_name, u.user_name, u.user_type, u.email, u.avatar, u.phonenumber,
		                u.status, u.create_time
		from trs_sys_user u
		left join sys_dept d on u.dept_id = d.id
		left join sys_user_role ur on u.id = ur.user_id
		left join trs_sys_role r on r.id = ur.role_id
		where u.del_flag = '0' and (r.id != #{roleId} or r.id IS NULL)
		and u.id not in (select u.id from trs_sys_user u inner join sys_user_role ur on u.id = ur.user_id and ur.role_id = #{roleId})
		<if test="loginName != null and loginName != ''">
			AND u.login_name like concat('%', #{loginName}, '%')
		</if>
		<if test="phonenumber != null and phonenumber != ''">
			AND u.phonenumber like concat('%', #{phonenumber}, '%')
		</if>
		<!-- 数据范围过滤 -->
		${params.dataScope}
	</select>

	<select id="selectUserByLoginNameAndTenantId" parameterType="String" resultMap="SysUserResult">
		<include refid="selectUserVo"/>
		where u.login_name = #{userName}
		and u.tenant_id = #{tenantId}
	</select>

	<select id="selectUserById" parameterType="String" resultMap="SysUserResult">
		<include refid="selectUserVo"/>
		where u.id = #{userId}
	</select>

</mapper>