package com.ff.base.config;

import com.ff.base.utils.SnowflakeIdGenerator;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

/**
 * 雪花算法id
 *
 * @author liukang
 * @date 2024-11-04
 */
@Configuration
public class IdGeneratorUtil {

    private static SnowflakeIdGenerator snowflakeIdGenerator;

    @Value("${snowflake.worker.id}")
    private long workerId;

    @Value("${snowflake.datacenter.id}")
    private long dataCenterId;

    public void init() {
        snowflakeIdGenerator = new SnowflakeIdGenerator(workerId, dataCenterId);
    }

    public static long generateId() {
        if (snowflakeIdGenerator == null) {
            throw new IllegalStateException("SnowflakeIdGenerator is not initialized");
        }
        return snowflakeIdGenerator.nextId();
    }
}
