QisdaCache.java
16.3 KB
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
package com.myproject.webapp.controller.webService;
import com.myproject.bean.qisda.InquiryShelfBean;
import com.myproject.bean.qisda.QisdaApiRequest;
import com.myproject.bean.qisda.ReelLockPosInfo;
import com.myproject.bean.qisda.ShelfInfo;
import com.myproject.bean.update.qisda.CacheInfo;
import com.myproject.bean.update.qisda.DNInfo;
import com.myproject.bean.update.qisda.DNItem;
import com.myproject.dao.mongo.qisda.ICacheInfoDao;
import com.myproject.dao.mongo.qisda.IDNItemDao;
import com.myproject.exception.ApiException;
import com.myproject.util.HttpHelper;
import com.myproject.util.XmlUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
/**
* 缓存
* Created by sunke on 17/3/17.
*/
@Repository
public class QisdaCache {
protected static final transient Logger log = LogManager.getLogger(QisdaCache.class);
private static ICacheInfoDao cacheInfoDao;
private static IDNItemDao dNItemDao;
private static String CURRENT_ORDER_HSERIAL_KEY = "currentOrderHSerial";
/**
* RFID绑定的DN单信息Key
*/
private static String RFID_DN_MAP_KEY = "rfidDnMap";
/**
* 料架信息缓存key
*/
private static String HSERIAL_SHELF_MAP_KEY = "hSerialShelfMap";
/**
* 重发消息key
*/
private static String FAILED_REQUEST_MAP_KEY = "failedRequestMap";
/**
* 料盘锁定库位信息,用于包装料分配到其他料仓
*/
private static String REEL_LOCK_POS_MAP_KEY = "reelLocKPosMap";
/**
* 料串/料架目的地Map
*/
private static String SHELF_HSERIAL_MAP = "shelfHSerialMap";
/**
* 当前正在执行的工单需求单(首盘,补料),未完成时,其他工单需求单不可执行
*/
private static String currentOrderHSerial;
/**
* 上个需求单使用的料架信息
*/
private static List<String> lastHSerialUsedRfidList;
/**
* RFID绑定DN单和Facility, Key为RFID, Value为DN单信息
*/
private static Map<String, DNInfo> rfidDnMap;
/**
* 需要重新向佳世达发送的指令列表
*/
private static Map<String, QisdaApiRequest> failedRequestMap;
/**
* 料盘锁定库位信息,用于包装料分配到其他料仓
*/
private static Map<String, ReelLockPosInfo> reelLocKPosMap;
/**
* 料串/料架目的地Map
*/
private static Map<String,String> shelfHSerialMap;
private static boolean isProcessTimer = false;
/**
* 定时器,每10秒执行一次
*/
public static void runTimer(){
if(!isProcessTimer){
isProcessTimer = true;
try {
sendFailedRequest();
}catch (Exception e){
log.error("发送失败请求定时器执行出错:"+e.getMessage());
}finally {
isProcessTimer = false;
}
}
}
/**
* 从数据库中加载缓存信息
*/
public static void initFromDb(){
initApiRequestMap();
initReelLockPosMap();
initShelfDestinationMap();
}
private static void initShelfDestinationMap(){
if(shelfHSerialMap == null){
shelfHSerialMap = new ConcurrentHashMap<>();
CacheInfo cacheInfo = cacheInfoDao.getCacheInfo(SHELF_HSERIAL_MAP);
if(cacheInfo != null){
Map<String, String> shelfMap = (Map<String, String>) cacheInfo.getCacheValue();
shelfHSerialMap.putAll(shelfMap);
log.info("当前料架目的地信息:" + shelfMap);
}
}
}
/**
* 添加料架对应的需求单号
*/
public static void addShelfHSerial(String rfid, String hSerial){
String oldHSerial = shelfHSerialMap.get(rfid);
log.info("添加料架["+rfid+"]的需求单号:" + hSerial);
shelfHSerialMap.put(rfid, hSerial);
cacheInfoDao.updateCacheItem(SHELF_HSERIAL_MAP, shelfHSerialMap);
if(oldHSerial != null){
if(!oldHSerial.equals(hSerial)){
log.error("添加料架["+rfid+"]需求单号["+hSerial+"]失败,料架已有需求单号:" + oldHSerial);
}
}
}
/**
* 清除料架的目的地信息
*/
public static void removeShelfHSerial(String rfid){
String oldHSerial = shelfHSerialMap.get(rfid);
log.info("清除料架["+rfid+"]的需求单号["+oldHSerial+"]信息");
shelfHSerialMap.remove(rfid);
cacheInfoDao.updateCacheItem(SHELF_HSERIAL_MAP, shelfHSerialMap);
}
/**
* 获取料架的目的地
*/
public static String getShelfHSerial(String rfid){
String hSerial = shelfHSerialMap.get(rfid);
return hSerial;
}
/**
* 添加条码锁定库位信息
* @param reelLockPosInfo
*/
public static synchronized ReelLockPosInfo addReelLockPosInfo(ReelLockPosInfo reelLockPosInfo){
log.info("为["+reelLockPosInfo.getBarcode()+"]锁定库位["+reelLockPosInfo.getLockPosName()+"]");
ReelLockPosInfo oldLocInfo = reelLocKPosMap.get(reelLockPosInfo.getBarcode());
if(oldLocInfo != null && oldLocInfo.getLockPosName() != null){
log.info("["+oldLocInfo.getBarcode()+"]已有锁定库位["+oldLocInfo.getLockPosName()+"],返回之前锁定的库位");
return oldLocInfo;
}
for (ReelLockPosInfo locInfo : reelLocKPosMap.values()) {
if(locInfo.getLockPosId().equals(reelLockPosInfo.getLockPosId()) && !locInfo.getBarcode().equals(reelLockPosInfo.getBarcode())){
log.info("为["+reelLockPosInfo.getBarcode()+"]锁定库位["+reelLockPosInfo.getLockPosName()+"]时,库位已被["+locInfo.getBarcode()+"]锁定");
return null;
}
}
reelLocKPosMap.put(reelLockPosInfo.getBarcode(), reelLockPosInfo);
cacheInfoDao.updateCacheItem(REEL_LOCK_POS_MAP_KEY, reelLocKPosMap);
return reelLockPosInfo;
}
/**
* 根据料架和料架位置获取锁定的库位信息,如果不存在,返回null
* @param rfid
* @param rfidLoc
* @return
*/
public static ReelLockPosInfo getLockPosInfoByRfidLoc(String rfid, String rfidLoc){
if(Strings.isNotBlank(rfid) && Strings.isNotBlank(rfidLoc)){
for (ReelLockPosInfo reelLockPosInfo : reelLocKPosMap.values()) {
String lockRfid = reelLockPosInfo.getRfid();
String lockRfidLoc = reelLockPosInfo.getRfidLoc();
if(Strings.isNotBlank(lockRfid) && Strings.isNotBlank(lockRfidLoc)){
if(rfid.equals(lockRfid) && rfidLoc.equals(lockRfidLoc)){
return reelLockPosInfo;
}
}
}
}
return null;
}
/**
* 获取料架上料盘锁定的库位信息
* @param rfid
* @return
*/
public static List<ReelLockPosInfo> getShelfLockPosInfo(String rfid){
List<ReelLockPosInfo> shelfLocPosInfoList = new ArrayList<>();
if(Strings.isNotBlank(rfid)){
for (ReelLockPosInfo reelLockPosInfo : reelLocKPosMap.values()) {
if(rfid.equals(reelLockPosInfo.getRfid())){
shelfLocPosInfoList.add(reelLockPosInfo);
}
}
}
return shelfLocPosInfoList;
}
/**
* 清理条码锁定库位信息
*/
public static void removeReelLockPosInfo(String barcode){
reelLocKPosMap.remove(barcode);
cacheInfoDao.updateCacheItem(REEL_LOCK_POS_MAP_KEY, reelLocKPosMap);
}
/**
* 获取条码锁定的库位Id
* @param barcode 条码信息
* @return
*/
public static String getReelLockPosId(String barcode){
ReelLockPosInfo lockPosInfo = reelLocKPosMap.get(barcode);
if(lockPosInfo != null){
return lockPosInfo.getLockPosId();
}
return "";
}
/**
* 获取所有锁定的库位Id列表
*/
public static Set<String> getAllLockPosIds(){
Set<String> lockPosIds = new HashSet<>();
for (ReelLockPosInfo reelLockPosInfo : reelLocKPosMap.values()) {
lockPosIds.add(reelLockPosInfo.getLockPosId());
}
return lockPosIds;
}
public static void addFailedRequest(QisdaApiRequest apiRequest){
String mapKey = apiRequest.getMapKey();
if(failedRequestMap.get(mapKey) == null){
log.info(mapKey + "通知指令发送到Qisda失败");
// failedRequestMap.put(apiRequest.getMapKey(), apiRequest);
// cacheInfoDao.updateCacheItem(FAILED_REQUEST_MAP_KEY, failedRequestMap);
}
}
public static void removeFailedRequest(QisdaApiRequest apiRequest){
String mapKey = apiRequest.getMapKey();
if(failedRequestMap.get(mapKey) != null){
log.info(mapKey + "通知指令发送到Qisda成功,从缓存中移除");
failedRequestMap.remove(apiRequest.getMapKey());
cacheInfoDao.updateCacheItem(FAILED_REQUEST_MAP_KEY, failedRequestMap);
}
}
/**
* 保存料架缓存信息
* @param hSerialShelfMap
*/
public static void saveShelfMap(Map<String,Map<String,ShelfInfo>> hSerialShelfMap){
cacheInfoDao.updateCacheItem(HSERIAL_SHELF_MAP_KEY, hSerialShelfMap);
}
/**
* 加载料架缓存信息
*/
public static Map<String,Map<String,ShelfInfo>> loadShelfMap(){
Map<String,Map<String,ShelfInfo>> hSerialShelfMap = new ConcurrentHashMap<>();
CacheInfo cacheInfo = cacheInfoDao.getCacheInfo(HSERIAL_SHELF_MAP_KEY);
if(cacheInfo != null){
Map<String, Map<String, ShelfInfo>> dbShelfMap = (Map<String, Map<String, ShelfInfo>>) cacheInfo.getCacheValue();
hSerialShelfMap.putAll(dbShelfMap);
}
return hSerialShelfMap;
}
/**
* 初始化DN单绑定缓存信息
*/
public void initRfidDnMap(){
if(rfidDnMap == null){
CacheInfo cacheInfo = cacheInfoDao.getCacheInfo(RFID_DN_MAP_KEY);
rfidDnMap = new ConcurrentHashMap<>();
if(cacheInfo != null){
Map<String, DNInfo> dbRfidDnMap = (Map<String, DNInfo>) cacheInfo.getCacheValue();
rfidDnMap.putAll(dbRfidDnMap);
log.info("当前收料绑定:" + rfidDnMap);
}
}
}
/**
* 获取RFID绑定的DN单信息
* @param rfid
* @return
*/
public static DNInfo getDnInfo(String rfid){
if(rfid == null || rfid.equals("000")){
//rfid = "";
DNInfo dnInfo = new DNInfo();
dnInfo.setDnNo("");
return dnInfo;
}
DNInfo dnInfo = rfidDnMap.get(rfid);
String usedRfid = "=" + rfid;
if(dnInfo == null){
dnInfo = rfidDnMap.get(usedRfid);
}else{
//已经使用过,清除掉
rfidDnMap.remove(rfid);
rfidDnMap.put(usedRfid, dnInfo);
saveRfidDnMap();
}
if(dnInfo == null){
//如果未找到料架对应的DNInfo,当作纯入库处理
dnInfo = new DNInfo();
dnInfo.setDnNo("");
}else{
if(dnInfo.getItems().isEmpty()){
List<DNItem> items = dNItemDao.findDnItems(dnInfo.getDnNo());
dnInfo.addItems(items);
}
}
return dnInfo;
}
public static void bindRfidDnInfo(String rfid, DNInfo dnInfo){
rfidDnMap.put(rfid, dnInfo);
saveRfidDnMap();
}
/**
* 清理RFID绑定的DN单信息
*/
public static void clearRfidDn(String rfid){
rfidDnMap.remove(rfid);
saveRfidDnMap();
}
/**
* 当前正在执行的工单需求单
* @param executeHSerial
*/
public static void setCurrentOrderHSerial(String executeHSerial) {
if(currentOrderHSerial == null){
currentOrderHSerial = "";
}
String oldHSerial = currentOrderHSerial;
log.info("设置当前正在执行的工单料需求为:" + executeHSerial+"清理之前["+oldHSerial+"]出库使用的料架");
InquiryShelfBean.clearShelf(oldHSerial);
currentOrderHSerial = executeHSerial;
cacheInfoDao.updateCacheItem(CURRENT_ORDER_HSERIAL_KEY, currentOrderHSerial);
}
public static void updateUsedRfid(List<String> usedRfidList){
String oldUsedRfid = "";
if(lastHSerialUsedRfidList != null){
oldUsedRfid = String.join(",",lastHSerialUsedRfidList);
}
String newUsedRfid = "";
if(usedRfidList != null){
newUsedRfid = String.join(",",usedRfidList);
}
log.info("已使用过的料架信息更新:["+oldUsedRfid+"]=>["+newUsedRfid+"]");
lastHSerialUsedRfidList = usedRfidList;
}
/**
* 是否是上个工单需求单使用过的料架
*/
public static boolean isLastHSerialUsedRfid(String rfid){
if(lastHSerialUsedRfidList != null){
return lastHSerialUsedRfidList.contains(rfid);
}
return false;
}
/**
* 当前正在执行的工单需求单
*/
public static String getCurrentOrderHSerial() {
if(currentOrderHSerial == null){
CacheInfo cacheInfo = cacheInfoDao.getCacheInfo(CURRENT_ORDER_HSERIAL_KEY);
if(cacheInfo == null){
currentOrderHSerial = "";
}else{
currentOrderHSerial = cacheInfo.getCacheValue().toString();
}
}
return currentOrderHSerial;
}
/**
* 加载请求指令信息
*/
private static void initReelLockPosMap(){
if(reelLocKPosMap == null){
reelLocKPosMap = new ConcurrentHashMap<>();
CacheInfo cacheInfo = cacheInfoDao.getCacheInfo(REEL_LOCK_POS_MAP_KEY);
if(cacheInfo != null){
Map<String, ReelLockPosInfo> locPosMap = (Map<String, ReelLockPosInfo>) cacheInfo.getCacheValue();
reelLocKPosMap.putAll(locPosMap);
log.info("当前料盘锁定库位信息:" + reelLocKPosMap);
}
}
}
/**
* 加载请求指令信息
*/
private static void initApiRequestMap(){
if(failedRequestMap == null){
failedRequestMap = new ConcurrentHashMap<>();
CacheInfo cacheInfo = cacheInfoDao.getCacheInfo(FAILED_REQUEST_MAP_KEY);
if(cacheInfo != null){
Map<String, QisdaApiRequest> dbFailedRequestMap = (Map<String, QisdaApiRequest>) cacheInfo.getCacheValue();
failedRequestMap.putAll(dbFailedRequestMap);
log.info("当前发送通知失败指令:" + failedRequestMap);
}
}
}
/**
* 获取失败列表,进行定时发送
*/
private static void sendFailedRequest() throws ApiException {
List<QisdaApiRequest> failedList = new ArrayList<>();
failedList.addAll(failedRequestMap.values());
failedList.sort(new Comparator<QisdaApiRequest>() {
@Override
public int compare(QisdaApiRequest o1, QisdaApiRequest o2) {
return o1.getDate().compareTo(o2.getDate()) ;
}
});
for (QisdaApiRequest apiRequest : failedList) {
//log.info("重发通知指令[" + apiRequest.getMapKey() + "]到Qisda");
String result = HttpHelper.postParam(apiRequest.getUrl(),apiRequest.getParamMap());
//log.info("重发通知指令[" + apiRequest.getMapKey() + "]到Qisda返回:" + result);
String resultStr = XmlUtil.getNodeBody("string", result);
if(resultStr.startsWith("-1") || resultStr.startsWith("0")){
//log.info(apiRequest.getMapKey() + "Qisda接口调用失败,需要进行重发");
}else{
QisdaCache.removeFailedRequest(apiRequest);
return;
}
}
}
private synchronized static void saveRfidDnMap(){
cacheInfoDao.updateCacheItem(RFID_DN_MAP_KEY,rfidDnMap);
}
@Autowired
public void setCacheInfoDao(ICacheInfoDao cacheInfoDao) {
QisdaCache.cacheInfoDao = cacheInfoDao;
}
@Autowired
public void setdNItemDao(IDNItemDao dNItemDao) {
QisdaCache.dNItemDao = dNItemDao;
}
}