YYC
2023-10-24 703c4642be0a57c3b8eaee327d0a95eb0e79de8d
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
package com.ld.igds.util;
 
import com.bstek.bdf2.core.business.IDept;
import com.bstek.bdf2.core.business.IUser;
import com.bstek.bdf2.core.context.ContextHolder;
import com.bstek.bdf2.core.exception.NoneLoginException;
import com.ld.igds.constant.Constant;
import com.ld.igds.constant.GrainFrequence;
import com.ld.igds.data.ConfigData;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.RandomUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
 
import java.util.*;
 
/**
 * 系统公用方法
 *
 * @author Andy
 */
public class ContextUtil {
 
    //全局用户实时坐在部门
    public static Map<String, String> contextUserDept = new HashMap<>();
 
    /**
     * 全局用于存放SN与组织编码的关系,例如分机SN和组织编码关系
     */
    public static Map<String, String> contextSnCompanyIdMap = new HashMap<>();
 
 
    /**
     * 全局命令ID
     */
    public static Map<String, Integer> contextOrderId = new HashMap<>();
 
    /**
     * UUID生成规则
     *
     * @return
     */
    public static String getUUID() {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }
 
    public static String getCurTimeMillis() {
        return System.currentTimeMillis() + "";
    }
 
    public static String getTimeId() {
        return DateFormatUtils.format(new Date(), "yyyyMMddHHmmss");
    }
 
    public static String getTimeId(int random) {
        return DateFormatUtils.format(new Date(), "yyyyMMddHHmmss") + "_" + RandomUtils.nextInt(random);
    }
 
    /**
     * 根据参数生成 执行命令的缓存KEY
     *
     * @param companyId
     * @param serId
     * @param bizType
     * @return
     */
    public static final String buildExeOrderId(String companyId, String serId, String bizType) {
        Integer start = contextOrderId.get("ORDER_ID") == null ? 5000 : contextOrderId.get("ORDER_ID");
 
        contextOrderId.put("ORDER_ID", start + 1);
 
        return start + "";
    }
 
    /**
     * 设备ID生成规则
     *
     * @param companyId
     * @param depotId
     * @param passCode
     * @return
     */
    public static final String buildDeviceId(String companyId, String depotId, int passCode) {
        return companyId + "_" + depotId + "_" + passCode;
    }
 
    /**
     * 设备ID生成规则
     *
     * @param companyId
     * @param depotId
     * @param passCode
     * @return
     */
    public static final String buildDeviceId(String companyId, String depotId, String serId, int passCode) {
        return companyId + depotId + serId + "_" + passCode;
    }
 
    /**
     * 根据信息获取INFO表的主键ID 在粮情,气体和虫害中均需要当前方法
     *
     * @param companyId
     * @param depotId
     * @param batchId
     * @return
     */
    public static String buildInfoId(String companyId, String depotId,
                                     String batchId) {
        return companyId + "_" + depotId + "_" + batchId;
    }
 
    public static String getDefaultBatchId() {
        return DateFormatUtils.format(new Date(), "yyyyMMddHHmm");
    }
 
    /**
     * 根据系统配置的批次频率获取批次编号
     *
     * @param freq
     * @return
     */
    public static String getBatchIdByFireq(String freq) {
        // 一天一次
        if (GrainFrequence.FREQ_02.getCode().equals(freq)) {
            return DateFormatUtils.format(new Date(), "yyyyMMdd") + "1801";
        }
 
        // 一天两次
        if (GrainFrequence.FREQ_03.getCode().equals(freq)) {
            String hour = DateFormatUtils.format(new Date(), "HH");
            if (Integer.valueOf(hour) >= 0 && Integer.valueOf(hour) <= 12) {
                return DateFormatUtils.format(new Date(), "yyyyMMdd") + "1301";
            } else {
                return DateFormatUtils.format(new Date(), "yyyyMMdd") + "1802";
            }
        }
        return getDefaultBatchId();
    }
 
