<?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="location"      column="location"       />
		<result property="examType"      column="exam_type"       />
		<result property="regStatus"     column="reg_status"          />
		<result property="endYear"       column="end_year"     />
		<result property="inviteCode"    column="invite_code"     />
		<result property="scores"        column="scores" typeHandler="com.ruoyi.system.handlers.SubjectScoreTypeHandler"        />
		<result property="selectSubject" column="select_subject"       />
		<result property="directedStudy" column="directed_study"       />
		<result property="evalCounts"     column="eval_counts" />
		<result property="cardId"        column="card_id"         />
        <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="pwdUpdateDate" column="pwd_update_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="userType"        column="user_type"          />
		<result property="userTypeId"        column="user_type_id"          />
		<result property="accountType"        column="account_type"          />
        <association property="dept"     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.password, u.sex, u.status, u.del_flag, u.login_ip, u.login_date, u.pwd_update_date, u.create_by, u.create_time, u.remark,
		u.location,u.exam_type,u.reg_status,u.end_year,u.invite_code,u.scores,u.select_subject,u.directed_study,u.eval_counts,u.card_id,u.account_type,
        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, u.user_type, u.user_type_id
        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.sex, u.status, u.del_flag, u.login_ip, u.login_date, u.create_by, u.create_time, u.remark, d.dept_name, d.leader, u.user_type, u.user_type_id, u.account_type
		,u.card_id,u.end_year,u.location,u.invite_code,u.exam_type from sys_user u
		left join sys_dept d on u.dept_id = d.dept_id
		left join dz_cards c on u.card_id = c.card_id
		left join dz_agent a on CAST(u.invite_code AS UNSIGNED) = a.agent_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="status != null and status != ''">
			AND u.status = #{status}
		</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>
		<if test="userType != null and userType != ''">
			AND u.user_type = #{userType}
		</if>
		<if test="location != null and location != ''">
			AND u.location like concat('%', #{location}, '%')
		</if>
		<if test="endYear != null">
			AND u.end_year = #{endYear}
		</if>
		<if test="nickName != null and nickName != ''">
			AND u.nick_name like concat('%', #{nickName}, '%')
		</if>
		<if test="examType != null">
			AND u.exam_type = #{examType}
		</if>
		<if test="deptName != null and deptName != ''">
			AND d.dept_name like concat('%', #{deptName}, '%')
		</if>
		<if test="inviteCode != null and inviteCode != ''">
			AND u.invite_code like concat('%', #{inviteCode}, '%')
		</if>
		<if test="inviteDeptId != null and inviteDeptId != 0">
			AND a.dept_id = #{inviteDeptId}
		</if>
		<if test="usercardType != null and usercardType != ''">
			<choose>
				<when test="usercardType == '99' or usercardType == 99">
					AND u.card_id IS NULL
				</when>
				<when test="usercardType == '1' or usercardType == 1">
					AND u.card_id IS NOT NULL AND c.type IS NOT NULL AND c.type IN (1, 2, 6, 7)
				</when>
				<when test="usercardType == '9' or usercardType == 9">
					AND u.card_id IS NOT NULL AND c.type IS NOT NULL AND c.type = 9
				</when>
			</choose>
		</if>
		<!-- 数据范围过滤 -->
		${params.dataScope}

		order by u.user_id desc
	</select>

	<select id="selectCardUserList" parameterType="SysUser" resultMap="SysUserResult">
		select u.user_id, u.dept_id, u.nick_name, u.user_name, u.email, u.avatar, u.phonenumber, u.sex, u.status, u.del_flag, u.login_ip, u.login_date, u.create_by, u.create_time, u.remark, d.dept_name, d.leader, u.user_type, u.user_type_id, u.account_type
		,u.card_id,u.end_year,u.location,u.invite_code,u.exam_type from sys_user u
		left join sys_dept d on u.dept_id = d.dept_id
		left join dz_cards c on u.card_id = c.card_id
		left join dz_agent a on CAST(u.invite_code AS UNSIGNED) = a.agent_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="status != null and status != ''">
			AND u.status = #{status}
		</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="agentId != null and agentId != ''"> and (c.agent_id = #{agentId} or c.leaf_agent_id = #{agentId} or a.agent_id = #{agentId} OR a.agent_id IN ( SELECT t.agent_id FROM dz_agent t WHERE t.parent_id = #{agentId}))</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>
		<if test="inviteDeptId != null and inviteDeptId != 0">
			AND a.dept_id = #{inviteDeptId}
		</if>
		<if test="userType != null and userType != ''">
			AND u.user_type = #{userType}
		</if>
		<if test="location != null and location != ''">
			AND u.location like concat('%', #{location}, '%')
		</if>
		<if test="endYear != null">
			AND u.end_year = #{endYear}
		</if>
		<if test="nickName != null and nickName != ''">
			AND u.nick_name like concat('%', #{nickName}, '%')
		</if>
		<if test="examType != null">
			AND u.exam_type = #{examType}
		</if>
		<if test="deptName != null and deptName != ''">
			AND d.dept_name like concat('%', #{deptName}, '%')
		</if>
		<if test="inviteCode != null and inviteCode != ''">
			AND u.invite_code like concat('%', #{inviteCode}, '%')
		</if>
		<if test="usercardType != null and usercardType != ''">
			<choose>
				<when test="usercardType == '99' or usercardType == 99">
					AND u.card_id IS NULL
				</when>
				<when test="usercardType == '1' or usercardType == 1">
					AND u.card_id IS NOT NULL AND c.type IS NOT NULL AND c.type IN (1, 2, 6, 7)
				</when>
				<when test="usercardType == '9' or usercardType == 9">
					AND u.card_id IS NOT NULL AND c.type IS NOT NULL AND c.type = 9
				</when>
			</choose>
		</if>

		order by u.user_id desc
	</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
	    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
	    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="selectPasswordByCardId" parameterType="Long" resultType="String">
		select u.password2 from sys_user u where u.card_id = #{cardId} and u.del_flag = '0'
	</select>

	<select id="selectPassword2ByUserIds" resultType="map">
		select u.user_id, u.password2 from sys_user u
		where u.user_id in <foreach collection="userIds" item="userId" open="(" separator="," close=")">#{userId}</foreach>
		and u.del_flag = '0'
	</select>

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

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

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

	<select id="selectUserByCardIds" resultMap="SysUserResult">
		<include refid="selectUserVo"/>
		where u.card_id in <foreach collection="ids" item="id" open="(" separator="," close=")">#{id}</foreach>
	</select>

	<select id="selectUserByUserIds" resultMap="SysUserResult">
		select u.user_id, u.dept_id, u.user_name, u.nick_name, u.email, u.avatar, u.phonenumber, u.password, u.password2, u.sex, u.status, u.del_flag, u.login_ip, u.login_date, u.pwd_update_date, u.create_by, u.create_time, u.remark,
		u.location,u.exam_type,u.reg_status,u.end_year,u.invite_code,u.scores,u.select_subject,u.directed_study,u.eval_counts,u.card_id,u.account_type
        from sys_user u
		where u.user_id in <foreach collection="ids" item="id" open="(" separator="," close=")">#{id}</foreach>
	</select>

	<select id="selectUserByUserNames" resultMap="SysUserResult">
		select u.user_id, u.dept_id, u.user_name, u.nick_name, u.email, u.avatar, u.phonenumber, u.password, u.password2, u.sex, u.status, u.del_flag, u.login_ip, u.login_date, u.pwd_update_date, u.create_by, u.create_time, u.remark,
		u.location,u.exam_type,u.reg_status,u.end_year,u.invite_code,u.scores,u.select_subject,u.directed_study,u.eval_counts,u.card_id,u.account_type
        from sys_user u
		where u.user_name in <foreach collection="userNames" item="userName" open="(" separator="," close=")">#{userName}</foreach>
		and u.del_flag = '0'
	</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>

	<select id="selectUsers" parameterType="map" resultMap="SysUserResult">
		select user_id, dept_id, nick_name, avatar, user_name, email,exam_type 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>

	<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">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="password2 != null and password2 != ''">password2,</if>
 			<if test="status != null and status != ''">status,</if>
 			<if test="pwdUpdateDate != null">pwd_update_date,</if>
		    <if test="location != null and location != ''">location,</if>
		    <if test="examType != null">exam_type,</if>
		    <if test="regStatus != null">reg_status,</if>
		    <if test="endYear != null">end_year,</if>
		    <if test="inviteCode != null and inviteCode != ''">invite_code,</if>
		    <if test="scores != null and scores != ''">scores,</if>
		    <if test="evalCounts != null">eval_counts,</if>
		    <if test="selectSubject != null and selectSubject != ''">select_subject,</if>
		    <if test="directedStudy != null and directedStudy != ''">directed_study,</if>
		    <if test="cardId != null and cardId != ''">card_id,</if>
 			<if test="createBy != null and createBy != ''">create_by,</if>
 			<if test="remark != null and remark != ''">remark,</if>
		    <if test="userType != null and userType != ''">user_type,</if>
		    <if test="userTypeId != null">user_type_id,</if>
		    <if test="accountType != null">account_type,</if>
 			create_time
 		)values(
 			<if test="userId != null and userId != ''">#{userId},</if>
 			<if test="deptId != null">#{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="password2 != null and password2 != ''">#{password2},</if>
 			<if test="status != null and status != ''">#{status},</if>
 			<if test="pwdUpdateDate != null">#{pwdUpdateDate},</if>
 			<if test="location != null and location != ''">#{location},</if>
 			<if test="examType != null">#{examType},</if>
 			<if test="regStatus != null">#{regStatus},</if>
 			<if test="endYear != null and endYear != ''">#{endYear},</if>
 			<if test="inviteCode != null and inviteCode != ''">#{inviteCode},</if>
 			<if test="scores != null and scores != ''">#{scores, typeHandler=com.ruoyi.system.handlers.SubjectScoreTypeHandler},</if>
		    <if test="evalCounts != null">#{evalCounts},</if>
 			<if test="selectSubject != null and selectSubject != ''">#{selectSubject},</if>
 			<if test="directedStudy != null and directedStudy != ''">#{directedStudy},</if>
 			<if test="cardId != null and cardId != ''">#{cardId},</if>
 			<if test="createBy != null and createBy != ''">#{createBy},</if>
 			<if test="remark != null and remark != ''">#{remark},</if>
		    <if test="userType != null">#{userType},</if>
		    <if test="userTypeId != null">#{userTypeId},</if>
		    <if test="accountType != null">#{accountType},</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="nickName != null and nickName != ''">nick_name = #{nickName},</if>
 			<if test="userName != null and userName != ''">user_name = #{userName},</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="password2 != null and password2 != ''">password2 = #{password2},</if>
 			<if test="status != null and status != ''">status = #{status},</if>
			<if test="location != null and location != ''">location = #{location},</if>
			<if test="examType != null">exam_type = #{examType},</if>
			<if test="regStatus != null">reg_status = #{regStatus},</if>
			<if test="endYear != null">end_year = #{endYear},</if>
			<if test="inviteCode != null and inviteCode != ''">invite_code = #{inviteCode},</if>
			<if test="scores != null and scores != ''">scores = #{scores, typeHandler=com.ruoyi.system.handlers.SubjectScoreTypeHandler},</if>
			<if test="selectSubject != null and selectSubject != ''">select_subject = #{selectSubject},</if>
			<if test="directedStudy != null and directedStudy != ''">directed_study = #{directedStudy},</if>
			<if test="evalCounts != null and evalCounts != ''">eval_counts = #{evalCounts},</if>
			<if test="cardId != null and cardId != ''">card_id = #{cardId},</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>
			<if test="userTypeId != null">user_type_id = #{userTypeId},</if>
			<if test="accountType != null">account_type = #{accountType},</if>
 			update_time = sysdate()
 		</set>
 		where user_id = #{userId}
	</update>

	<update id="updateUserStatus" parameterType="SysUser">
 		update sys_user set status = #{status}, update_time = sysdate() where user_id = #{userId}
	</update>

	<update id="updateUserAvatar" parameterType="SysUser">
 		update sys_user set avatar = #{avatar}, update_time = sysdate() where user_id = #{userId}
	</update>

  	<update id="updateLoginInfo" parameterType="SysUser">
 		update sys_user set login_ip = #{loginIp}, login_date = #{loginDate} where user_id = #{userId}
	</update>

	<update id="resetUserPwd" parameterType="SysUser">
 		update sys_user set pwd_update_date = sysdate(), password = #{password}, password2 = #{password2}, update_time = sysdate() where user_id = #{userId}
	</update>

	<delete id="deleteUserById" parameterType="Long">
 		update sys_user set del_flag = '2' 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>

	<!-- 查询所有非空的省份列表（去重） -->
	<select id="selectDistinctLocations" resultType="String">
		SELECT DISTINCT location
		FROM sys_user
		WHERE location IS NOT NULL AND location != '' AND del_flag = '0'
		ORDER BY location
	</select>

</mapper>
