<?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.vetti.hotake.mapper.HotakeSecurityLoginSessionsMapper">
    
    <resultMap type="HotakeSecurityLoginSessions" id="HotakeSecurityLoginSessionsResult">
        <result property="id"    column="id"    />
        <result property="userId"    column="user_id"    />
        <result property="sessionId"    column="session_id"    />
        <result property="sessionToken"    column="session_token"    />
        <result property="deviceType"    column="device_type"    />
        <result property="deviceName"    column="device_name"    />
        <result property="browser"    column="browser"    />
        <result property="browserVersion"    column="browser_version"    />
        <result property="os"    column="os"    />
        <result property="osVersion"    column="os_version"    />
        <result property="ipAddress"    column="ip_address"    />
        <result property="locationCountry"    column="location_country"    />
        <result property="locationCity"    column="location_city"    />
        <result property="isActive"    column="is_active"    />
        <result property="lastActivityTime"    column="last_activity_time"    />
        <result property="loginTime"    column="login_time"    />
        <result property="logoutTime"    column="logout_time"    />
        <result property="expiresAt"    column="expires_at"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="remark"    column="remark"    />
    </resultMap>

    <sql id="selectHotakeSecurityLoginSessionsVo">
        select id, user_id, session_id, session_token, device_type, device_name, 
               browser, browser_version, os, os_version, ip_address, 
               location_country, location_city, is_active, last_activity_time, 
               login_time, logout_time, expires_at,
               del_flag, create_by, create_time, update_by, update_time, remark 
        from hotake_security_login_sessions
    </sql>

    <select id="selectHotakeSecurityLoginSessionsList" parameterType="HotakeSecurityLoginSessions" resultMap="HotakeSecurityLoginSessionsResult">
        <include refid="selectHotakeSecurityLoginSessionsVo"/>
        <where>
            del_flag = '0'
            <if test="userId != null"> and user_id = #{userId}</if>
            <if test="sessionToken != null and sessionToken != ''"> and session_token = #{sessionToken}</if>
            <if test="isActive != null"> and is_active = #{isActive}</if>
        </where>
        order by login_time desc
    </select>
    
    <select id="selectHotakeSecurityLoginSessionsById" parameterType="Long" resultMap="HotakeSecurityLoginSessionsResult">
        <include refid="selectHotakeSecurityLoginSessionsVo"/>
        where id = #{id} and del_flag = '0'
    </select>

    <select id="selectHotakeSecurityLoginSessionsByToken" parameterType="String" resultMap="HotakeSecurityLoginSessionsResult">
        <include refid="selectHotakeSecurityLoginSessionsVo"/>
        where session_token = #{sessionToken} and del_flag = '0'
        limit 1
    </select>

    <select id="selectActiveSessionsByUserId" parameterType="Long" resultMap="HotakeSecurityLoginSessionsResult">
        <include refid="selectHotakeSecurityLoginSessionsVo"/>
        where user_id = #{userId} 
          and is_active = 1 
          and del_flag = '0'
          and expires_at > now()
        order by login_time desc
    </select>
        
    <insert id="insertHotakeSecurityLoginSessions" parameterType="HotakeSecurityLoginSessions" useGeneratedKeys="true" keyProperty="id">
        insert into hotake_security_login_sessions
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="userId != null">user_id,</if>
            <if test="sessionId != null">session_id,</if>
            <if test="sessionToken != null">session_token,</if>
            <if test="deviceType != null">device_type,</if>
            <if test="deviceName != null">device_name,</if>
            <if test="browser != null">browser,</if>
            <if test="browserVersion != null">browser_version,</if>
            <if test="os != null">os,</if>
            <if test="osVersion != null">os_version,</if>
            <if test="ipAddress != null">ip_address,</if>
            <if test="locationCountry != null">location_country,</if>
            <if test="locationCity != null">location_city,</if>
            <if test="isActive != null">is_active,</if>
            <if test="lastActivityTime != null">last_activity_time,</if>
            <if test="loginTime != null">login_time,</if>
            <if test="logoutTime != null">logout_time,</if>
            <if test="expiresAt != null">expires_at,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="remark != null">remark,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="userId != null">#{userId},</if>
            <if test="sessionId != null">#{sessionId},</if>
            <if test="sessionToken != null">#{sessionToken},</if>
            <if test="deviceType != null">#{deviceType},</if>
            <if test="deviceName != null">#{deviceName},</if>
            <if test="browser != null">#{browser},</if>
            <if test="browserVersion != null">#{browserVersion},</if>
            <if test="os != null">#{os},</if>
            <if test="osVersion != null">#{osVersion},</if>
            <if test="ipAddress != null">#{ipAddress},</if>
            <if test="locationCountry != null">#{locationCountry},</if>
            <if test="locationCity != null">#{locationCity},</if>
            <if test="isActive != null">#{isActive},</if>
            <if test="lastActivityTime != null">#{lastActivityTime},</if>
            <if test="loginTime != null">#{loginTime},</if>
            <if test="logoutTime != null">#{logoutTime},</if>
            <if test="expiresAt != null">#{expiresAt},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="remark != null">#{remark},</if>
         </trim>
    </insert>

    <update id="updateHotakeSecurityLoginSessions" parameterType="HotakeSecurityLoginSessions">
        update hotake_security_login_sessions
        <trim prefix="SET" suffixOverrides=",">
            <if test="userId != null">user_id = #{userId},</if>
            <if test="sessionId != null">session_id = #{sessionId},</if>
            <if test="sessionToken != null">session_token = #{sessionToken},</if>
            <if test="deviceType != null">device_type = #{deviceType},</if>
            <if test="deviceName != null">device_name = #{deviceName},</if>
            <if test="browser != null">browser = #{browser},</if>
            <if test="browserVersion != null">browser_version = #{browserVersion},</if>
            <if test="os != null">os = #{os},</if>
            <if test="osVersion != null">os_version = #{osVersion},</if>
            <if test="ipAddress != null">ip_address = #{ipAddress},</if>
            <if test="locationCountry != null">location_country = #{locationCountry},</if>
            <if test="locationCity != null">location_city = #{locationCity},</if>
            <if test="isActive != null">is_active = #{isActive},</if>
            <if test="lastActivityTime != null">last_activity_time = #{lastActivityTime},</if>
            <if test="loginTime != null">login_time = #{loginTime},</if>
            <if test="logoutTime != null">logout_time = #{logoutTime},</if>
            <if test="expiresAt != null">expires_at = #{expiresAt},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where id = #{id}
    </update>

    <update id="terminateOtherSessions">
        update hotake_security_login_sessions
        set is_active = 0,
            logout_time = now(),
            update_time = now()
        where user_id = #{userId}
          and session_token != #{currentSessionToken}
          and is_active = 1
          and del_flag = '0'
    </update>

    <update id="terminateSession">
        update hotake_security_login_sessions
        set is_active = 0,
            logout_time = now(),
            update_time = now()
        where id = #{id}
          and user_id = #{userId}
          and del_flag = '0'
    </update>

    <delete id="deleteHotakeSecurityLoginSessionsById" parameterType="Long">
        update hotake_security_login_sessions set del_flag = '2' where id = #{id}
    </delete>

    <delete id="deleteHotakeSecurityLoginSessionsByIds" parameterType="String">
        update hotake_security_login_sessions set del_flag = '2' where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
</mapper>
