<?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="cn.com.countrygarden.bi.main.dao.AccessTokenMapper">
    <resultMap id="AccessTokenResultMap" type="cn.com.countrygarden.bi.main.model.AccessToken">
        <id column="id" jdbcType="INTEGER" property="id"/>
        <result column="access_token" jdbcType="VARCHAR" property="accessToken"/>
        <result column="salt" jdbcType="VARCHAR" property="salt"/>
        <result column="user_code" jdbcType="VARCHAR" property="userCode"/>
    </resultMap>

    <!-- 根据 accessToken 查 user_code -->
    <select id="getAccessToken" parameterType="Map" resultType="AccessToken" resultMap="AccessTokenResultMap">
    <![CDATA[
            SELECT id, access_token, salt, user_code
            FROM access_token t
            WHERE t.access_token = #{accessToken}
        ]]>
    </select>


    <!-- 更新 access_token  -->
    <select id="insertOrUpdate" parameterType="Map" resultType="AccessToken" resultMap="AccessTokenResultMap">
        <![CDATA[
            INSERT INTO access_token(user_code, access_token, salt)
            VALUES(#{userCode}, #{accessToken}, #{salt})
            ON DUPLICATE KEY UPDATE access_token = #{accessToken}, salt = #{salt}
        ]]>
    </select>

    <select id="updateAccessToken" parameterType="Map" resultType="AccessToken" resultMap="AccessTokenResultMap">
        <![CDATA[
            UPDATE access_token
            SET access_token = "", salt = ""
            WHERE access_token = #{accessToken} AND user_code = #{userCode}
        ]]>
    </select>

    <select id="findByUserCode" resultMap="AccessTokenResultMap">
        <![CDATA[
            SELECT id, access_token, salt, user_code
            FROM access_token t
            WHERE t.user_code = #{userCode}
        ]]>
    </select>
</mapper>