package com.test.musichouduan.util;

/**
 * 雪花算法ID生成工具类
 */
public class SnowflakeIdUtil {

    // 起始时间戳 (2021-01-01 00:00:00)
    private static final long START_TIMESTAMP = 1609459200000L;

    // 各部分占用的位数
    private static final long SEQUENCE_BIT = 12; // 序列号占用的位数
    private static final long MACHINE_BIT = 5;   // 机器标识占用的位数
    private static final long DATA_CENTER_BIT = 5; // 数据中心占用的位数

    // 各部分的最大值
    private static final long MAX_SEQUENCE = ~(-1L << SEQUENCE_BIT);
    private static final long MAX_MACHINE_NUM = ~(-1L << MACHINE_BIT);
    private static final long MAX_DATA_CENTER_NUM = ~(-1L << DATA_CENTER_BIT);

    // 各部分向左的位移
    private static final long MACHINE_LEFT = SEQUENCE_BIT;
    private static final long DATA_CENTER_LEFT = SEQUENCE_BIT + MACHINE_BIT;
    private static final long TIMESTAMP_LEFT = DATA_CENTER_LEFT + DATA_CENTER_BIT;

    private long dataCenterId;  // 数据中心ID
    private long machineId;     // 机器ID
    private long sequence = 0L; // 序列号
    private long lastStamp = -1L; // 上一次时间戳

    private static SnowflakeIdUtil instance;

    /**
     * 获取单例实例
     * @return SnowflakeIdUtil实例
     */
    public static synchronized SnowflakeIdUtil getInstance() {
        if (instance == null) {
            // 默认数据中心ID和机器ID为1
            instance = new SnowflakeIdUtil(1, 1);
        }
        return instance;
    }

    /**
     * 构造函数
     * @param dataCenterId 数据中心ID
     * @param machineId 机器ID
     */
    public SnowflakeIdUtil(long dataCenterId, long machineId) {
        if (dataCenterId > MAX_DATA_CENTER_NUM || dataCenterId < 0) {
            throw new IllegalArgumentException("数据中心ID不能大于 " + MAX_DATA_CENTER_NUM + " 或小于 0");
        }
        if (machineId > MAX_MACHINE_NUM || machineId < 0) {
            throw new IllegalArgumentException("机器ID不能大于 " + MAX_MACHINE_NUM + " 或小于 0");
        }
        this.dataCenterId = dataCenterId;
        this.machineId = machineId;
    }

    /**
     * 生成下一个ID
     * @return 雪花ID
     */
    public synchronized long nextId() {
        long currStamp = getNewStamp();
        if (currStamp < lastStamp) {
            throw new RuntimeException("时钟向后移动。拒绝生成ID");
        }

        if (currStamp == lastStamp) {
            // 同一毫秒内，序列号自增
            sequence = (sequence + 1) & MAX_SEQUENCE;
            // 如果序列号溢出，等待下一毫秒
            if (sequence == 0L) {
                currStamp = getNextMill();
            }
        } else {
            // 不同毫秒内，序列号重置为0
            sequence = 0L;
        }

        lastStamp = currStamp;

        // 拼接成64位ID
        return (currStamp - START_TIMESTAMP) << TIMESTAMP_LEFT // 时间戳部分
                | dataCenterId << DATA_CENTER_LEFT             // 数据中心部分
                | machineId << MACHINE_LEFT                    // 机器标识部分
                | sequence;                                    // 序列号部分
    }

    /**
     * 获取下一毫秒的时间戳
     * @return 下一毫秒的时间戳
     */
    private long getNextMill() {
        long mill = getNewStamp();
        while (mill <= lastStamp) {
            mill = getNewStamp();
        }
        return mill;
    }

    /**
     * 获取当前时间戳
     * @return 当前时间戳
     */
    private long getNewStamp() {
        return System.currentTimeMillis();
    }
}
