<?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="pinyin"     	column="pinyin"    />
        <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="pmLevel"       column="pm_level"       />
        <result property="titles"       column="titles"       />
        <result property="certificates"       column="certificates"       />
        <result property="updateReason"       column="update_reason"       />
        <result property="engineerLevel"       column="engineer_level"       />
        <result property="postLevel"       column="post_level"       />
        <result property="salaryLevel"       column="salary_level"       />
		<result property="operatorLevel"    column="operator_level"    />
        <result property="entryDate"       column="entry_date"       />
        <result property="birthday"       column="birthday"       />
        <result property="age"       column="age"       />
		<result property="idCard"    column="id_card"    />
		<result property="nativePlace"    column="native_place"    />
		<result property="politicalAffiliation"    column="political_affiliation"    />
		<result property="ethnic"    column="ethnic"    />
		<result property="graduateSchool"    column="graduate_school"    />
		<result property="major"    column="major"    />
		<result property="degree"    column="degree"    />
		<result property="contractSign"    column="contract_sign"    />
		<result property="contractExpire"    column="contract_expire"    />
		<result property="homePlace"    column="home_place"    />
		<result property="initialDegree"    column="initial_degree"    />
		<result property="initialMajor"    column="initial_major"    />
		<result property="initialSchool"    column="initial_school"    />
		<result property="titleProfession"    column="title_profession"    />
		<result property="contact"    column="contact"    />
		<result property="telephone"    column="telephone"    />
        <association property="dept"    javaType="SysDept"         resultMap="deptResult" />
        <association property="salary"    javaType="CmcPostSalary"         resultMap="CmcPostSalaryResult" />
        <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>

    <resultMap id="CmcPostSalaryResult" type="CmcPostSalary">
		<result property="salaryId"    column="salary_id"    />
		<result property="postLevel"    column="post_level"    />
		<result property="salaryLevel"    column="salary_level"    />
		<result property="salary"    column="salary"    />
    </resultMap>

	<sql id="selectUserVo">
        select u.user_id, u.dept_id, u.user_name, u.nick_name, u.pinyin, 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,
               u.pm_level, u.titles, u.certificates, u.update_reason, u.engineer_level, u.post_level, u.salary_level,
               u.operator_level, u.entry_date, u.birthday, u.age, u.id_card, u.native_place, u.political_affiliation,
               u.ethnic, u.graduate_school, u.major, u.degree, u.contract_sign, u.contract_expire, u.home_place, u.initial_degree,
               u.initial_major, u.initial_school, u.title_profession, u.contact, u.telephone,
               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, ps.salary
        from sys_user u
		    left join sys_dept d on u.dept_id = d.dept_id
		    left join cmc_post_salary ps on u.post_level = ps.post_level and u.salary_level = ps.salary_level
		    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.pinyin, 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, u.pm_level, u.titles, u.certificates, u.update_reason,
		       u.engineer_level, u.post_level, u.salary_level, u.operator_level, u.entry_date, u.birthday, u.age, u.id_card,
		       u.native_place, u.political_affiliation, u.ethnic, u.graduate_school, u.major, u.degree, u.contract_sign, u.contract_expire,
		       u.home_place, d.dept_name, d.leader, ps.salary, u.initial_degree, u.initial_major, u.initial_school, u.title_profession, u.contact, u.telephone from sys_user u
		left join sys_dept d on u.dept_id = d.dept_id
		left join cmc_post_salary ps on u.post_level = ps.post_level and u.salary_level = ps.salary_level
		where u.del_flag = '0' and u.user_id != 1
		<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="phonenumber != null and phonenumber != ''">
			AND u.phonenumber like concat('%', #{phonenumber}, '%')
		</if>
		<if test="degree != null and degree != ''">
			AND u.degree = #{degree}
		</if>
		<if test="titles != null and titles != ''">
			AND find_in_set(#{titles}, u.titles)
		</if>
		<if test="certificates != null and certificates != ''">
			AND find_in_set(#{certificates}, u.certificates)
		</if>
		<if test="politicalAffiliation != null and politicalAffiliation != ''">
			AND find_in_set(#{politicalAffiliation}, u.political_affiliation)
		</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}
		order by u.dept_id, u.user_id
	</select>

    <select id="selectUserServingList" parameterType="SysUser" resultMap="SysUserResult">
		select u.user_id, u.dept_id, u.nick_name, u.pinyin, 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, u.pm_level, u.titles, u.certificates, u.update_reason,
		       u.engineer_level, u.post_level, u.salary_level, u.operator_level, u.entry_date, u.birthday, u.age, u.id_card,
		       u.native_place, u.political_affiliation, u.ethnic, u.graduate_school, u.major, u.degree, u.contract_sign, u.contract_expire,
		       u.home_place, d.dept_name, d.leader, ps.salary, u.initial_degree, u.initial_major, u.initial_school, u.title_profession, u.contact, u.telephone from sys_user u
		left join sys_dept d on u.dept_id = d.dept_id
		left join cmc_post_salary ps on u.post_level = ps.post_level and u.salary_level = ps.salary_level
		where u.del_flag = '0' and u.user_id != 1 AND (u.status = '0' or u.status = '3' or u.status = '4')
		<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="phonenumber != null and phonenumber != ''">
			AND u.phonenumber like concat('%', #{phonenumber}, '%')
		</if>
		<if test="degree != null and degree != ''">
			AND u.degree = #{degree}
		</if>
		<if test="titles != null and titles != ''">
			AND find_in_set(#{titles}, u.titles)
		</if>
		<if test="certificates != null and certificates != ''">
			AND find_in_set(#{certificates}, u.certificates)
		</if>
		<if test="politicalAffiliation != null and politicalAffiliation != ''">
			AND find_in_set(#{politicalAffiliation}, u.political_affiliation)
		</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}
		order by u.dept_id, u.user_id
	</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="selectUserByUserName" parameterType="String" resultMap="SysUserResult">
	    <include refid="selectUserVo"/>
		where u.user_name = #{userName} and u.del_flag = '0'
	</select>

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

	<select id="selectUserById" parameterType="Long" resultMap="SysUserResult">
		<include refid="selectUserVo"/>
		where u.user_id = #{userId}
	</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="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 and status != ''">status,</if>
 			<if test="createBy != null and createBy != ''">create_by,</if>
 			<if test="remark != null and remark != ''">remark,</if>
 			<if test="pmLevel != null and pmLevel != ''">pm_level,</if>
 			<if test="titles != null and titles != ''">titles,</if>
 			<if test="certificates != null and certificates != ''">certificates,</if>
 			<if test="updateReason != null and updateReason != ''">update_reason,</if>
 			<if test="engineerLevel != null and engineerLevel != ''">engineer_level,</if>
 			<if test="postLevel != null and postLevel != ''">post_level,</if>
 			<if test="salaryLevel != null and salaryLevel != ''">salary_level,</if>
			<if test="operatorLevel != null and operatorLevel != ''">operator_level,</if>
 			<if test="entryDate != null">entry_date,</if>
 			<if test="birthday != null">birthday,</if>
 			<if test="age != null and age != ''">age,</if>
			<if test="idCard != null">id_card,</if>
			<if test="nativePlace != null">native_place,</if>
			<if test="politicalAffiliation != null">political_affiliation,</if>
			<if test="ethnic != null">ethnic,</if>
			<if test="graduateSchool != null">graduate_school,</if>
			<if test="major != null">major,</if>
			<if test="degree != null">degree,</if>
			<if test="contractSign != null">contract_sign,</if>
			<if test="contractExpire != null">contract_expire,</if>
			<if test="homePlace != null">home_place,</if>
			<if test="pinyin != null">pinyin,</if>
			<if test="initialDegree != null">initial_degree,</if>
			<if test="initialMajor != null">initial_major,</if>
			<if test="initialSchool != null">initial_school,</if>
			<if test="titleProfession != null">title_profession,</if>
			<if test="contact != null">contact,</if>
			<if test="telephone != null">telephone,</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 and status != ''">#{status},</if>
 			<if test="createBy != null and createBy != ''">#{createBy},</if>
 			<if test="remark != null and remark != ''">#{remark},</if>
 			<if test="pmLevel != null and pmLevel != ''">#{pmLevel},</if>
 			<if test="titles != null and titles != ''">#{titles},</if>
 			<if test="certificates != null and certificates != ''">#{certificates},</if>
 			<if test="updateReason != null and updateReason != ''">#{updateReason},</if>
 			<if test="engineerLevel != null and engineerLevel != ''">#{engineerLevel},</if>
 			<if test="postLevel != null and postLevel != ''">#{postLevel},</if>
 			<if test="salaryLevel != null and salaryLevel != ''">#{salaryLevel},</if>
			<if test="operatorLevel != null and operatorLevel != ''">#{operatorLevel},</if>
 			<if test="entryDate != null">#{entryDate},</if>
 			<if test="birthday != null">#{birthday},</if>
 			<if test="age != null and age != ''">#{age},</if>
			<if test="idCard != null">#{idCard},</if>
			<if test="nativePlace != null">#{nativePlace},</if>
			<if test="politicalAffiliation != null">#{politicalAffiliation},</if>
			<if test="ethnic != null">#{ethnic},</if>
			<if test="graduateSchool != null">#{graduateSchool},</if>
			<if test="major != null">#{major},</if>
			<if test="degree != null">#{degree},</if>
			<if test="contractSign != null">#{contractSign},</if>
			<if test="contractExpire != null">#{contractExpire},</if>
			<if test="homePlace != null">#{homePlace},</if>
			<if test="pinyin != null">#{pinyin},</if>
			<if test="initialDegree != null">#{initialDegree},</if>
			<if test="initialMajor != null">#{initialMajor},</if>
			<if test="initialSchool != null">#{initialSchool},</if>
			<if test="titleProfession != null">#{titleProfession},</if>
			<if test="contact != null">#{contact},</if>
			<if test="telephone != null">#{telephone},</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="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 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">remark = #{remark},</if>
 			<if test="pmLevel != null">pm_level = #{pmLevel},</if>
 			<if test="titles != null">titles = #{titles},</if>
 			<if test="certificates != null">certificates = #{certificates},</if>
 			<if test="updateReason != null">update_reason = #{updateReason},</if>
 			<if test="engineerLevel != null">engineer_level = #{engineerLevel},</if>
 			<if test="postLevel != null">post_level = #{postLevel},</if>
 			<if test="salaryLevel != null">salary_level = #{salaryLevel},</if>
			<if test="operatorLevel != null">operator_level = #{operatorLevel},</if>
 			<if test="entryDate != null">entry_date = #{entryDate},</if>
 			<if test="birthday != null">birthday = #{birthday},</if>
 			<if test="age != null">age = #{age},</if>
			<if test="idCard != null">id_card = #{idCard},</if>
			<if test="nativePlace != null">native_place = #{nativePlace},</if>
			<if test="politicalAffiliation != null">political_affiliation = #{politicalAffiliation},</if>
			<if test="ethnic != null">ethnic = #{ethnic},</if>
			<if test="graduateSchool != null">graduate_school = #{graduateSchool},</if>
			<if test="major != null">major = #{major},</if>
			<if test="degree != null">degree = #{degree},</if>
			<if test="contractSign != null">contract_sign = #{contractSign},</if>
			<if test="contractExpire != null">contract_expire = #{contractExpire},</if>
			<if test="homePlace != null">home_place = #{homePlace},</if>
			<if test="pinyin != null">pinyin = #{pinyin},</if>
			<if test="initialDegree != null">initial_degree = #{initialDegree},</if>
			<if test="initialMajor != null">initial_major = #{initialMajor},</if>
			<if test="initialSchool != null">initial_school = #{initialSchool},</if>
			<if test="titleProfession != null">title_profession = #{titleProfession},</if>
			<if test="contact != null">contact = #{contact},</if>
			<if test="telephone != null">telephone = #{telephone},</if>
 			update_time = sysdate()
 		</set>
 		where user_id = #{userId}
	</update>
	
	<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="updateUserEntryDate" parameterType="SysUser">
 		update sys_user set entry_date = #{entryDate} where user_id = #{userId}
	</update>

	<update id="updateUserContractSign" parameterType="SysUser">
 		update sys_user set contract_sign = #{contractSign} where user_id = #{userId}
	</update>

	<update id="updateUserContractExpire" parameterType="SysUser">
 		update sys_user set contract_expire = #{contractExpire} where user_id = #{userId}
	</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="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> 