<?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.ctop.common.module.mapper.CtopOauthTokenMapper">

    <resultMap id="BaseResultMap" type="cn.com.ctop.common.module.entity.CtopOauthToken">
        <result column="id" property="id" jdbcType="BIGINT"/>
        <result column="media_id" property="mediaId" jdbcType="VARCHAR"/>
        <result column="advertiser_id" property="advertiserId" jdbcType="VARCHAR"/>
        <result column="account_id" property="accountId" jdbcType="BIGINT"/>
        <result column="access_token" property="accessToken" jdbcType="VARCHAR"/>
        <result column="refresh_token" property="refreshToken" jdbcType="VARCHAR"/>
        <result column="access_token_expires_in" property="accessTokenExpiresIn" jdbcType="TIMESTAMP"/>
        <result column="refresh_token_expires_in" property="refreshTokenExpiresIn" jdbcType="TIMESTAMP"/>
        <result column="agent_type" property="agentType" jdbcType="INTEGER"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
        <result column="update_time" property="updateTime" jdbcType="TIMESTAMP"/>
    </resultMap>

    <sql id="Base_Column_List">
        id,media_id,advertiser_id,account_id,access_token,refresh_token,access_token_expires_in,refresh_token_expires_in,agent_type,create_time,update_time
   </sql>

    <select id="selectByteDanceTokens" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from ctop_oauth_token
        where media_id = #{mediaId}
        group by access_token
    </select>

    <select id="selectKuaiShouTokens" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from ctop_oauth_token
        where media_id = #{mediaId}
        group by account_id
    </select>


    <update id="updateByPrimaryKeySelective" parameterType="cn.com.ctop.common.module.entity.CtopOauthToken">
        update ctop_oauth_token
        <set>

            <if test="mediaId != null">
                media_id = #{mediaId},
            </if>

            <if test="advertiserId != null">
                advertiser_id = #{advertiserId},
            </if>

            <if test="accessToken != null">
                access_token = #{accessToken},
            </if>

            <if test="accessTokenExpiresIn != null">
                access_token_expires_in = #{accessTokenExpiresIn},
            </if>

            <if test="refreshToken != null">
                refresh_token = #{refreshToken,jdbcType=VARCHAR},
            </if>

            <if test="refreshTokenExpiresIn != null">
                refresh_token_expires_in = #{refreshTokenExpiresIn},
            </if>

            <if test="createTime != null">
                create_time = #{createTime},
            </if>

            <if test="updateTime != null">
                update_time = #{updateTime},
            </if>
        </set>
        where account_id = #{accountId}
    </update>

    <select id="selectByAccountId" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from ctop_oauth_token
        where account_id = #{accountId}
        order by create_time desc
        limit 1
    </select>


    <select id="selectKuaiShouToken" resultType="cn.com.ctop.common.module.entity.CtopOauthToken">
        select  t1.*
        from ctop_oauth_token t1
        left join ctop_user_allocation t2
        on t1.account_id = t2.account_id
        where t2.account_status = 0
        and t1.media_id = 2

    </select>

    <select id="selectKuaiShouTokenBefore" resultType="cn.com.ctop.common.module.entity.CtopOauthToken">
        select  t1.*
        from ctop_oauth_token t1
        left join ctop_user_allocation t2
        on t1.account_id = t2.account_id
        where t1.media_id = 2
        and date_format(t2.create_time, '%Y-%m-%d') = date_sub(curdate(),interval 1 day)
    </select>

    <select id="selectToutiaoToken" resultType="cn.com.ctop.common.module.entity.CtopOauthToken">
        select t1.*
        from ctop_oauth_token t1
        left join ctop_user_allocation t2
        on t1.account_id = t2.account_id
        where t1.media_id in (1,3) and t2.account_status = 0
        group by account_id
    </select>

    <select id="getAccessTokenByAccountIdAndMediaId" resultType="cn.com.ctop.common.module.entity.CtopOauthToken">
        select
        *
        from
        ctop_oauth_token
        where media_id = #{mediaId}
        and account_id = #{accountId}
    </select>

    <!-- 根据mediaType 和 账户绑定助手时间createTime 获取accountId -->
    <select id="getToutiaoTokenByCreateTime" resultType="cn.com.ctop.common.module.entity.CtopOauthToken">
            select t1.*
        from ctop_oauth_token t1
        left join ctop_user_allocation t2
        on t1.account_id = t2.account_id
        where t2.account_status = 0
        and t1.media_id in (1,3)
        and date_format(t2.create_time, '%Y-%m-%d') = #{createTime}
        group by account_id
    </select>

</mapper>
