<?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.UserSessionsMapper">
    
    <resultMap type="UserSessions" id="UserSessionsResult">
        <result property="sessionId"    column="session_id"    />
        <result property="userId"    column="user_id"    />
        <result property="sessionStart"    column="session_start"    />
        <result property="sessionEnd"    column="session_end"    />
        <result property="lastActivity"    column="last_activity"    />
        <result property="sessionToken"    column="session_token"    />
        <result property="status"    column="status"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
    </resultMap>

    <sql id="selectUserSessionsVo">
        select session_id, user_id, session_start, session_end, last_activity, session_token, status, create_time, update_time from user_sessions
    </sql>

    <select id="selectUserSessionsList" parameterType="UserSessions" resultMap="UserSessionsResult">
        <include refid="selectUserSessionsVo"/>
        <where>  
            <if test="userId != null "> and user_id = #{userId}</if>
            <if test="sessionToken != null  and sessionToken != ''"> and session_token = #{sessionToken}</if>
            <if test="status != null  and status != ''"> and status = #{status}</if>
        </where>
    </select>
    
    <select id="selectUserSessionsById" parameterType="String" resultMap="UserSessionsResult">
        <include refid="selectUserSessionsVo"/>
        where session_id = #{sessionId}
    </select>
        
    <insert id="insertUserSessions" parameterType="UserSessions">
        insert into user_sessions
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="sessionId != null">session_id,</if>
            <if test="userId != null">user_id,</if>
            <if test="sessionStart != null">session_start,</if>
            <if test="sessionEnd != null">session_end,</if>
            <if test="lastActivity != null">last_activity,</if>
            <if test="sessionToken != null">session_token,</if>
            <if test="status != null">status,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="sessionId != null">#{sessionId},</if>
            <if test="userId != null">#{userId},</if>
            <if test="sessionStart != null">#{sessionStart},</if>
            <if test="sessionEnd != null">#{sessionEnd},</if>
            <if test="lastActivity != null">#{lastActivity},</if>
            <if test="sessionToken != null">#{sessionToken},</if>
            <if test="status != null">#{status},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
         </trim>
    </insert>

    <update id="updateUserSessions" parameterType="UserSessions">
        update user_sessions
        <trim prefix="SET" suffixOverrides=",">
            <if test="userId != null">user_id = #{userId},</if>
            <if test="sessionStart != null">session_start = #{sessionStart},</if>
            <if test="sessionEnd != null">session_end = #{sessionEnd},</if>
            <if test="lastActivity != null">last_activity = #{lastActivity},</if>
            <if test="sessionToken != null">session_token = #{sessionToken},</if>
            <if test="status != null">status = #{status},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where session_id = #{sessionId}
    </update>

    <delete id="deleteUserSessionsById" parameterType="String">
        delete from user_sessions where session_id = #{sessionId}
    </delete>

    <delete id="deleteUserSessionsByIds" parameterType="String">
        delete from user_sessions where session_id in 
        <foreach item="sessionId" collection="array" open="(" separator="," close=")">
            #{sessionId}
        </foreach>
    </delete>

</mapper>