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

    <resultMap type="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="password2"     column="password2"     />
        <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="schoolId"    column="school_id"    />
		<result property="gradeId"    column="grade_id"    />
		<result property="classId"    column="class_id"    />
		<result property="cardId"    column="card_id"    />
		<result property="provinceName"    column="province_name"    />
		<result property="year"    column="year"    />
		<result property="isBind"    column="is_bind"    />
		<result property="currentRoleId"    column="current_role_id"  />
		<result property="groupId"    column="group_id"  />
		<result property="score"    column="score"  />
		<result property="scoreLock"    column="score_lock"  />
		<result property="customSchoolName"    column="custom_school_name"  />
		<result property="customClassName"    column="custom_class_name"  />
		<result property="gaokaoYear"    column="gaokaoYear"  />
		<result property="seatInput"    column="seat_input"  />
		<result property="examType"    column="exam_type"  />
		<result property="examMajor"    column="exam_major"  />
		<result property="examMajorName"    column="exam_major_name"  />
        <association property="dept"    column="dept_id" javaType="SysDept" resultMap="deptResult" />
        <collection  property="roles"   javaType="java.util.List"           resultMap="RoleResult" />
    </resultMap>

    <resultMap id="deptResult" type="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" />
    </resultMap>

    <resultMap id="RoleResult" type="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.custom_school_name,u.custom_class_name,u.gaokaoYear,u.seat_input,
        u.password, u.sex, u.status, u.del_flag, u.login_ip, u.login_date, u.create_by,u.score, u.group_id, u.is_bind ,u.score_lock,
        u.create_time, u.remark, u.school_id, u.grade_id, u.class_id, u.year, u.province_name, u.card_id,u.current_role_id,u.exam_type, u.exam_major, u.exam_major_name,
        d.dept_id, d.parent_id, d.ancestors, d.dept_name, d.order_num, d.leader, d.status as dept_status,
        r.role_id, r.role_name, r.role_key, r.role_sort, r.data_scope, r.status as role_status
        from sys_user u
		    left join sys_dept d on u.dept_id = d.dept_id
		    left join sys_user_role ur on u.user_id = ur.user_id
		    left join sys_role r on r.role_id = ur.role_id
    </sql>

    <select id="selectUserList" parameterType="SysUser" resultMap="SysUserResult">
		select u.user_id, u.dept_id, u.nick_name, u.user_name, u.email, u.avatar, u.phonenumber, u.is_bind,
		u.sex, u.status, u.del_flag, u.login_ip, u.login_date, u.create_by, u.create_time,u.score, u.group_id,
		u.score_lock,u.custom_school_name,u.custom_class_name, u.gaokaoYear,u.seat_input,
		u.remark,u.school_id, u.grade_id, u.class_id, u.year, u.province_name, u.card_id,u.current_role_id,u.exam_type, u.exam_major, u.exam_major_name,
		d.dept_name, d.leader from sys_user u
		left join sys_dept d on u.dept_id = d.dept_id
		where u.del_flag = '0'
		<if test="userId != null and userId != 0">
			AND u.user_id = #{userId}
		</if>
		<if test="userName != null and userName != ''">
			AND u.user_name like concat('%', #{userName}, '%')
		</if>
		<if test="nickName != null and nickName != ''">
			AND u.nick_name like concat('%', #{nickName}, '%')
		</if>
		<if test="status != null and status != ''">
			AND u.status = #{status}
		</if>
		<if test="isBind != null and isBind != ''">
			AND u.isBind = #{isBind}
		</if>
		<if test="phonenumber != null and phonenumber != ''">
			AND u.phonenumber like concat('%', #{phonenumber}, '%')
		</if>
		<if test="params.beginTime != null and params.beginTime != ''"><!-- 开始时间检索 -->
			AND date_format(u.create_time,'%y%m%d') &gt;= date_format(#{params.beginTime},'%y%m%d')
		</if>
		<if test="params.endTime != null and params.endTime != ''"><!-- 结束时间检索 -->
			AND date_format(u.create_time,'%y%m%d') &lt;= date_format(#{params.endTime},'%y%m%d')
		</if>
		<if test="deptId != null and deptId != 0">
			AND (u.dept_id = #{deptId} OR u.dept_id IN ( SELECT t.dept_id FROM sys_dept t WHERE find_in_set(#{deptId}, ancestors) ))
		</if>
		<!-- 数据范围过滤 -->
		${params.dataScope}
	</select>

	<select id="selectUsers" parameterType="map" resultMap="SysUserResult">
		select user_id, dept_id, nick_name, avatar, code, user_name, email,current_role_id,exam_type, exam_major, exam_major_name from sys_user
		<where>
			<if test="codes != null"> AND code IN <foreach collection="codes" item="v" open="(" separator="," close=")">#{v}</foreach> </if>
			<if test="userNames != null"> AND user_name IN <foreach collection="userNames" item="v" open="(" separator="," close=")">#{v}</foreach> </if>
			<if test="userIds != null">AND user_id IN <foreach collection="userIds" item="v" open="(" separator="," close=")">#{v}</foreach> </if>
		</where>
	</select>

	<select id="selectAllocatedList" parameterType="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,u.school_id, u.grade_id, u.class_id, u.year, u.province_name
	    from sys_user u
			 left join sys_dept d on u.dept_id = d.dept_id
			 left join sys_user_role ur on u.user_id = ur.user_id
			 left join sys_role r on r.role_id = ur.role_id
	    where u.del_flag = '0' and r.role_id = #{roleId}
	    <if test="userName != null and userName != ''">
			AND u.user_name like concat('%', #{userName}, '%')
		</if>
		<if test="phonenumber != null and phonenumber != ''">
			AND u.phonenumber like concat('%', #{phonenumber}, '%')
		</if>
		<!-- 数据范围过滤 -->
		${params.dataScope}
	</select>

	<select id="selectUnallocatedList" parameterType="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, u.school_id, u.grade_id, u.class_id, u.year
	    from sys_user u
			 left join sys_dept d on u.dept_id = d.dept_id
			 left join sys_user_role ur on u.user_id = ur.user_id
			 left join sys_role r on r.role_id = ur.role_id
	    where u.del_flag = '0' and (r.role_id != #{roleId} or r.role_id IS NULL)
	    and u.user_id not in (select u.user_id from sys_user u inner join sys_user_role ur on u.user_id = ur.user_id and ur.role_id = #{roleId})
	    <if test="userName != null and userName != ''">
			AND u.user_name like concat('%', #{userName}, '%')
		</if>
		<if test="phonenumber != null and phonenumber != ''">
			AND u.phonenumber like concat('%', #{phonenumber}, '%')
		</if>
		<!-- 数据范围过滤 -->
		${params.dataScope}
	</select>

	<select id="selectScoreLockUserList" parameterType="SysUser" resultMap="SysUserResult">
		select * from sys_user u
		where u.del_flag = '0'
		<!-- and u.is_bind =#{isBind}  -->
		<if test="scoreLock != null">
			and u.score_lock=#{scoreLock}
		</if>

		<if test="provinceNames != null"> and province_name in <foreach item="o" collection="provinceNames" open="(" separator="," close=")">#{o}</foreach></if>

	</select>

	<select id="selectUserCleartextPassword" parameterType="Long" resultType="String">
		SELECT password2 from sys_user where user_id = #{userId}
	</select>

	<select id="selectPassword2ByUserIds" parameterType="Long"  resultMap="SysUserResult">
		SELECT user_id,password2 from sys_user
		where 1=1 and user_id in
		<foreach collection="list"  item="userId"  open="(" separator="," close=")">
			#{userId}
		</foreach>
	</select>

	<select id="selectUserByUserNameOrPhone" resultMap="SysUserResult">
		<include refid="selectUserVo"/>
		where (u.user_name = #{userNameOrPhone} OR u.phonenumber = #{userNameOrPhone})
	</select>

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

	<select id="selectBatchUserByUserName" parameterType="String" resultMap="SysUserResult">
		<include refid="selectUserVo"/>
		where 1=1 and u.user_name in
		<foreach collection="list"  item="userName"  open="(" separator="," close=")">
			#{userName}
		</foreach>
	</select>

	<select id="selectBatchUserByCardId" parameterType="Long" resultMap="SysUserResult">
		<include refid="selectUserVo"/>
		where 1=1 and u.card_id in
		<foreach collection="list"  item="cardId"  open="(" separator="," close=")">
			#{cardId}
		</foreach>
	</select>

	<select id="selectUserByPhonenumber" resultMap="SysUserResult">
		select u.user_id, u.dept_id, u.user_name, u.nick_name, u.email, u.avatar,
		u.is_bind, u.phonenumber, u.password, u.sex, u.status, u.del_flag,u.score, u.group_id,u.score_lock,
		u.login_ip, u.login_date, u.create_by, u.create_time, u.remark,
		u.school_id, u.grade_id, u.class_id, u.year, u.province_name, u.card_id, u.current_role_id,u.exam_type, u.exam_major, u.exam_major_name,
		u.custom_school_name,u.custom_class_name,u.gaokaoYear,u.seat_input
		from sys_user u
		where (u.user_name = #{phonenumber} OR u.phonenumber = #{phonenumber})
	</select>

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

    <select id="selectUserByCardId" parameterType="Long" resultMap="SysUserResult">
        <include refid="selectUserVo"/>
        where u.card_id = #{cardId}
    </select>

	<select id="checkUserNameUnique" parameterType="String" resultMap="SysUserResult">
		select user_id, user_name from sys_user where user_name = #{userName} and del_flag = '0' limit 1
	</select>

	<select id="checkPhoneUnique" parameterType="String" resultMap="SysUserResult">
		select user_id, phonenumber from sys_user where phonenumber = #{phonenumber} and del_flag = '0' limit 1
	</select>

	<select id="checkEmailUnique" parameterType="String" resultMap="SysUserResult">
		select user_id, email from sys_user where email = #{email} and del_flag = '0' limit 1
	</select>

	<insert id="insertUser" parameterType="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="userType != null and userType != ''">user_type,</if>
			<if test="examType != null and examType != ''">exam_type,</if>
			<if test="examMajor != null and examMajor != ''">exam_major,</if>
			<if test="examMajorName != null and examMajorName != ''">exam_major_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="password2 != null and password2 != ''">password2,</if>
 			<if test="status != null and status != ''">status,</if>
 			<if test="createBy != null and createBy != ''">create_by,</if>
 			<if test="remark != null and remark != ''">remark,</if>
 			<if test="provinceName != null and provinceName != ''">province_name,</if>
 			<if test="delFlag != null and delFlag != ''">del_flag,</if>
 			<if test="customSchoolName != null and customSchoolName != ''">custom_school_name,</if>
		    <if test="customClassName != null and customClassName != ''">custom_class_name,</if>
			<if test="gaokaoYear != null">gaokaoYear,</if>
		    <if test="seatInput != null">seat_input,</if>
			<if test="schoolId != null">school_id,</if>
			<if test="gradeId != null">grade_id,</if>
			<if test="classId != null">class_id,</if>
			<if test="year != null">year,</if>
			<if test="cardId != null">card_id,</if>
			<if test="currentRoleId != null">current_role_id,</if>
			<if test="groupId != null">group_id,</if>
			<if test="score != null">score,</if>
			<if test="isBind != null">is_bind,</if>
			<if test="scoreLock != null">score_lock,</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="userType != null and userType != ''">#{userType},</if>
			<if test="examType != null and examType != ''">#{examType},</if>
			<if test="examMajor != null and examMajor != ''">#{examMajor},</if>
		    <if test="examMajorName != null and examMajorName != ''">#{examMajorName},</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="password2 != null and password2 != ''">#{password2},</if>
 			<if test="status != null and status != ''">#{status},</if>
 			<if test="createBy != null and createBy != ''">#{createBy},</if>
 			<if test="remark != null and remark != ''">#{remark},</if>
 			<if test="provinceName != null and provinceName != ''">#{provinceName},</if>
 			<if test="delFlag != null and delFlag != ''">#{delFlag},</if>
 			<if test="customSchoolName != null and customSchoolName != ''">#{customSchoolName},</if>
		    <if test="customClassName != null and customClassName != ''">#{customClassName},</if>
			<if test="gaokaoYear != null">#{gaokaoYear},</if>
		    <if test="seatInput != null">#{seatInput},</if>
			<if test="schoolId != null">#{schoolId},</if>
			<if test="gradeId != null">#{gradeId},</if>
			<if test="classId != null">#{classId},</if>
			<if test="year != null">#{year},</if>
			<if test="cardId != null">#{cardId},</if>
			<if test="currentRoleId != null">#{currentRoleId},</if>
			<if test="groupId != null">#{groupId},</if>
			<if test="score != null">#{score},</if>
			<if test="isBind != null">#{isBind},</if>
			<if test="scoreLock != null">#{scoreLock},</if>
			sysdate()
 		)
	</insert>

	<update id="updateUser" parameterType="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="userType != null and userType != ''">user_type = #{userType},</if>
			<if test="examType != null and examType != ''">exam_type = #{examType},</if>
			<if test="examMajor != null">exam_major = #{examMajor},</if>
			<if test="examMajorName != null">exam_major_name = #{examMajorName},</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="delFlag != null and delFlag != ''">del_flag = #{delFlag},</if>
 			<if test="avatar != null and avatar != ''">avatar = #{avatar},</if>
 			<if test="password != null and password != ''">password = #{password},</if>
 			<if test="password2 != null and password2 != ''">password2 = #{password2},</if>
 			<if test="status != null and status != ''">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 and remark != ''">remark = #{remark},</if>
 			<if test="provinceName != null and provinceName != ''">province_name = #{provinceName},</if>
 			<if test="customSchoolName != null">custom_school_name = #{customSchoolName},</if>
			<if test="customClassName != null">custom_class_name = #{customClassName},</if>
			<if test="gaokaoYear != null">gaokaoYear = #{gaokaoYear},</if>
			<if test="seatInput != null">seat_input = #{seatInput},</if>
			<if test="schoolId != null ">school_id = #{schoolId},</if>
			<if test="gradeId != null ">grade_id = #{gradeId},</if>
			<if test="classId != null ">class_id = #{classId},</if>
			<if test="year != null ">year = #{year},</if>
			<if test="cardId != null ">card_id = #{cardId},</if>
			<if test="currentRoleId != null ">current_role_id = #{currentRoleId},</if>
			<if test="groupId != null ">group_id = #{groupId},</if>
			<if test="score != null ">score = #{score},</if>
			<if test="isBind != null ">is_bind = #{isBind},</if>
			<if test="scoreLock != null ">score_lock = #{scoreLock},</if>
			<if test="updateTime != null ">update_time = #{updateTime},</if>
			update_time = sysdate()
 		</set>
 		where user_id = #{userId}
	</update>

	<insert id="batchUpdateUser" parameterType="java.util.List" useGeneratedKeys="true" keyProperty="userId">
		INSERT INTO sys_user(
		user_id, user_name, score_lock, update_time, is_bind, current_role_id
		) VALUES
		<foreach collection="list" item="item" index="index" separator=",">
			(#{item.userId}, #{item.userName}, #{item.scoreLock}, #{item.updateTime}, #{item.isBind}, #{item.currentRoleId})
		</foreach>
		ON DUPLICATE KEY UPDATE score_lock = VALUES(score_lock), user_name = VALUES(user_name),
		 update_time = VALUES(update_time),  is_bind = VALUES(is_bind),  current_role_id = VALUES(current_role_id)
	</insert>

	<update id="updateUserStatus" parameterType="SysUser">
 		update sys_user set status = #{status} where user_id = #{userId}
	</update>

	<update id="updateUserAvatar" parameterType="SysUser">
 		update sys_user set avatar = #{avatar} where user_name = #{userName}
	</update>

	<update id="resetUserPwd" parameterType="SysUser">
 		update sys_user set password = #{password} where user_name = #{userName}
	</update>

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

	<delete id="deleteUserPhysicalById" parameterType="Long">
		delete from sys_user where user_id = #{userId}
	</delete>

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

</mapper>
