UserMapper.xml 7.64 KB
<?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.huaheng.project.system.user.mapper.UserMapper">

	<resultMap type="User" id="UserResult">
		<id     property="userId"       column="userId"      />
		<result property="deptId"       column="deptId"      />
		<result property="loginName"    column="loginName"   />
		<result property="userName"     column="userName"    />
		<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="salt"         column="salt"         />
		<result property="status"       column="status"       />
		<result property="delFlag"      column="delFlag"     />
		<result property="loginIp"      column="loginIp"     />
		<result property="loginDate"    column="loginDate"   />
		<result property="createBy"     column="createBy"    />
		<result property="createTime"   column="createTime"  />
		<result property="updateBy"     column="updateBy"    />
		<result property="updateTime"   column="updateTime"  />
		<result property="remark"       column="remark"       />
		<association property="dept"    column="deptId" javaType="Dept" resultMap="deptResult"/>
	</resultMap>
	
	<resultMap id="deptResult" type="Dept">
		<id     property="deptId"   column="deptId"     />
		<result property="parentId" column="parentId"   />
		<result property="deptName" column="deptName"   />
		<result property="orderNum" column="orderNum"   />
		<result property="status"   column="dept_status" />
	</resultMap>
	
	<sql id="selectUserVo">
        select  u.userId, u.deptId, u.loginName, u.userName, u.email, u.phoneNumber, u.sex, u.avatar, u.password, u.salt, u.status, u.delFlag, u.loginIp, u.loginDate, u.createTime, u.remark,
       		    d.deptId, d.parentId, d.deptName, d.orderNum, d.status as dept_status
		from sys_user u
			 left join sys_dept d on u.deptId = d.deptId
			 left join sys_user_role ur on u.userId = ur.userId
    </sql>
	
	<select id="selectUserList" parameterType="User" resultMap="UserResult">
		select u.userId, u.deptId, u.loginName, u.userName, u.email, u.phoneNumber, u.password, u.sex, u.avatar, u.salt, u.status, u.delFlag, u.loginIp, u.loginDate, u.createBy, u.createTime, u.remark, d.deptName from sys_user u
		left join sys_dept d on u.deptId = d.deptId
		where u.delFlag = '0'
		<if test="loginName != null and loginName != ''">
			AND u.loginName like concat('%', #{loginName}, '%')
		</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 != null and params.beginTime != ''"><!-- 开始时间检索 -->
			AND date_format(u.createTime,'%y%m%d') &gt;= date_format(#{params.beginTime},'%y%m%d')
		</if>
		<if test="params != null and params.endTime != ''"><!-- 结束时间检索 -->
			AND date_format(u.createTime,'%y%m%d') &lt;= date_format(#{params.endTime},'%y%m%d')
		</if>
		<if test="deptId != null and deptId != 0">
			AND (u.deptId = #{deptId} OR u.deptId IN ( SELECT t.deptId FROM sys_dept t WHERE FIND_IN_SET (#{deptId},ancestors) ))
		</if>
	</select>
	
	<select id="selectUserByLoginName" parameterType="String" resultMap="UserResult">
	    <include refid="selectUserVo"/>
		where u.loginName = #{userName}
	</select>
	
	<select id="selectUserByPhoneNumber" parameterType="String" resultMap="UserResult">
		<include refid="selectUserVo"/>
		where u.phoneNumber = #{phoneNumber}
	</select>
	
	<select id="selectUserByEmail" parameterType="String" resultMap="UserResult">
	    <include refid="selectUserVo"/>
		where u.email = #{email}
	</select>
	
	<select id="checkLoginNameUnique" parameterType="String" resultType="int">
		select count(1) from sys_user where loginName=#{loginName}
	</select>
	
	<select id="checkPhoneUnique" parameterType="String" resultMap="UserResult">
		select userId, phoneNumber from sys_user where phoneNumber=#{phoneNumber}
	</select>
	
	<select id="checkEmailUnique" parameterType="String" resultMap="UserResult">
		select userId, email from sys_user where email=#{email}
	</select>
	
	<select id="selectUserById" parameterType="Long" resultMap="UserResult">
		<include refid="selectUserVo"/>
		where u.userId = #{userId}
	</select>
	
	<delete id="deleteUserById" parameterType="Long">
 		delete from sys_user where userId = #{userId}
 	</delete>
 	
 	<delete id="deleteUserByIds" parameterType="Long">
 		update sys_user set delFlag = '2' where userId in
 		<foreach collection="array" item="userId" open="(" separator="," close=")">
 			#{userId}
        </foreach> 
 	</delete>
 	
 	<update id="updateUser" parameterType="User">
 		update sys_user
 		<set>
 			<if test="deptId != null and deptId != 0">deptId = #{deptId},</if>
 			<if test="loginName != null and loginName != ''">loginName = #{loginName},</if>
 			<if test="userName != null and userName != ''">userName = #{userName},</if>
 			<if test="email != null and email != ''">email = #{email},</if>
 			<if test="phoneNumber != null and phoneNumber != ''">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="salt != null and salt != ''">salt = #{salt},</if>
 			<if test="status != null and status != ''">status = #{status},</if>
 			<if test="loginIp != null and loginIp != ''">loginIp = #{loginIp},</if>
 			<if test="loginDate != null">loginDate = #{loginDate},</if>
 			<if test="updateBy != null and updateBy != ''">updateBy = #{updateBy},</if>
 			<if test="remark != null and remark != ''">remark = #{remark},</if>
 			updateTime = sysdate()
 		</set>
 		where userId = #{userId}
	</update>
 	
 	<insert id="insertUser" parameterType="User" useGeneratedKeys="true" keyProperty="userId">
 		insert into sys_user(
 			<if test="userId != null and userId != 0">userId,</if>
 			<if test="deptId != null and deptId != 0">deptId,</if>
 			<if test="loginName != null and loginName != ''">loginName,</if>
 			<if test="userName != null and userName != ''">userName,</if>
 			<if test="email != null and email != ''">email,</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="salt != null and salt != ''">salt,</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>
 			createTime
 		)values(
 			<if test="userId != null and userId != ''">#{userId},</if>
 			<if test="deptId != null and deptId != ''">#{deptId},</if>
 			<if test="loginName != null and loginName != ''">#{loginName},</if>
 			<if test="userName != null and userName != ''">#{userName},</if>
 			<if test="email != null and email != ''">#{email},</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="salt != null and salt != ''">#{salt},</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>
 			sysdate()
 		)
	</insert>
	
</mapper>