package com.ruoyi.common.utils.uuid;

import cn.hutool.core.date.DateUtil;

import java.util.Date;
import java.util.Random;

/**
 * ID生成器工具类
 *
 * @author ruoyi
 */
public class IdUtils
{
    /**
     * 获取随机UUID
     *
     * @return 随机UUID
     */
    public static String randomUUID()
    {
        return UUID.randomUUID().toString();
    }

    /**
     * 简化的UUID，去掉了横线
     *
     * @return 简化的UUID，去掉了横线
     */
    public static String simpleUUID()
    {
        return UUID.randomUUID().toString(true);
    }

    /**
     * 获取随机UUID，使用性能更好的ThreadLocalRandom生成UUID
     *
     * @return 随机UUID
     */
    public static String fastUUID()
    {
        return UUID.fastUUID().toString();
    }

    /**
     * 简化的UUID，去掉了横线，使用性能更好的ThreadLocalRandom生成UUID
     *
     * @return 简化的UUID，去掉了横线
     */
    public static String fastSimpleUUID()
    {
        return UUID.fastUUID().toString(true);
    }

    /**
     * 生成随机数
     * @param step 位数
     * @return 结果
     */
    public static String buildNumberCode(Integer step) {
        String sources = "0123456789";
        Random rand = new Random();
        StringBuilder flag = new StringBuilder();
        for (int j = 0; j < step; j++) {
            flag.append(sources.charAt(rand.nextInt(10)));
        }
        return flag.toString();
    }

    /**
     * 订单号，时间+随机七位数字
     * @return 订单号
     */
    public static String createCode(){
        // 时间
        String time = DateUtil.format(new Date(),"yyyyMMddHHmmssSSS");
        // 随机长度字符串
        String randomStr = buildNumberCode(7);
        return time+randomStr;
    }

    /**
     * 生成6位vip码
     * @return 结果
     */
    public static String createVipCode(){
        String baseStr = "0123456789";
        int length = baseStr.length();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 6; i++) {
            Random random = new Random();
            int r = random.nextInt(length);
            sb.append(baseStr.substring(r, r+1));
        }
        return sb.toString();
    }
}
