<?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.nbclass.system.mapper.UserMapper">
    <resultMap id="BaseResultMap" type="com.nbclass.system.model.User" >
        <id column="id" property="id" jdbcType="INTEGER" />
        <id column="user_id" property="userId" jdbcType="VARCHAR" />
        <result column="username" property="username" jdbcType="VARCHAR" />
        <result column="password" property="password" jdbcType="VARCHAR" />
        <result column="salt" property="salt" jdbcType="VARCHAR" />
        <result column="email" property="email" jdbcType="VARCHAR" />
        <result column="phone" property="phone" jdbcType="VARCHAR" />
        <result column="sex" property="sex" jdbcType="INTEGER" />
        <result column="age" property="age" jdbcType="INTEGER" />
        <result column="status" property="status" jdbcType="INTEGER" />
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP" />
        <result column="update_time" property="updateTime" jdbcType="TIMESTAMP" />
        <result column="last_login_time" property="lastLoginTime" jdbcType="TIMESTAMP" />
    </resultMap>
    <sql id="Base_Column_List" >
        user.id, user.user_id, user.username, user.password, user.salt, user.email, user.phone, user.sex, user.age, user.status,
        user.create_time, user.update_time, user.last_login_time 
    </sql>

    <select id="selectUsers" resultMap="BaseResultMap" parameterType="com.nbclass.system.model.User">
        SELECT <include refid="Base_Column_List"/> , role.name as rolename 
        FROM user 
        left join user_role on user.user_id = user_role.user_id 
        left join role on user_role.role_id = role.role_id 
        WHERE user.status = 1 
        <if test="username !=null and username != ''">
            AND user.username like concat('%', #{username}, '%')
        </if>
        <if test="email !=null and email != ''">
            AND user.email like concat('%', #{email}, '%')
        </if>
        <if test="phone !=null and phone != ''">
            AND user.phone like concat('%', #{phone}, '%')
        </if>
    </select>

    <select id="selectByUsername" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"/> , role.name as rolename 
        FROM user 
        left join user_role on user.user_id = user_role.user_id 
        left join role on user_role.role_id = role.role_id 
        WHERE user.username = #{username,jdbcType=VARCHAR}
    </select>

    <select id="selectByUserId" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"/> FROM user WHERE user_id = #{userId,jdbcType=VARCHAR}
    </select>

    <update id="updateLastLoginTime" parameterType="com.nbclass.system.model.User">
        update user SET last_login_time = now() where id = #{id,jdbcType=BIGINT}
    </update>

    <update id="updateByUserId" parameterType="com.nbclass.system.model.User">
        update user SET email=#{email},phone=#{phone},sex=#{sex},age=#{age},update_time = now() where user_id = #{userId,jdbcType=BIGINT}
    </update>

    <update id="updateStatusBatch" parameterType="map">
        UPDATE
        user
        SET
        status=#{status}, update_time=now()
        where
        user_id in
        <foreach collection="userIds" item="item" index="index" open="(" separator="," close=")">
            #{item}
        </foreach>
    </update>
    <select id="findByRoleId" resultMap="BaseResultMap">
        SELECT a.id, a.user_id, a.username, a.password, a.salt, a.email, a.phone, a.sex, a.age, a.status,
        a.create_time, a.update_time, a.last_login_time FROM user a INNER JOIN user_role b ON a.user_id = b.user_id WHERE b.role_id = #{roleId,jdbcType=VARCHAR}
    </select>

    <select id="findByRoleIds" resultMap="BaseResultMap">
        SELECT a.id, a.user_id, a.username, a.password, a.salt, a.email, a.phone, a.sex, a.age, a.status,
        a.create_time, a.update_time, a.last_login_time FROM user a INNER JOIN user_role b ON a.user_id = b.user_id
        WHERE b.role_id IN
        <foreach collection="list" item="item" index="index" open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>
</mapper>