<?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.platform.dao.ApiTokenMapper">

    <resultMap type="com.platform.entity.TokenEntity" id="tbTokenMap">
        <result property="userId" column="user_id"/>
        <result property="token" column="token"/>
        <result property="expireTime" column="expire_time"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>

    <select id="queryObject" resultType="com.platform.entity.TokenEntity">
		select
			user_id,
			token,
			expire_time,
			update_time
		from tb_token
		where user_id = #{id}
	</select>

    <select id="queryByToken" resultType="com.platform.entity.TokenEntity">
        select
        user_id,
        token,
        expire_time,
        update_time
        from tb_token
        where token = #{token}
    </select>

    <select id="queryByUserId" resultType="com.platform.entity.TokenEntity">
        select
        user_id,
        token,
        expire_time,
        update_time
        from tb_token
        where user_id = #{userId}
    </select>

    <select id="queryList" resultType="com.platform.entity.TokenEntity">
        select
        user_id,
        token,
        expire_time,
        update_time
        from tb_token
        WHERE 1=1
        <choose>
            <when test="sidx != null and sidx.trim() != ''">
                order by ${sidx} ${order}
            </when>
            <otherwise>
                order by user_id desc
            </otherwise>
        </choose>
        <if test="offset != null and limit != null">
            limit #{offset}, #{limit}
        </if>
    </select>

    <select id="queryTotal" resultType="int">
		select count(*) from tb_token
		WHERE 1=1
	</select>

    <insert id="save" parameterType="com.platform.entity.TokenEntity">
		insert into tb_token(
			`user_id`,
			`token`,
			`expire_time`,
			`update_time`)
		values(
			#{userId},
			#{token},
			#{expireTime},
			#{updateTime})
	</insert>

    <update id="update" parameterType="com.platform.entity.TokenEntity">
        update tb_token
        <set>
            <if test="token != null">`token` = #{token},</if>
            <if test="expireTime != null">`expire_time` = #{expireTime},</if>
            <if test="updateTime != null">`update_time` = #{updateTime}</if>
        </set>
        where user_id = #{userId}
    </update>

    <delete id="delete">
		delete from tb_token where user_id = #{value}
	</delete>

    <delete id="deleteBatch">
        delete from tb_token where user_id in
        <foreach item="userId" collection="array" open="(" separator="," close=")">
            #{userId}
        </foreach>
    </delete>

</mapper>