czt
2025-12-01 096296cd7485c5583c8194d88cca700e3c4d84a0
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
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 com.ruoyi.common.utils.StringUtils;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
 
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.concurrent.atomic.AtomicLong;
 
/**
 * 系统公用方法
 *
 * @author Andy
 */
public class ContextUtil {
 
    private static final AtomicLong sequence = new AtomicLong(0);
    private static String lastTimestamp = "";
 
 
    //全局用户实时坐在部门
    public static Map<String, String> contextUserDept = new HashMap<>();
 
    /**
     * 全局用于存放SN与组织编码的关系,例如分机SN和组织编码关系
     */
    public static Map<String, String> contextSnCompanyIdMap = new HashMap<>();
 
 
    /**
     * 生成顺序ID:年月日时分秒毫秒(17位) + 序列号(4位) = 21位
     */
    public static String generateId() {
        String pattern = "yyyyMMddHHmmssSSS";
        return generateId(null, pattern);
    }
 
    /**
     * 生成单据编码, (格式: yyyyMMddHHmmss + 4位随机数)
     * @param prefix
     * @return
     */
    public static String generateOrderId(String prefix) {
        String pattern = "yyyyMMddHHmmss";
        return generateId(prefix, pattern);
    }
 
    /**
     *
     *
     * @return
     */
    private static String generateId(String prefix, String pattern) {
        if (null == pattern) pattern = "yyyyMMddHHmmss";
        String currentTimestamp = LocalDateTime.now().format(DateTimeFormatter.ofPattern(pattern));
        long seq;
        synchronized (ContextUtil.class) {
            if (currentTimestamp.equals(lastTimestamp)) {
                seq = sequence.incrementAndGet();
            } else {
                sequence.set(0);
                seq = 0;
                lastTimestamp = currentTimestamp;
            }
        }
 
        if (null == prefix) return String.format("%s%04d", currentTimestamp, seq);
        return prefix + "_" + String.format("%s%04d", currentTimestamp, seq);
    }
 
 
    public static String UUID() {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }
 
 
    /**
     * 默认获取登录人所在组织,如果未登录则获取系统默认组织
     * @return 组织ID
     */
    public static String getCompanyId() {
 
        SysUser user = ShiroUtils.getSysUser();
        if (null != user) return user.getCompanyId();
        return FrameworkConfig.getCompanyId();
    }
 
    public static String getLoginUserName() {
        SysUser user = ShiroUtils.getSysUser();
        return null == user ? "系统" : user.getUserName();
    }
 
    public static SysUser getLoginUser() {
        return ShiroUtils.getSysUser();
    }
 
    public static String subDeptId(SysUser user) {
        if (null == user) {
            user = getLoginUser();
        }
 
        if (null == user) {
            return getCompanyId() + "001";
        }
 
        //从全局获取,如果有则取全局的默认,如果没有则取自己所属
        if (null != contextUserDept.get(user.getLoginName())) {
            return contextUserDept.get(user.getLoginName());
        }
 
        if (null == user.getDeptId()) {
            return user.getCompanyId();
        }
        return user.getDeptId().toString();
    }
 
    public static void updateSubDept(String userId, String deptId) {
        if (null == deptId) return;
        contextUserDept.put(userId, deptId);
    }
 
    /**
     * 获取顺序号
     * @param oldOrderId 原有顺序号
     * @param num 位数
     * @return
     */
    public static String getOrderId(String oldOrderId, Integer num) {
        Integer index3 = 1001;
        Integer index4 = 10001;
        Integer index5 = 100001;
        Integer index = 0;
        String orderId = "001";
        if (StringUtils.isNotEmpty(oldOrderId)) {
            index = Integer.valueOf(oldOrderId);
        }
        if (3 == num) {
            orderId = ((index3 + index) + "").substring(1);
        }
        if (4 == num) {
            orderId = ((index4 + index) + "").substring(1);
        }
        if (5 == num) {
            orderId = ((index5 + index) + "").substring(1);
        }
        return orderId;
    }
 
    /**
     * 存放SN与所属组织的关系
     *
     * @param sn
     * @param companyId
     */
    public static void addSerCompany(String sn, String companyId) {
        contextSnCompanyIdMap.put(sn, companyId);
    }
 
    /**
     * 通过SN获取当前SN所属的组织
     *
     * @param sn
     * @return
     */
    public static String getCompanyIdBySn(String sn) {
        return contextSnCompanyIdMap.get(sn);
    }
}