<?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.datai.auth.mapper.DataiSfLoginSessionMapper">
    
    <resultMap type="DataiSfLoginSession" id="DataiSfLoginSessionResult">
        <result property="id" column="id" />
        <result property="username" column="username" />
        <result property="loginType" column="login_type" />
        <result property="status" column="status" />
        <result property="loginTime" column="login_time" />
        <result property="expireTime" column="expire_time" />
        <result property="lastActivityTime" column="last_activity_time" />
        <result property="loginIp" column="login_ip" />
        <result property="deviceInfo" column="device_info" />
        <result property="browserInfo" column="browser_info" />
        <result property="sessionId" column="session_id" />
        <result property="deptId" column="dept_id" />
        <result property="sfUserId" column="sf_user_id" />
        <result property="sfOrganizationId" column="sf_organization_id" />
        <result property="instanceUrl" column="instance_url" />
        <result property="orgType" column="org_type" />
        <result property="createBy" column="create_by" />
        <result property="createTime" column="create_time" />
        <result property="updateBy" column="update_by" />
        <result property="updateTime" column="update_time" />
    </resultMap>

    <sql id="selectDataiSfLoginSessionVo">
        select 
            dsls.id,
            dsls.username,
            dsls.login_type,
            dsls.status,
            dsls.login_time,
            dsls.expire_time,
            dsls.last_activity_time,
            dsls.login_ip,
            dsls.device_info,
            dsls.browser_info,
            dsls.session_id,
            dsls.dept_id,
            dsls.sf_user_id,
            dsls.sf_organization_id,
            dsls.instance_url,
            dsls.org_type,
            dsls.create_by,
            dsls.create_time,
            dsls.update_by,
            dsls.update_time
        from datai_sf_login_session dsls
    </sql>

    <select id="selectDataiSfLoginSessionList" parameterType="DataiSfLoginSession" resultMap="DataiSfLoginSessionResult">
        <include refid="selectDataiSfLoginSessionVo"/>
        <where>
            <if test="username != null  and username != ''"> and dsls.username like concat('%', #{username}, '%')</if>
            <if test="loginType != null  and loginType != ''"> and dsls.login_type = #{loginType}</if>
            <if test="status != null  and status != ''"> and dsls.status = #{status}</if>
            <if test="loginTime != null "> and dsls.login_time = #{loginTime}</if>
            <if test="expireTime != null "> and dsls.expire_time = #{expireTime}</if>
            <if test="lastActivityTime != null "> and dsls.last_activity_time = #{lastActivityTime}</if>
            <if test="loginIp != null  and loginIp != ''"> and dsls.login_ip = #{loginIp}</if>
            <if test="deviceInfo != null  and deviceInfo != ''"> and dsls.device_info = #{deviceInfo}</if>
            <if test="browserInfo != null  and browserInfo != ''"> and dsls.browser_info = #{browserInfo}</if>
            <if test="sessionId != null  and sessionId != ''"> and dsls.session_id = #{sessionId}</if>
            <if test="deptId != null "> and dsls.dept_id = #{deptId}</if>
        </where>
    </select>
    
    <select id="selectDataiSfLoginSessionById" parameterType="Long" resultMap="DataiSfLoginSessionResult">
        <include refid="selectDataiSfLoginSessionVo"/>
        where dsls.id = #{id}
    </select>
    
    <select id="selectDataiSfLoginSessionBySessionId" parameterType="String" resultMap="DataiSfLoginSessionResult">
        <include refid="selectDataiSfLoginSessionVo"/>
        where dsls.session_id = #{sessionId}
        order by dsls.login_time desc
        limit 1
    </select>

    <select id="selectLatestActiveSession" resultMap="DataiSfLoginSessionResult">
        <include refid="selectDataiSfLoginSessionVo"/>
        where dsls.status = 'active'
        order by dsls.login_time desc
        limit 1
    </select>

    <select id="selectLatestActiveSessionByOrgType" parameterType="String" resultMap="DataiSfLoginSessionResult">
        <include refid="selectDataiSfLoginSessionVo"/>
        where dsls.status = 'active' and dsls.org_type = #{orgType}
        order by dsls.login_time desc
        limit 1
    </select>
        
    <insert id="insertDataiSfLoginSession" parameterType="DataiSfLoginSession" useGeneratedKeys="true" keyProperty="id">
        insert into datai_sf_login_session
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="username != null and username != ''">username,</if>
            <if test="loginType != null and loginType != ''">login_type,</if>
            <if test="status != null and status != ''">status,</if>
            <if test="loginTime != null">login_time,</if>
            <if test="expireTime != null">expire_time,</if>
            <if test="lastActivityTime != null">last_activity_time,</if>
            <if test="loginIp != null">login_ip,</if>
            <if test="deviceInfo != null">device_info,</if>
            <if test="browserInfo != null">browser_info,</if>
            <if test="sessionId != null">session_id,</if>
            <if test="deptId != null">dept_id,</if>
            <if test="sfUserId != null">sf_user_id,</if>
            <if test="sfOrganizationId != null">sf_organization_id,</if>
            <if test="instanceUrl != null">instance_url,</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>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="username != null and username != ''">#{username},</if>
            <if test="loginType != null and loginType != ''">#{loginType},</if>
            <if test="status != null and status != ''">#{status},</if>
            <if test="loginTime != null">#{loginTime},</if>
            <if test="expireTime != null">#{expireTime},</if>
            <if test="lastActivityTime != null">#{lastActivityTime},</if>
            <if test="loginIp != null">#{loginIp},</if>
            <if test="deviceInfo != null">#{deviceInfo},</if>
            <if test="browserInfo != null">#{browserInfo},</if>
            <if test="sessionId != null">#{sessionId},</if>
            <if test="deptId != null">#{deptId},</if>
            <if test="sfUserId != null">#{sfUserId},</if>
            <if test="sfOrganizationId != null">#{sfOrganizationId},</if>
            <if test="instanceUrl != null">#{instanceUrl},</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>
         </trim>
    </insert>

    <update id="updateDataiSfLoginSession" parameterType="DataiSfLoginSession">
        update datai_sf_login_session
        <trim prefix="SET" suffixOverrides=",">
            <if test="username != null and username != ''">username = #{username},</if>
            <if test="loginType != null and loginType != ''">login_type = #{loginType},</if>
            <if test="status != null and status != ''">status = #{status},</if>
            <if test="loginTime != null">login_time = #{loginTime},</if>
            <if test="expireTime != null">expire_time = #{expireTime},</if>
            <if test="lastActivityTime != null">last_activity_time = #{lastActivityTime},</if>
            <if test="loginIp != null">login_ip = #{loginIp},</if>
            <if test="deviceInfo != null">device_info = #{deviceInfo},</if>
            <if test="browserInfo != null">browser_info = #{browserInfo},</if>
            <if test="sessionId != null">session_id = #{sessionId},</if>
            <if test="deptId != null">dept_id = #{deptId},</if>
            <if test="sfUserId != null">sf_user_id = #{sfUserId},</if>
            <if test="sfOrganizationId != null">sf_organization_id = #{sfOrganizationId},</if>
            <if test="instanceUrl != null">instance_url = #{instanceUrl},</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>
        </trim>
        where datai_sf_login_session.id = #{id}
    </update>

    <delete id="deleteDataiSfLoginSessionById" parameterType="Long">
        delete from datai_sf_login_session where id = #{id}
    </delete>

    <delete id="deleteDataiSfLoginSessionByIds" parameterType="String">
        delete from datai_sf_login_session where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
</mapper>