package com.css.txw.sso.mapper.oauth2;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.css.txw.sso.pojo.domain.oauth2.OAuth2RefreshTokenDO;
import com.css.ggzc.framework.mybatis.core.mapper.BaseMapperX;
import com.css.ggzc.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.css.ggzc.framework.mybatis.core.query.QueryWrapperX;
import org.apache.ibatis.annotations.Mapper;

import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface OAuth2RefreshTokenMapper extends BaseMapperX<OAuth2RefreshTokenDO> {

    default int deleteByRefreshToken(String refreshToken) {
        return delete(new LambdaQueryWrapperX<OAuth2RefreshTokenDO>()
                .eq(OAuth2RefreshTokenDO::getRefreshToken, refreshToken));
    }

    default OAuth2RefreshTokenDO selectByRefreshToken(String refreshToken) {
        return selectOne(OAuth2RefreshTokenDO::getRefreshToken, refreshToken);
    }

    default Integer updateJguuid(String refreshToken,String currentJguuid){
        UpdateWrapper<OAuth2RefreshTokenDO> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("jguuid_1",currentJguuid);
        updateWrapper.set("xgrq", LocalDateTime.now());
        updateWrapper.set("xgrsfid","SSO");
        updateWrapper.eq("refresh_token",refreshToken);
        return update(updateWrapper);
    }

    default Integer deleteByTokens(List<String> tokens){
        QueryWrapperX<OAuth2RefreshTokenDO> wrapperX = new QueryWrapperX<>();
        wrapperX.lambda().in(OAuth2RefreshTokenDO::getRefreshToken, tokens);
        return delete(wrapperX);
    }

}
