<?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>
		</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.*,
			d.name as departmentName,
			c.status as corpInfoStatus
		from
			sys_user u
				left join oa_department d on d.department_id = u.department_id
				left join bus_corp_info c on c.corpinfo_id = u.corpinfo_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>


	<!-- 通过用户卡号获取数据(查询企业端用户) -->
	<select id="findByCardNo" parameterType="pd" resultType="pd">
		select
		u.USER_ID,
		u.USERNAME,
		u.PASSWORD,
		u.NAME,
		u.ROLE_ID,
		u.LAST_LOGIN,
		u.IP,
		u.STATUS,
		u.BZ,
		u.SKIN,
		u.EMAIL,
		u.NUMBER,
		u.PHONE,
		u.ROLE_IDS,
		u.DEPARTMENT_ID,
		u.CORPINFO_ID,
		u.FUN_IDS,
		u.APPID,
		u.POST_ID,
		u.ISMAIN,
		u.ERROR_COUNT,
		u.SORT,
		u.LEARNERCATEGORY,
		u.USERAVATARPREFIX,
		u.USERAVATARURL,
		u.SHIFTDUTYONE,
		u.SHIFTDUTYTWO,
		u.DURATION,
		u.WORKSTATUS,
		u.WORKPERIOD,
		u.IS_RECORDER,
		u.PERSONNEL_TYPE,
		u.IS_HAZARDCONFIRMER,
		u.IS_ONLINELEARNING,
		u.PERSON_TYPE,
		u.JCR,
		u.ISDELETE,
		u.PUSH_CID,
		u.EMPNO,
		u.CFD_STATUS,
		u.CARDNO,
		d.NAME as DEPARTMENT_NAME,
		p.NAME as POST_NAME
		from
		sys_user u
		left join  oa_department d on d.DEPARTMENT_ID = u.DEPARTMENT_ID
		left join  sys_post p on p.POST_ID = u.POST_ID
		where 1=1
		<if test=" CARDNO != null and CARDNO != ''">
			and u.CARDNO = #{CARDNO}
		</if>
		<if test=" empNo != null and empNo != ''">
			and u.empNo = #{empNo}
		</if>
	</select>

</mapper>
