Helius
2021-06-23 b8ca59259b0690f664ee70ecca69dfb53700c653
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package com.xzx.gc.common.utils;
 
import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
 
@Component
public class IdUtils {
 
    @Value("${workerId}")
    private String workerId;
 
    /**
     *  生成分布式唯一全局ID
     * @param prefix ID前缀 如支付:ZF
     * @param datacenterId 数据中心ID(0~31)  0:支付 1 订单 2入库 3 用户 4账户 5特殊用户 6废品站 7结算 8请假
     * @return
     */
    public  String generate(String prefix,long datacenterId){
        Snowflake snowflake = IdUtil.getSnowflake(Convert.toLong(workerId), datacenterId);
        return prefix+snowflake.nextIdStr();
    }
 
}