package com.liangjian.dataplatform.member.entity.genernator;

import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.id.IdentifierGenerator;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.Serializable;

/**
 * @Author: 黄子奇
 * @Date: 2019/12/5
 */
public class SnowflakeGenernator implements IdentifierGenerator {
    //无法通过spring注入
//    @Value("${snowflake.workerid}")
//    private Long workId;
//    @Value("${snowflake.datacenterid}")
//    private Long datacenterId;

    @Override
    public Serializable generate(SharedSessionContractImplementor session, Object object) throws HibernateException {
        Long workId = Long.valueOf(System.getenv("workerid"));
        Long datacenterId = Long.valueOf(System.getenv("datacenterid"));
        SnowflakeIdWorker worker = new SnowflakeIdWorker(workId,datacenterId);
        return worker.nextId().toString();
    }

}
