<?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.platform.dao.SysUserDao">
	<select id="queryObject" resultType="com.platform.entity.SysUserEntity">
		select * from sys_user where user_id = #{value}
	</select>

	<select id="getUserNumOfDept" resultType="java.lang.Long">
		select count(*) from sys_user where  dept_id = #{deptId} and status = 1
	</select>


	<select id="queryList" resultType="com.platform.entity.SysUserEntity">
		select u.*,sys_user_role.role_id, (select d.name from sys_dept d where d.dept_id = u.dept_id) deptName from sys_user u
		left join sys_user_role on u.user_id = sys_user_role.user_id
		<where>
			<if test="createUserId != null">
				and `create_user_id` = #{createUserId} 
			</if>
			<if test="username != null and username.trim() != ''">
				and u.`username` like concat('%',#{username},'%')
			</if>

			<if test="deptIdList != null and deptIdList.size() > 0">
				AND dept_id in
				<foreach item="id" collection="deptIdList" open="(" separator="," close=")">
					#{id}
				</foreach>
			</if>

		</where>
		<choose>
			<when test="sidx != null and sidx.trim() != ''">
				order by u.${sidx} ${order}
			</when>
			<otherwise>
				order by u.user_id desc
			</otherwise>
		</choose>
		<if test="offset != null and limit != null">
			limit #{offset}, #{limit}
		</if>
	</select>

	<select id="queryListByBean" resultType="com.platform.entity.UserWindowDto">
		select user.id,user.username name,user.dept_Id
		from sys_user user
		left join
		left join sys_role role on user.
		WHERE 1=1
		<if test="name != null and name.trim() != ''" >
			AND username LIKE concat('%',#{name},'%')
		</if>
	</select>

	<select id="getSysUserListByDeptIds" resultType="com.platform.entity.SysUserEntity">
		select * from sys_user WHERE status = 1 and dept_id in
		<foreach collection="deptList" open="(" separator="," close=")" item="deptId">
			#{deptId}
		</foreach>
	</select>

	<select id="queryTotal" resultType="int">
		select count(*) from sys_user user
		left join sys_user_role role on user.user_id = role.user_id
		<where>
			<if test="createUserId != null">
				and `create_user_id` = #{createUserId} 
			</if>
			<if test="username != null and username.trim() != ''">
				and `username` like concat('%',#{username},'%') 
			</if>
			<if test="status != null and status.trim() != ''">
				and `status` = #{status}
			</if>
			<if test="deptIdList != null and deptIdList.size() > 0">
				AND dept_id in
				<foreach item="id" collection="deptIdList" open="(" separator="," close=")">
					#{id}
				</foreach>
			</if>
			<if test="roleIdList != null and roleIdList.size() > 0">
				AND dept_id in
				<foreach item="id" collection="deptIdList" open="(" separator="," close=")">
					#{id}
				</foreach>
			</if>
		</where>
	</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="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="getBindList" resultType="com.platform.entity.SysUserEntity">
		select sysUser.*,
		 brand.can_show as can_show,
		 role.role_name as role_name,
		 role.role_id as role_id
		 from sys_user sysUser
           left join sys_user_brand brand on (sysUser.user_id = brand.user_id and brand.enable = 1 and brand.brand_id=#{brandId})
           left join sys_user_role userRole on userRole.user_id = sysUser.user_id
           left join sys_role role on role.role_id = userRole.role_id
		 ${ew.customSqlSegment}
	</select>

	<select id="getCompanyUserList" resultType="com.platform.entity.SysUserEntity">
		select sysUser.*,
		 role.role_name as role_name,
		 dept.name as dept_name
		 from sys_user sysUser
           left join sys_user_role userRole on userRole.user_id = sysUser.user_id
           left join sys_role role on role.role_id = userRole.role_id
           left join sys_dept dept on sysUser.dept_id = dept.dept_id
		 ${ew.customSqlSegment}
	</select>


	<select id="queryByUserName" resultType="com.platform.entity.SysUserEntity">
		select * from sys_user where username = #{username} and status = 1 limit 0 , 1
	</select>

	<select id="queryByUserMobile" resultType="com.platform.entity.SysUserEntity">
		select * from sys_user where mobile = #{mobile} and status = 1 limit 0 , 1
	</select>

	<select id="queryByMobileAndDeptId" resultType="com.platform.entity.SysUserEntity">
		select * from sys_user where mobile = #{mobile} and dept_id = #{deptId}
	</select>
	
	<insert id="save" parameterType="com.platform.entity.SysUserEntity" useGeneratedKeys="true" keyProperty="userId">
		insert into sys_user(`user_id`, `username`, `password`, `email`, `mobile`, `status`, `create_user_id`,
		`dept_id`, `create_time`, `is_platform_streamer`)
		values(#{userId}, #{username}, #{password},#{email}, #{mobile}, #{status}, #{createUserId},#{deptId},
			#{createTime},#{isPlatformStreamer})
	</insert>
	 
	<update id="update" parameterType="com.platform.entity.SysUserEntity">
		update sys_user 
		<set> 
			<if test="username != null">`username` = #{username}, </if>
			<if test="email != null">`email` = #{email}, </if>
			<if test="mobile != null">`mobile` = #{mobile}, </if>
			<if test="status != null">`status` = #{status}, </if>
			<if test="deptId != null">`dept_id` = #{deptId}, </if>
			<if test="isPlatformStreamer != null">`is_platform_streamer` = #{isPlatformStreamer}, </if>
		</set>
		where user_id = #{userId} 
		<if test="createUserId != null">
			and `create_user_id` = #{createUserId} 
		</if>
	</update>
	
	<update id="updatePassword" parameterType="map">
		update sys_user set fdkk_user = #{fdkkUser} ,fdkk_password = #{fdkkPassword} where user_id = #{userId}
	</update>
	
	<delete id="deleteBatch">
		delete from sys_user where user_id in 
		<foreach item="userId" collection="array" open="(" separator="," close=")">
			#{userId}
		</foreach>
		;
		delete from sys_user_role where user_id in 
		<foreach item="userId" collection="array" open="(" separator="," close=")">
			#{userId}
		</foreach>
	</delete>

</mapper>