package com.chinaweal.aiccs.org.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.chinaweal.aiccs.org.entity.OauthAuthorizationCode;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;

/**
 * <p>
 * OAuth授权码 Mapper 接口
 * </p>
 *
 * @author chinaweal
 * @since 2025-11-21
 */
public interface OauthAuthorizationCodeMapper extends BaseMapper<OauthAuthorizationCode> {

    /**
     * 根据授权码查询信息
     *
     * @param authorizationCode 授权码
     * @return 授权码信息
     */
    OauthAuthorizationCode selectByAuthorizationCode(@Param("authorizationCode") String authorizationCode);

    /**
     * 根据授权码和客户端ID查询信息
     *
     * @param authorizationCode 授权码
     * @param clientId          客户端ID
     * @return 授权码信息
     */
    OauthAuthorizationCode selectByCodeAndClientId(@Param("authorizationCode") String authorizationCode,
                                                   @Param("clientId") String clientId);

    /**
     * 将授权码标记为已使用
     *
     * @param authorizationCode 授权码
     * @param clientId          客户端id
     * @return 更新行数
     */
    @Update("UPDATE oauth_authorization_code SET is_used = '1', is_active = '0', update_time = NOW() WHERE authorization_code = #{authorizationCode} and client_id = #{clientId}")
    int markAsUsed(@Param("authorizationCode") String authorizationCode, @Param("clientId") String clientId);

    /**
     * 将过期的授权码标记为无效
     *
     * @return 更新行数
     */
    @Update("UPDATE oauth_authorization_code SET is_active = '0', update_time = NOW() WHERE expires_at < NOW() AND is_active = '1'")
    int markExpiredAsInactive();

}