ZarshService.java
39.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
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
package com.huaheng.pc.sap.service;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import com.huaheng.api.general.service.BasicDataApiService;
import com.huaheng.api.utils.SAPUtils;
import com.huaheng.api.wcs.service.warecellAllocation.WarecellAllocationService;
import com.huaheng.common.utils.DateUtils;
import com.huaheng.framework.aspectj.lang.annotation.ProcessTrack;
import com.huaheng.framework.aspectj.lang.constant.ProcessCode;
import com.huaheng.pc.config.location.service.LocationService;
import com.huaheng.pc.config.material.domain.Material;
import com.huaheng.pc.config.material.service.MaterialService;
import com.huaheng.pc.inventory.inventoryDetail.domain.InventoryDetail;
import com.huaheng.pc.inventory.inventoryDetail.service.InventoryDetailService;
import com.huaheng.pc.sap.domain.SAPTaskDomain;
import com.huaheng.pc.task.agvTask.domain.AgvTask;
import com.huaheng.pc.task.agvTask.service.AgvTaskService;
import com.huaheng.pc.task.taskDetail.domain.TaskDetail;
import com.huaheng.pc.task.taskDetail.service.TaskDetailService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huaheng.api.sap.domain.ZarDomain;
import com.huaheng.api.sap.service.ZarApiService;
import com.huaheng.common.constant.QuantityConstant;
import com.huaheng.common.exception.service.ServiceException;
import com.huaheng.common.utils.StringUtils;
import com.huaheng.common.utils.security.ShiroUtils;
import com.huaheng.framework.web.domain.AjaxResult;
import com.huaheng.pc.config.container.domain.Container;
import com.huaheng.pc.config.container.service.ContainerService;
import com.huaheng.pc.config.location.domain.Location;
import com.huaheng.pc.config.station.domain.Station;
import com.huaheng.pc.config.station.service.StationService;
import com.huaheng.pc.config.zone.service.ZoneService;
import com.huaheng.pc.receipt.receiptContainerHeader.service.ReceiptContainerHeaderService;
import com.huaheng.pc.receipt.receiptHeader.domain.ReceiptHeader;
import com.huaheng.pc.receipt.receiptHeader.service.ReceiptHeaderService;
import com.huaheng.pc.sap.domain.Zarsh;
import com.huaheng.pc.sap.domain.Zarsi;
import com.huaheng.pc.sap.mapper.ZarshMapper;
import com.huaheng.pc.shipment.shipmentContainerHeader.domain.ShipmentContainerHeader;
import com.huaheng.pc.shipment.shipmentContainerHeader.service.ShipmentContainerHeaderService;
import com.huaheng.pc.shipment.shipmentHeader.domain.ShipmentHeader;
import com.huaheng.pc.shipment.shipmentHeader.service.ShipmentHeaderService;
import com.huaheng.pc.task.taskHeader.domain.TaskHeader;
import com.huaheng.pc.task.taskHeader.service.ReceiptTaskService;
import com.huaheng.pc.task.taskHeader.service.TaskHeaderService;
import com.huaheng.pc.task.taskHeader.service.WorkTaskEmptyContainerService;
import com.huaheng.pc.task.taskHeader.service.WorkTaskService;
import static java.util.stream.Collectors.toList;
/**
* Created by Enzo Cotter on 2022/5/11.
* @author zhouhong
*/
@Service
public class ZarshService extends ServiceImpl<ZarshMapper, Zarsh> {
@Resource
private ZarsiService zarsiService;
@Resource
private ReceiptContainerHeaderService receiptContainerHeaderService;
@Resource
private ShipmentContainerHeaderService shipmentContainerHeaderService;
@Resource
private ReceiptHeaderService receiptHeaderService;
@Resource
private ShipmentHeaderService shipmentHeaderService;
@Resource
private TaskHeaderService taskHeaderService;
@Resource
private ContainerService containerService;
@Resource
private BackSapStatusService backSapStatusService;
@Resource
private ZoneService zoneService;
@Resource
private StationService stationService;
@Resource
private LocationService locationService;
@Resource
private TaskDetailService taskDetailService;
@Resource
private WorkTaskService workTaskService;
@Resource
private WorkTaskEmptyContainerService workTaskEmptyContainerService;
@Resource
private ZarshService zarshService;
@Resource
private ZarApiService zarApiService;
@Resource
private ReceiptTaskService receiptTaskService;
@Resource
private InventoryDetailService inventoryDetailService;
@Resource
private MaterialService materialService;
@Resource
private BasicDataApiService basicDataApiService;
@Resource
private WarecellAllocationService warecellAllocationService;
@Resource
private AgvTaskService agvTaskService;
public Zarsh checkZarshByUniqueId(String uniqueId) {
LambdaQueryWrapper<Zarsh> lambdaQueryWrapper = Wrappers.lambdaQuery();
lambdaQueryWrapper.eq(Zarsh::getUniqueId, uniqueId);
lambdaQueryWrapper.last("limit 1");
Zarsh zarsh = this.getOne(lambdaQueryWrapper);
if (zarsh != null) {
return zarsh;
}
return null;
}
public boolean saveZarsh(Zarsh zarsh) {
Date date = new Date();
String uniqueId = UUID.randomUUID().toString();
zarsh.setUniqueId(uniqueId);
zarsh.setCreated(date);
zarsh.setCreateBy("wms");
zarsh.setLastUpdatedBy("wms");
zarsh.setLastUpdated(date);
return this.save(zarsh);
}
public boolean editZarsh(Zarsh zarsh) {
Date date = new Date();
zarsh.setLastUpdatedBy("wms");
zarsh.setLastUpdated(date);
return this.updateById(zarsh);
}
public void removeByZarshId(String uniqueId, Integer id) {
LambdaQueryWrapper<Zarsh> queryWrapper = Wrappers.lambdaQuery();
queryWrapper.eq(StringUtils.isNotEmpty(uniqueId), Zarsh::getUniqueId, uniqueId);
queryWrapper.eq(StringUtils.isNotNull(id), Zarsh::getId, id);
queryWrapper.last("limit 1");
Zarsh zarsh = this.getOne(queryWrapper);
if (zarsh != null) {
LambdaQueryWrapper<Zarsi> query = Wrappers.lambdaQuery();
query.eq(Zarsi::getUniqueId, zarsh.getUniqueId());
List<Zarsi> list = zarsiService.list(query);
List<Integer> ids = list.stream().map(detail -> detail.getId()).collect(Collectors.toList());
if (ids.size() > 0) {
zarsiService.removeByIds(ids);
}
this.removeById(zarsh.getId());
}
}
public Zarsh getZarshByUnique(String uniqueId) {
LambdaQueryWrapper<Zarsh> queryWrapper = Wrappers.lambdaQuery();
queryWrapper.eq(StringUtils.isNotEmpty(uniqueId), Zarsh::getUniqueId, uniqueId);
queryWrapper.last("limit 1");
Zarsh zarsh = this.getOne(queryWrapper);
return zarsh;
}
public AjaxResult saveSapDataByApi(ZarDomain zarDomain) {
// 用主表UniqueId查找子表zarsi信息
List<Zarsi> zarsiList = zarDomain.getZarsiList();
Zarsh zarsh = zarDomain.getZarsh();
/**
* 任务取消
*/
if (StringUtils.isNotEmpty(zarsh.getCFlag()) && zarsh.getCFlag().equals("D")) {
// 取消
return zarApiService.cancelTaskBySap(zarsh);
}
Zarsh zarsh1 = zarshService.checkZarshByUniqueId(zarsh.getUniqueId());
if (zarsh1 != null) {
throw new ServiceException("单据已存在,请不要重复下发");
}
zarshService.createTaskBySAP(zarsiList,zarsh);
return AjaxResult.success("添加任务成功");
}
// 入库
public TaskHeader receiptBySap(List<Zarsi> zarsiList, Zarsh zarsh) {
TaskHeader taskHeader = null;
// 空托盘入库
if (zarsiList == null || zarsiList.size() == 0) {
String area = zoneService.getZoneAreaByCode(zarsh.getLgnum());
// SAP创建 空容器号的入库任务
AjaxResult result = workTaskEmptyContainerService.createEmptyIn(zarsh.getDrumId(), "", area, zarsh.getFromPos(), zarsh.getCrnIn());
if (result.hasErr()) {
throw new ServiceException(result.getMsg());
}
Integer taskId = (Integer)result.getData();
taskHeader = taskHeaderService.getById(taskId);
taskHeader.setUniqueIds(zarsh.getUniqueId());
taskHeaderService.updateById(taskHeader);
// 调用sap的api,传递任务状态
backSapStatusService.saveBackSapStatus(zarsh.getUniqueId(), zarsh.getLgnum(), null, 1, "1", null, null);
return null;
}
// 保存入库信息
ReceiptHeader receiptHeader = null;
for (Zarsi zarsi : zarsiList) {
if (zarsi.getVerme() == null || zarsi.getVerme().compareTo(BigDecimal.ZERO) == 0) {
throw new ServiceException("入库数量不能为0");
}
}
AjaxResult ajaxResult = receiptHeaderService.saveReceiptBySap(zarsh, zarsiList);
if (ajaxResult != null && ajaxResult.hasErr()) {
throw new ServiceException(ajaxResult.getMsg());
}
receiptHeader = (ReceiptHeader)ajaxResult.getData();
// 调用sap的api,传递任务状态
backSapStatusService.saveBackSapStatus(zarsh.getUniqueId(), zarsh.getLgnum(), null, 1, "1", null, null);
// 如果是玻璃布2楼入库,机台到立库,站台机台,线边库到立库,站台立库口,机台到线边库
Container container = containerService.getContainerByCode(zarsh.getDrumId(), QuantityConstant.WAREHOUSECODE);
// 指定一个托盘生成任务
if (StringUtils.isEmpty(zarsh.getDrumId()) || container == null) {
if (zarsh.getLgnum().contains("A")) {
Container container1 = containerService.createContainerByZoneNoTrasa("A");
zarsh.setDrumId(container1.getCode());
container = container1;
// 创建临时托盘
}
}
// 判断有没有托盘号,有则生成组盘
if (StringUtils.isNotEmpty(zarsh.getDrumId()) && container != null) {
// 保存组盘,生成任务信息
// 设置wcs固定登录缓存,调用公共组盘接口
ShiroUtils.setUserByWcsZone("SAP", QuantityConstant.WAREHOUSECODE, receiptHeader.getZoneCode());
AjaxResult ajaxResult_receiptcontainerH = receiptContainerHeaderService.saveContainerByReceiptHeaderId(receiptHeader.getId(), zarsh.getDrumId());
if (ajaxResult_receiptcontainerH != null && ajaxResult_receiptcontainerH.hasErr()) {
throw new ServiceException(ajaxResult_receiptcontainerH.getMsg());
}
// AjaxResult ajaxResult_receiptcontainer =
// receiptContainerHeaderService.saveReceiptContainerBySap(receiptHeader, zarsh.getDrumId(), zarsh.getFromPos(), zarsh.getToPos());
// if (ajaxResult_receiptcontainer != null && ajaxResult_receiptcontainer.hasErr()) {
// throw new ServiceException(ajaxResult_receiptcontainer.getMsg());
// }
}
return taskHeader;
}
// 出库
public TaskHeader shipmentBySap(List<Zarsi> zarsiList, Zarsh zarsh) {
TaskHeader taskHeader = null;
// 空托盘出库
if (zarsiList == null || zarsiList.size() == 0) {
// 获取对应库区编码 zarsh.getLgnum() --> F
List<Location> list = containerService.getEmptyContainerInLocation(zarsh.getLgnum(), "", "", QuantityConstant.WAREHOUSECODE);
List<String> emptyContainerCodeList = list.stream().map(t -> t.getContainerCode()).collect(Collectors.toList());
List<String> removeContainerList = new ArrayList<>();
for (String containerCode : emptyContainerCodeList) {
Container container = containerService.getContainerByCode(containerCode, QuantityConstant.WAREHOUSECODE);
if (container.getStatus().equals(QuantityConstant.STATUS_CONTAINER_LOCK)) {
removeContainerList.add(containerCode);
}
}
emptyContainerCodeList.removeAll(removeContainerList);
if (emptyContainerCodeList == null || emptyContainerCodeList.isEmpty()) {
throw new ServiceException("库内没有找到空容器");
}
// 自动匹配空容器出库
Container container = null;
if (StringUtils.isNotEmpty(zarsh.getDrumId())) {
container = containerService.getContainerByCode(zarsh.getDrumId(), QuantityConstant.WAREHOUSECODE);
} else {
container = containerService.getContainerByCode(emptyContainerCodeList.get(0), QuantityConstant.WAREHOUSECODE);
}
if (container == null) {
throw new ServiceException("请检查容器码:" + zarsh.getDrumId() + "是否正确!");
}
// 创建空容器出库任务
AjaxResult result = workTaskService.createEmptyOut(container.getCode(), container.getLocationCode(), zarsh.getToPos(), QuantityConstant.WAREHOUSECODE,
zarsh.getUniqueId());
if (result.hasErr()) {
throw new ServiceException(result.getMsg());
}
// 调用sap的api,传递任务状态
backSapStatusService.saveBackSapStatus(zarsh.getUniqueId(), zarsh.getLgnum(), null, 2, "1", null, null);
return taskHeader;
}
// 有料出库
// 保存出库信息
ShipmentHeader shipmentHeader = null;
AjaxResult ajaxResult = shipmentHeaderService.saveShipmentBySap(zarsh, zarsiList);
if (ajaxResult != null && ajaxResult.hasErr()) {
throw new ServiceException(ajaxResult.getMsg());
}
shipmentHeader = (ShipmentHeader)ajaxResult.getData();
// 调用sap的api,传递任务状态
backSapStatusService.saveBackSapStatus(zarsh.getUniqueId(), zarsh.getLgnum(), null, 2, "1", null, null);
// 判断有没有库位号,有则生成组盘
if (StringUtils.isNotEmpty(zarsh.getLocation())) {
// 保存组盘信息
ShipmentContainerHeader shipmentContainerHeader = null;
AjaxResult result = shipmentContainerHeaderService.saveShipmentContainerBySap(shipmentHeader, zarsh);
if (result != null && result.hasErr()) {
throw new ServiceException(result.getMsg());
}
shipmentContainerHeader = (ShipmentContainerHeader)result.getData();
// 创建任务表数据
AjaxResult ajaxResult_taskheader = taskHeaderService.saveTaskBySapLK(shipmentContainerHeader);
// 生成任务成功之后在修改 组盘状态
if (ajaxResult_taskheader.hasErr()) {
throw new ServiceException("生成任务失败");
}
}
return taskHeader;
}
@Transactional(rollbackFor = Exception.class)
public void createTaskBySAP(List<Zarsi> zarsiList, Zarsh zarsh) {
String warehouseCode = QuantityConstant.WAREHOUSECODE;
String zoneCode = SAPUtils.getZoneCode(zarsh);
String area = SAPUtils.getArea(zarsh);
/**
* 1:整盘 / 等待组盘
* 2:空托
*/
Integer billType = zarsh.getInKind();
Integer plType = zarsh.getPlType();
/**
* 出入类型
* 1 3 出库
* 2 8 入库
*/
Integer type = zarsh.getMFlag();
/**
* 0:卷托盘
* 1:片托盘
* 2:特殊托盘
*/
Integer containerType = zarsh.getPlType();
String containerCode = zarsh.getDrumId();
String location = zarsh.getLocation();
String uniqueId = zarsh.getUniqueId();
String formPort = zarsh.getFromPos();
String toPort = zarsh.getToPos();
SAPTaskDomain domain = new SAPTaskDomain();
domain.setType(type);
domain.setArea(area);
domain.setZoneCode(zoneCode);
domain.setContainerType(plType);
domain.setContainerCode(containerCode);
domain.setLocationCode(location);
domain.setUniqueId(uniqueId);
domain.setFormPort(formPort);
domain.setToPort(toPort);
domain.setZarsh(zarsh);
domain.setZarsiList(zarsiList);
if (!zarshService.save(zarsh)) {
throw new ServiceException("保存主表数据失败");
}
if (!zarsiService.saveBatchs(zarsiList, zarsh)) {
throw new ServiceException("保存子表数据失败");
}
switch (billType) {
case 1:
createHavingTask(domain);
break;
case 2:
createEmptyTask(domain);
break;
default:
throw new ServiceException("inKind不支持的业务类型");
}
int mFlag = 0;
switch (type) {
case 2:
case 8:
mFlag = 1;
break;
case 1:
case 3:
mFlag = 2;
break;
}
backSapStatusService.saveBackSapStatus(uniqueId, zoneCode, null, mFlag, "1", null, null);
}
/**
* SAP无货指令
*
* @param domain
*/
public void createEmptyTask(SAPTaskDomain domain) {
String formPort = domain.getFormPort();
String toPort = domain.getToPort();
Integer type = domain.getType();
String area = domain.getArea();
String zoneCode = domain.getZoneCode();
Integer containerType = domain.getContainerType();
String containerCode = domain.getContainerCode();
String locationCode = domain.getLocationCode();
String uniqueId = domain.getUniqueId();
Zarsh zarsh = domain.getZarsh();
List<Zarsi> zarsiList = domain.getZarsiList();
LambdaQueryWrapper<Station> lambdaQuery1 = Wrappers.lambdaQuery();
lambdaQuery1.eq(Station::getByName, toPort);
Station station = stationService.getOne(lambdaQuery1);
if (station == null) {
throw new ServiceException(toPort + "站台异常,未找到站台");
}
//入库站台属性
Integer defineProperty = station.getDefineProperty();
String stationCode = station.getCode();
switch (type) {
// 入库
case 2:
case 8:
//判断是否为AGV任务
if(defineProperty.equals(1)){
//获取 AGV立库交互站台
createAgvTask(station,zarsh,zarsiList);
}else {
workTaskService.createEmptyIn(containerCode, null, area, stationCode, uniqueId);
}
break;
// 出库
case 1:
case 3:
//区域移库 映射站台 SAP任务下发的是移入库区的站台 通过移入站台获取到 出库的站台
String shipmentStationCode = null;
if (StringUtils.isEmpty(station.getByPassPort())) {
shipmentStationCode = stationCode;
} else {
shipmentStationCode = station.getByPassPort();
}
Station station1 = stationService.getOne(new LambdaQueryWrapper<Station>()
.eq(Station::getCode, shipmentStationCode));
if (station1 == null) {
throw new ServiceException(shipmentStationCode + "站台异常,未找到站台");
}
String roadWay = station1.getRoadWay();
String[] split = roadWay.split(",");
List<String> roadWays = Arrays.asList(split);
//根据 SAP containerType 来获取托盘类型
List<String> emptyContainerList = containerService.getEmptyContainerList(zoneCode, roadWays, station1.getAreaByWcs(), containerType);
if (emptyContainerList.isEmpty()) {
throw new ServiceException(containerType + "容器类型未匹配成功");
}
String palletCode = emptyContainerList.get(0);
Container container = containerService.getContainerByCode(palletCode, QuantityConstant.WAREHOUSECODE);
if (container == null) {
throw new ServiceException(palletCode + "容器异常,未找到容器");
}
AjaxResult emptyOut = workTaskService.createEmptyOut(container.getCode(), container.getLocationCode(), shipmentStationCode, QuantityConstant.WAREHOUSECODE, uniqueId);
if (emptyOut.hasErr()) {
throw new ServiceException(emptyOut.getMsg());
}
break;
default:
throw new ServiceException("MFlag不支持的业务类型");
}
}
/**
* SAP有货指令
*
* @param domain
*/
public void createHavingTask(SAPTaskDomain domain) {
String formPort = domain.getFormPort();
String toPort = domain.getToPort();
Integer type = domain.getType();
String area = domain.getArea();
String zoneCode = domain.getZoneCode();
Integer containerType = domain.getContainerType();
String containerCode = domain.getContainerCode();
String locationCode = domain.getLocationCode();
String uniqueId = domain.getUniqueId();
Zarsh zarsh = domain.getZarsh();
String crnIn = zarsh.getCrnIn();
List<Zarsi> zarsiList = domain.getZarsiList();
//标志是否有 容器编码
int containerFlag = 0;
Container container = null;
if (StringUtils.isNotEmpty(containerCode)) {
container = containerService.getContainerByCode(containerCode, QuantityConstant.WAREHOUSECODE);
if (container != null) {
containerFlag = 1;
}
if (QuantityConstant.STATUS_LOCATION_LOCK.equals(container.getStatus())) {
throw new ServiceException("容器状态为锁定" + containerCode);
}
if (QuantityConstant.STATUS_LOCATION_SOME.equals(container.getStatus())) {
throw new ServiceException("容器状态为有货" + containerCode);
}
List<InventoryDetail> inventoryDetails = inventoryDetailService.list(new LambdaQueryWrapper<InventoryDetail>()
.eq(InventoryDetail::getContainerCode, container.getCode()));
if(!inventoryDetails.isEmpty()){
throw new ServiceException("容器存在库存" + containerCode);
}
}
switch (type) {
// 入库
case 2:
case 8:
Station station = stationService.getStationBySAPCode(formPort);
if (station == null) {
throw new ServiceException("站台未找到!" + formPort);
}
//入库站台编码
String formPortCode = station.getCode();
//入库站台属性
Integer defineProperty = station.getDefineProperty();
switch (containerFlag) {
//无托盘
case 0:
zarsh.setLgnum(zoneCode);
zarsh.setToPos(toPort);
zarsh.setFromPos(formPortCode);
//agv站台
if (defineProperty == 1) {
//创建agv任务
AgvTask agvTask = createAgvTask(station, zarsh, zarsiList);
if (StringUtils.isEmpty(crnIn)) {
//获取分配agv任务去向的站台
String toPoint = agvTask.getToPoint();
zarsh.setFromPos(toPoint);
receiptHeaderService.saveReceiptHeaderBySap(uniqueId, zarsh, zarsiList);
}
break;
}
//生成入库单 用于AGV调用组盘接口
receiptHeaderService.saveReceiptHeaderBySap(uniqueId, zarsh, zarsiList);
break;
//有托盘
case 1:
//判断 from_pos 来源
domain.setFormPort(formPortCode);
//立库agv交互点入库
if (defineProperty == 2) {
createSAPReceiptTask(uniqueId,domain);
}
//agv站台入库
if (defineProperty == 1) {
createAgvTask(station, zarsh, zarsiList);
}
//立库站台入库
if (defineProperty == 0) {
createSAPReceiptTask(uniqueId,domain);
}
break;
default:
throw new IllegalStateException("Unexpected value: " + containerFlag);
}
break;
// 出库
case 1:
case 3:
Station toStation = stationService.getStationBySAPCode(toPort);
if (toStation == null) {
throw new ServiceException("站台未找到!" + formPort);
}
//出库站台编码
String toPortCode1 = toStation.getCode();
Integer defineProperty1 = toStation.getDefineProperty();
if (StringUtils.isEmpty(locationCode)) {
throw new ServiceException("SAP数据异常 locationCode 值为空");
}
switch (defineProperty1) {
//立库站台
case 0:
createSAPShipmentTask(domain);
break;
//agv站台
case 1:
Location location = locationService.getLocationByCode(locationCode, QuantityConstant.WAREHOUSECODE);
if (location == null) {
throw new ServiceException("库位不存在" + locationCode);
}
String roadway = location.getRoadway();
List<Station> stations = stationService.list(new LambdaQueryWrapper<Station>()
.like(Station::getRoadWay, roadway)
.eq(Station::getDefineProperty, 2));
if (stations.isEmpty()) {
throw new ServiceException("agv 立库站台未配置请配置站台");
}
String code = location.getCode();
String[] split = code.split("");
String lkagvPort = getLKAGVPort(split[0]);
domain.setToPort(lkagvPort);
createSAPShipmentTask(domain);
break;
//agv立库交互站台
case 2:
createSAPShipmentTask(domain);
break;
default:
throw new ServiceException("站台数据异常" + toPortCode1);
}
break;
default:
throw new IllegalStateException("Unexpected value: " + type);
}
}
/**
* 根据sap创建入库任务
*
* @param domain
*/
@ProcessTrack(taskName = "创建入库任务", processCode = ProcessCode.RECEIPT_TASK_CODE)
public void createSAPReceiptTask(String uniqueId,SAPTaskDomain domain) {
String warehouseCode = QuantityConstant.WAREHOUSECODE;
String containerCode = domain.getContainerCode();
String port = domain.getFormPort();
String zoneCode = domain.getZoneCode();
Zarsh zarsh = domain.getZarsh();
List<Zarsi> zarsiList = domain.getZarsiList();
Container container = containerService.getContainerByCode(containerCode, warehouseCode);
if (StringUtils.isNull(container)) {
throw new ServiceException("托盘不存在!");
}
if (container.getStatus().equals(QuantityConstant.STATUS_CONTAINER_LOCK)) {
throw new ServiceException("托盘已经锁定!");
}
containerService.updateStatus(container.getCode(), QuantityConstant.STATUS_LOCATION_LOCK, warehouseCode);
// 创建任务头
TaskHeader task = new TaskHeader();
task.setTaskType(QuantityConstant.TASK_TYPE_WHOLERECEIPT);
task.setUniqueIds(uniqueId);
task.setZoneCode(zoneCode);
task.setInternalTaskType(QuantityConstant.TASK_INTENERTYPE_RECEIPT);
task.setAllocationHeadId(0);
task.setWarehouseCode(warehouseCode);
task.setAssignedUser(ShiroUtils.getLoginName() == null ? QuantityConstant.PLATFORM_WCS : ShiroUtils.getLoginName());
task.setConfirmedBy(ShiroUtils.getLoginName() == null ? QuantityConstant.PLATFORM_WCS : ShiroUtils.getLoginName());
task.setPort(port);
task.setStatus(QuantityConstant.TASK_STATUS_BUILD);
task.setContainerCode(container.getCode());
task.setCompanyCode(QuantityConstant.COMPANYCODE);
task.setCreatedBy(QuantityConstant.PLATFORM_SAP);
task.setCreated(DateUtils.getNowDate());
if (!taskHeaderService.save(task)) {
throw new ServiceException("生成任务失败");
}
List<TaskDetail> taskDetailList = new ArrayList<>();
for (Zarsi zarsi : zarsiList) {
String materialCode = zarsi.getMatnr();
Material material = materialService.getMaterialByCode(materialCode, warehouseCode);
if (material == null) {
material = new Material();
material.setCode(materialCode);
material.setName(materialCode);
basicDataApiService.material(material);
}
TaskDetail taskDetail = new TaskDetail();
taskDetail.setTaskId(task.getId());
taskDetail.setTaskType(task.getTaskType());
taskDetail.setInternalTaskType(QuantityConstant.TASK_INTENERTYPE_RECEIPT);
taskDetail.setWarehouseCode(task.getWarehouseCode());
taskDetail.setAllocationId(0);
taskDetail.setCompanyCode(task.getCompanyCode());
taskDetail.setMaterialCode(material.getCode());
taskDetail.setMaterialName(material.getName());
taskDetail.setMaterialSpec(material.getSpec());
taskDetail.setMaterialUnit(material.getUnit());
taskDetail.setInventorySts(QuantityConstant.GOOD);
taskDetail.setBillCode(QuantityConstant.EMPTY_STRING);
taskDetail.setBillDetailId(0);
// 查找上游号
taskDetail.setReferenceCode(uniqueId);
taskDetail.setQty(zarsi.getVerme());
taskDetail.setContainerCode(task.getContainerCode());
taskDetail.setFromLocation(task.getFromLocation());
taskDetail.setRollNumber(zarsi.getCharg());
taskDetail.setCreatedBy(QuantityConstant.PLATFORM_SAP);
taskDetail.setCreated(DateUtils.getNowDate());
taskDetailList.add(taskDetail);
}
taskDetailService.saveBatch(taskDetailList);
}
/**
* 根据sap创建出库任务
*
* @param domain
*/
public void createSAPShipmentTask(SAPTaskDomain domain) {
String warehouseCode = QuantityConstant.WAREHOUSECODE;
String locationCode = domain.getLocationCode();
String port = domain.getToPort();
String zoneCode = domain.getZoneCode();
String uniqueId = domain.getUniqueId();
Zarsh zarsh = domain.getZarsh();
List<Zarsi> zarsiList = domain.getZarsiList();
Location location = locationService.getLocationByCode(locationCode, warehouseCode);
if (StringUtils.isNull(location)) {
throw new ServiceException("库位禁用或不存在!");
}
if (StringUtils.isEmpty(location.getContainerCode())) {
throw new ServiceException("库位容器不存在 请联系现场人员是否通过WMS出库!");
}
Container container = containerService.getContainerByCode(location.getContainerCode(), warehouseCode);
if (StringUtils.isNull(container)) {
throw new ServiceException("托盘不存在!");
}
if (container.getStatus().equals(QuantityConstant.STATUS_CONTAINER_LOCK)) {
throw new ServiceException("托盘已经锁定!");
}
containerService.updateStatus(container.getCode(), QuantityConstant.STATUS_LOCATION_LOCK, warehouseCode);
locationService.updateStatus(location.getCode(), QuantityConstant.STATUS_LOCATION_LOCK, warehouseCode);
List<InventoryDetail> inventoryDetails =
inventoryDetailService.list(new LambdaQueryWrapper<InventoryDetail>().eq(InventoryDetail::getLocationCode, locationCode));
TaskHeader task = new TaskHeader();
task.setAllocationHeadId(0);
task.setInternalTaskType(QuantityConstant.TASK_INTENERTYPE_SHIPMENT);
task.setWarehouseCode(warehouseCode);
task.setZoneCode(location.getZoneCode());
task.setCompanyCode(QuantityConstant.COMPANYCODE);
task.setStatus(QuantityConstant.TASK_STATUS_BUILD);
task.setTaskType(QuantityConstant.TASK_TYPE_WHOLESHIPMENT);
task.setFromLocation(locationCode);
task.setCrnIn(zarsh.getCrnIn());
task.setContainerCode(container.getCode());
task.setPort(port);
task.setSendTo(0);
task.setUniqueIds(uniqueId);
taskHeaderService.save(task);
List<TaskDetail> taskDetailList = new ArrayList<>();
for (InventoryDetail inventoryDetail : inventoryDetails) {
TaskDetail taskDetail = new TaskDetail();
taskDetail.setTaskId(task.getId());
taskDetail.setInternalTaskType(task.getInternalTaskType());
taskDetail.setWarehouseCode(task.getWarehouseCode());
taskDetail.setCompanyCode(task.getCompanyCode());
taskDetail.setTaskType(task.getTaskType());
taskDetail.setToInventoryId(inventoryDetail.getId());
taskDetail.setFromInventoryId(inventoryDetail.getId());
taskDetail.setAllocationId(0);
taskDetail.setBillCode(QuantityConstant.EMPTY_STRING);
taskDetail.setBillDetailId(0);
taskDetail.setMaterialCode(inventoryDetail.getMaterialCode());
taskDetail.setMaterialName(inventoryDetail.getMaterialName());
taskDetail.setMaterialSpec(inventoryDetail.getMaterialSpec());
taskDetail.setMaterialUnit(inventoryDetail.getMaterialUnit());
taskDetail.setLevel(inventoryDetail.getLevel());
taskDetail.setQty(inventoryDetail.getQty());
taskDetail.setContainerCode(task.getContainerCode());
taskDetail.setFromLocation(task.getFromLocation());
taskDetail.setLot(inventoryDetail.getLot());
taskDetail.setBatch(inventoryDetail.getBatch());
taskDetail.setProjectNo(inventoryDetail.getProjectNo());
taskDetail.setStatus(QuantityConstant.TASK_STATUS_BUILD);
taskDetail.setWaveId(0);
taskDetail.setInventorySts(inventoryDetail.getInventorySts());
taskDetail.setReferenceCode(inventoryDetail.getReferCode());
taskDetail.setRollNumber(inventoryDetail.getRollNumber());
taskDetail.setContainerDetailNumber(inventoryDetail.getContainerDetailNumber());
taskDetailList.add(taskDetail);
inventoryDetail.setTaskQty(inventoryDetail.getQty());
}
inventoryDetailService.updateBatchById(inventoryDetails);
taskDetailService.saveBatch(taskDetailList);
}
/**
* 根据sap创建AGV任务
*/
public AgvTask createAgvTask(Station station, Zarsh zarsh, List<Zarsi> zarsiList) {
String formPortCode = station.getCode();
String zoneCode = station.getZoneCode();
String containerCode = zarsh.getDrumId();
String crnIn = zarsh.getCrnIn();
AgvTask agvTask = new AgvTask();
agvTask.setFromPoint(formPortCode);
agvTask.setZoneCode(zoneCode);
agvTask.setTaskType(QuantityConstant.PEISONG_AGV);
agvTask.setTaskTypes(QuantityConstant.RECEIPT_AGV);
if (StringUtils.isNotEmpty(containerCode)) {
Container container = containerService.getContainerByCode(containerCode, QuantityConstant.WAREHOUSECODE);
if (container == null) {
containerCode = containerService.createContainerByZone(zoneCode);
}
if (container.getStatus().equals(QuantityConstant.STATUS_CONTAINER_LOCK)) {
throw new ServiceException("托盘已经锁定!" + containerCode);
}
}
agvTask.setContainerCode(containerCode);
agvTask.setUniqueId(zarsh.getUniqueId());
agvTask.setCreatedBy(QuantityConstant.PLATFORM_SAP);
agvTask.setCreated(DateUtils.getNowDate());
agvTaskService.save(agvTask);
if (StringUtils.isEmpty(crnIn)) {
//机台 - 立库
//agv任务 - 子任务 - 立库任务
//推荐立库站台
String[] split = zoneCode.split("");
String toZoneCode = split[0];
String lkagvPort = getLKAGVPort(toZoneCode);
agvTask.setToPoint(lkagvPort);
agvTaskService.updateById(agvTask);
} else {
//机台 - 缓存台
//agv任务
//toPoint 库位分配
warecellAllocationService.agvWarecellAllocation(agvTask);
}
return agvTask;
}
/**
* 获取立库 agv交互站台
*
* @param toZoneCode
* @return
*/
public String getLKAGVPort(String toZoneCode) {
List<Station> list = stationService.list(new LambdaQueryWrapper<Station>()
.eq(Station::getZoneCode, toZoneCode)
.eq(Station::getDefineProperty, QuantityConstant.LK_AGV_PORT));
List<String> stationCodes = list.stream().map(Station::getCode).collect(Collectors.toList());
if (stationCodes.isEmpty()) {
throw new ServiceException("该库区没有配置AGV 立库站台请前往配置");
}
LambdaQueryWrapper<TaskHeader> queryWrapper = Wrappers.lambdaQuery();
queryWrapper
.eq(TaskHeader::getZoneCode, toZoneCode)
.in(TaskHeader::getPort, stationCodes)
.in(TaskHeader::getTaskType, 100, 200, 300, 400, 500, 600, 1100, 1200)
.le(TaskHeader::getStatus,
QuantityConstant.TASK_STATUS_ARRIVED_STATION);
List<TaskHeader> list1 = taskHeaderService.list(queryWrapper);
if (list1.isEmpty()) {
return stationCodes.get(0);
}
Map<String, Integer> map = new HashMap<>();
for (TaskHeader taskHeader : list1) {
if (map.containsKey(taskHeader)) {
int num = map.get(taskHeader.getPort());
num++;
map.put(taskHeader.getPort(), num);
} else {
map.put(taskHeader.getPort(), 1);
}
}
List<Map.Entry<String, Integer>> list2 = new ArrayList(map.entrySet());
Collections.sort(list2, (o1, o2) -> (o1.getValue() - o2.getValue()));
List<String> ports = list2.stream().map(Map.Entry::getKey).collect(toList());
String s = ports.get(0);
return s;
}
}