package com.zbf.web.util;
import java.util.HashSet;
import java.util.Random;
import java.util.Set;

public class RID {


    private static final String PREFIX = "RK";
    private static final int MIN_LENGTH = 6;
    private static final int MAX_LENGTH = 8;
    private static final Set<String> generatedIds = new HashSet<>(); // 用于存储已生成的 ID
    private static final Random random = new Random();

    public static String generateUniqueId() {
        int length = random.nextInt(MAX_LENGTH - MIN_LENGTH + 1) + MIN_LENGTH; // 随机生成长度
        StringBuilder idBuilder = new StringBuilder(PREFIX);

        // 生成随机数字
        for (int i = 0; i < length - PREFIX.length(); i++) {
            char randomChar = (char) (random.nextInt(10) + '0'); // 生成数字字符
            idBuilder.append(randomChar);
        }

        String uniqueId = idBuilder.toString();

        // 确保 ID 不重复
        while (generatedIds.contains(uniqueId)) {
            uniqueId = generateUniqueId(); // 递归调用以生成新的 ID
        }

        generatedIds.add(uniqueId); // 添加到已生成的 ID 集合中
        return uniqueId;

    }
}
