<?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.lz.modules.sys.dao.SysUserDao">
    <!-- 查询用户的所有权限 -->
    <select id="queryAllPerms" resultType="string">
		select m.perms from sys_user_role ur
			LEFT JOIN sys_role_menu rm on ur.role_id = rm.role_id
			LEFT JOIN sys_menu m on rm.menu_id = m.menu_id
		where ur.user_id = #{userId}
	</select>

    <!-- 查询用户的所有菜单ID -->
    <select id="queryAllMenuId" resultType="long">
		select distinct rm.menu_id from sys_user_role ur
			LEFT JOIN sys_role_menu rm on ur.role_id = rm.role_id
		where ur.user_id = #{userId}
	</select>

    <select id="queryByUserName" resultType="com.lz.modules.sys.entity.SysUserEntity">
		select * from sys_user where username = #{username} or mobile = #{username} limit 1
	</select>

    <select id="getUserInfos" resultType="com.lz.modules.app.dto.UserDto">
		select su.*,GROUP_CONCAT(sr.role_name) role_name from sys_user su left join sys_user_role sur on su.user_id=sur.user_id left join sys_role sr on
		sur.role_id=sr.role_id where 1=1
        <if test="realName != null and realName != ''">
            and su.real_name like concat('%',#{realName},'%')
        </if>
        <if test="createUserId != null and createUserId != ''">
            and su.create_user_id =#{createUserId}
        </if>
        <if test="mobile != null and mobile != ''">
            and su.mobile = #{mobile}
        </if>
        <if test="roleName != null and roleName != ''">
            and sr.role_name like concat('%',#{roleName},'%')
        </if>
        GROUP BY su.user_id
        limit #{startIndex}, #{pageLimit}
    </select>

    <select id="getTotalCount" resultType="java.lang.Integer">
		select count(DISTINCT(su.user_id)) from sys_user su left join sys_user_role sur on su.user_id=sur.user_id left join sys_role sr on
		sur.role_id=sr.role_id where 1=1
        <if test="realName != null and realName != ''">
            and su.real_name like concat('%',#{realName},'%')
        </if>
        <if test="createUserId != null and createUserId != ''">
            and su.create_user_id =#{createUserId}
        </if>
        <if test="mobile != null and mobile != ''">
            and su.mobile = #{mobile}
        </if>
        <if test="roleName != null and roleName != ''">
            and sr.role_name like concat('%',#{roleName},'%')
        </if>
    </select>



    <select id="queryMenuIdListByRoleId" resultType="java.lang.Long">
        select distinct rm.menu_id from sys_user_role ur
			LEFT JOIN sys_role_menu rm on ur.role_id = rm.role_id
		where ur.role_id = #{roleId}
    </select>



    <select id="queryMenuIdListByRoleIds" resultType="java.lang.Long">
        select distinct rm.menu_id from sys_user_role ur
        LEFT JOIN sys_role_menu rm on ur.role_id = rm.role_id
        <if test="roleIds !=null and roleIds.size()>0">
            where ur.role_id in
            <foreach collection="roleIds" item="item" index="index" separator="," open="(" close=")">
                #{item}
            </foreach>
        </if>
    </select>


</mapper>
