package com.ruoyi.sim.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.lang.Snowflake;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Date;

@Service
public class SnowflakeIdService {

    public interface Consts {
        Long WORKER_ID = 10L;

        Long DATACENTER_ID = 10L;

        Date EPOCH = Date.from(
                LocalDateTime.of(
                                2015, 1, 1,
                                0, 0, 0,
                                0)
                        .toInstant(ZoneOffset.UTC)
        );
    }


    private Snowflake snowflake;

    public Snowflake getSnowflake() {
        if (snowflake == null) {
            snowflake = new Snowflake(Consts.EPOCH, Consts.WORKER_ID, Consts.DATACENTER_ID, true);
        }
        return snowflake;
    }

    public long nextId() {
        return getSnowflake().nextId();
    }

    public String nextIdStr() {
        return getSnowflake().nextIdStr();
    }
}
