<?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.fastbee.system.mapper.SysUserMapper">

	<resultMap type="com.fastbee.common.extend.core.domin.entity.SysUser" id="SysUserResult">
		<id     property="userId"       column="user_id"      />
		<result property="deptId"       column="dept_id"      />
		<result property="userName"     column="user_name"    />
		<result property="nickName"     column="nick_name"    />
		<result property="email"        column="email"        />
		<result property="phonenumber"  column="phonenumber"  />
		<result property="sex"          column="sex"          />
		<result property="avatar"       column="avatar"       />
		<result property="password"     column="password"     />
		<result property="status"       column="status"       />
		<result property="delFlag"      column="del_flag"     />
		<result property="loginIp"      column="login_ip"     />
		<result property="loginDate"    column="login_date"   />
		<result property="createBy"     column="create_by"    />
		<result property="createTime"   column="create_time"  />
		<result property="updateBy"     column="update_by"    />
		<result property="updateTime"   column="update_time"  />
		<result property="remark"       column="remark"       />
		<result property="language"     column="language"/>
		<result property="timeZone"     column="time_zone"/>
		<association property="dept"    column="dept_id"  javaType="com.fastbee.common.extend.core.domin.entity.SysDept" resultMap="deptResult" />
		<collection  property="roles"   javaType="java.util.List"           resultMap="RoleResult" />
	</resultMap>

	<resultMap id="deptResult" type="com.fastbee.common.extend.core.domin.entity.SysDept">
		<id     property="deptId"    column="dept_id"     />
		<result property="parentId"  column="parent_id"   />
		<result property="deptName"  column="dept_name"   />
		<result property="ancestors" column="ancestors"   />
		<result property="orderNum"  column="order_num"   />
		<result property="leader"    column="leader"      />
		<result property="status"    column="dept_status" />
		<result property="deptUserId" column="dept_user_id"/>
		<result property="deptUserName" column="dept_user_name"/>
	</resultMap>

	<resultMap id="RoleResult" type="com.fastbee.common.extend.core.domin.entity.SysRole">
		<id     property="roleId"       column="role_id"        />
		<result property="roleName"     column="role_name"      />
		<result property="roleKey"      column="role_key"       />
		<result property="roleSort"     column="role_sort"      />
		<result property="dataScope"     column="data_scope"    />
		<result property="status"       column="role_status"    />
	</resultMap>

	<sql id="selectUserVo">
		select u.user_id, u.dept_id, u.user_name, u.nick_name, u.email, u.avatar, u.phonenumber, u.password, u.sex, u.status, u.del_flag, u.login_ip, u.login_date, u.create_by, u.create_time, u.remark,
			   d.dept_id, d.parent_id, d.ancestors, d.dept_name, d.order_num, d.leader, d.status as dept_status, d.dept_user_id,
			   r.role_id, r.role_name, r.role_key, r.role_sort, r.data_scope, r.status as role_status,
			   (select su.user_name from sys_user su where su.user_id = d.dept_user_id) as dept_user_name,
			   ap.language,ap.time_zone
		from sys_user u
				 left join sys_dept d on u.dept_id = d.dept_id
				 left join sys_user_role sur on u.user_id = sur.user_id
				 left join sys_role r on r.role_id = sur.role_id
				 left join app_preferences ap on u.user_id = ap.user_id
	</sql>

	<select id="selectAllocatedList" parameterType="com.fastbee.common.extend.core.domin.entity.SysUser" resultMap="SysUserResult">
		select distinct u.user_id, u.dept_id, u.user_name, u.nick_name, u.email, u.phonenumber, u.status, u.create_time
		from sys_user u
		left join sys_dept d on u.dept_id = d.dept_id
		left join sys_user_role sur on u.user_id = sur.user_id
		left join sys_role r on r.role_id = sur.role_id
		where u.del_flag = 0 and r.role_id = #{user.roleId}
		<if test="user.userName != null and user.userName != ''">
			AND u.user_name like concat('%', #{user.userName}, '%')
		</if>
		<if test="user.phonenumber != null and user.phonenumber != ''">
			AND u.phonenumber like concat('%', #{user.phonenumber}, '%')
		</if>
		<!-- 数据范围过滤 -->
		${user.params.dataScope}
	</select>

	<select id="selectUnallocatedList" parameterType="com.fastbee.common.extend.core.domin.entity.SysUser" resultMap="SysUserResult">
		select distinct u.user_id, u.dept_id, u.user_name, u.nick_name, u.email, u.phonenumber, u.status, u.create_time
		from sys_user u
		left join sys_dept d on u.dept_id = d.dept_id
		left join sys_user_role sur on u.user_id = sur.user_id
		left join sys_role r on r.role_id = sur.role_id
		where u.del_flag = 0 and (r.role_id != #{user.roleId} or r.role_id IS NULL)
		and u.dept_id = #{user.deptId}
		and u.user_id not in (select u.user_id from sys_user u inner join sys_user_role sur on u.user_id = sur.user_id and sur.role_id = #{user.roleId})
		<if test="user.userName != null and user.userName != ''">
			AND u.user_name like concat('%', #{user.userName}, '%')
		</if>
		<if test="user.phonenumber != null and user.phonenumber != ''">
			AND u.phonenumber like concat('%', #{user.phonenumber}, '%')
		</if>
		<!-- 数据范围过滤 -->
		${user.params.dataScope}
	</select>

	<select id="selectUserByUserName" parameterType="String" resultMap="SysUserResult">
		<include refid="selectUserVo"/>
		where u.user_name = #{userName} and u.del_flag = 0
	</select>

	<select id="selectUserById" parameterType="Long" resultMap="SysUserResult">
		<include refid="selectUserVo"/>
		where u.user_id = #{userId} and u.del_flag = 0
	</select>

	<select id="selectUserByPhoneNumber" parameterType="String" resultMap="SysUserResult">
		<include refid="selectUserVo"/>
		where u.phonenumber = #{phoneNumber} and u.del_flag = 0
	</select>

    <insert id="insertUser" parameterType="com.fastbee.common.extend.core.domin.entity.SysUser" useGeneratedKeys="true" keyProperty="userId">
 		insert into sys_user(
 			<if test="userId != null and userId != 0">user_id,</if>
 			<if test="deptId != null and deptId != 0">dept_id,</if>
 			<if test="userName != null and userName != ''">user_name,</if>
 			<if test="nickName != null and nickName != ''">nick_name,</if>
 			<if test="email != null and email != ''">email,</if>
 			<if test="avatar != null and avatar != ''">avatar,</if>
 			<if test="phonenumber != null and phonenumber != ''">phonenumber,</if>
 			<if test="sex != null and sex != ''">sex,</if>
 			<if test="password != null and password != ''">password,</if>
 			<if test="status != null">status,</if>
 			<if test="createBy != null and createBy != ''">create_by,</if>
 			<if test="remark != null and remark != ''">remark,</if>
 			create_time
 		)values(
 			<if test="userId != null and userId != ''">#{userId},</if>
 			<if test="deptId != null and deptId != ''">#{deptId},</if>
 			<if test="userName != null and userName != ''">#{userName},</if>
 			<if test="nickName != null and nickName != ''">#{nickName},</if>
 			<if test="email != null and email != ''">#{email},</if>
 			<if test="avatar != null and avatar != ''">#{avatar},</if>
 			<if test="phonenumber != null and phonenumber != ''">#{phonenumber},</if>
 			<if test="sex != null and sex != ''">#{sex},</if>
 			<if test="password != null and password != ''">#{password},</if>
 			<if test="status != null">#{status},</if>
 			<if test="createBy != null and createBy != ''">#{createBy},</if>
 			<if test="remark != null and remark != ''">#{remark},</if>
			CURRENT_TIMESTAMP
 		)
	</insert>

	<update id="updateUser" parameterType="com.fastbee.common.extend.core.domin.entity.SysUser">
 		update sys_user
 		<set>
 			<if test="deptId != null and deptId != 0">dept_id = #{deptId},</if>
 			<if test="userName != null and userName != ''">user_name = #{userName},</if>
 			<if test="nickName != null and nickName != ''">nick_name = #{nickName},</if>
 			<if test="email != null ">email = #{email},</if>
 			<if test="phonenumber != null ">phonenumber = #{phonenumber},</if>
 			<if test="sex != null and sex != ''">sex = #{sex},</if>
 			<if test="avatar != null and avatar != ''">avatar = #{avatar},</if>
 			<if test="password != null and password != ''">password = #{password},</if>
 			<if test="status != null">status = #{status},</if>
 			<if test="loginIp != null and loginIp != ''">login_ip = #{loginIp},</if>
 			<if test="loginDate != null">login_date = #{loginDate},</if>
 			<if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
 			<if test="remark != null">remark = #{remark},</if>
 			update_time = CURRENT_TIMESTAMP
 		</set>
 		where user_id = #{userId}
	</update>

	<update id="updateUserStatus" parameterType="com.fastbee.common.extend.core.domin.entity.SysUser">
		update sys_user set status = #{status} where user_id = #{userId}
	</update>

	<update id="updateUserAvatar" parameterType="com.fastbee.common.extend.core.domin.entity.SysUser">
		update sys_user set avatar = #{avatar} where user_name = #{userName}
	</update>

	<update id="resetUserPwd" parameterType="com.fastbee.common.extend.core.domin.entity.SysUser">
		update sys_user set password = #{password} where user_name = #{userName}
	</update>

	<delete id="deleteUserById" parameterType="Long">
 		update sys_user set del_flag = 1 where user_id = #{userId}
 	</delete>

 	<delete id="deleteUserByIds" parameterType="Long">
 		update sys_user set del_flag = 1 where user_id in
 		<foreach collection="array" item="userId" open="(" separator="," close=")">
 			#{userId}
        </foreach>
 	</delete>

	<delete id="deleteBySysUserIdAndSourceClient">
		update iot_social_user
		set del_flag = 1,
		update_time = now()
		where sys_user_id = #{sysUserId}
		and source_client in
		<foreach item="sourceClient" collection="sourceClientList" open="(" separator="," close=")">
			#{sourceClient}
		</foreach>
	</delete>

	<delete id="deleteBySysUserIdsAndSourceClient">
		update iot_social_user
		set del_flag = 1,
		update_time = now()
		where sys_user_id in
		<foreach item="sysUserId" collection="sysUserIds" open="(" separator="," close=")">
			#{sysUserId}
		</foreach>
		and source_client in
		<foreach item="sourceClient" collection="sourceClientList" open="(" separator="," close=")">
			#{sourceClient}
		</foreach>
	</delete>

	<delete id="deleteUserByDeptID">
		update sys_user set del_flag = 1
		where dept_id = #{deptId}
	</delete>

    <delete id="deleteDeviceUserShareByUserIds">
		update iot_device_share set del_flag = '1'
		where device_id in
		    (select device_id
		     from iot_device_user
		     where user_id in
				<foreach collection="array" item="userId" open="(" separator="," close=")">
					#{userId}
				</foreach>
			)
	</delete>

	<delete id="deleteDeviceUserByUserIds">
		update iot_device_user set del_flag = '1'
		where user_id in
			<foreach collection="array" item="userId" open="(" separator="," close=")">
				#{userId}
			</foreach>
	</delete>

	<delete id="deleteDeviceShareByUserIds">
		update iot_device_share set del_flag = '1'
		where user_id in
			<foreach collection="array" item="userId" open="(" separator="," close=")">
				#{userId}
			</foreach>
	</delete>


	<select id="getDeptUserByUserId" parameterType="Long" resultMap="SysUserResult">
		SELECT
			u.user_id,
			u.dept_id,
			u.nick_name,
			u.user_name
		FROM
			sys_user u
		WHERE
			u.del_flag = '0' and
				u.user_id = (
				SELECT
					d.dept_user_id
				FROM
					sys_dept d
						INNER JOIN sys_user su ON d.dept_id = su.dept_id
				WHERE
					su.user_id = #{userId,jdbcType=BIGINT})
    </select>

</mapper>
