<?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.zcloud.modules.sys.dao.SysUserDao">

	<!--分页查询-->
	<select id="listPage" resultType="com.zcloud.modules.sys.entity.SysUserEntity">
        SELECT
        u.*,
        d.name department_name,
        urs.roles_id,
        urs.roles_name,
        degree.name degree_name
        FROM sys_user u
        left join oa_department d on d.department_id = u.department_id
        left join sys_dictionaries degree on degree.dictionaries_id = u.degree
        left join(select ur.user_id,group_concat(r.role_name) roles_name, group_concat(r.role_id) roles_id,r.type
        from sys_user_role ur,sys_role r where r.role_id = ur.role_id group by ur.user_id) urs on urs.user_id =
        u.user_id
        <where>
            u.is_delete = 0 and u.user_id != '1' and urs.type = 1
            <if test="params.name != null and params.name != ''">
                AND u.name like concat('%',#{params.name},'%')
            </if>
            <if test="params.roleId != null and params.roleId != ''">
                AND urs.roles_id like concat('%',#{params.roleId},'%')
            </if>
            <if test="params.username != null and params.username != ''">
                AND u.username like concat('%',#{params.username},'%')
            </if>
            <if test="params.titleCode != null and params.titleCode != ''">
                AND u.title_code = #{params.titleCode}
            </if>
            <if test="params.professionalCode != null and params.professionalCode != ''">
                AND u.professional_code = #{params.professionalCode}
            </if>
        </where>
        ORDER BY create_time DESC
    </select>

	<select id="queryById" resultType="com.zcloud.modules.sys.entity.SysUserEntity">
		select
			u.*,
			urs.roles_id,
			urs.roles_name,
			degree.name degree_name
		from sys_user u
		left join sys_dictionaries degree on degree.dictionaries_id = u.degree
	    left join(select ur.user_id,group_concat(r.role_name) roles_name, group_concat(r.role_id) roles_id
		                        from sys_user_role ur,sys_role r where r.role_id = ur.role_id group by ur.user_id) urs on urs.user_id = u.user_id
		where u.user_id = #{userId}
	</select>

	<!-- 查询用户的所有权限 -->
	<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="String">
		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.zcloud.modules.sys.entity.SysUserEntity">
		select
			u.*,
			urs.roles_id,
			urs.roles_name
		from
			sys_user u
			left join(select ur.user_id,group_concat(r.role_name) roles_name, group_concat(r.role_id) roles_id
			from sys_user_role ur,sys_role r where r.role_id = ur.role_id group by ur.user_id) urs on urs.user_id = u.user_id
		where u.username = #{username} and u.is_delete = 0
	</select>

	 <select id="listAll" resultType="com.zcloud.modules.sys.entity.SysUserEntity">
		SELECT
		u.*,
		d.name department_name,
		p.name post_name,
		urs.roles_id,
		urs.roles_name,
		type.name type_name,
		degree.name degree_name
		FROM sys_user u
		left join oa_department d on d.department_id = u.department_id
		left join oa_post p on p.post_id = u.post_id
		left join sys_dictionaries type on type.dictionaries_id = u.type
		left join sys_dictionaries degree on degree.dictionaries_id = u.degree
		left join(select ur.user_id,group_concat(r.role_name) roles_name, group_concat(r.role_id) roles_id, r.type
		from sys_user_role ur,sys_role r where r.role_id = ur.role_id group by ur.user_id) urs on urs.user_id = u.user_id
		<where>
			and u.user_id != '1' and urs.type = 1
			<if test="idList != null">
				AND u.user_id in
				<foreach item="item" index="index" collection="idList" open="(" separator="," close=")">
					#{item}
				</foreach>
			</if>
			<if test="isDelete != null">
				AND u.is_delete = #{isDelete}
			</if>
			<if test="roleId != null and roleId != ''">
				AND urs.roles_id like concat('%',#{roleId},'%')
			</if>
			<if test="name != null and name != ''">
				AND u.name like concat('%',#{name},'%')
			</if>
			<if test="username != null and username != ''">
				AND u.username like concat('%',#{username},'%')
			</if>
			<if test="departmentId != null and departmentId != ''">
				AND u.department_id = #{departmentId}
			</if>
			<if test="postId != null and postId != ''">
				AND u.post_id = #{postId}
			</if>
			<if test="type != null and type != ''">
				AND u.type = #{type}
			</if>
		</where>
		ORDER BY create_time DESC
	</select>

</mapper>
