<?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.ylcm.sys.mapper.UserMapper">

	<resultMap type="com.ylcm.sys.domain.User" id="userMap">
		<id property="uid" column="uid" />
		<result property="loginName" column="login_name" />
		<result property="loginPwd" column="login_pwd" />
		<result property="nickName" column="nick_name" />
		<result property="telephone" column="telephone" />
		<result property="email" column="email" />
		<result property="sex" column="sex" />
		<result property="roleName" column="roleName" />
		<result property="status" column="status" />
		<result property="crtTime" column="crt_time" />
		<result property="updTime" column="upd_time" />
	</resultMap>
	
	<resultMap type="com.ylcm.sys.domain.User" id="userRoleMap">
		<id property="uid" column="uid" />
		<result property="loginName" column="login_name" />
		<result property="loginPwd" column="login_pwd" />
		<result property="nickName" column="nick_name" />
		<result property="telephone" column="telephone" />
		<result property="email" column="email" />
		<result property="sex" column="sex" />
		<result property="status" column="status" />
		<result property="crtTime" column="crt_time" />
		<result property="updTime" column="upd_time" />
		<association property="role" column="uid" 
			select="com.ylcm.sys.mapper.RoleMapper.getRolesByUserId" /> 
	</resultMap>
	
	<resultMap type="com.ylcm.sys.domain.User" id="userLoginMap">
		<id property="uid" column="uid" />
		<result property="loginName" column="login_name" />
		<result property="loginPwd" column="login_pwd" />
		<result property="nickName" column="nick_name" />
		<result property="telephone" column="telephone" />
		<result property="email" column="email" />
		<result property="sex" column="sex" />
		<result property="status" column="status" />
		<result property="crtTime" column="crt_time" />
		<result property="updTime" column="upd_time" />
		<association property="role" column="uid" 
			select="com.ylcm.sys.mapper.RoleMapper.getByUserIdForLogin" /> 
	</resultMap>
	
	
	<!-- 新增用户 -->
	<insert id="addUser" parameterType="com.ylcm.sys.domain.User" useGeneratedKeys="true" keyProperty="uid">
		INSERT INTO user 
        <trim prefix="(" suffix=")" suffixOverrides="," >
			<if test="loginName != null and loginName != ''">
				login_name ,
			</if>
			<if test="loginPwd != null and loginPwd != ''">
				login_pwd ,
			</if>
			<if test="nickName != null and nickName != ''">
				nick_name ,
			</if>
			<if test="telephone != null and telephone != ''">
				telephone ,
			</if>
			<if test="email != null and email != ''">
				email ,
			</if>
			<if test="sex != null">
				sex ,
			</if>
				status ,
			<if test="crtTime != null">
				crt_time ,
			</if>
			<if test="updTime != null">
				upd_time
			</if>
        </trim>
        <trim prefix="VALUES (" suffix=")" suffixOverrides="," >
			<if test="loginName != null and loginName != ''">
				#{loginName},
			</if>
			<if test="loginPwd != null and loginPwd != ''">
				#{loginPwd},
			</if>
			<if test="nickName != null and nickName != ''">
				#{nickName},
			</if>
			<if test="telephone != null and telephone != ''">
				#{telephone},
			</if>
			<if test="email != null and email != ''">
				#{email},
			</if>
			<if test="sex != null">
				#{sex},
			</if>
				#{status},
			<if test="crtTime != null">
				#{crtTime},
			</if>
			<if test="updTime != null">
				#{updTime}
			</if>
		</trim>
	</insert>
	
	<!-- 更新用户 -->
	<update id="updUser" parameterType="com.ylcm.sys.domain.User" >
		UPDATE user  
		<set>
		  <if test="loginName != null and loginName != ''">
			login_name = #{loginName},
		  </if>
		  <if test="nickName != null and nickName != ''">
			nick_name = #{nickName},
		  </if>
		  <if test="loginPwd != null and loginPwd != ''">
			login_pwd = #{loginPwd},
		  </if>
		  <if test="telephone != null and telephone != ''">
			telephone = #{telephone},
		  </if>
		  <if test="email != null and email != ''">
			email = #{email},
		  </if>
		  <if test="sex != null">
			sex = #{sex},
		  </if>
		  <if test="status != null and status != ''">
			status = #{status},
		  </if>
		  <if test="updTime != null">
			upd_time = #{updTime}
		  </if>
		</set>
		WHERE uid = #{uid}
	</update>
	
	<!-- 删除用户 -->
	<delete id="delUser">
		UPDATE user
		SET status = 4
		WHERE uid = #{uid}
	</delete>
	
	<!-- 根据条件统计用户总数-->
	<select id="getRowCount" parameterType="java.util.Map" resultType="java.lang.Integer">
		SELECT COUNT(u.uid) count
		FROM user u
		LEFT JOIN user_role ur on u.uid = ur.user_id		
		<trim prefix="WHERE" prefixOverrides="AND |OR ">
			AND status = 0
			<if test="loginName != null and loginName != '' ">
				AND u.login_name LIKE CONCAT('%',#{loginName},'%')
			</if>
			<if test="nickName != null and nickName != ''">
				AND u.nick_name LIKE CONCAT('%',#{nickName},'%')
			</if>
			<if test="email != null and email != ''">
				AND u.email LIKE CONCAT('%',#{email},'%')
			</if>
			<if test="telephone != null and telephone != ''">
				AND u.telephone LIKE CONCAT('%',#{telephone},'%')
			</if>
			<if test="sex != null">
				AND u.sex = #{sex}
			</if>
			<if test="userRole != null">
				AND ur.role_id = #{userRole}
			</if>
		</trim>
	</select>
	
	<!-- 分页获取用户信息 -->
	<select id="getUserList" resultMap="userMap">
		SELECT u.uid, u.login_name, u.nick_name,u.telephone ,u.email,ifnull(u.sex,'2') as sex, u.status, u.crt_time, u.upd_time,r.name as roleName
		FROM user u
		LEFT JOIN user_role ur on u.uid = ur.user_id
		LEFT JOIN role r on ur.role_id = r.role_id
		<trim prefix="WHERE" prefixOverrides="AND |OR ">
			AND status = 0
			<if test="loginName != null and loginName != '' ">
				AND u.login_name LIKE CONCAT('%',#{loginName},'%')
			</if>
			<if test="nickName != null and nickName != ''">
				AND u.nick_name LIKE CONCAT('%',#{nickName},'%')
			</if>
			<if test="email != null and email != ''">
				AND u.email LIKE CONCAT('%',#{email},'%')
			</if>
			<if test="telephone != null and telephone != ''">
				AND u.telephone LIKE CONCAT('%',#{telephone},'%')
			</if>
			<if test="sex != null">
				AND u.sex = #{sex}
			</if>
			<if test="userRole != null">
				AND ur.role_id = #{userRole}
			</if>
		</trim>
		ORDER BY upd_time DESC
		<if test="pageIndex != null">
		LIMIT #{pageSize} OFFSET #{pageIndex}
		</if>
	</select>
	
	<!-- 根据用户标识获取用户信息 -->
	<select id="getUserById" resultMap="userLoginMap">
		SELECT uid, login_name, nick_name,telephone ,email,sex, status, crt_time, upd_time
		FROM user
		WHERE uid = #{userId}
	</select>
	
	<!-- 批量删除用户（未关联角色的用户） -->
	<delete id="delUsersByIds">
		UPDATE user
		set status = 4
		WHERE uid IN
		<foreach item="id" collection="array" open="(" separator="," close=")">
			#{id}
		</foreach>
		<!-- AND NOT EXISTS (SELECT NULL FROM user_role ur WHERE ur.user_id = user.uid) -->
	</delete>
	
	<!-- 修改密码 -->
	<delete id="updatePwd">
		UPDATE user
		SET login_pwd = #{password}
		WHERE uid = #{userId}
	</delete>
	
	<!-- 唯一性检测 -->
	<select id="getCheckUser" parameterType="java.util.Map" resultType="java.lang.Integer">
		SELECT count(1) 
		FROM user  
		<trim prefix="WHERE" prefixOverrides="AND |OR ">
			AND status = 0
			<if test="loginName != null and loginName != ''">
				AND login_name = #{loginName}
			</if>
		</trim>
	</select>
	
	<!-- 根据登录名和用户标识查找登录名是否已存在 -->
	<select id="getUserByLoginNameNeId" parameterType="java.util.Map" resultType="java.lang.Integer">
		<![CDATA[
		SELECT count(1) 
		FROM user
		WHERE login_name = #{loginName} AND uid <> #{uid} AND status = 0
		]]>
	</select>
	
	<!-- 根据登录名获取用户信息 -->
	<select id="getUserByLoginName" resultMap="userLoginMap">
		SELECT uid, login_name, login_pwd, nick_name,telephone,email,sex, status, crt_time, upd_time
		FROM user
		WHERE login_name = #{loginName} AND status = 0
	</select>
	
	<!-- 更新用户状态 -->
	<update id="updUserStatus" parameterType="com.ylcm.sys.domain.User" >
		UPDATE user  
		SET 
		  <if test="status > 0">
			status = #{status}
		  </if>
		WHERE uid = #{uid}
	</update>
</mapper>