<?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="lingtao.net.dao.SysUserMapper">
  <resultMap id="BaseResultMap" type="lingtao.net.bean.SysUser">
    <id column="userId" jdbcType="INTEGER" property="userId" />
    <result column="realname" jdbcType="VARCHAR" property="realname" />
    <result column="username" jdbcType="VARCHAR" property="username" />
    <result column="password" jdbcType="VARCHAR" property="password" />
    <result column="userStatus" jdbcType="VARCHAR" property="userStatus" />
    <result column="role" jdbcType="VARCHAR" property="role" />
    <result column="sysStatus" jdbcType="VARCHAR" property="sysStatus" />
    <result column="needIp" jdbcType="VARCHAR" property="needIp" />
    <result column="createBy" jdbcType="VARCHAR" property="createBy" />
    <result column="createDate" jdbcType="TIMESTAMP" property="createDate" />
    <result column="updateBy" jdbcType="VARCHAR" property="updateBy" />
    <result column="updateDate" jdbcType="TIMESTAMP" property="updateDate" />
  </resultMap>
  
  <!-- 根据用户名查询用户 -->
  <!-- <select id="getUserByUsername" parameterType="String" resultMap="BaseResultMap">
  		select * from tbl_sys_user where username = #{username}
  </select> -->
  
  <!-- 获取用户 -->
  <select id="getUsers" parameterType="SysUser" resultMap="BaseResultMap">
  	select * from tbl_sys_user
  	<where>
  		<if test="username != null and username != ''">
  			and username like '%${username}%'
  		</if>
  		<if test="realname != null and realname != ''">
  			and realname like '%${realname}%'
  		</if>
  		<if test="userStatus != null and userStatus != ''">
  			and userStatus = #{userStatus}
  		</if>
  		<if test="roleSearch != null and roleSearch != ''">
  			and role like '%${roleSearch}%'
  		</if>
  		<if test = "roleArr != null and roleArr != '' " > 
			and	role in
			<foreach collection="roleArr" item="item" open="(" separator="," close=")">
				#{item}
			</foreach>
			
			<foreach collection="roleArr" item="item">
				or role like '%${item}%'
			</foreach>
		</if>
  	</where>
  	ORDER BY userId DESC
  </select>
  
  <!-- 改变用户启用状态 -->
  <update id="changeUserStatus">
		UPDATE 
	  		tbl_sys_user 
		SET
		  userStatus = 
		  	CASE
		    WHEN 
		    	userStatus = '1' 
		    THEN 
		    	'0' 
		    ELSE 
		    	'1' 
		  END,
		  sysStatus = CASE WHEN sysStatus = '2' THEN '0' ElSE '2' END 
		WHERE userId = #{userId} 
   </update>
   
  <!-- 改变用户系统状态 -->
  <update id="changeSysStatus">
		UPDATE 
	  		tbl_sys_user 
		SET
		  sysStatus = 
		  	CASE
		    WHEN 
		    	sysStatus = '2' 
		    THEN 
		    	'1' 
		    ELSE 
		    	'2' 
		  END 
		WHERE userId = #{userId} 
   </update>
   
   	<!-- 改变所有用户的Ip状态 -->
  	<update id="changeNeedIp">
		UPDATE 
		  tbl_sys_user 
		SET
		  needIp = 
		  CASE
		    WHEN needIp = '1' 
		    THEN '0' 
		    WHEN needIp = '0' 
		    THEN '1' 
		    ELSE '2' 
		  END 
   	</update>
  
   <insert id="insertSelective"  useGeneratedKeys="true" keyProperty="userId" parameterType="lingtao.net.bean.SysUser">
    insert into tbl_sys_user
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="realname != null">
        realname,
      </if>
      <if test="username != null">
        username,
      </if>
      <if test="password != null">
        password,
      </if>
      <if test="userStatus != null">
        userStatus,
      </if>
      <if test="role != null">
        role,
      </if>
      <if test="sysStatus != null">
        sysStatus,
      </if>
      <if test="needIp != null">
        needIp,
      </if>
      <if test="createBy != null">
        createBy,
      </if>
      <if test="createDate != null">
        createDate,
      </if>
      <if test="updateBy != null">
        updateBy,
      </if>
      <if test="updateDate != null">
        updateDate,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="realname != null">
        #{realname,jdbcType=VARCHAR},
      </if>
      <if test="username != null">
        #{username,jdbcType=VARCHAR},
      </if>
      <if test="password != null">
        #{password,jdbcType=VARCHAR},
      </if>
      <if test="userStatus != null">
        #{userStatus,jdbcType=VARCHAR},
      </if>
      <if test="role != null">
        #{role,jdbcType=VARCHAR},
      </if>
      <if test="sysStatus != null">
        #{sysStatus,jdbcType=VARCHAR},
      </if>
      <if test="needIp != null">
        #{needIp,jdbcType=VARCHAR},
      </if>
      <if test="createBy != null">
        #{createBy,jdbcType=VARCHAR},
      </if>
      <if test="createDate != null">
        #{createDate,jdbcType=TIMESTAMP},
      </if>
      <if test="updateBy != null">
        #{updateBy,jdbcType=VARCHAR},
      </if>
      <if test="updateDate != null">
        #{updateDate,jdbcType=TIMESTAMP},
      </if>
    </trim>
  </insert>
  
  <update id="updateByPrimaryKeySelective" parameterType="lingtao.net.bean.SysUser">
    update tbl_sys_user
    <set>
      <if test="realname != null">
        realname = #{realname,jdbcType=VARCHAR},
      </if>
      <if test="username != null">
        username = #{username,jdbcType=VARCHAR},
      </if>
      <if test="password != null">
        password = #{password,jdbcType=VARCHAR},
      </if>
      <if test="userStatus != null">
        userStatus = #{userStatus,jdbcType=VARCHAR},
      </if>
      <if test="role != null">
        role = #{role,jdbcType=VARCHAR},
      </if>
      <if test="sysStatus != null">
        sysStatus = #{sysStatus,jdbcType=VARCHAR},
      </if>
      <if test="needIp != null">
        needIp = #{needIp,jdbcType=VARCHAR},
      </if>
      <if test="birthDay != null">
        birthDay = #{birthDay,jdbcType=VARCHAR},
      </if>
      <if test="birthType != null">
        birthType = #{birthType,jdbcType=VARCHAR},
      </if>
      <if test="isBirthDay != null">
        isBirthDay = #{isBirthDay,jdbcType=INTEGER},
      </if>
      <if test="entryDate != null">
        entryDate = #{entryDate,jdbcType=VARCHAR},
      </if>
      <if test="createBy != null">
        createBy = #{createBy,jdbcType=VARCHAR},
      </if>
      <if test="createDate != null">
        createDate = #{createDate,jdbcType=TIMESTAMP},
      </if>
      <if test="updateBy != null">
        updateBy = #{updateBy,jdbcType=VARCHAR},
      </if>
      <if test="updateDate != null">
        updateDate = #{updateDate,jdbcType=TIMESTAMP},
      </if>
    </set>
    where userId = #{userId,jdbcType=INTEGER}
  </update>
  
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
    delete from tbl_sys_user
    where userId = #{userId,jdbcType=INTEGER}
  </delete>
  <select id="deleteUserRoles">
  	 delete from tbl_sys_user_role where userId = #{userId}
  </select>
  
   <insert id="addUserRoles">
        insert into tbl_sys_user_role(userId, roleId)
          values
          <foreach collection="roleIds" item="item" separator=",">
              (#{userId},#{item})
          </foreach>
    </insert>
  
</mapper>