jiazx0107
2025-11-20 e9998eb7f991a88e19b3c63c7dcffb64e1c409ea
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package com.fzzy.igds.utils;
 
import com.ruoyi.common.config.FrameworkConfig;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.ShiroUtils;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
 
import java.util.*;
 
/**
 * 系统公用方法
 *
 * @author Andy
 */
public class ContextUtil {
 
    public static Map<String, Boolean> l = new HashMap<>();
    //全局用户实时坐在部门
    public static Map<String, String> contextUserDept = new HashMap<>();
    /**
     * 全局命令ID
     */
    public static Map<String, Integer> contextOrderId = new HashMap<>();
 
    /**
     * UUID生成规则
     *
     * @return
     */
    public static String getUUID() {
        return UUID.randomUUID().toString().replaceAll("-", "").toUpperCase();
    }
 
    public static String getBusinessID() {
        return DateFormatUtils.format(new Date(), "yyyyMMdd") + String.format("%04d", RandomUtils.nextInt(0, 10000));
    }
 
    public static String getCurTimeMillis() {
        return System.currentTimeMillis() + "";
    }
 
    public static String getTimeId() {
        return DateFormatUtils.format(new Date(), "yyyyMMddHHmmss");
    }
 
    public static String getTimeId(int start, int end) {
        return DateFormatUtils.format(new Date(), "yyyyMMddHHmmss") + RandomUtils.nextInt(start, end);
    }
 
 
    /**
     * 获取当前年份
     *
     * @return
     */
    public static String getYear() {
        return DateFormatUtils.format(new Date(), "yyyyMM");
    }
 
    /**
     * 生成资产编码ID
     * @return
     */
    public static String createAssetId() {
        return "NO" + randomChar(4) + getYear() + randomNum(6);
    }
 
 
    /**
     * 生成业务ID
     * @param prefix
     * @return
     */
    public static String getBizId(String prefix) {
        if (prefix == null) prefix = "ID";
        return prefix + getTimeId() + randomNum(4);
    }
 
 
    /**
     * 随机生成大写字母
     *
     * @param length 长度
     * @return
     */
    public static String randomChar(int length) {
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            char randomLetter = (char) (random.nextInt(26) + 'A'); // 65-90
            sb.append(randomLetter);
        }
        return sb.toString();
    }
 
    /**
     * 随机生成数字
     * @param length
     * @return
     */
    public static String randomNum(int length) {
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int digit = random.nextInt(10); // 生成0-9的随机数
            sb.append(digit);
        }
        return sb.toString();
    }
 
 
    public static String getDefaultBatchId() {
        return DateFormatUtils.format(new Date(), "yyyyMMddHHmm");
    }
 
    /**
     * 根据当前登陆人获取组织编号,如果没有登陆人获取系统默认的组织编号
     *
     * @return
     */
    public static String getCompanyId() {
        return ShiroUtils.getLoginUserCompanyId();
    }
 
    /**
     * 获取系统默认的=companyId
     *
     * @return
     */
    public static String getDefaultCompanyId() {
        SysUser user = ShiroUtils.getSysUser();
        if (null != user) {
            return user.getCompanyId();
        } else {
           return FrameworkConfig.getCompanyId();
        }
    }
 
 
    /**
     * 最高的编码为:组织编码,然后逐级往下,如:5013,5013_001,5013_002,5013_001_001
     *
     * @param companyId 必须
     * @param parentId  可空
     * @param endId     可空
     * @param format    必须 三位传1000,四位传10000
     * @return
     */
    public static String getNextId(String companyId, String parentId,
                                   String endId, int format) {
        if (parentId == null) {
            return companyId;
        }
        try {
            int endNum = 0;
            if (StringUtils.isNotEmpty(endId)) {
                endNum = Integer.valueOf(endId.substring(endId.length() - 3));
            }
            String endStr = ("" + (endNum + format + 1)).substring(1);
 
            if (parentId.equals(companyId)) {
                return companyId + "_" + endStr;
            }
            parentId = parentId.substring(parentId.lastIndexOf("_") + 1);
            return companyId + "_" + parentId + "_" + endStr;
 
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
 
    /**
     * 获取仓库编码  格式:
     *
     * @param deptId 5102_001_001  5102_001_002
     *               必须 分库编码
     * @param endId  必须 最后的仓库编码
     * @param format 必须 三位传1000,四位传10000,默认都传1000
     * @return
     */
    public static String getNextDepotId(String deptId, String endId, int format) {
        try {
            int endNum = 0;
            if (StringUtils.isNotEmpty(endId)) {
                endNum = Integer.valueOf(endId.substring(endId.length() - 3));
            }
            String endStr = ("" + (endNum + format + 1)).substring(1);
 
            endStr = deptId.substring(deptId.indexOf("_") + 1) + "_" + endStr;
 
            return endStr.replaceAll("_", "");
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
 
 
    /**
     * 获取删除注释
     *
     * @author sgj
     * @date 2025/08/05
     * @param desc 删除文案描述
     */
    public static String getDeleteDesc(String desc) {
        if (StringUtils.isEmpty(desc)) {
            desc = "删除数据";
        }
        return desc + "_" + ShiroUtils.getLoginName() + "_" + new Date();
    }
 
 
}