package com.elab.redis.spring;

import com.elab.redis.CacheTemplate;
import com.elab.redis.RedisSpringBoot;
import com.google.common.base.Splitter;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.*;
import org.springframework.data.redis.serializer.RedisSerializer;

import java.io.IOException;
import java.util.*;

/**
 * springData
 *
 * @author ： liukx
 * @time ： 2020/7/15 - 11:12
 */
public class SerializationDataTest extends RedisSpringBoot {

    @Autowired
    private CacheTemplate cacheTemplate;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Test
    public void testEntryis(){
        Map<Object, Object> entries =
            cacheTemplate.getRedisTemplate().opsForHash().entries("data_center:material_labels_key:6");

        System.out.println(entries);

    }

    @Test
    public void serializerTest() {
        RedisTemplate<String, Object> redisTemplate = cacheTemplate.getRedisTemplate();
        //        Set<Object> lyx =
        //                setOperations.members("lyx");
        //        List<String> collect = lyx.stream().map(String::valueOf).collect(Collectors.toList());
        //        redisTemplate.delete(collect);
        String typeKey = "user:organize:userInfo:10018:13602651614";
        String classKey = "user:organize:userInfo:10018:18154115526";

        // 默认当前版本是 [GenericFastJsonRedisSerializer]
        //        Object classObject = redisTemplate.opsForValue().get(classKey);
        //        Object typeObject = redisTemplate.opsForValue().get(typeKey);
        //        String tmpKey = "user:organize:userInfo:10018:testKey2";
        //        redisTemplate.opsForValue().set(tmpKey, "1");
        //        redisTemplate.opsForValue().increment(tmpKey, 10);
        //        // 之前版本是 [GenericJackson2JsonRedisSerializer]
        //        redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        //        Object classObject1 = redisTemplate.opsForValue().get(classKey);
        //        Object typeObject1 = redisTemplate.opsForValue().get(typeKey);
        //        tmpKey = "user:organize:userInfo:10018:testKey";
        //        redisTemplate.opsForValue().set(tmpKey, "1");
        //        redisTemplate.opsForValue().increment(tmpKey, 10);
        //        // 兼容版本
        //        redisTemplate.setValueSerializer(new CompatibilityJsonRedisSerializer());
        //        Object classObject2 = redisTemplate.opsForValue().get(classKey);
        //        Object typeObject2 = redisTemplate.opsForValue().get(typeKey);
        //
        //        if (classObject instanceof OrganizeInfoModel) {
        //            System.out.println(true);
        //        }

        //        redisTemplate.setHashKeySerializer(new GenericJackson2JsonRedisSerializer());
        //        redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
        redisTemplate.opsForHash().put("aaaa", 10, 1000L);
        Object aaaa = redisTemplate.opsForHash().get("aaaa", 10);
        System.out.println("=================" + aaaa);
    }

    @Test
    public void set() throws IOException {

        RedisTemplate<String, Object> redisTemplate = cacheTemplate.getRedisTemplate();
        //        Set<Object> lyx =
        //                setOperations.members("lyx");
        //        List<String> collect = lyx.stream().map(String::valueOf).collect(Collectors.toList());
        //        redisTemplate.delete(collect);

        //        cacheTemplate.set().add("aaa","a","b","c");
        //
        //
        //        RLock xxx = cacheTemplate.getRedissonClient().getLock("xxx");
        //        cacheTemplate.string().set("abc", "cccc");
        //        Object abc = cacheTemplate.string().get("abc");
        //        System.out.println("--->" + abc);
        //
        //        redisTemplate.opsForValue().set("kkx", "abc");
        //
        //        Map<String, String> map = new HashMap<>();
        //        map.put("lkx", "xxx");
        //        map.put("xxx", "sss");
        //        redisTemplate.opsForValue().set("obj", map);
        //        Map<String, String> xiong = (Map<String, String>) redisTemplate.opsForValue().get("obj");
        //        System.out.println(xiong);

        //        // 存储值
        //        cacheTemplate.map().put("aaa", "a-key", "v-value");
        //        cacheTemplate.map().put("aaa", "b-key", "b-value");
        //        cacheTemplate.map().put("aaa", "c-key", "c-value");
        //        cacheTemplate.map().put("aaa", "d-key", "d-value");
        //
        //        // 获取key的hashkey的值
        //        String val = (String) cacheTemplate.map().get("aaa", "d-key");
        //        String val = (String) cacheTemplate.map().get("aaa", "fff");

        //        String key = "data_center:material_labels_key:1";
        //        Object value = cacheTemplate.string().get(key);
        //
        //        List<JSONObject> jsonObjects = JSON.parseArray(value.toString(), JSONObject.class);
        //        jsonObjects.forEach((str) -> {
        //            String materialsId = str.getString("materialsId");
        //            cacheTemplate.map().put("data_center:material_labels_key:1111", materialsId, str.toJSONString());
        //        });
        //
        //        System.out.println(jsonObjects.toArray());
        //
        //        // 获取hash的key的所有值
        ////        Map<String, Object> dataMap = cacheTemplate.map().entries("data_center:material_labels_key:1");
        ////        System.out.println(dataMap.toString());
        ////        System.out.println(val);
        //
        //        System.out.println("OK");
        //        System.in.read();
    }


    private Cursor<String> scan(String match, int count) {
        ScanOptions scanOptions = ScanOptions.scanOptions().match(match).count(count).build();
        RedisSerializer<?> keySerializer = redisTemplate.getKeySerializer();
        return (Cursor)redisTemplate.executeWithStickyConnection(
            (RedisCallback)redisConnection -> new ConvertingCursor<>(redisConnection.scan(scanOptions),
                keySerializer::deserialize));
    }
}