    /**
     * 根据当前登陆人获取组织编号,如果没有登陆人获取系统默认的组织编号
     *
     * @return
     */
    public static String getCompanyId() {
        IUser user = ContextHolder.getLoginUser();
        if (null != user) {
            return user.getCompanyId();
        } else {
            throw new NoneLoginException("Please login first");
        }
    }
 
    /**
     * 获取系统默认的=companyId
     *
     * @return
     */
    public static String getDefaultCompanyId() {
        IUser user = ContextHolder.getLoginUser();
        if (null != user) {
            return user.getCompanyId();
        } else {
            ConfigData configData = SpringUtil.getBean(ConfigData.class);
            return configData.getDefaultCompanyId();
        }
    }
 
    /**
     * 获取登陆人帐号
     *
     * @return
     */
    public static String getLoginUserName() {
        IUser user = ContextHolder.getLoginUser();
        return null == user ? "SYS" : user.getUsername();
    }
 
    /**
     * 获取登陆人中文名称
     *
     * @return
     */
    public static String getLoginUserCName() {
        IUser user = ContextHolder.getLoginUser();
        return null == user ? "系统" : user.getCname();
    }
 
    public static IUser getLoginUser() {
        return ContextHolder.getLoginUser();
    }
 
    /**
     * 最高的编码为:组织编码,然后逐级往下,如: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 || Constant.DEFAULT_PARENT_CODE.equals(parentId)) {
            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;
        }
    }
 
    /**
     * 生成TCP连接的KEY
     *
     * @param ip
     * @param port
     * @return
     */
    public static String getServerKey(String ip, Integer port) {
        return ip + ":" + port;
    }
 
    /**
     * 根据当前登录人获取,获取二级部门ID,当前人所在部门的二级就是 分库ID
     *
     * @param user
     * @return
     */
    public static String subDeptId(IUser user) {
        if (null == user) user = getLoginUser();
 
        if (null == user) {
            return getDefaultCompanyId() + "_001";
        }
 
        //从全局获取,如果有则取全局的默认,如果没有则取自己所属
        if (null != contextUserDept.get(user.getUsername())) {
            return contextUserDept.get(user.getUsername());
        }
 
        if (null == user.getDeptId()) {
            return user.getCompanyId();
        }
        return user.getDeptId();
    }
 
    public static void updateSubDept(String userId, String deptId) {
        if (null == deptId) return;
        contextUserDept.put(userId, deptId);
    }
 
    /**
     * 验证 当前人是否在主部门,即当前人所属部门默认在最高部门
     *
     * @param user
     * @return
     */
    public static boolean isMainDept(IUser user) {
        if (null == user) user = getLoginUser();
        if (null == user)
            return false;
 
        List<IDept> list = user.getDepts();
 
        if (null == list || list.isEmpty()) {
            return false;
        }
 
        if (null == list.get(0).getParentId()) {
            return true;
        }
        if (list.get(0).getId().equals(user.getCompanyId())) {
            return true;
        }
 
        return false;
    }
 
 
    /**
     * 存放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);
    }
 
 
    public static String buildDeviceStatusKey(String companyId, String serId, String passCode) {
        if (null == passCode) passCode = "NULL";
        return companyId + "_" + serId + "_STATUS_" + passCode;
    }
 
    public static String buildDeviceStatusKey(String companyId, String serId, int passCode) {
        return companyId + "_" + serId + "_STATUS_" + passCode;
    }
 
    public static <K extends Comparable<? super K>, V> Map<K, V> sortByKey(Map<K, V> map) {
        Map<K, V> result = new LinkedHashMap<>();
 
        map.entrySet().stream()
                .sorted(Map.Entry.<K, V>comparingByKey()).forEachOrdered(e -> result.put(e.getKey(), e.getValue()));
        return result;
 
    }
 
}