<?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="cn.xluobo.business.sys.admin.repo.mapper.ISysUserMapper">

    <select id="selectUserList" resultType="com.ruoyi.common.core.domain.entity.SysUser">
        select user_id,username,name,u.phone,email_address,enable,locked,account_expired,pwd_expired,u.dept_id,d.dept_name,
               (select group_concat(t.tenant_name) from sys_tenant t,sys_user_tenant ut where ut.user_id=u.user_id and t.tenant_id=ut.tenant_id) as tenant_names
        from sys_user u left join sys_dept d on u.dept_id=d.dept_id
        where 1=1
        <if test="reqSearchSysUser.username != null and reqSearchSysUser.username != ''">
            and u.username like concat('%',#{reqSearchSysUser.username},'%')
        </if>
        <if test="reqSearchSysUser.name != null and reqSearchSysUser.name != ''">
            and u.name like concat('%',#{reqSearchSysUser.name},'%')
        </if>
        <if test="reqSearchSysUser.phone != null and reqSearchSysUser.phone != ''">
            and u.phone like concat('%',#{reqSearchSysUser.phone},'%')
        </if>
        <if test="reqSearchSysUser.deptId != null">
            and (u.dept_id=#{reqSearchSysUser.deptId} or u.dept_id in (select dept_id from sys_dept dd where find_in_set(#{reqSearchSysUser.deptId} ,dd.ancestors)))
        </if>
    </select>
    <select id="selectSuperTenantUserRoleCount" resultType="int">
        select count(1) from sys_user_role ur,sys_user u,sys_tenant t
        where u.user_id=#{userId} and ur.user_id=u.user_id
          and ur.tenant_id=t.tenant_id and t.super_tenant=1
    </select>
    <select id="selectUserHasAnyRoleCount" resultType="int">
        select count(1) from sys_user_role ur,sys_role r
        where ur.user_id=#{userId}
          and ur.tenant_id=#{tenantId}
          and ur.role_id=r.role_id
          <foreach collection="roleCodes" item="item" open="and r.role_code in (" close=")" separator=",">
            #{item}
          </foreach>
    </select>
</mapper>
