TaskService.java
89.0 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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
package com.myproject.webapp.controller.webService;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Strings;
import com.google.common.collect.*;
import com.myproject.api.ITaskNotify;
import com.myproject.bean.CodeBean;
import com.myproject.bean.json.LiteOrder;
import com.myproject.bean.json.LiteOrderItem;
import com.myproject.bean.qisda.AppendInfo;
import com.myproject.bean.qisda.InquiryShelfBean;
import com.myproject.bean.update.*;
import com.myproject.bean.update.qisda.DNInfo;
import com.myproject.bean.update.qisda.DNItem;
import com.myproject.bean.update.qisda.OutInfo;
import com.myproject.bean.update.qisda.OutItem;
import com.myproject.bean.utils.BoxStatusBean;
import com.myproject.bean.utils.StatusBean;
import com.myproject.dao.mongo.*;
import com.myproject.dao.mongo.qisda.IDNItemDao;
import com.myproject.dao.mongo.qisda.IOutInfoDao;
import com.myproject.dao.mongo.qisda.IOutItemDao;
import com.myproject.exception.ValidateException;
import com.myproject.manager.IBarcodeManager;
import com.myproject.manager.IComponentManager;
import com.myproject.manager.IHumitureManager;
import com.myproject.manager.IStoragePosManager;
import com.myproject.model.User;
import com.myproject.service.UserManager;
import com.myproject.util.HttpHelper;
import com.myproject.util.QisdaApi;
import com.myproject.util.StorageConstants;
import com.myproject.webapp.controller.qisda.QisdaController;
import com.myproject.webapp.controller.qisda.util.OutInfoCache;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.RandomUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
/**
* 出入库操作队列缓存等
*/
@Service
public class TaskService implements ITaskService {
private ObjectMapper mapper = new ObjectMapper();
@Autowired
private IStoragePosManager storagePosManager;
@Autowired
private IHumitureManager humitureManager;
@Autowired
private IBarcodeManager barcodeManager;
@Autowired
private IComponentManager componentManager;
@Autowired
private IDataLogDao dataLogDao;
@Autowired
private ILiteOrderDao liteOrderDao;
@Autowired
private ILiteOrderItemDao liteOrderItemDao;
@Autowired
private DataCache dataCache;
@Autowired
private IAlarmInfoDao alarmInfoDao;
@Autowired
private UserManager userManager;
@Autowired
private IDNItemDao dNItemDao;
@Autowired
private IOutItemDao outItemDao;
@Autowired
private OutInfoCache outInfoCache;
@Autowired
private IOutInfoDao outInfoDao;
/**
* 任务队列,Key 为dataLog的ID,value 为本区域待执行的任务
*/
private static Map<String, DataLog> taskMap = Maps.newConcurrentMap();
/**
* 正在执行的任务列表rowKey 为 cid, columnKey 为 ReelId 即 barcode, Value为 DataLog
*/
//private static Map<String, DataLog> executingTaskMap = Maps.newConcurrentMap();
/**
* 完成的任务列表Key 为为 ReelId 即 barcode, Value 为 Datalog
*/
private static Map<String,DataLog> finishedTaskMap = Maps.newConcurrentMap();
/**
* 状态 map,key为 cid value 为状态 Bean
*/
protected static Map<String, StatusBean> statusMap = Maps.newConcurrentMap();
protected final transient Logger log = LogManager.getLogger(getClass());
/**
* CID的设备故障消息(key 为 cid)
*/
private static Map<String, String> clientMsgs = new ConcurrentHashMap<>();
/**
* CID的服务器消息(key 为 cid)
*/
private static Map<String, String> serverMsgs = new ConcurrentHashMap<>();
/**
* 正在执行的liteOrderMap, key 为orderNo,value 为order
*/
public static Map<String, LiteOrder> liteOrderMap = new ConcurrentHashMap<>();
/**
* 当前正在执行的DN单信息(默认是纯入库)
*/
// private static DNInfo currentDnInfo = new DNInfo();
/**
* 获取当前正在执行的DN单信息
*/
// @Override
// public DNInfo getCurrentDnInfo(){
// return currentDnInfo;
// }
//
// /**
// * 切换收料的DN单
// * @param dnInfo
// */
// @Override
// public void changeCurrentDnInfo(DNInfo dnInfo){
// String currentDnNo = currentDnInfo.getDnNo();
// String newDnNo = dnInfo.getDnNo();
// log.info("切换当前DN单["+currentDnNo+"]为:" + newDnNo);
// currentDnInfo = dnInfo;
// }
/**
* 更新客户端发上来的消息(设备故障等消息)
*/
public static void updateClientMsg(String cid, String clientMsg){
if(clientMsg == null){
clientMsg = "";
}
clientMsgs.put(cid,clientMsg);
}
/**
* 获取某区域正在执行的任务
*/
@Override
public Collection<DataLog> getQueueTasks(){
return taskMap.values();
}
/**
* 获取某区域已完成的任务
*/
@Override
public List<DataLog> getFinishedTasks(){
List<DataLog> areaFinishedTasks = Lists.newArrayList(finishedTaskMap.values());
List<DataLog> resultTasks = Lists.newArrayList();
for(DataLog task : areaFinishedTasks){
if(task.needRemoveFromCache()){
finishedTaskMap.remove(task.getBarcode());
}else{
resultTasks.add(task);
}
}
return resultTasks;
}
/**
* 获取所有任务
*/
@Override
public List<DataLog> getAllTasks(){
Collection<DataLog> queueTasks = getQueueTasks();
List<DataLog> allTasks = getFinishedTasks();
if(!queueTasks.isEmpty()){
allTasks.addAll(queueTasks);
}
return allTasks;
}
@Override
public DataLog getFinishedTask(String barcode){
if(barcode == null){
return null;
}
return finishedTaskMap.get(barcode);
}
/**
* 更新任务(状态,位置信息等)
*/
@Override
public void updateFinishedTask(DataLog task){
//DataLog cacheTask = finishedTaskMap.get(task.getBarcode());
if(task == null){
log.error("更新["+task.getBarcode()+"]任务时,任务不存在");
}else{
if(task.isFinished()){
finishedTaskMap.remove(task.getBarcode());
log.info("任务["+task.getBarcode()+"]完成,更改状态,清理缓存");
}else{
finishedTaskMap.put(task.getBarcode(), task);
}
}
}
/**
* 流水线入库:优先查找空闲BOX中同尺寸的,如果找不到,再查找可入库 BOX(可用且不是出库状态) 同尺寸或比盘尺寸大的仓位
*/
public StatusBean putInLine(Storage storage, StatusBean statusBean){
String errorMsg = "";
String barcode = statusBean.getCode();
try {
Barcode barcodeSave = resolveBarcode(barcode);
//入库时的RFID
String inRFID = statusBean.getInRfid();
verifyBarcodePutIn(Lists.<Storage>newArrayList(storage),barcodeSave,inRFID);
int w = barcodeSave.getPlateSize();
int h = barcodeSave.getHeight();
if(!storage.canPutIn(w, h)){
throw new ValidateException("尺寸["+w+"x"+h+"]不符");
}
StoragePos storagePos;
DataLog executingTask = null;
//如果有正在执行的任务,把库位发过去
for (DataLog task : taskMap.values()) {
if(task.getBarcode().equals(barcodeSave.getBarcode())){
//同条码,且同料仓的入库任务
if(task.isPutInTask() && task.getStorageId().equals(storage.getId())){
executingTask = task;
}else{
//同条码,但不是同料仓
log.error("条码["+barcodeSave.getBarcode()+"]任务正在执行,但任务料仓为:" + task.getStorageId() + " 请求料仓为:" + task.getStorageId());
throw new ValidateException("条码["+barcodeSave.getBarcode()+"]任务正在执行");
}
}
}
if(executingTask != null){
String posId = executingTask.getPosId();
log.info(barcodeSave.getBarcode() + " 已有任务,返回任务中的库位:" + executingTask.getPosName());
storagePos = storagePosManager.get(posId);
}else{
String posName = statusBean.getFromData("inPos");
if(!Strings.isNullOrEmpty(posName)){
log.info("指定入库到" + posName);
storagePos = storagePosManager.getByPosName(posName);
if(storagePos == null){
throw new ValidateException("库位【"+posName+"】不存在,无法入库");
}
if(!storage.getId().equals(storagePos.getStorageId())){
throw new ValidateException("库位【"+posName+"】与料仓["+storage.getCid()+"]不匹配,无法入库");
};
if(storagePos.getBarcode() != null){
throw new ValidateException("库位【"+posName+"】中已有物料,无法入库");
}
if(!storage.canPutInPos(barcodeSave.getPlateSize(),barcodeSave.getHeight(), storagePos.getW(), storagePos.getH())){
throw new ValidateException("料盘尺寸与库位【"+posName+"】不符,无法入库");
}
}else{
storagePos = findLineEmptyPosForPutIn(storage, barcodeSave);
}
String singleIn = statusBean.getFromData("singleIn");
if(!Strings.isNullOrEmpty(singleIn)){
//批量料仓单盘入库的只能单盘出,不可批量
if(singleIn.equalsIgnoreCase("true")){
log.info(barcodeSave.getBarcode() + "单盘入库,出库时此条码只可单盘出库");
barcodeSave.setOnlySingleOut(true);
barcodeManager.save(barcodeSave);
}
}
addPutInTaskToExecute(storage, barcodeSave, storagePos);
}
String posId = storagePos.getPosName();
int plateW = barcodeSave.getPlateSize();
int plateH = barcodeSave.getHeight();
statusBean.addPosInfo(posId,plateW,plateH,false);
log.info(barcode+"["+plateW+"x"+plateH + "]开始入库到"+storage.getCid()+"["+posId+"]");
//清空展示的消息
serverMsgs.put(storage.getCid(),"");
} catch (ValidateException e) {
log.warn("入库到"+storage.getCid() + "失败:"+e.getMessage());
statusBean.setMsg(e.getMessage());
serverMsgs.put(storage.getCid(),e.getMessage());
errorMsg=e.getMessage();
} catch (Exception e) {
log.error(statusBean.getCode() + "入库到"+storage.getCid() + "失败", e);
statusBean.setMsg(e.getMessage());
serverMsgs.put(storage.getCid(),e.getMessage());
errorMsg=e.getMessage();
}
if(!errorMsg.isEmpty()){
//有错误,记录日志
AlarmInfo alarmInfo = new AlarmInfo();
alarmInfo.setBoxId("0");
alarmInfo.setStorageName(storage.getName());
alarmInfo.setInOutStatus("1");
alarmInfo.setAlarmType("入库失败");
Date date = new Date();
alarmInfo.setStartTime(date);
alarmInfo.setEndTime(date);
String msg = "["+barcode+"]"+errorMsg;
alarmInfo.setAlarmMsg(msg);
alarmInfoDao.save(alarmInfo);
}
return statusBean;
}
private Barcode findFixtureCode(Collection<CodeBean> codeBeans) throws ValidateException {
if(codeBeans.isEmpty()){
throw new ValidateException("未扫描到条码");
}
Barcode fixtureCode = null;
for (CodeBean codeBean : codeBeans) {
Barcode barcode = codeBean.getBarcode();
if(codeBean.isFixtureCode()){
if (fixtureCode == null) {
fixtureCode = barcode;
log.info("找到夹具编码:"+ fixtureCode.getBarcode());
}else{
String msg = "出现两个夹具编码["+fixtureCode.getBarcode()+"]和["+barcode.getBarcode()+"]";
log.info(msg);
throw new ValidateException(msg);
}
}else{
if(barcode == null){
log.info("条码为空,Error:"+ codeBean.getError());
}else{
log.info(barcode.getBarcode() + "不是夹具" + "codeBean Error:" + codeBean.getError() + " type:"+ barcode.getType());
}
}
}
return fixtureCode;
}
private Barcode resolveBarcode(String barcodeStr)throws ValidateException {
Collection<CodeBean> codeBeans = dataCache.resolveCodeStr(barcodeStr);
Barcode fixtureCode = findFixtureCode(codeBeans);
List<Barcode> allBarcode = Lists.newArrayList();
for (CodeBean codeBean : codeBeans) {
if(!codeBean.isFixtureCode()){
if(codeBean.getError() == null){
Barcode barcode = codeBean.getBarcode();
if(barcode != null){
if(fixtureCode != null){//有夹具条码,判断夹具与编码是否一致
//物料对应无夹具,或夹具与此夹具不一致,不可放入
if(Strings.isNullOrEmpty(barcode.getFixtureNumber()) || !fixtureCode.getPartNumber().equals(barcode.getFixtureNumber())){
throw new ValidateException(barcode.getPartNumber() + "不能放入夹具"+fixtureCode.getPartNumber()+"中");
}
fixtureCode.addRelationCode(barcode.getBarcode());
}else{
//条码中无夹具,但物料需要放入夹具
if(!Strings.isNullOrEmpty(barcode.getFixtureNumber())){
throw new ValidateException(barcode.getPartNumber() + "需要放入对应夹具中才可入库");
}
}
allBarcode.add(barcode);
}
}else{
//throw new ValidateException(codeBean.getError());
}
}
}
//有夹具条码,可以直接返回
if(fixtureCode != null){
return fixtureCode;
}
int codeSize = allBarcode.size();
if(codeSize == 0){
throw new ValidateException(barcodeStr + "不是有效的条码");
}else if(codeSize > 1){
throw new ValidateException("找到"+codeSize+"个有效条码,无法入库");
}
return allBarcode.get(0);
}
/**
* 检查二维码是否合法并且可以入库,没问题的话存入到数据库
*/
private Barcode verifyBarcodePutIn(List<Storage> storageList, Barcode barcodeSave, String inRFID) throws ValidateException {
if(barcodeSave == null){
throw new ValidateException("条码无效");
}
// Date expireDate = barcodeSave.getExpireDate();
// if(expireDate != null){
// if(System.currentTimeMillis() > expireDate.getTime()){
// throw new ValidateException("物料已过期,无法入库.");
// }
// }
// if(barcodeSave.getPlateSize() <=0 || barcodeSave.getHeight() <= 0){
// throw new ValidateException("无法入库,请先设置料盘尺寸");
// }
StoragePos pos;
//夹具,查询 relationCode
if(StorageConstants.COMPONENT_TYPE.FIXTURE == barcodeSave.getType()){
pos = storagePosManager.getByFixtureCode(barcodeSave.getBarcode());
}else {
pos = storagePosManager.getByBarcodeId(barcodeSave.getId());
if (barcodeSave.getAmount() <= 0) {
throw new ValidateException("条码[" + barcodeSave + "]对应的数量<=0为: " + barcodeSave.getAmount());
}
}
if (pos != null) {
Storage storage = dataCache.getStorageById(pos.getStorageId());
throw new ValidateException("[ " + barcodeSave.getBarcode() + "]已在"+storage.getName()+"["+pos.getPosName()+"]中");
}
Collection<DataLog> queueTasks = getQueueTasks();
List<DataLog> allTasks = getFinishedTasks();
if(!queueTasks.isEmpty()){
allTasks.addAll(queueTasks);
}
for (DataLog task : allTasks) {
if(task.isPutInTask()){
if(task.getBarcode().equals(barcodeSave.getBarcode())){
//同一个条码的入库任务
for (Storage storage : storageList) {
if(task.getStorageId().equals(storage.getId()) && task.isPutInTask()){
return barcodeSave;
}
}
throw new ValidateException("料盘[ " + barcodeSave.getBarcode() + "]的操作未完成,无法执行入库操作");
}
}
}
AppendInfo appendInfo = barcodeSave.getAppendInfo();
String oldDnNo = appendInfo.getDnNo();
// if(Strings.isNullOrEmpty(oldDnNo)){
// //已经有旧的DN单,使用纯入库操作
// appendInfo.setCisIn(true);
// }else{
appendInfo.setCisIn(false);
//}
//DN单验证
appendInfo.setDnNo("");
appendInfo.setFacility("");
DNInfo dnInfo = QisdaController.getDnInfo(inRFID);
if(dnInfo.isDNIn()){
String pn = barcodeSave.getPartNumber();
DNItem dnItem = dnInfo.getItem(pn);
if(dnItem == null){
String msg = "DN单["+dnInfo.getDnNo()+"]中无["+pn+"],无法收料";
log.info(msg);
throw new ValidateException(msg);
}
appendInfo.setDnNo(dnInfo.getDnNo());
appendInfo.setFacility(dnItem.getFacility());
appendInfo.setCompany(dnItem.getCompany());
}else if(dnInfo.isFacilityIn()){
appendInfo.setDnNo(dnInfo.getDnNo());
appendInfo.setFacility(dnInfo.getFacility());
appendInfo.setCompany(dnInfo.getCompany());
}else{
appendInfo.setDnNo("");
}
log.info("[" + inRFID + "]"+ barcodeSave.getBarcode() + " 上次入库DN单:" + oldDnNo + " 当前入库DN单:" + appendInfo.getDnNo());
barcodeSave.setAppendInfo(appendInfo);
//3. CIS入库判定接口 (没绑过料串的条码调用此接口)
//6. CIS收料判定接口(绑过料串的条码扫码入库时判断)
if(DataCache.isProductionFor(DataCache.CUSTOMER.QISDA)){
if(barcodeSave.needToQisda()){
Barcode result = QisdaApiController.CISInCheck(dnInfo, barcodeSave);
if(result != null){
barcodeSave = result;
}
barcodeSave = barcodeManager.save(barcodeSave);
}else{
log.info("条码["+barcodeSave.getBarcode()+"]不需要到Qisda验证");
appendInfo.setFacility("AAA");
barcodeSave.setAppendInfo(appendInfo);
barcodeSave = barcodeManager.save(barcodeSave);
}
}else{
log.info("Qisda接口验证已关闭");
}
return barcodeSave;
}
/**
* 查找可以入库的空位
* @param storageList
* @param barcode
* @return
*/
@Override
public StoragePos findEmptyPosForPutIn(List<Storage> storageList, Barcode barcode, String inRFID) throws ValidateException{
verifyBarcodePutIn(storageList ,barcode, inRFID);
//如果有正在执行的任务,把库位发过去
Collection<DataLog> allTasks = taskMap.values();
for (DataLog task : allTasks) {
if(barcode.getBarcode().equals(task.getBarcode())){
String posId = task.getPosId();
log.info(barcode.getBarcode() + " 已有任务,返回任务中的库位:" + task.getPosName());
return storagePosManager.get(posId);
}
}
//查找任务数最少的料仓
Map<String,Integer> countMap = new HashMap<>();
for (Storage storage : storageList) {
countMap.put(storage.getId(), 0);
}
//Set<String> hasOutTaskStorageIds = new HashSet<>();
for (DataLog task : allTasks) {
String storageId = task.getStorageId();
if(!Strings.isNullOrEmpty(storageId)){
Integer taskCount = countMap.get(storageId);
if(taskCount != null){
taskCount = taskCount + 1;
countMap.put(storageId, taskCount);
}
// if(task.isCheckOutTask()){
// hasOutTaskStorageIds.add(storageId);
// }
}
}
//可用的料仓(在线,且可以放入)
List<Storage> availbleStorageList = new ArrayList<>();
for(Storage storage : storageList){
StatusBean status = getStatus(storage.getCid());
if(status.timeOut()){
continue;
}
// if(hasOutTaskStorageIds.contains(storage.getId())){
// //流水线有出库任务的,不分配入库任务(出库优先)
// continue;
// }
if(storage.canPutIn(barcode.getPlateSize(),barcode.getHeight())){
availbleStorageList.add(storage);
}
}
for (Storage storage : availbleStorageList) {
String storageId = storage.getId();
Integer taskCount = countMap.get(storageId);
if(taskCount >= 2){
continue;
}
try{
log.info("尝试从"+storage.getName()+"["+storage.getCid()+"]查找空位,当前料仓任务数:" + taskCount);
return findLineEmptyPosForPutIn(storage,barcode);
}catch(Exception e){
log.info("从"+storage.getName()+"["+storage.getCid()+"]查找空位失败:" + e.getMessage());
}
}
//只有一个料仓可以入时,只能继续往里面放
log.info("可用料仓太少,不按任务数分配,重新查找...");
for (Storage storage : availbleStorageList) {
try{
log.info("不按任务数分配,尝试从"+storage.getName()+"["+storage.getCid()+"]查找空位");
return findLineEmptyPosForPutIn(storage,barcode);
}catch(Exception e){
log.info("从"+storage.getName()+"["+storage.getCid()+"]查找空位失败:" + e.getMessage());
}
}
return null;
}
/**
* 为 barcode 查找流水线料仓中空闲的仓位
*/
private StoragePos findLineEmptyPosForPutIn(Storage storage, Barcode barcode) throws ValidateException {
String storageCid = storage.getCid();
//先查找空闲 BOX同尺寸的,如果找不到,再查找可入库 BOX 同尺寸或比盘尺寸大的仓位
StatusBean statusBean = statusMap.get(storageCid);
if (statusBean == null) {//当前料仓不可用
throw new ValidateException("料仓[ " + storageCid + "]不可用");
}
//还需要排除掉正在队列里的仓位
StoragePos storagePos = null;
if(statusBean.isBoxCanPutIn()){
log.info("从"+storage.getName()+"中为"+barcode.getBarcode()+"寻找空的仓位");
Collection<String> operatingPosIds = excludePosIds();
storagePos = storagePosManager.getEmptyPosByStorage(storage, barcode , operatingPosIds);
}else{
throw new ValidateException(storage.getName() + "的状态为不可入库");
}
if (storagePos == null) {
throw new ValidateException(storage.getName() + "的料格["+barcode.getPlateSize() +"x" + barcode.getHeight() +"]已满,无法继续放入");
}
log.info("["+ barcode.getBarcode() + "]寻找到"+storage.getName()+"的空仓位["+storagePos.getPosName()+"]");
return storagePos;
}
/**
* 出库处理
*/
public StatusBean checkOut(Storage storage, StatusBean statusBean){
try{
String cid = storage.getCid();
if(statusBean.canCheckout()){
DataLog task = findCheckoutBoxTask(cid);
if (task != null) {
statusBean.setOp(StorageConstants.OP.CHECKOUT);
String posName = task.getPosName();
//taskService.updateBoxStatus(statusBean.getCid(),posName, StorageConstants.OP.CHECKOUT);
Barcode codeObj = barcodeManager.findByBarcode(task.getBarcode());
int plateW = 0;
int plateH = 0;
//是否是单盘出库,批量上下料使用
boolean isSingleOut = task.isSingleOut();
if(codeObj != null){
plateW = codeObj.getPlateSize();
plateH = codeObj.getHeight();
if(codeObj.isOnlySingleOut()){
log.info(codeObj.getBarcode() + " 只能单盘出库");
isSingleOut = true;
}
}else{
log.warn("出库无料仓位"+storage.getName()+"["+posName+"]");
}
statusBean.addPosInfo(posName,plateW,plateH, isSingleOut);
AppendInfo appendInfo = task.getAppendInfo();
//紧急料
statusBean.addData("urgentReel",task.isUrgentReel() + "");
//需要分盘,进入分盘料
statusBean.addData("cutReel",task.isCutReel() + "");
statusBean.addData("rfid", task.getTempRfid());
statusBean.addData("realRfid",appendInfo.getRfid());
statusBean.addData("rfidLoc", appendInfo.getRfidLoc() + "");
statusBean.addData("barcode", task.getBarcode());
boolean smallReel = task.isSmallReel();
if(storage.isPackage()){
smallReel = false;
}
statusBean.addData("smallReel", smallReel+"");
task.setStatus(StorageConstants.OP_STATUS.EXECUTING.name());
taskMap.put(task.getId(), task);
dataLogDao.save(task);
log.info("出库"+storage.getName()+"["+posName+"]物料["+task.getBarcode()+"]"+isSingleOut+"发送到客户端" + cid);
}
}
String posId = statusBean.getData().get("posId");
if(!Strings.isNullOrEmpty(posId)){
log.info("SEQ:" + statusBean.getSeq() + "出库库位信息:["+posId+"]发送到客户端");
}
}catch (Exception e){
log.error("出库出错",e);
}
return statusBean;
}
// /**
// * 从库位中查找20条呆滞料,并生成任务保存到数据库中,返回生成的任务数
// */
// private List<DataLog> saveInactionTasks(InactionTaskSet inactionTaskSet){
// int day = inactionTaskSet.getDay();
// List<String> storageIds = dataCache.needClearInactionStorageIds();
// //每个料仓找5个
// int count = 3;
// List<StoragePos> list = Lists.newArrayList();
// for(String storageId : storageIds){
// List<StoragePos> storageList = storagePosManager.findInaction(Lists.<String>newArrayList(storageId), day, excludePosIds(), count);
// list.addAll(storageList);
// }
// List<DataLog> tasks = Lists.newArrayList();
// for (StoragePos pos : list){
// DataLog task = newTask(pos);
// task.setType(StorageConstants.OP.CHECKOUT);
// task.setStatus(StorageConstants.OP_STATUS.WAIT.name());
// //来源
// task.setSourceType(StorageConstants.TASK_SOURCE.INACTION.name());
// task.setSourceId(inactionTaskSet.getId());
// task.setSourceName(inactionTaskSet.getName());
// //将天数放在 subSourceId 中
// task.setSubSourceId(day+"");
// task.setSubSourceInfo("");
// tasks.add(task);
// }
// if(!tasks.isEmpty()){
// dataLogDao.insertAll(tasks);
// }
// return tasks;
//
// }
public DataLog newTask(StoragePos pos){
DataLog task = new DataLog();
Barcode barcode = pos.getBarcode();
if(barcode != null){
task.setPartNumber(barcode.getPartNumber());
task.setBarcode(barcode.getBarcode());
task.setNum(barcode.getAmount());
task.setW(barcode.getPlateSize());
task.setH(barcode.getHeight());
AppendInfo appendInfo = barcode.getAppendInfo();
String shelfType = InquiryShelfBean.getShelfType(task);
appendInfo.setShelfType(shelfType);
if(barcode.hasCutInfo()){
task.setCutReel(true);
}
task.setAppendInfo(appendInfo);
}
Storage storage = dataCache.getStorageById(pos.getStorageId());
if(storage.isPackage()){
//包装料仓
task.setPackageReel(true);
}
task.setCid(storage.getCid());
task.setStorageId(storage.getId());
task.setStorageName(storage.getName());
task.setPosId(pos.getId());
task.setPosName(pos.getPosName());
return task;
}
private boolean cancelTask(DataLog task){
if(task != null){
//从正在执行和等待列表中移除
taskMap.remove(task.getId());
String barcode = task.getBarcode();
if(!Strings.isNullOrEmpty(barcode)){
finishedTaskMap.put(task.getBarcode(), task);
}
task.setStatus(StorageConstants.OP_STATUS.CANCEL.name());
dataLogDao.save(task);
log.info("任务["+task.getId() + "] posName["+task.getPosName()+"] Reel Id["+task.getBarcode()+"]取消成功");
finishedOrderTask(task);
InquiryShelfBean.cancelReelTask(task);
return true;
}
return false;
}
@Override
public boolean cancelTask(String taskId) {
DataLog task = dataLogDao.findOneById(taskId);
if(task.isCheckOutTask()){
log.info(task.getBarcode() + "出库任务取消失败: 不允许取消");
return false;
}
return cancelTask(task);
}
@Override
public void hideTask(String taskId) {
DataLog task = dataLogDao.findOneById(taskId);
if(task.isCheckOutTask()){
//发送取消(发料)指令到佳世达
AppendInfo taskAppendInfo = task.getAppendInfo();
if(taskAppendInfo.isFirstReelAction() || taskAppendInfo.isTailAction()){
Barcode barcode = barcodeManager.findByBarcode(task.getBarcode());
log.info("["+task.getBarcode()+"]任务已出库完成,但未放上小车,发送E状态到佳世达,同时解除料架架位,发料任务完成数量+1");
OutInfo outInfo = outInfoDao.findByHSerial(taskAppendInfo.gethSerial());
if(outInfo != null) {
outInfo.setTaskFinishNum(outInfo.getTaskFinishNum() + 1);
log.info("需求单[" + outInfo.gethSerial() + "]任务完成数量:" + outInfo.getTaskFinishNum() + "/" + outInfo.getTaskNum() + "已出仓料盘数量:" + outInfo.getOutReelNum());
outInfoDao.save(outInfo);
InquiryShelfBean.cancelReelTask(task);
QisdaApi.VMIMateriaRecAss(task, barcode, "E");
}
}
finishedTaskMap.remove(task.getBarcode());
}
}
/**
* 获取正在执行出库(或入库)的 boxID,用作出入库时分配仓位,防止卡死的问题
*/
private Set<Integer> getExcutingBoxIds(String cid, int type){
Set<Integer> excuteBoxIds = Sets.newHashSet();
for (DataLog task : taskMap.values()){
if(type == task.getType()){
String posName = task.getPosName();
int index = posName.indexOf("#");
if(index>0){
String boxId = posName.substring(0,index);
excuteBoxIds.add(Integer.valueOf(boxId));
}
}
}
return excuteBoxIds;
}
/**
* 防止仓位任务重复,需要排除掉已经分配掉的仓位
*/
@Override
public Collection<String> excludePosIds(){
//排除掉正在执行的仓位
Collection<DataLog> allTasks = new ArrayList<>(taskMap.values());
// try{
// for (DataLog finishTask : finishedTaskMap.values()) {
// allTasks.add(finishTask);
// }
// }catch (Exception e){
// log.error("获取排除库位出错",e);
// }
Collection<String> operatingPosIds = new HashSet<>();
for(DataLog task : allTasks){
String posId = task.getPosId();
if(!Strings.isNullOrEmpty(posId)){
operatingPosIds.add(task.getPosId());
}
}
return operatingPosIds;
}
/**
* 为 box 分配出库任务
*/
private DataLog findCheckoutBoxTask(String cid) {
Storage storage = dataCache.getStorage(cid);
if(storage == null){//该 Box 已经被限制出入库
return null;
}
int checkoutSize = 0;
Collection<DataLog> waitTasks = taskMap.values();
for(DataLog task : waitTasks){
//如果该BOX在已执行任务中已经有入库任务,不再分配直接返回
if(cid.equals(task.getCid()) && task.isExecuting()){
if(task.isPutInTask()){
//log.error("cid["+cid + "]box["+boxId+"]已有入库任务,不可再分配出库任务");
return null;
}else if(task.needReSendToClient()){//超过30秒仍未完成的出库再次发送到客户端
log.error("cid["+cid + "]的出库任务["+ task.getPosName()+"]超过60秒仍未完成,重新发送到客户端!");
task.setUpdateDate(new Date());
return task;
}
//某个 Box 只能同时有两个正在执行的出库任务,如果超过两个不再分配了
if(task.isCheckOutTask()){
checkoutSize ++;
if(checkoutSize >=2){
//log.error("cid["+cid + "]的BOX["+ boxId+"]的出库任务已经超过2个,不再分配!");
return null;
}
}
}
}
//if(DataCache.isProductionFor(DataCache.CUSTOMER.QISDA)){
//分盘料,按时间顺序进行出库
DataLog urgentTask = null;
for (DataLog task : waitTasks) {
if(cid.equals(task.getCid()) && task.isCheckOutTask() && task.isWait()) {
//分盘料,紧急料和包装料,可以按时间顺序先出
if(task.isCutReel() || task.isUrgentReel() || task.isPackageReel()){
if(urgentTask == null || urgentTask.getCreateDate().after(task.getCreateDate())){
urgentTask = task;
}
}
}
}
if(urgentTask != null){
log.info("出库最先生成的分盘/紧急/包装料任务"+urgentTask.getBarcode()+"["+urgentTask.getPosName()+"]");
return urgentTask;
}
//按料架顺序,从小盘开始出库
boolean hasFirstReelAction = false;
//编号最小的料架
int minCRfidIndex = -1;
int minDRfidIndex = -1;
int minARfidIndex = -1;
for (DataLog task : waitTasks) {
if(task.isCheckOutTask()) {
AppendInfo appendInfo = task.getAppendInfo();
if(appendInfo.isFirstReelAction()){
//首盘料
hasFirstReelAction = true;
}
int rfidIndex = appendInfo.getRfidIndex();
boolean isAShelf = StorageConstants.SHEFL_TYPE.isAShelf(appendInfo.getShelfType());
if(isAShelf){
//包装料最小的料架编号
if(minARfidIndex == -1 || rfidIndex < minARfidIndex){
minARfidIndex = rfidIndex;
}
}
boolean isDShelf = StorageConstants.SHEFL_TYPE.isDShelf(appendInfo.getShelfType());
if(isDShelf){
//小料架
if(minDRfidIndex == -1 || rfidIndex < minDRfidIndex){
minDRfidIndex = rfidIndex;
}
}
boolean isCShelf = StorageConstants.SHEFL_TYPE.isCShelf(appendInfo.getShelfType());
if(isCShelf){
//大料架
if(minCRfidIndex == -1 || rfidIndex < minCRfidIndex){
minCRfidIndex = rfidIndex;
}
}
}
}
if(hasFirstReelAction){
return findFirstActionask(storage, minARfidIndex,minDRfidIndex, minCRfidIndex);
}else{
//补料
return findTailActionTask(storage, minARfidIndex,minDRfidIndex, minCRfidIndex);
}
//}
}
/**
* 查找包装料最小料架的任务
*/
private DataLog findPackageMinTask(Storage storage, int minARfidIndex){
DataLog storageTask = null;
if(storage.isPackage()){
//找当前料仓最小料架,最小架位的
for (DataLog task : taskMap.values()) {
if (storage.getCid().equals(task.getCid()) && task.isCheckOutTask() && task.isWait()) {
//按料架顺序
if(minARfidIndex != -1){
AppendInfo appendInfo = task.getAppendInfo();
int rfidIndex = appendInfo.getRfidIndex();
boolean isAShelf = StorageConstants.SHEFL_TYPE.isAShelf(appendInfo.getShelfType());
if(isAShelf && rfidIndex <= minARfidIndex){
if(storageTask == null || appendInfo.getRfidLoc() < storageTask.getAppendInfo().getRfidLoc()){
storageTask = task;
}
}
}
}
}
if(storageTask != null){
log.info("出库首盘料任务[包装料]"+storageTask.getBarcode()+"["+storageTask.getPosName()+"]" + storageTask.getAppendInfo());
}
}
return storageTask;
}
/**
* 查找首盘料出库任务
*/
private DataLog findFirstActionask(Storage storage, int minARfidIndex, int minDRfidIndex, int minCRfidIndex){
DataLog storageTask = null;
//包装料仓
if(storage.isPackage()){
//包装料仓的最小料架的任务
return findPackageMinTask(storage, minARfidIndex);
}else{
//首套料需要先出D料架,再出C料架,并且要按料架顺序
//架位顺序随机,保证左右机器人均衡
boolean locAsc = RandomUtils.nextBoolean();
for (DataLog task : taskMap.values()) {
if(storage.getCid().equals(task.getCid()) && task.isCheckOutTask() && task.isWait()){
AppendInfo appendInfo = task.getAppendInfo();
int rfidIndex = appendInfo.getRfidIndex();
if(minDRfidIndex != -1){
//有小料架(D),出最小的D料架
boolean isDShelf = StorageConstants.SHEFL_TYPE.isDShelf(appendInfo.getShelfType());
if(isDShelf && rfidIndex <= minDRfidIndex){
if(storageTask == null){
storageTask = task;
}else{
if(locAsc){
//升序,取最小的架位
if(appendInfo.getRfidLoc() < storageTask.getAppendInfo().getRfidLoc()){
storageTask = task;
}
}else{
//降序,取最大的架位
if(appendInfo.getRfidLoc() > storageTask.getAppendInfo().getRfidLoc()){
storageTask = task;
}
}
}
}
}else{
//没有小料架(D),那么可以出C料架了
boolean isCShelf = StorageConstants.SHEFL_TYPE.isCShelf(appendInfo.getShelfType());
if(isCShelf && rfidIndex <= minCRfidIndex){
if(storageTask == null){
storageTask = task;
}else{
if(locAsc){
//升序,取最小的架位
if(appendInfo.getRfidLoc() < storageTask.getAppendInfo().getRfidLoc()){
storageTask = task;
}
}else{
//降序,取最大的架位
if(appendInfo.getRfidLoc() > storageTask.getAppendInfo().getRfidLoc()){
storageTask = task;
}
}
}
}
}
}
}
}
if(storageTask != null){
//如果双层线上有两个料架,但任务不属于这两个料架,不允许出库
if(InquiryShelfBean.canCheckOutFistActionTask(storageTask)){
log.info("出库首盘料任务"+storageTask.getBarcode()+"["+storageTask.getPosName()+"]" + storageTask.getAppendInfo());
return storageTask;
}
}
return null;
}
/**
* 查找补料任务
* @return
*/
private DataLog findTailActionTask(Storage storage, int minARfidIndex, int minDRfidIndex, int minCRfidIndex){
//包装料仓,不按顺序,可以同时出
if(storage.isPackage()){
return findPackageMinTask(storage, minARfidIndex);
}else{
//一次性把D料架出完,再出C料架,包装料可以同时出
for (DataLog task : taskMap.values()) {
if(storage.getCid().equals(task.getCid()) && task.isCheckOutTask() && task.isWait()){
//还有小料架出库任务,出小料架
if(minDRfidIndex != -1){
if(task.isSmallReel()){
log.info("出库小料任务"+task.getBarcode()+"["+task.getPosName()+"]");
return task;
}
}else{
//开始出大料架,不需要按顺序
if(!task.isSmallReel()){
//未完成的大料数量(正在执行和未放上料架的)
int unFinishedBigTaskCount = 0;
for (DataLog dataLog : taskMap.values()) {
if(!dataLog.isPackageReel() && !dataLog.isSmallReel() && dataLog.isExecuting()){
unFinishedBigTaskCount = unFinishedBigTaskCount + 1;
}
}
for (DataLog dataLog : finishedTaskMap.values()) {
if(!dataLog.isPackageReel()){
if(!dataLog.isSmallReel() && !dataLog.isFinished()){
//非包装料大料任务还未完成(未放入料架),暂时不出大料
unFinishedBigTaskCount = unFinishedBigTaskCount + 1;
}
}
}
if(InquiryShelfBean.canCheckOutTailActionTask(task, unFinishedBigTaskCount)){
log.info("出库大料任务"+task.getBarcode()+"["+task.getPosName()+"]");
return task;
}
}
}
}
}
}
return null;
}
@Override
public synchronized String checkOutLiteOrder(String orderNo, boolean outBom){
LiteOrder cacheOrder = liteOrderMap.get(orderNo);
if(cacheOrder != null && !cacheOrder.isTaskFinished() && !cacheOrder.isNew()){
log.info("工单["+orderNo+"]正在执行");
return "当前工单正在执行";
}
if(cacheOrder == null){
cacheOrder = liteOrderDao.findWithItemsByOrderNo(orderNo);
}
if(cacheOrder == null){
return "未找到工单";
}
log.info("开始执行工单["+orderNo+"] outBom="+outBom);
cacheOrder.setTaskReelCount(0);
cacheOrder.setTaskFinishedTime(-1);
cacheOrder.setFinishedReelCount(0);
if(outBom){
cacheOrder.setStatus(StorageConstants.LITEORDER_STATUS.BOM);
}else{
cacheOrder.setStatus(StorageConstants.LITEORDER_STATUS.TAILS);
}
//liteOrderMap.put(cacheOrder.getOrderNo(), cacheOrder);
int taskReelCount = 0;
StorageConstants.CHECKOUT_TYPE checkoutType = dataCache.getCheckOutType();
//其他出库模式一次性全部生成任务
for (LiteOrderItem orderItem : cacheOrder.getOrderItems()) {
//剩余未出数量
int remainNum = orderItem.getNeedNum() - orderItem.getOutNum();
//此PN未完成
if(remainNum > 0 ){
if(outBom){
//套料出库,设置剩余数量为1,这样就只会出一盘
remainNum = 1;
}
int assignNum = 0;
while (assignNum < remainNum){
Collection<String> excludePosIds = excludePosIds();
String partNumber = orderItem.getPn();
StoragePos pos = storagePosManager.findPartNumberPos(null, partNumber, excludePosIds, checkoutType);
if(pos == null){
log.error("未找到可以出库的物料["+partNumber+"]");
break;
}else{
assignNum = assignNum + pos.getBarcode().getAmount();
taskReelCount = taskReelCount + 1;
log.info("工单["+orderNo+"],任务数["+taskReelCount+"]出库位置仓位【"+pos.getPosName()+"】RI=["+pos.getBarcode().getBarcode()+"] PN=[" + partNumber + "] num:" + pos.getBarcode().getAmount());
DataLog task = newTask(pos);
task.setSourceId(cacheOrder.getId());
task.setSourceName(cacheOrder.getOrderNo());
task.setSubSourceId(orderItem.getId());
task.setSubSourceInfo(orderItem.getFeederInfo());
task.setAppendInfo(pos.getBarcode().getAppendInfo());
task.setType(StorageConstants.OP.CHECKOUT);
task.setStatus(StorageConstants.OP_STATUS.WAIT.name());
task = dataLogDao.save(task);
addTaskToExecute(task);
}
}
}
}
cacheOrder.setTaskReelCount(taskReelCount);
log.info("工单["+orderNo+"]任务分配结束,任务数["+taskReelCount+"]");
//有需要出库的
if(taskReelCount <= 0){
cacheOrder.finishedTasks();
}
liteOrderDao.save(cacheOrder);
liteOrderMap.put(cacheOrder.getOrderNo(), cacheOrder);
if(taskReelCount <= 0){
return "工单无可执行的任务";
}
return "";
}
/**
* 处理客户端发送上来的请求
*/
@Override
public StatusBean handleClientRequest(StatusBean statusBean,HttpServletRequest request) {
try {
String cid = statusBean.getCid();
Storage storage = dataCache.getStorage(cid);
if(storage == null){
log.error("料仓cid: [" + cid + "]不存在");
return null;
}
//处理客户端发上来的消息
handleMsg(statusBean);
synchronized (storage){
//log.info("reqseq:"+statusBean.getSeq());
StatusBean resultStatus = saveStatus(statusBean);
//清空 msg 的内容,因为客户端会据此决定命令是否执行
statusBean.setMsg("");
//料柜
if(storage.isCabinet() || storage.isShelf() || storage.isAccShelf()){
String cardResult = statusBean.getFromData("cardResult");
if(!Strings.isNullOrEmpty(cardResult)){
//写入卡片完成,后续再加密解密,暂就先使用用户名=授权码
try{
log.info("收到卡片写入结果【"+cardResult+"】");
String[] cardInfo = cardResult.split("=");
User user = userManager.getUser(cardInfo[0]);
if(user != null && Strings.isNullOrEmpty(user.getAuthCode())){
user.setAuthCode(cardInfo[1]);
userManager.save(user);
log.info("卡片信息【"+cardResult+"】写入成功");
}
}catch (Exception e){
log.error("卡片写入结果出错");
}
}
//出库任务开灯或者开门
Collection<DataLog> areaWaitTasks = taskMap.values();
for (DataLog task : areaWaitTasks) {
if(cid.equals(task.getCid()) && task.isCheckOutTask()){
//加入到正在执行的列表中
statusBean.addData("open", task.getPosName());
//从等待列表中删除,加入到执行列表中
task.setStatus(StorageConstants.OP_STATUS.EXECUTING.name());
taskMap.put(task.getId(), task);
dataLogDao.save(task);
}
}
return statusBean;
}
if(statusBean.getOp() == StorageConstants.OP.ALARM_DATA){
log.info("获取温湿度报警值");
statusBean.setTemperature(dataCache.getSettings().getMaxTemperature());
statusBean.setHumidity(dataCache.getSettings().getMaxHumidity());
resultStatus = statusBean;
} else if(statusBean.getOp() == StorageConstants.OP.PUT_IN){
if(!dataCache.getSettings().isStopOut()){
log.debug("入库:"+mapper.writeValueAsString(statusBean));
resultStatus = putInLine(storage, statusBean);
}else {
resultStatus.setMsg("系统更新中,暂停出入库");
serverMsgs.put(cid,"系统更新中,暂停出入库");
}
}else {
if(dataCache.needUpdateHumidiy(cid)){
log.info("发送温湿度报警值");
statusBean.setTemperature(dataCache.getSettings().getMaxTemperature());
statusBean.setHumidity(dataCache.getSettings().getMaxHumidity());
resultStatus = statusBean;
}else{
//查看是否有要出库的操作
resultStatus = checkOut(storage, resultStatus);
}
}
//log.info(mapper.writeValueAsString(resultStatus));
return resultStatus;
}
} catch (Exception e) {
log.error("", e);
}
return null;
}
/**
* 处理客户端发送上来的消息(保存故障信息,并显示界面)
* @param statusBean
*/
private void handleMsg(StatusBean statusBean){
try{
//展示到界面
String msg = statusBean.getMsg();
updateClientMsg(statusBean.getCid(),msg);
}catch (Exception e){
log.error("客户端故障消息处理出错",e);
}
}
/**
* 出库完成 status为 OUT_FINISHED = 10 出仓位完成,设置posId
* @param statusBeanToSave
* @return
*/
public StatusBean saveStatus(StatusBean statusBeanToSave) {
String cid = statusBeanToSave.getCid();
StatusBean statusBean = statusMap.get(cid);
boolean needSaveToMongo = false;
if (statusBean == null) {
statusBean = statusBeanToSave;
needSaveToMongo = true;
} else {
needSaveToMongo = statusBean.needSaveToMongo();
}
statusBean.setTime(System.currentTimeMillis());
Map<Integer, BoxStatusBean> statusOfBoxes = statusBeanToSave.getBoxStatus();
statusBean.setBoxStatus(statusOfBoxes);
statusBean.setData(statusBeanToSave.getData());
statusBean.setMsg(statusBeanToSave.getMsg());
statusBean.setStatus(statusBeanToSave.getStatus());
statusBean.setOp(statusBeanToSave.getOp());
statusBean.setSeq(statusBeanToSave.getSeq());
/**
* 已解除的报警信息存到数据库中
*/
Storage storage = dataCache.getStorage(cid);
if(storage != null){
List<AlarmInfo> endAlarmList = statusBean.getEndAlarmList(storage.getName(), statusBeanToSave.getAlarmList());
alarmInfoDao.insertAll(endAlarmList);
}
if (statusOfBoxes != null) {
for (BoxStatusBean boxStatus : statusOfBoxes.values()) {
int boxId = boxStatus.getBoxId();
//判断设备状态是否需要保存到数据库中
dataCache.updateDeviceStatus(cid, boxId, boxStatus.getData());
if (needSaveToMongo) {
//保存温湿度到数据库
Humiture humiture = new Humiture();
humiture.setCid(cid);
humiture.setBoxId(boxId);
String humidity = boxStatus.getHumidity();
String temperature = boxStatus.getTemperature();
if (!Strings.isNullOrEmpty(humidity) && !Strings.isNullOrEmpty(temperature)) {
humiture.setHumidity(humidity);
humiture.setTemperature(temperature);
try {
humitureManager.save(humiture);
statusBean.setLastSaveTime(System.currentTimeMillis());
} catch (ValidateException e) {
}
}
}
try {
//出库入库完成处理
int status = boxStatus.getStatus();
String posName = boxStatus.getPosId();
if(!Strings.isNullOrEmpty(posName)){//客户端发一次完成之后,会发空的 posName,不需要处理
if (StorageConstants.BOX_STATUS.IN_FINISHED == status) {//入仓完成
DataLog task = findExecutingTask(statusBeanToSave.getCid(), boxStatus.getPosId());
if (task != null && task.isPutInTask()) {
log.info(task.getBarcode() + "入仓位[" + task.getPosName() + "]完成");
DataLog cancelTask = findFinishedTask(statusBeanToSave.getCid(), boxStatus.getPosId());
if(cancelTask != null && cancelTask.isCancel()){
//将相同库位已经取消的任务从完成队列里删除
finishedTaskMap.remove(cancelTask.getBarcode());
log.info("从已完成的任务列表中删除之前取消的任务:"+cancelTask.getPosName() + " ReelId:" + cancelTask.getBarcode());
}
putInFinished(task);
} else {
//从已完成列表中找,如果还找不到就忽略
task = findFinishedTask(statusBeanToSave.getCid(), boxStatus.getPosId());
if (task != null) {
if(task.isCancel()){//被取消的任务,客户端发完成信号过来,修改取消状态为已完成
log.info(task.getBarcode() + "入仓位[" + task.getPosName() + "]完成,但任务已被取消,修改为完成");
putInFinished(task);
}
}else {
log.error(statusBeanToSave.getCid() + "入仓位[" + boxStatus.getPosId() + "]完成时任务不存在");
}
}
} else if (StorageConstants.BOX_STATUS.IN_FAILED == status) {//入库失败
//暂不处理
} else if (StorageConstants.BOX_STATUS.OUT_FINISHED == status) {//出仓完成
DataLog task = findExecutingTask(statusBeanToSave.getCid(), boxStatus.getPosId());
if (task != null) {
log.info(task.getBarcode() + "出仓位[" + task.getPosName() + "]完成");
DataLog cancelTask = findFinishedTask(statusBeanToSave.getCid(), boxStatus.getPosId());
if(cancelTask != null && cancelTask.isCancel()){
//将相同库位已经取消的任务从完成队列里删除
finishedTaskMap.remove(cancelTask.getBarcode());
log.info("从已完成的任务列表中删除之前取消的任务:"+cancelTask.getPosName() + " ReelId:" + cancelTask.getBarcode());
}
//dataCache.unLockOneReel(task.getCid(),task.getPartNumber());
checkoutFinished(task);
}else{
//log.error(operationKey + "触发仓位完成时,操作队列中不存在");
//从已完成列表中找,如果还找不到就忽略
task = findFinishedTask(statusBeanToSave.getCid(), boxStatus.getPosId());
if (task != null) {
if(task.isCancel()){//被取消的任务,客户端发完成信号过来,修改取消状态为已完成
log.info(task.getBarcode() + "出仓位[" + task.getPosName() + "]完成,但任务已被取消,修改为完成");
checkoutFinished(task);
}
}else {
log.warn(statusBeanToSave.getCid() + "出仓位[" + boxStatus.getPosId() + "]完成时任务不存在");
}
}
} else if (StorageConstants.BOX_STATUS.OUT_FAILED == status) {//出库失败
//暂不处理
}
}
} catch (ValidateException e) {
log.error("更新状态时出错" + e.getMessage());
}
}
}
statusMap.put(cid, statusBean);
return statusBean;
}
private DataLog findFinishedTask(String cid, String posName){
Collection<DataLog> areaFinishedTasks = getFinishedTasks();
for (DataLog task : areaFinishedTasks) {
if (task.getPosName().equals(posName) && task.getCid().equals(cid)) {
return task;
}
}
return null;
}
/**
* 根据cid 和 posName 查找正在执行的任务,不存在时返回 null
*/
private DataLog findExecutingTask(String cid, String posName) {
for (DataLog task : taskMap.values()) {
if (task.getCid().equals(cid) && task.getPosName().equals(posName)) {
return task;
}
}
return null;
}
@Override
public StatusBean getStatus(String cid) {
StatusBean statusBean = statusMap.get(cid);
if (statusBean == null || statusBean.timeOut()) {
statusBean = new StatusBean();
statusBean.setCid(cid);
statusBean.setStatus(StorageConstants.STATUS.OFFLINE);
}
Storage storage = dataCache.getStorage(cid);
if(storage != null && storage.isVirtual()){
statusBean.setStatus(StorageConstants.STATUS.READY);
statusBean.setTime(System.currentTimeMillis());
}
String serverMsg = serverMsgs.get(cid);
if(!Strings.isNullOrEmpty(serverMsg)){
statusBean.setMsg(serverMsg);
}
return statusBean;
}
@Override
public Collection<StatusBean> allStatus(){
return statusMap.values();
}
@Override
public synchronized String checkout(StoragePos pos, String subSourceId, boolean isSingleOut){
Collection<DataLog> allTasks = taskMap.values();
for(DataLog taskInList : allTasks){
if(taskInList.getPosId() == null) continue;
if(taskInList.getPosId().equals(pos.getId())){
log.info("库位【"+pos.getPosName()+"已在任务列表中,忽略】");
return "库位【"+pos.getPosName()+"已在任务列表中,忽略】";
}
}
DataLog task = newTask(pos);
//手动出库的当做是紧急料
task.setUrgentReel(true);
task.setType(StorageConstants.OP.CHECKOUT);
task.setStatus(StorageConstants.OP_STATUS.WAIT.name());
task.setSingleOut(isSingleOut);
//task.setAppendInfo(pos.getBarcode().getAppendInfo());
//工单出库任务
if(!Strings.isNullOrEmpty(subSourceId)){
LiteOrderItem liteOrderItem = liteOrderItemDao.findOneById(subSourceId);
if(liteOrderItem != null){
String orderNo = liteOrderItem.getOrderNo();
LiteOrder cacheOrder = liteOrderMap.get(orderNo);
if(cacheOrder == null || cacheOrder.isOutOne() || cacheOrder.isTaskFinished() || cacheOrder.isNew()){
if(cacheOrder == null){
//缓存中没有,加入到缓存中
cacheOrder = liteOrderDao.findWithItemsByOrderNo(orderNo);
cacheOrder.setTaskReelCount(1);
cacheOrder.setFinishedReelCount(0);
}else if(cacheOrder.isOutOne() || cacheOrder.isOutOneFinished()){
//数量+上去
cacheOrder.setTaskReelCount(cacheOrder.getTaskReelCount() + 1);
}else{
cacheOrder.setTaskReelCount(1);
cacheOrder.setFinishedReelCount(0);
}
cacheOrder.setTaskFinishedTime(-1);
log.info("订单["+orderNo+"]补料任务" + task.getBarcode() + " 任务数:" +cacheOrder.getFinishedReelCount() + "/"+ cacheOrder.getTaskReelCount());
task.setSourceId(cacheOrder.getId());
task.setSourceName(orderNo);
task.setSubSourceId(liteOrderItem.getId());
task.setSubSourceInfo(liteOrderItem.getFeederInfo());
cacheOrder.setStatus(StorageConstants.LITEORDER_STATUS.ONE);
liteOrderDao.save(cacheOrder);
liteOrderMap.put(orderNo,cacheOrder);
}else{
return "无法执行工单补料任务";
}
}else{
return "未找到工单信息";
}
}
task = dataLogDao.save(task);
// Barcode barcode = pos.getBarcode();
// if(barcode != null){
// if(barcode.needToQisda()){
// log.info("非测试物料["+barcode.getBarcode()+"]出库" + barcode.getAppendInfo());
// }else{
// log.info("测试物料["+barcode.getBarcode()+"]出库");
// if(this.getQueueTasks().isEmpty() && this.getFinishedTasks().isEmpty()){
// //没有任务了,清空料架缓存
// InquiryShelfBean.hSerialShelfMap.clear();
// }
// //测试用的料盘,分配架位
// OutItem outItem = new OutItem();
// outItem.sethSerial("Test");
// task.setSourceName("Test_AAA");
//
// task = InquiryShelfBean.addUnlimitLoc(task, outItem);
// }
// }
addTaskToExecute(task);
return "";
}
@Override
public void addTaskToFinished(StoragePos pos, Barcode barcode, String opUser){
try{
//当前库位或barcode有正在执行的任务,完成掉
//Storage storage = dataCache.getStorageById(pos.getStorageId());
if(Strings.isNullOrEmpty(opUser)){
opUser = StorageDataController.getLoginUsername();
}
Collection<DataLog> allTasks = taskMap.values();
if(pos.getBarcode() != null){
barcode = pos.getBarcode();
for(DataLog task : allTasks){
if(task.isCheckOutTask()){
String executeBarcode = task.getBarcode();
String executePosId = task.getPosId();
boolean isSameTask = false;
if(executeBarcode != null && barcode.getBarcode().equals(executeBarcode)){
isSameTask = true;
}
if(executePosId != null && pos.getId().equals(executePosId)){
isSameTask = true;
}
if(isSameTask){
log.info(executeBarcode + "找到正在执行的出库任务["+pos.getPosName()+"],直接完成");
if(task.isWait()){
taskMap.put(task.getId(),task);
}
task.setOperator(opUser);
checkoutFinished(task);
return;
}
}
}
}
//没有正在执行的任务,直接添加一条已完成的任务
DataLog task = newTask(pos);
if(pos.getBarcode() == null){
log.info(opUser + "入库【"+barcode.getBarcode()+"】到【"+pos.getPosName()+"】");
task.setType(StorageConstants.OP.PUT_IN);
barcode.setUsedCount(barcode.getUsedCount() + 1);
barcode.setUsedDate(new Date());
barcode.setPutInTime(System.currentTimeMillis());
barcode.setInOpor(opUser);
barcode.setCheckOutDate(null,"");
barcode.setPosName(task.getPosName());
barcodeManager.save(barcode);
pos.setBarcode(barcode);
pos.setUsed(true);
pos.setCanCheckOutTime(System.currentTimeMillis());
storagePosManager.save(pos);
task.setPartNumber(barcode.getPartNumber());
task.setBarcode(barcode.getBarcode());
task.setAppendInfo(barcode.getAppendInfo());
task.setNum(barcode.getAmount());
dataCache.updateInventory(pos,barcode);
//dataCache.updateStorage(task.getCid());
}else{
barcode = pos.getBarcode();
log.info(opUser + "将【"+barcode.getBarcode()+"】从【"+pos.getPosName()+"】出库");
task.setType(StorageConstants.OP.CHECKOUT);
barcode.setUsed(true);
barcode.setUsedDate(new Date());
//仓位状态
barcode.setCheckOutDate(new Date(),task.getOperator());
barcode.setPosName("");
barcodeManager.save(barcode);
pos.setBarcode(null);
pos.setUsed(false);
storagePosManager.save(pos);
dataCache.updateInventory(pos, barcode);
//dataCache.updateStorage(task.getCid());
}
task.setOperator(opUser);
task.setAppendInfo(barcode.getAppendInfo());
task.setStatus(StorageConstants.OP_STATUS.FINISHED.name());
task = dataLogDao.save(task);
finishedTaskMap.put(task.getBarcode(),task);
}catch (Exception e){
}
}
@Override
public void addTaskToExecute(DataLog task) {
if(Strings.isNullOrEmpty(task.getOperator())){
String loginUser = StorageDataController.getLoginUsername();
task.setOperator(loginUser);
}
task = dataLogDao.save(task);
taskMap.put(task.getId(),task);
}
/**
* 加入要执行的任务
*/
@Override
public synchronized DataLog addPutInTaskToExecute(Storage storage, Barcode barcode, StoragePos storagePos) throws ValidateException {
Collection<DataLog> tasks = taskMap.values();
for (DataLog task : tasks) {
//检查 barcode
if (task.getBarcode().equals(barcode.getBarcode())) {
String msg = "二维码:[" + barcode.getBarcode() + "]已在操作队列中,操作失败";
log.info(msg);
throw new ValidateException(msg);
} else if (task.getPosId().equals(storagePos.getId())) {
String msg = "位置:[" + storagePos.getPosName() + "]已在操作队列中,操作失败";
log.info(msg);
throw new ValidateException(msg);
}
}
DataLog dataLog = new DataLog();
dataLog.setStorageId(storage.getId());
dataLog.setStorageName(storage.getName());
dataLog.setBarcode(barcode.getBarcode());
dataLog.setAppendInfo(barcode.getAppendInfo());
dataLog.setRelationCodes(barcode.getRelationCodes());
dataLog.setType(StorageConstants.OP.PUT_IN);
dataLog.setNum(barcode.getAmount());
dataLog.setCid(storage.getCid());
String loginUser = StorageDataController.getLoginUsername();
dataLog.setOperator(loginUser);
dataLog.setPartNumber(barcode.getPartNumber());
dataLog.setPosId(storagePos.getId());
dataLog.setPosName(storagePos.getPosName());
dataLog.setStatus(StorageConstants.OP_STATUS.EXECUTING.name());
dataLog.setSourceName(barcode.getAppendInfo().getDnNo());
dataLogDao.save(dataLog);
taskMap.put(dataLog.getId(), dataLog);
log.info("cid:[" + storage.getCid() + "] barcode:[" + barcode.getBarcode() + "] partNumber:[" + dataLog.getPartNumber() + "]位置[" + storagePos.getPosName() + "]的入库操作成功加入队列");
return dataLog;
}
//入仓位完成
public void putInFinished(DataLog task) throws ValidateException {
StoragePos storagePos = storagePosManager.get(task.getPosId());
//二维码状态
Barcode barcode = barcodeManager.findByBarcode(task.getBarcode());
if(StorageConstants.COMPONENT_TYPE.FIXTURE == barcode.getType()){//条码是夹具
//夹具编码,需要将相应的位置全部填上
String posLabel = storagePos.getLabelStr();
List<StoragePos> allPos = storagePosManager.findByLabel(storagePos.getStorageId(), posLabel);
List<String> relationCodeStrs = task.getRelationCodes();
log.info("夹具【"+task.getBarcode()+"】入库到["+posLabel+"]完成,夹具上物料条码:"+StringUtils.join(relationCodeStrs," , "));
//此夹具的可出库时间
long canCheckOutTime = 0;
List<Barcode> relationBarcodes = Lists.newArrayList();
long now = System.currentTimeMillis();
for (String relationCodeStr : relationCodeStrs) {
Barcode relationBarcode = barcodeManager.findByBarcode(relationCodeStr);
//入库时间,如果之前已入过库是不会更改的
relationBarcode.setPutInTime(now);
relationBarcode.setInOpor(task.getOperator());
long reachWarmTime = relationBarcode.getReachWarmTime();
if(reachWarmTime > canCheckOutTime){
canCheckOutTime = reachWarmTime;
}
relationBarcodes.add(relationBarcode);
}
int relationCount = relationBarcodes.size();
for (int i = 0; i < allPos.size(); i++) {
StoragePos componentPos = allPos.get(i);
Barcode componentBarcode = new Barcode();
if(i < relationCount){//夹具上有料
componentBarcode = relationBarcodes.get(i);
componentBarcode.setInFixture(barcode.getBarcode());
componentBarcode.setUsedCount(barcode.getUsedCount() + 1);
componentBarcode.setPutInTime(now);
componentBarcode.setInOpor(task.getOperator());
componentBarcode.setPosName(task.getPosName());
//入库后设置出库时间为-1
componentBarcode.setCheckOutDate(null,"");
barcodeManager.save(componentBarcode);
}else{
componentBarcode.setFixtureNumber(barcode.getPartNumber());
componentBarcode.setInFixture(barcode.getBarcode());
componentBarcode.setType(barcode.getType());
componentBarcode.setBarcode(barcode.getBarcode());
componentBarcode.setPutInTime(now);
componentBarcode.setInOpor(task.getOperator());
componentBarcode.setPartNumber(barcode.getPartNumber());
componentBarcode.setInitialAmount(1);
componentBarcode.setAmount(1);
}
/**
* 仓位状态
*/
componentPos.setBarcode(componentBarcode);
componentPos.setUsed(true);
componentPos.setCanCheckOutTime(canCheckOutTime);
storagePosManager.save(componentPos);
//更新缓存中的库存信息
int stockReel = dataCache.updateInventory(storagePos,componentBarcode);
//dataCache.updateStorage(task.getCid());
log.info("入库"+componentBarcode.getPartNumber()+"["+componentBarcode.getBarcode()+"]当前库存:"+stockReel+"到"+componentPos.getPosName()+"可出库时间:"+new Date(canCheckOutTime));
}
}else{//条码不是夹具
barcode.setUsedCount(barcode.getUsedCount() + 1);
barcode.setUsedDate(new Date());
barcode.setPutInTime(System.currentTimeMillis());
barcode.setInOpor(task.getOperator());
barcode.setCheckOutDate(null,"");
barcode.setPosName(task.getPosName());
barcode.setInitialAmount(barcode.getAmount());
if(Strings.isNullOrEmpty(barcode.getProviderNumber())){//补上供应商编号
Component component = componentManager.findByPartNumber(barcode.getPartNumber());
barcode.setProviderNumber(component.getProviderNumber());
}
// AppendInfo appendInfo = barcode.getAppendInfo();
// appendInfo.setDnNo(task.getSourceName());
// appendInfo.setFacility(task.getSubSourceInfo());
// appendInfo.setCompany(task.getSubSourceInfo());
// barcode.setAppendInfo(appendInfo);
barcodeManager.save(barcode);
/**
* 仓位状态
*/
storagePos.setBarcode(barcode);
storagePos.setUsed(true);
storagePos.setCanCheckOutTime(System.currentTimeMillis());
storagePosManager.save(storagePos);
//更新缓存中的库存信息
dataCache.updateInventory(storagePos,barcode);
//dataCache.updateStorage(task.getCid());
//
// Storage storage = dataCache.getStorage(task.getCid());
// if(storage != null){
// postInNotification(dataCache.getSettings().getInNotifyApi(), task.getBarcode(), task.getStorageId());
// }
}
//记录日志,完成 task
task.setNum(barcode.getAmount());
task.setStatus(StorageConstants.OP_STATUS.FINISHED.name());
dataLogDao.save(task);
finishedTaskMap.put(task.getBarcode(),task);
//从队列里面移除操作
taskMap.remove(task.getId());
updateInQisdaData(barcode, task);
}
private synchronized void updateInQisdaData(Barcode barcode, DataLog task){
if(task.isPutInTask() && !task.isCancel()){
//更新DN单收料数量
AppendInfo appendInfo = barcode.getAppendInfo();
if(!appendInfo.isCISIn()){
DNItem dnItem = dNItemDao.findDnItem(appendInfo.getDnNo(), barcode.getPartNumber(), appendInfo.getFacility(), appendInfo.getCompany());
//Facility入库
if(appendInfo.isFacilityIn()){
if(dnItem == null){
dnItem = new DNItem();
dnItem.setDnNo(appendInfo.getDnNo());
dnItem.setPn(barcode.getPartNumber());
dnItem.setFacility(appendInfo.getFacility());
dnItem.setCompany(appendInfo.getCompany());
log.info("新增Facility ["+ appendInfo.getDnNo()+"]详情[" + barcode.getPartNumber() + "]");
}
}else{
//DN单入库
if(dnItem == null){
log.error("["+barcode.getBarcode()+"]入库时DN单对应详情不存在");
return;
}else{
}
}
int inQty = dnItem.getInQty();
log.info("["+barcode.getBarcode()+"]入库完成,DN单["+dnItem.getDnNo()+"]中" + dnItem.getFacility() + "数量"+inQty+"增加:" + task.getNum() +" 盘数["+dnItem.getInNum()+"]+1");
dnItem.setInQty(inQty + task.getNum());
dnItem.setInNum(dnItem.getInNum() + 1);
dnItem.setFirstReelDate(new Date());
dnItem = dNItemDao.save(dnItem);
}else{
//纯入库,更新绑定数量
String soseq = appendInfo.getSoseq();
if(soseq != null && !soseq.isEmpty()){
int slotIndex = appendInfo.getSlotIndex();
OutItem cutItem = outItemDao.findCutItem(soseq, slotIndex);
if(cutItem != null){
int realBindQty = cutItem.getRealLockQty() + barcode.getAmount();
log.info(barcode.getBarcode() + "纯入库完成,分盘需求单soseq=["+soseq+"]slotSeq=["+slotIndex+"]绑定数量增加"+barcode.getAmount()+"="+ realBindQty);
cutItem.setRealLockQty(realBindQty);
outItemDao.save(cutItem);
outInfoCache.updateOutItem(cutItem.getId());
}
}
}
if(DataCache.isProductionFor(DataCache.CUSTOMER.QISDA)){
//通知VMI入库完成
if(barcode.needToQisda()){
QisdaApiController.PutInFinished(barcode, task);
}else{
log.info("入库完成,条码["+barcode.getBarcode()+"]不需要通知Qisda");
}
}else{
log.info("入库完成通知Qisda接口关闭");
}
}
}
/**
* 出库完成
*/
@Override
public void checkoutFinished(DataLog task) throws ValidateException {
StoragePos storagePos = storagePosManager.get(task.getPosId());
Barcode barcode = storagePos.getBarcode();
if(barcode == null){
log.warn("任务:"+task.getId() +" 仓位:"+task.getPosId()+" 的 Barcode 为null, 之前可能处理过直接返回");
return;
}
barcode = barcodeManager.get(barcode.getId());
if(barcode != null){
//二维码状态
barcode.setUsed(true);
barcode.setUsedDate(new Date());
//仓位状态
barcode.setCheckOutDate(new Date(),task.getOperator());
barcode.setPosName("");
barcodeManager.save(barcode);
String specifiedBatchId = barcode.getLockId();
if(!Strings.isNullOrEmpty(specifiedBatchId)){
task.setBatchId(specifiedBatchId);
task.setBatchInfo(barcode.getLockName());
}
}
storagePos.setBarcode(null);
storagePos.setUsed(false);
storagePosManager.save(storagePos);
AppendInfo appendInfo = task.getAppendInfo();
String outItemId = appendInfo.getOutItemId();
log.info("出库完成,清空仓位: " + storagePos.getId() + "[" + storagePos.getPosName() + "]outItemId"+ outItemId);
//更新缓存中的库存信息
dataCache.updateInventory(storagePos,barcode);
//记录日志
task.setStatus(StorageConstants.OP_STATUS.OUTBOX.name());
dataLogDao.save(task);
//从队列里面移除操作
taskMap.remove(task.getId());
finishedTaskMap.put(task.getBarcode(),task);
//dataCache.updateStorage(task.getCid());
//finishedOrderTask(task);
//checkTaskSetFinished(task);
updateOutInfo(outItemId, task, barcode);
}
private synchronized void updateOutInfo(String outItemId, DataLog task, Barcode barcode){
//更新出库数量
OutInfo outInfo;
if(!Strings.isNullOrEmpty(outItemId)){
OutItem outItem = outItemDao.findOneById(outItemId);
if(outItem != null){
if(barcode.hasCutInfo()){
//分盘料,需要更新对应的需求单出库数量
final List<Map<String, Object>> cutItems = barcode.getCutItems();
for (Map<String, Object> cutItem : cutItems) {
String soseqStr = cutItem.get("soseq").toString();
String slotlocation = cutItem.get("slotlocation").toString();
String qty = cutItem.get("qty").toString();
OutItem cutInfoItem = outItemDao.findCutItem(soseqStr, Integer.valueOf(slotlocation));
if(cutInfoItem == null){
log.error("未找到soseq=["+soseqStr+"]slotseq=["+slotlocation+"]的需求信息");
continue;
}
int outQty = outItem.getOutQty() + Integer.valueOf(qty);
log.info("分盘需求单["+cutInfoItem.gethSerial()+"]["+slotlocation+"]的出库数量增加"+qty+"="+outQty);
outItem.setOutQty(outQty);
outItem = outItemDao.save(outItem);
outInfoCache.updateOutItem(outItem.getId());
}
}else{
int outQty = outItem.getOutQty() + task.getNum();
outItem.setOutQty(outQty);
if(outItem.isUrgentAction()){
log.info("紧急料设置发料数量与出库数量一致");
//紧急料发料数量与出库数量一致
outItem.setSendQty(outQty);
}
outItem = outItemDao.save(outItem);
outInfoCache.updateOutItem(outItem.getId());
}
outInfo = outInfoDao.findByHSerial(outItem.gethSerial());
int outReelNum = outInfo.getOutReelNum();
outInfo.setOutReelNum(outReelNum + 1);
if(outItem.isUrgentAction() || outItem.isReelCutAction()){
outInfo.setTaskFinishNum(outInfo.getTaskFinishNum() + 1);
}
outInfo = outInfoDao.save(outInfo);
String latest = outInfo.getOutLatest();
QisdaApiController.OutFinished(task, barcode, latest);
if (latest.equals("L")) {
log.info("工单[" + outItem.getSo() + "]需求单["+outItem.gethSerial()+"]的最后一盘出库完成,发送缺料通知");
List<OutItem> outItemList = outItemDao.findByHSerial(outItem.gethSerial());
boolean lessBind = false;
QisdaApi.VMILocationOutFeedback(outItemList, lessBind);
}
}
}
}
/**
* 更新工单状态信息
*/
private synchronized void finishedOrderTask(DataLog task){
// if(StorageConstants.OP.CHECKOUT == task.getType()){
// //更新工单状态
// String orderNo = task.getSourceName();
// if(!Strings.isNullOrEmpty(orderNo)){
// LiteOrder order = liteOrderMap.get(orderNo);
// if(order == null){
// log.info("缓存中未找到["+orderNo+"],从数据库中重新加载");
// order = liteOrderDao.findWithItemsByOrderNo(orderNo);
// }
// if(order != null){
// //任务是取消的,需要将总待出库数量-1
// if(task.isCancel()){
// order.setTaskReelCount(order.getTaskReelCount() -1);
// log.info("工单["+orderNo+"]的任务"+task.getPartNumber()+"["+task.getBarcode()+"]已取消,任务数-1="+order.getFinishedReelCount() + "/" + order.getTaskReelCount());
// }else if(task.isFinished()){
// order.setFinishedReelCount(order.getFinishedReelCount() + 1);
// String orderItemId = task.getSubSourceId();
// List<LiteOrderItem> items = new ArrayList<>();
// for (LiteOrderItem liteOrderItem : order.getOrderItems()) {
// if(liteOrderItem.getId().equals(orderItemId)){
// log.info("工单["+orderNo+"]的任务"+task.getPartNumber()+"["+task.getBarcode()+"]出库完成,已完成数量+1="+order.getFinishedReelCount()+"/" + order.getTaskReelCount());
// //更新对应条目的已出库数量和出库盘数
// liteOrderItem.setOutNum(liteOrderItem.getOutNum() + task.getNum());
// liteOrderItem.setOutReelCount(liteOrderItem.getOutReelCount() + 1);
// liteOrderItem = liteOrderItemDao.save(liteOrderItem);
// }
// items.add(liteOrderItem);
// }
// }else{
// log.error("工单["+orderNo+"]的任务["+task.getBarcode()+"]完成时,状态为:"+ task.getStatus());
// }
// if(order.getFinishedReelCount() >= order.getTaskReelCount()){
// log.info("工单["+orderNo+"]的出库任务已完成,共出库:"+ order.getFinishedReelCount() +" 盘");
// order.finishedTasks();
// }
// liteOrderDao.save(order);
// liteOrderMap.put(orderNo, order);
// } else{
// log.error("完成任务时,未找到工单["+orderNo+"]信息");
// }
// }
// }
}
}