<?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.sso.dao.mapper.SsoOnlineUserMapper">

    <resultMap id="BaseResultMap" type="com.sso.dao.entity.SsoOnlineUser">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <result column="request_id" property="requestId" jdbcType="VARCHAR"/>
        <result column="user_id" property="userId" jdbcType="BIGINT"/>
        <result column="source_ip" property="sourceIp" jdbcType="VARCHAR"/>
        <result column="source_address" property="sourceAddress" jdbcType="VARCHAR"/>
        <result column="browser_name" property="browserName" jdbcType="VARCHAR"/>
        <result column="operate_system" property="operateSystem" jdbcType="VARCHAR"/>
        <result column="login_time" property="loginTime" jdbcType="BIGINT"/>
        <result column="login_out_time" property="loginOutTime" jdbcType="BIGINT"/>
        <result column="expire_time" property="expireTime" jdbcType="BIGINT"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
        <result column="update_time" property="updateTime" jdbcType="TIMESTAMP"/>
    </resultMap>


    <resultMap id="CustomResultMap" type="com.sso.dao.dto.SsoOnlineUserDTO">
        <result column="request_id" property="requestId" jdbcType="VARCHAR"/>
        <result column="user_id" property="userId" jdbcType="BIGINT"/>
        <result column="username" property="username" jdbcType="VARCHAR"/>
        <result column="password" property="password" jdbcType="VARCHAR"/>
        <result column="nick_name" property="nickName" jdbcType="VARCHAR"/>
        <result column="real_name" property="realName" jdbcType="VARCHAR"/>
        <result column="source_ip" property="sourceIp" jdbcType="VARCHAR"/>
        <result column="source_address" property="sourceAddress" jdbcType="VARCHAR"/>
        <result column="browser_name" property="browserName" jdbcType="VARCHAR"/>
        <result column="operate_system" property="operateSystem" jdbcType="VARCHAR"/>
        <result column="login_time" property="loginTime" jdbcType="BIGINT"/>
        <result column="login_out_time" property="loginOutTime" jdbcType="BIGINT"/>
        <result column="expire_time" property="expireTime" jdbcType="BIGINT"/>
    </resultMap>

    <sql id="Base_Column_List">
      id,
      request_id,
      user_id,
      source_ip,
      source_address,
      browser_name,
      operate_system,
      login_time,
      login_out_time,
      expire_time,
      create_time,
      update_time
    </sql>

    <select id="getByRequestId" resultMap="BaseResultMap" parameterType="java.lang.Long">
        SELECT
        <include refid="Base_Column_List"/>
        FROM sso_online_user
        WHERE request_id = #{requestId,jdbcType=VARCHAR}
    </select>

    <insert id="insert" parameterType="com.sso.dao.entity.SsoOnlineUser">
      INSERT INTO sso_online_user
       (
        id,
        request_id,
        user_id,
        source_ip,
        source_address,
        browser_name,
        operate_system,
        login_time,
        login_out_time,
        expire_time,
        create_time,
        update_time
       )
      values
       (
          #{id,jdbcType=BIGINT},
          #{requestId,jdbcType=VARCHAR},
          #{userId,jdbcType=BIGINT},
          #{sourceIp,jdbcType=VARCHAR},
          #{sourceAddress,jdbcType=VARCHAR},
          #{browserName,jdbcType=VARCHAR},
          #{operateSystem,jdbcType=VARCHAR},
          #{loginTime,jdbcType=BIGINT},
          #{loginOutTime,jdbcType=BIGINT},
          #{expireTime,jdbcType=BIGINT},
          #{createTime,jdbcType=TIMESTAMP},
          #{updateTime,jdbcType=TIMESTAMP}
       )
    </insert>

    <insert id="insertSelective" parameterType="com.sso.dao.entity.SsoOnlineUser">
        INSERT INTO sso_online_user
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">
                id,
            </if>
            <if test="requestId != null">
                request_id,
            </if>
            <if test="userId != null">
                user_id,
            </if>
            <if test="sourceIp != null">
                source_ip,
            </if>
            <if test="sourceAddress != null">
                source_address,
            </if>
            <if test="browserName != null">
                browser_name,
            </if>
            <if test="operateSystem != null">
                operate_system,
            </if>
            <if test="loginTime != null">
                login_time,
            </if>
            <if test="loginOutTime != null">
                login_out_time,
            </if>
            <if test="expireTime != null">
                expire_time,
            </if>
            <if test="createTime != null">
                create_time,
            </if>
            <if test="updateTime != null">
                update_time,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">
                #{id,jdbcType=BIGINT},
            </if>
            <if test="requestId != null">
                #{requestId,jdbcType=VARCHAR},
            </if>
            <if test="userId != null">
                #{userId,jdbcType=BIGINT},
            </if>
            <if test="sourceIp != null">
                #{sourceIp,jdbcType=VARCHAR},
            </if>
            <if test="sourceAddress != null">
                #{sourceAddress,jdbcType=VARCHAR},
            </if>
            <if test="browserName != null">
                #{browserName,jdbcType=VARCHAR},
            </if>
            <if test="operateSystem != null">
                #{operateSystem,jdbcType=VARCHAR},
            </if>
            <if test="loginTime != null">
                #{loginTime,jdbcType=BIGINT},
            </if>
            <if test="loginOutTime != null">
                #{loginOutTime,jdbcType=BIGINT},
            </if>
            <if test="expireTime != null">
                #{expireTime,jdbcType=BIGINT},
            </if>
            <if test="createTime != null">
                #{createTime,jdbcType=TIMESTAMP},
            </if>
            <if test="updateTime != null">
                #{updateTime,jdbcType=TIMESTAMP},
            </if>
        </trim>
    </insert>

    <delete id="deleteByPrimaryKey" parameterType="java.lang.Long">
      DELETE FROM sso_online_user
      WHERE id = #{id,jdbcType=BIGINT}
    </delete>

    <update id="updateByPrimaryKey" parameterType="com.sso.dao.entity.SsoOnlineUser">
      UPDATE sso_online_user
      SET
       request_id = #{requestId,jdbcType=VARCHAR},
       user_id = #{userId,jdbcType=BIGINT},
       source_ip = #{sourceIp,jdbcType=VARCHAR},
       source_address = #{sourceAddress,jdbcType=VARCHAR},
       browser_name = #{browserName,jdbcType=VARCHAR},
       operate_system = #{operateSystem,jdbcType=VARCHAR},
       login_time = #{loginTime,jdbcType=BIGINT},
       login_out_time = #{loginOutTime,jdbcType=BIGINT},
       expire_time = #{expireTime,jdbcType=BIGINT},
       create_time = #{createTime,jdbcType=TIMESTAMP},
       update_time = #{updateTime,jdbcType=TIMESTAMP}
      WHERE id = #{id,jdbcType=BIGINT}
    </update>

    <update id="updateByPrimaryKeySelective" parameterType="com.sso.dao.entity.SsoOnlineUser">
        UPDATE sso_online_user
        <set>
            <if test="requestId != null">
                request_id = #{requestId,jdbcType=VARCHAR},
            </if>
            <if test="userId != null">
                user_id = #{userId,jdbcType=BIGINT},
            </if>
            <if test="sourceIp != null">
                source_ip = #{sourceIp,jdbcType=VARCHAR},
            </if>
            <if test="sourceAddress != null">
                source_address = #{sourceAddress,jdbcType=VARCHAR},
            </if>
            <if test="browserName != null">
                browser_name = #{browserName,jdbcType=VARCHAR},
            </if>
            <if test="operateSystem != null">
                operate_system = #{operateSystem,jdbcType=VARCHAR},
            </if>
            <if test="loginTime != null">
                login_time = #{loginTime,jdbcType=BIGINT},
            </if>
            <if test="loginOutTime != null">
                login_out_time = #{loginOutTime,jdbcType=BIGINT},
            </if>
            <if test="expireTime != null">
                expire_time = #{expireTime,jdbcType=BIGINT},
            </if>
            <if test="createTime != null">
                create_time = #{createTime,jdbcType=TIMESTAMP},
            </if>
            <if test="updateTime != null">
                update_time = #{updateTime,jdbcType=TIMESTAMP},
            </if>
        </set>
        WHERE id = #{id,jdbcType=BIGINT}
    </update>

    <select id="countByCondition" resultType="java.lang.Integer" parameterType="com.sso.dao.query.UserOnlineQuery">
        SELECT
        COUNT(*)
        FROM  sso_online_user a
        INNER JOIN sso_user b on a.user_id=b.user_id
        <where>
            <if test="expireStartTime != null ">
                AND a.expire_time <![CDATA[ >= ]]> #{expireStartTime,jdbcType=BIGINT}
            </if>
            <if test="usernameLike != null">
                AND b.username LIKE CONCAT(#{usernameLike,jdbcType=VARCHAR},'%')
            </if>
            <if test="phoneLike != null">
                AND b.phone LIKE CONCAT(#{phoneLike,jdbcType=VARCHAR},'%')
            </if>
            <if test="loginStartTime != null ">
                AND a.login_time <![CDATA[ >= ]]> #{loginStartTime,jdbcType=BIGINT}
            </if>
            <if test="loginStartTime != null ">
                AND a.login_time <![CDATA[ <= ]]> #{loginEndTime,jdbcType=BIGINT}
            </if>
        </where>
    </select>

    <select id="listPageByCondition" resultMap="CustomResultMap" parameterType="com.sso.dao.query.UserOnlineQuery">
        SELECT
        a.request_id,
        a.user_id,
        b.username,
        b.nick_name,
        b.real_name,
        b.phone,
        a.source_ip,
        a.source_address,
        a.browser_name,
        a.operate_system,
        a.login_time,
        a.login_out_time,
        a.expire_time
        FROM sso_online_user a
        INNER JOIN sso_user b on a.user_id=b.user_id
        <where>
            <if test="expireStartTime != null ">
                AND a.expire_time <![CDATA[ >= ]]> #{expireStartTime,jdbcType=BIGINT}
            </if>
            <if test="usernameLike != null">
                AND b.username LIKE CONCAT(#{usernameLike,jdbcType=VARCHAR},'%')
            </if>
            <if test="phoneLike != null">
                AND b.phone LIKE CONCAT(#{phoneLike,jdbcType=VARCHAR},'%')
            </if>
            <if test="loginStartTime != null ">
                AND a.login_time <![CDATA[ >= ]]> #{loginStartTime,jdbcType=BIGINT}
            </if>
            <if test="loginStartTime != null ">
                AND a.login_time <![CDATA[ <= ]]> #{loginEndTime,jdbcType=BIGINT}
            </if>
        </where>
        ORDER BY a.login_time desc
        LIMIT #{startRow}, #{pageSize}
    </select>

    <update id="refreshExpireTimeByRequestId">
      UPDATE sso_online_user
      SET
       expire_time = #{expireTime,jdbcType=BIGINT}
       WHERE request_id = #{requestId,jdbcType=VARCHAR}
    </update>

    <update id="updateByLogoOut">
      UPDATE sso_online_user
      SET
       expire_time = #{expireTime,jdbcType=BIGINT},
       login_out_time = #{loginOutTime,jdbcType=BIGINT}
      WHERE request_id = #{requestId,jdbcType=VARCHAR}
    </update>

</mapper>
