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

import org.apache.commons.lang.StringUtils;
import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.id.IdentifierGenerator;

import java.io.Serializable;

/**
 * @Author: pengzhaocheng
 * @Date: 2019/12/5
 */
public class SnowflakeGenernatorLong implements IdentifierGenerator {

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

}
