package com.jy.framework.satoken;

import cn.dev33.satoken.dao.SaTokenDao;
import cn.dev33.satoken.util.SaFoxUtil;
import com.jy.framework.redis.RedisCache;

import java.time.Duration;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * Sa-Token持久层接口
 */
public class JySaTokenDao implements SaTokenDao {

    /**
     * 获取Value，如无返空
     */
    @Override
    public String get(String key) {
        return RedisCache.get(key);
    }

    /**
     * 写入Value，并设定存活时间 (单位: 秒)
     */
    @Override
    public void set(String key, String value, long timeout) {
        if (timeout > 0) {
            RedisCache.set(key, value, Duration.ofSeconds(timeout));
        } else if (timeout == -1) {
            RedisCache.set(key, value);
        }
    }

    /**
     * 修修改指定key-value键值对 (过期时间不变)
     */
    @Override
    public void update(String key, String value) {
        long expire = RedisCache.getExpire(key, TimeUnit.SECONDS);
        if (expire > 0) {
            RedisCache.set(key, value, Duration.ofSeconds(expire));
        }
    }

    /**
     * 删除Value
     */
    @Override
    public void delete(String key) {
        RedisCache.delete(key);
    }

    /**
     * 获取Value的剩余存活时间 (单位: 秒)
     */
    @Override
    public long getTimeout(String key) {
        return RedisCache.getExpire(key, TimeUnit.SECONDS);
    }

    /**
     * 修改Value的剩余存活时间 (单位: 秒)
     */
    @Override
    public void updateTimeout(String key, long timeout) {
        RedisCache.setExpire(key, Duration.ofSeconds(timeout));
    }

    /**
     * 获取Object，如无返空
     */
    @Override
    public Object getObject(String key) {
        return RedisCache.get(key);
    }

    /**
     * 写入Object，并设定存活时间 (单位: 秒)
     */
    @Override
    public void setObject(String key, Object object, long timeout) {
        if (timeout > 0) {
            RedisCache.set(key, object, Duration.ofSeconds(timeout));
        } else if (timeout == -1) {
            RedisCache.set(key, object);
        }
    }

    /**
     * 更新Object (过期时间不变)
     */
    @Override
    public void updateObject(String key, Object object) {
        long expire = RedisCache.getExpire(key, TimeUnit.SECONDS);
        if (expire > 0) {
            RedisCache.set(key, object, Duration.ofSeconds(expire));
        }
    }

    /**
     * 删除Object
     */
    @Override
    public void deleteObject(String key) {
        RedisCache.delete(key);
    }

    /**
     * 获取Object的剩余存活时间 (单位: 秒)
     */
    @Override
    public long getObjectTimeout(String key) {
        return RedisCache.getExpire(key, TimeUnit.SECONDS);
    }

    /**
     * 修改Object的剩余存活时间 (单位: 秒)
     */
    @Override
    public void updateObjectTimeout(String key, long timeout) {
        RedisCache.setExpire(key, Duration.ofSeconds(timeout));
    }

    /**
     * 搜索数据
     */
    @Override
    public List<String> searchData(String prefix, String keyword, int start, int size, boolean sortType) {
        String keyStr = prefix + "*" + keyword + "*";
        return SaFoxUtil.searchList(RedisCache.scan(keyStr), start, size, sortType);
    }

}
