广丰卷烟厂数采质量分析系统
zhuguifei
2026-03-02 80ff784bf60637cd348ae665fc907f7b1e527dd8
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package org.dromara.common.mybatis.utils;
 
import com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.dromara.common.core.utils.SpringUtils;
 
/**
 * ID 生成工具类
 *
 * @author AprilWind
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class IdGeneratorUtil {
 
    private static final IdentifierGenerator GENERATOR = SpringUtils.getBean(IdentifierGenerator.class);
 
    /**
     * 生成字符串类型主键 ID
     * <p>
     * 调用 {@link IdentifierGenerator#nextId(Object)},返回 String 格式 ID。
     * </p>
     *
     * @return 字符串格式主键 ID
     */
    public static String nextId() {
        return GENERATOR.nextId(null).toString();
    }
 
    /**
     * 生成 Long 类型主键 ID
     * <p>
     * 自动将生成的数字型主键转换为 Long 类型
     * </p>
     *
     * @return Long 类型主键 ID
     */
    public static Long nextLongId() {
        return GENERATOR.nextId(null).longValue();
    }
 
    /**
     * 生成 Number 类型主键 ID
     * <p>
     * 推荐在需要保留原始 Number 类型时使用
     * </p>
     *
     * @return Number 类型主键 ID
     */
    public static Number nextNumberId() {
        return GENERATOR.nextId(null);
    }
 
    /**
     * 根据实体生成数字型主键 ID
     * <p>
     * 若自定义的 {@link IdentifierGenerator} 根据实体内容生成 ID,则可以使用本方法
     * </p>
     *
     * @param entity 实体对象
     * @return Number 类型主键 ID
     */
    public static Number nextId(Object entity) {
        return GENERATOR.nextId(entity);
    }
 
    /**
     * 根据实体生成字符串主键 ID
     * <p>
     * 与 {@link #nextId(Object)} 类似,但返回 String 类型
     * </p>
     *
     * @param entity 实体对象
     * @return 字符串格式主键 ID
     */
    public static String nextStringId(Object entity) {
        return GENERATOR.nextId(entity).toString();
    }
 
    /**
     * 生成 32 位 UUID
     * <p>
     * 底层使用 {@link IdWorker#get32UUID()}
     * </p>
     *
     * @return 32 位 UUID 字符串
     */
    public static String nextUUID() {
        return IdWorker.get32UUID();
    }
 
    /**
     * 根据实体生成 32 位 UUID
     * <p>
     * 默认 {@link IdentifierGenerator#nextUUID(Object)} 实现忽略实体,但保留该方法便于扩展。
     * </p>
     *
     * @param entity 实体对象
     * @return 32 位 UUID 字符串
     */
    public static String nextUUID(Object entity) {
        return GENERATOR.nextUUID(entity);
    }
 
    /**
     * 生成带指定前缀的字符串主键 ID
     * <p>
     * 示例:prefix = "ORD",生成结果形如:{@code ORD20251211000123}
     * </p>
     *
     * @param prefix 自定义前缀
     * @return 带前缀的字符串主键 ID
     */
    public static String nextIdWithPrefix(String prefix) {
        return prefix + nextId();
    }
 
    /**
     * 生成带前缀的 UUID
     *
     * @param prefix 前缀
     * @return prefix + UUID
     */
    public static String nextUUIDWithPrefix(String prefix) {
        return prefix + nextUUID();
    }
 
}