<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.caliverse.admin.domain.dao.admin.TokenMapper">

    <select id="findAllValidTokenByUser" resultType="com.caliverse.admin.domain.entity.Token" parameterType="java.lang.Long">
        SELECT * FROM token WHERE admin_id = #{id} AND expired = false
    </select>

    <select id="findByToken" resultType="com.caliverse.admin.domain.entity.Token" parameterType="java.lang.String">
        SELECT * FROM token WHERE token = #{token}
    </select>

    <insert id="save" parameterType="com.caliverse.admin.domain.entity.Token">
        INSERT INTO token (token, token_type, revoked, expired, admin_id)
        VALUES (#{token}, #{tokenType}, #{revoked}, #{expired}, #{adminId})
    </insert>

    <update id="updateToken" parameterType="com.caliverse.admin.domain.entity.Token">
        update token set revoked = #{revoked}, expired = #{expired} where id = #{id}
    </update>

    <select id="getCount" resultType="java.lang.Integer" parameterType="java.lang.Long">
        SELECT count(*) FROM token WHERE admin_id = #{id} AND expired = false
    </select>

    <update id="updateResetToken" parameterType="java.lang.Long">
        UPDATE token
        SET expired = true,
            revoked = true
        WHERE admin_id = #{id}
        AND expired = false
        AND id &lt; (SELECT MAX(id) FROM token WHERE admin_id = #{id})
    </update>

</mapper>
