package com.cf.imes.module.system.dal.redis.oauth2;

import com.alibaba.fastjson.JSON;
import com.cf.imes.framework.common.util.collection.CollectionUtils;
import com.cf.imes.framework.common.util.json.JsonUtils;
import com.cf.imes.module.system.dal.dataobject.oauth2.OAuth2AccessTokenDO;
import com.cf.imes.module.system.dal.redis.RedisKeyConstants;
import org.springframework.data.redis.core.Cursor;
import org.springframework.data.redis.core.ScanOptions;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Repository;

import jakarta.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.TimeUnit;

import static com.cf.imes.module.system.dal.redis.RedisKeyConstants.OAUTH2_ACCESS_TOKEN;

/**
 * {@link OAuth2AccessTokenDO} 的 RedisDAO
 *
 * @author 晨丰科技
 */
@Repository
public class OAuth2AccessTokenRedisDAO {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 不带key获取
     *
     * @param accessToken
     * @return
     */
    public OAuth2AccessTokenDO get(String accessToken) {
        String redisKey = formatKey(accessToken);
        return JsonUtils.parseObject(stringRedisTemplate.opsForValue().get(redisKey), OAuth2AccessTokenDO.class);
    }

    /**
     * 带key获取，oauth2_access_token:xxxx
     *
     * @param accessToken
     * @return
     */
    public OAuth2AccessTokenDO getWithKey(String accessToken) {
        return JsonUtils.parseObject(stringRedisTemplate.opsForValue().get(accessToken), OAuth2AccessTokenDO.class);
    }

    public void set(OAuth2AccessTokenDO accessTokenDO) {
        String redisKey = formatKey(accessTokenDO.getAccessToken());
        // 清理多余字段，避免缓存
        accessTokenDO.setUpdater(null).setUpdateTime(null).setCreateTime(null).setCreator(null).setDeleted(null);
        // 创建有效期1天的token
        String jsonString = JSON.toJSONString(accessTokenDO);
        stringRedisTemplate.opsForValue().set(redisKey, jsonString, 1, TimeUnit.DAYS);
    }

    /**
     * 不带key删除
     *
     * @param accessToken
     */
    public void delete(String accessToken) {
        String redisKey = formatKey(accessToken);
        stringRedisTemplate.delete(redisKey);
    }

    /**
     * 带key删除，oauth2_access_token:xxxx
     *
     * @param accessToken
     * @return
     */
    public void deleteWithKey(String accessToken) {
        stringRedisTemplate.delete(accessToken);
    }

    public void deleteList(Collection<String> accessTokens) {
        List<String> redisKeys = CollectionUtils.convertList(accessTokens, OAuth2AccessTokenRedisDAO::formatKey);
        stringRedisTemplate.delete(redisKeys);
    }

    /**
     * 设置过期时间
     *
     * @param accessToken 访问令牌
     * @param timeout     过期时间
     * @param unit        时间单位
     */
    public void expire(String accessToken, long timeout, final TimeUnit unit) {
        stringRedisTemplate.expire(formatKey(accessToken), timeout, unit);
    }

    private static String formatKey(String accessToken) {
        return String.format(RedisKeyConstants.OAUTH2_ACCESS_TOKEN, accessToken);
    }

    /**
     * 构建token游标，使用时轮训一次获取两百个token
     *
     * @return
     */
    public Cursor<String> scan(){
        return stringRedisTemplate.scan(ScanOptions.scanOptions().match(String.format(OAUTH2_ACCESS_TOKEN, "*")).count(200).build());
    }
}
