package com.ningdatech.carapi.redis;

import java.time.Duration;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Set;

import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import com.ningdatech.cache.model.cache.CacheKey;
import com.ningdatech.cache.repository.CachePlusOps;
import com.ningdatech.carapi.AppTests;
import com.ningdatech.carapi.scheduler.contants.TaskConstant;

/**
 * @Classname RedisTest
 * @Description
 * @Date 2023/3/1 16:33
 * @Author PoffyZhang
 */
public class RedisTest extends AppTests {

    @Autowired
    private CachePlusOps cachePlusOps;

    @Test
    public void remove (){
        // 模糊匹配所有以keyword:开头的所有key值

        Set<String> keys = cachePlusOps.keys("cm:"+"*");

        System.out.println(keys.size());
    }

    @Test
    public void get (){
        CacheKey key = new CacheKey(TaskConstant.RedisKey.IRS_ACCIDENT_QUERY_REQUEST_SECRET);
        String requestSecret = cachePlusOps.get(key);
        System.out.println(requestSecret);
        requestSecret = "abcdefg123456";
        cachePlusOps.set(key,requestSecret,Boolean.TRUE);
        String requestSecret2 = cachePlusOps.get(key);
        System.out.println(requestSecret2);
    }

    @Test
    public void getValue (){
        String timeStamp = "1689081083";
        // 将时间戳转换为Instant对象
        Instant instant = Instant.ofEpochSecond(Long.parseLong(timeStamp));
        // 将Instant对象转换为LocalDateTime对象
        LocalDateTime startTime = LocalDateTime.now();
        LocalDateTime endTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
        Duration minuteDuration = Duration.between(endTime,startTime);
        long minute = minuteDuration.toMinutes();
        System.out.println( minute);
    }

    @Test
    public void removeBatch (){
        // 模糊匹配所有以keyword:开头的所有key值
        Set<String> keys = cachePlusOps.keys("*_online_index");
        for (String key : keys) {
            cachePlusOps.del(key);
        }
        System.out.println(keys.size());
    }
}
