package com.ruoyi.common.utils;

import com.ruoyi.common.utils.spring.SpringUtils;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.support.atomic.RedisAtomicInteger;

public class GenerateCode {

    public static String getCode(String prefix){
        StringRedisTemplate stringRedisTemplate = SpringUtils.getBean(StringRedisTemplate.class);
        RedisAtomicInteger entityIdCounter = new RedisAtomicInteger(prefix, stringRedisTemplate.getConnectionFactory());
        int andIncrement = entityIdCounter.incrementAndGet();
        String number = String.format("%03d", andIncrement);
        return prefix+number;
    }


    public static String getCode(String prefix,String fromat){
        StringRedisTemplate stringRedisTemplate = SpringUtils.getBean(StringRedisTemplate.class);
        RedisAtomicInteger entityIdCounter = new RedisAtomicInteger(prefix, stringRedisTemplate.getConnectionFactory());
        int andIncrement = entityIdCounter.incrementAndGet();
        String number = String.format(fromat, andIncrement);
        return prefix+number;
    }
}
