package cn.novalon.gym.manage.member.util;

import lombok.extern.slf4j.Slf4j;

import java.security.SecureRandom;

/**
 * 会员号生成器
 * 
 * @author 付嘉
 * @date 2026-05-01
 */

@Slf4j
public class MemberNoGenerator {

    // 会员号前缀
    private static final String PREFIX = "GYM";

    // 随机数长度
    private static final int RANDOM_LENGTH = 8;

    // 字符集（排除易混淆字符 0/O, 1/I/l
    private static final String CHARACTERS = "23456789ABCDEFGHJKLMNPQRSTUVWXYZ";

    // SecureRandom 实例（线程安全）    
    private static final SecureRandom SECURE_RANDOM = new SecureRandom();

    // 生成会员号   
    public static String generate() {
        StringBuilder sb = new StringBuilder(PREFIX);
        
        for (int i = 0; i < RANDOM_LENGTH; i++) {
            int index = SECURE_RANDOM.nextInt(CHARACTERS.length());
            sb.append(CHARACTERS.charAt(index));
        }
        
        String memberNo = sb.toString();
        log.debug("生成会员号: {}", memberNo);
        
        return memberNo;
    }

    // 批量生成会员号
    // count 生成数量
    public static String[] generateBatch(int count) {
        String[] memberNos = new String[count];
        for (int i = 0; i < count; i++) {
            memberNos[i] = generate();
        }
        return memberNos;
    }
}
