package com.liquidnet.service;

import com.liquidnet.common.cache.redis.util.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author AnJiabin <anjiabin@zhengzai.tv>
 * @version V1.0
 * @Description: TODO
 * @class: TestRedisConnection
 * @Package com.liquidnet.service
 * @Copyright: LightNet @ Copyright (c) 2021
 * @date 2021/8/31 11:17
 */
@Slf4j
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class TestRedisConnection {
    @Autowired
    private RedisUtil redisUtil;

    @Test
    public void getPayCodeList(){
//        List<Object> payCodeList = this.getPrefixKeyValue("dragon:pay:code:*");
//        for(Object payCode:payCodeList){
//            System.out.println("object=== "+ JsonUtils.toJson(payCode));
//        }
        //设置新的redis

//        for(int i=0 ;i<1000;i++){
//            System.out.println("第"+i+"个 "+redisUtil.get("dragon:pay:test:Pay"+i));
//        }
        String ss = "[\n" +
                "  \"com.liquidnet.service.dragon.dto.DragonOrdersDto\",\n" +
                "  {\n" +
                "    \"mid\": null,\n" +
                "    \"orderId\": \"20210810193315226361725\",\n" +
                "    \"status\": 0,\n" +
                "    \"code\": \"PAY20210810193315191566757\",\n" +
                "    \"type\": \"TICKET\",\n" +
                "    \"price\": [\n" +
                "      \"java.math.BigDecimal\",\n" +
                "      0.1\n" +
                "    ],\n" +
                "    \"name\": \"正在现场\",\n" +
                "    \"detail\": \"正在现场\",\n" +
                "    \"orderCode\": \"ORDER0001\",\n" +
                "    \"clientIp\": \"127.0.0.1\",\n" +
                "    \"notifyUrl\": \"devdragon.zhengzai.tv\",\n" +
                "    \"notifyStatus\": 0,\n" +
                "    \"paymentType\": \"WAPALIPAY\",\n" +
                "    \"paymentId\": null,\n" +
                "    \"paymentAt\": \"2021-08-10T19:33:15.226\",\n" +
                "    \"finishedAt\": null,\n" +
                "    \"createdAt\": \"2021-08-10T19:33:15.226\",\n" +
                "    \"updatedAt\": null,\n" +
                "    \"deletedAt\": null\n" +
                "  }\n" +
                "]";
        int thredNum = 2000;
        ExecutorService exec = Executors.newFixedThreadPool(thredNum);
        for (int i = 0; i < thredNum; i++) {
            final int k = i;
            log.info("==================" + k);
//            System.out.println("第"+i+"个 "+redisUtil.get("dragon:pay:test:Pay"+i));
            exec.execute(() -> {
                for(int j=0 ;j<1000;j++){
                    System.out.println("第"+j+"个 set");
                    redisUtil.set("dragon:pay:test:Pay"+k+j,ss);
                }
            });
            try { Thread.sleep(100L); } catch (InterruptedException ignored) {}
        }
        exec.shutdown();
//        List<String> keys = getPrefixKeys("dragon:pay:code:*");
//        log.info("总共迁移数据条数==="+keys.size());
//        DragonOrdersDto dragonOrdersDto = DragonOrdersDto.getNew();
//        for(String key:keys){
//            Object obj = redisUtil.getRedisTemplateByDb(0).opsForValue().get(key);
//            if(obj!=null){
//                dragonOrdersDto =  (DragonOrdersDto) obj;
//            }
//            redisUtil.set(key,dragonOrdersDto);
//        }
    }
}
