package com.youchain.utils;

import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;

import java.text.SimpleDateFormat;
import java.util.Calendar;

public class CodeForYmYtil {
    private static String key_time_type="yyMMdd";
    public void ce(){
        new Thread(()->{
            CodeForYmYtil codeForYmYtil=new CodeForYmYtil();
            for (int i = 0; i < 100; i++) {
                String code = codeForYmYtil.CodeForYmYtil("ce",5);
            }
        }).start();
        new Thread(()->{
            CodeForYmYtil codeForYmYtil=new CodeForYmYtil();
            for (int i = 0; i < 100; i++) {
                String code = codeForYmYtil.CodeForYmYtil("ce",5);
            }
        }).start();
        new Thread(()->{
            CodeForYmYtil codeForYmYtil=new CodeForYmYtil();
            for (int i = 0; i < 100; i++) {
                String code = codeForYmYtil.CodeForYmYtil("ce",5);
            }
        }).start();
        new Thread(()->{
            CodeForYmYtil codeForYmYtil=new CodeForYmYtil();
            for (int i = 0; i < 100; i++) {
                String code = codeForYmYtil.CodeForYmYtil("ce",5);
            }
        }).start();
    }
    public static synchronized String CodeForYmYtil(String key_code, int len){
        String code="";
        String key_time = new SimpleDateFormat(key_time_type).format(Calendar.getInstance().getTime());
        String key=key_code+key_time;
        String redisHost = "127.0.0.1";
        int redisPort = 6379;
        RedisStandaloneConfiguration config = new RedisStandaloneConfiguration(redisHost, redisPort);
        LettuceConnectionFactory factory = new LettuceConnectionFactory(config);
        factory.afterPropertiesSet();
        RedisTemplate redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(factory);
        redisTemplate.afterPropertiesSet();

        ValueOperations ops = redisTemplate.opsForValue();
        String value;
//        value = ops.get(key).toString();

        if (ops.get(key)==null||ops.get(key).equals("")){
            ops.set(key, "0");
        }
        value = (Integer.valueOf(ops.get(key).toString())+1)+"";
        ops.set(key, value);
        while (value.length()<len){
            value="0"+value;
        }
        code=key+value;
        factory.destroy();
        return  code;
    }

    public static void main(String[] args) {
        String code = CodeForYmYtil.CodeForYmYtil("pd",5);
       /* CodeForYmYtil codeForYmYtil=new CodeForYmYtil();
        codeForYmYtil.ce();*/
        /*String code = codeForYmYtil.CodeForYmYtil("pt",15);
        System.out.println(code);*/

       /* String redisHost = "127.0.0.1";
        int redisPort = 6379;
        RedisStandaloneConfiguration config = new RedisStandaloneConfiguration(redisHost, redisPort);
        LettuceConnectionFactory factory = new LettuceConnectionFactory(config);
        factory.afterPropertiesSet();
        RedisTemplate redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(factory);
        redisTemplate.afterPropertiesSet();
        ValueOperations ops = redisTemplate.opsForValue();

        String value = ops.get("pd")+"";
        System.out.println(":"+value);
        factory.destroy();
        System.out.println("poiuytrew");*/
    }
}
