package com.ruoyi.sensitive.cache;

import com.ruoyi.common.constant.DateConstant;
import org.redisson.api.RBucket;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Component
public class LastUpdateTimeCache {
    @Autowired
    private RedissonClient redissonClient;
    private String getKey(){
        return "sensitive-lastUpdateTime";
    }

    private String getRefreshKey(){
        return "sensitive-lastRefreshTime";
    }

    public void setRefreshTime(){
        String key = getRefreshKey();
        RBucket<LocalDateTime> bucket = redissonClient.getBucket(key);
        bucket.set(LocalDateTime.now());
    }

    public LocalDateTime getRefreshTime(){
        String key = getRefreshKey();
        RBucket<String> bucket = redissonClient.getBucket(key);
        String s = bucket.get();
        if(s == null){
            return null;
        }
        return LocalDateTime.parse(s, DateTimeFormatter.ofPattern(DateConstant.PATTERN_DATETIME));
    }


    public LocalDateTime getUpdateTime(){
        String key = getKey();
        RBucket<String> bucket = redissonClient.getBucket(key);
        String s = bucket.get();
        if(s == null){
            return null;
        }
        return LocalDateTime.parse(s, DateTimeFormatter.ofPattern(DateConstant.PATTERN_DATETIME));
    }

    public void setUpdateTime(){
        String key = getKey();
        RBucket<LocalDateTime> bucket = redissonClient.getBucket(key);
        bucket.set(LocalDateTime.now());
    }
}
