自动化立体仓库 - WMS系统
skyouc
3 天以前 56b1b873191d6e7022ed225cf81c3eda774f210e
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
package com.zy.api.service.impl;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.core.common.Cools;
import com.core.common.R;
import com.core.exception.CoolException;
import com.zy.api.controller.params.ReceviceTaskParams;
import com.zy.api.controller.params.WorkTaskParams;
import com.zy.api.entity.dto.LocMastDto;
import com.zy.api.service.WcsApiService;
import com.zy.asrs.entity.*;
import com.zy.asrs.entity.param.CompleteParam;
import com.zy.asrs.entity.result.FindLocNoAttributeVo;
import com.zy.asrs.enums.ContainerType;
import com.zy.asrs.enums.LocStsType;
import com.zy.asrs.enums.TaskIOType;
import com.zy.asrs.service.*;
import com.zy.asrs.service.impl.*;
import com.zy.asrs.utils.Utils;
import com.zy.common.constant.MesConstant;
import com.zy.common.model.LocTypeDto;
import com.zy.common.model.StartupDto;
import com.zy.common.service.CommonService;
import com.zy.common.utils.HttpHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;
 
import static jdk.nashorn.internal.runtime.regexp.joni.Config.log;
 
@Slf4j
@Service
public class WcsApiServiceImpl implements WcsApiService {
 
    @Autowired
    private LocMastService locMastService;
    @Autowired
    private WrkMastService wrkMastService;
    @Autowired
    private WorkService workService;
    @Autowired
    private WrkDetlService wrkDetlService;
    @Autowired
    private MatService matService;
    @Autowired
    private LocDetlService locDetlService;
    @Autowired
    private RowLastnoService rowLastnoService;
    @Autowired
    private OrderDetlPakoutService orderDetlPakoutService;
    @Autowired
    private WrkMastLogService wrkMastLogService;
    @Autowired
    private WrkDetlLogService wrkDetlLogService;
    @Autowired
    private BasContainerService basContainerService;
    @Autowired
    private BasDevpService basDevpService;
    @Autowired
    private WaitPakinService waitPakinService;
    @Autowired
    private CommonService commonService;
 
    /**
     * 通知WCS锁定库位,及禁止当前库位的一切操作
     *
     * @param params
     * @return com.core.common.R
     * @author Ryan
     * @date 2026/1/10 11:18
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R lockLocs(ReceviceTaskParams params) {
//        BasDevice basDevice = basDeviceService.selectOne(new EntityWrapper<BasDevice>()
//                .eq("status", 1)
//                .eq("dev_no", params.getDeviceNo()));
//        if (Objects.isNull(basDevice)) {
//            throw new CoolException("机台信息不存在或已禁用!!");
//        }
//        List<LocAroundBind> binds = locAroundBindService.selectList(new EntityWrapper<LocAroundBind>().eq("dev_no", basDevice.getDevNo()));
//        if (Objects.isNull(binds) || binds.isEmpty()) {
//            throw new CoolException("机台未绑定工作站台!!");
//        }
//        Set<String> locs = binds.stream().map(LocAroundBind::getBlocNo).collect(Collectors.toSet());
//
//        reportLockLocs(locs, "lock");
 
        return R.ok("上报成功!!");
    }
 
//    /**
//     * 余料回库 (搬运余料回库)
//     *
//     * @param params
//     * @return com.core.common.R
//     * @author Ryan
//     * @date 2026/1/10 13:19
//     */
//    @Override
//    @Transactional(rollbackFor = Exception.class)
//    public R backLocs(WorkTaskParams params) {
//        if (Objects.isNull(params.getWrkNo())) {
//            throw new CoolException("工作号不能为空!!");
//        }
////        String wrkCode = params.getTaskNo();
////       if (wrkCode.contains("-1")) {
////           throw new CoolException("配对任务编码错误,请检查后重新上传!!");
////       }
//        WrkMast mast = wrkMastService.selectOne(new EntityWrapper<WrkMast>().eq("wrk_no", params.getWrkNo()));
//        if (Objects.isNull(mast)) {
//            throw new CoolException("任务不存在!!");
//        }
////       if (!mast.getWrkSts().equals(103L)) {
////           throw new CoolException("当前任务并非余料出库任务!!");
////       }
////       if (params.getIsSuplus() == 1) {
////           mast.setIsSuplus(1);
////       }
////
////       if (!wrkMastService.updateById(mast)) {
////           throw new CoolException("任务状态更新失败!!");
////       }
//        workService.backLocOperation(mast.getWrkNo() + "", mast.getAppeUser());
//
//        return R.ok("接收成功,执行回库中...");
//    }
 
    /**
     * 下发任务至WCS
     *
     * @param params
     * @return com.core.common.R
     * @author Ryan
     * @date 2026/1/10 13:58
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R pubWrkToWcs(WorkTaskParams params) {
        if (Objects.isNull(params.getTaskNo())) {
            return R.error("任务号不能为空!!");
        }
        String url = MesConstant.PUB_TASK_IN;
        if (!Objects.isNull(params.getType()) && params.getType().equals("out")) {
            url = MesConstant.PUB_TASK_OUT;
        }
        log.info("出库任务下发参数:{}", JSON.toJSONString(params));
        String response;
        try {
            response = new HttpHandler.Builder()
                    .setUri(MesConstant.WCS_URL)
                    .setPath(url)
                    .setJson(JSON.toJSONString(params))
                    .build()
                    .doPost();
            Map<String, Object> result = JSON.parseObject(response, Map.class);
            if (result.get("code").equals(200)) {
                //TODO 上报是否成功
                WrkMast wrkMast = wrkMastService.selectOne(new EntityWrapper<WrkMast>().eq("wrk_no", params.getTaskNo()));
                wrkMast.setWrkSts(12L);
                if (!wrkMastService.updateById(wrkMast)) {
                    throw new CoolException("更新失败!!");
                }
            } else {
                throw new CoolException("出库下发任务下发失败---------------->" + result.get("msg"));
//                return R.error().add(result);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return R.ok();
    }
 
    /**
     * 堆垛机执行状态上报
     *
     * @param params
     * @return com.core.common.R
     * @author Ryan
     * @date 2026/1/10 16:30
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R receviceTaskFromWcs(ReceviceTaskParams params) {
        if (Objects.isNull(params.getSuperTaskNo())) {
            throw new CoolException("WMS任务号不能为空!!");
        }
        if (Objects.isNull(params.getNotifyType())) {
            throw new CoolException("动作类型不能为空!!");
        }
        WrkMast mast = wrkMastService.selectOne(new EntityWrapper<WrkMast>().eq("wrk_no", params.getSuperTaskNo()));
        if (Objects.isNull(mast)) {
            throw new CoolException("任务档不存在!!");
        }
        log.info("任务执行参数回写:{}", JSON.toJSONString(params));
        if (params.getMsgType().equals("task_complete") || params.getMsgType().equals("station_out_task_run_complete")) {
            JSONObject json = JSONObject.parseObject(params.getData());
            log.info(params.getSuperTaskNo() +  "==============>{}", json);
            if (mast.getIoType() == 1 || mast.getIoType() == 11 || mast.getIoType() == 10 || mast.getIoType() == 53 || mast.getIoType() == 54 || mast.getIoType() == 57) {
                if (params.getMsgType().equals("task_complete")) {
                    if (mast.getIoType() == 53 && !Objects.isNull(json) && json.getInteger("ioType") == 1) {
                        List<WrkDetl> wrkDetls103 = wrkDetlService
                                .selectList(new EntityWrapper<WrkDetl>().eq("wrk_no", mast.getWrkNo()));
                        if (wrkDetls103.isEmpty()) {
                            throw new CoolException("拣料出库 ===>> 更新订单完成数量失败; [workNo=" + mast.getWrkNo());
                        }
                        for (WrkDetl wrkDetl : wrkDetls103) {
                            // 更新订单完成数量
                            OrderDetlPakout orderDetlPakout = orderDetlPakoutService.selectItem(wrkDetl.getOrderNo(),
                                    wrkDetl.getMatnr(), wrkDetl.getBatch(), wrkDetl.getBrand(), wrkDetl.getStandby1(),
                                    wrkDetl.getStandby2(), wrkDetl.getStandby3(),
                                    wrkDetl.getBoxType1(), wrkDetl.getBoxType2(), wrkDetl.getBoxType3());
                            if (orderDetlPakout == null) {
                                orderDetlPakout = orderDetlPakoutService.selectItem(wrkDetl.getOrderNo(),
                                        wrkDetl.getMatnr(), wrkDetl.getBatch(), wrkDetl.getBrand(), wrkDetl.getStandby1(),
                                        wrkDetl.getStandby2(), wrkDetl.getStandby3(),
                                        wrkDetl.getBoxType1(), wrkDetl.getBoxType2(), wrkDetl.getBoxType3());
                            }
                            if (!Cools.isEmpty(orderDetlPakout)) {
                                if (!orderDetlPakoutService.increaseQtyByOrderNo(wrkDetl.getOrderNo(),
                                        wrkDetl.getMatnr(),
                                        orderDetlPakout.getBatch(), wrkDetl.getBrand(), wrkDetl.getStandby1(),
                                        wrkDetl.getStandby2(), wrkDetl.getStandby3(),
                                        wrkDetl.getBoxType1(), wrkDetl.getBoxType2(), wrkDetl.getBoxType3(),
                                        wrkDetl.getAnfme())) {
                                    throw new CoolException("全板出库 ===>> 更新订单完成数量失败; [workNo=" + mast.getWrkNo());
                                }
                            }
                        }
                        mast.setWrkSts(4L);
                    } else if ((mast.getIoType() == 54 || mast.getIoType() == 57) && !Objects.isNull(json) && json.getInteger("ioType") == 1 ) {
                        mast.setWrkSts(4L);
                    } else if (mast.getIoType() != 53 && mast.getIoType() != 54 && mast.getIoType() != 57) {
                        mast.setWrkSts(4L);
                    }
                }
            } else if (mast.getIoType() == 101 || mast.getIoType() == 110 || mast.getIoType() == 103 || mast.getIoType() == 104 || mast.getIoType() == 107) {
                if (mast.getWrkSts() != 14) {
                    //TODO 恢复正常后,这里代码要注释掉
//                    if (mast.getIoType() == 103  || mast.getIoType() == 107) {
//                        List<WrkDetl> wrkDetls103 = wrkDetlService
//                                .selectList(new EntityWrapper<WrkDetl>().eq("wrk_no", mast.getWrkNo()));
//                        if (wrkDetls103.isEmpty()) {
//                            throw new CoolException("拣料出库 ===>> 更新订单完成数量失败; [workNo=" + mast.getWrkNo());
//                        }
//                        for (WrkDetl wrkDetl : wrkDetls103) {
//                            // 更新订单完成数量
//                            OrderDetlPakout orderDetlPakout = orderDetlPakoutService.selectItem(wrkDetl.getOrderNo(),
//                                    wrkDetl.getMatnr(), wrkDetl.getBatch(), wrkDetl.getBrand(), wrkDetl.getStandby1(),
//                                    wrkDetl.getStandby2(), wrkDetl.getStandby3(),
//                                    wrkDetl.getBoxType1(), wrkDetl.getBoxType2(), wrkDetl.getBoxType3());
//                            if (orderDetlPakout == null) {
//                                orderDetlPakout = orderDetlPakoutService.selectItem(wrkDetl.getOrderNo(),
//                                        wrkDetl.getMatnr(), wrkDetl.getBatch(), wrkDetl.getBrand(), wrkDetl.getStandby1(),
//                                        wrkDetl.getStandby2(), wrkDetl.getStandby3(),
//                                        wrkDetl.getBoxType1(), wrkDetl.getBoxType2(), wrkDetl.getBoxType3());
//                            }
//                            if (!Cools.isEmpty(orderDetlPakout)) {
//                                if (!orderDetlPakoutService.increaseQtyByOrderNo(wrkDetl.getOrderNo(),
//                                        wrkDetl.getMatnr(),
//                                        orderDetlPakout.getBatch(), wrkDetl.getBrand(), wrkDetl.getStandby1(),
//                                        wrkDetl.getStandby2(), wrkDetl.getStandby3(),
//                                        wrkDetl.getBoxType1(), wrkDetl.getBoxType2(), wrkDetl.getBoxType3(),
//                                        wrkDetl.getAnfme())) {
//                                    throw new CoolException("全板出库 ===>> 更新订单完成数量失败; [workNo=" + mast.getWrkNo());
//                                }
//                            }
//                        }
//                    }
                }
                mast.setWrkSts(14L);
            }
            if (!wrkMastService.updateById(mast)) {
                throw new CoolException("任务状态修改失败!!");
            }
        } else if (params.getMsgType().equals("task_cancel")) {
            //todo 取消任务
        }
        return R.ok();
    }
 
    /**
     * 獲取庫位信息
     *
     * @param params
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R getLocs(Map<String, String> params) {
        String url = MesConstant.SYN_LOCS;
        String response;
        try {
            response = new HttpHandler.Builder()
                    .setUri(MesConstant.WCS_URL)
                    .setPath(url)
                    .setJson(JSON.toJSONString(params))
                    .build()
                    .doPost();
            JSONObject jsonObject = JSONObject.parseObject(response);
            if (jsonObject.get("code").equals(200)) {
                List<LocMastDto> dtos = JSONArray.parseArray(jsonObject.get("data").toString(), LocMastDto.class);
                dtos.forEach(e -> {
                    LocMast locMast = new LocMast();
                    BeanUtils.copyProperties(e, locMast);
                    locMast.setRow1(e.getRow());
                    locMast.setLev1(e.getLev());
                    locMast.setBay1(e.getBay());
                    System.out.println(JSONObject.toJSON(locMast));
                    if (!locMastService.insert(locMast)) {
                        throw new CoolException("库位保存失败!!");
                    }
                });
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return R.ok();
    }
 
    @Override
    public R getStations(Map<String, String> params) {
        return null;
    }
 
    /**
     * 条码站入库申请
     *
     * @param param
     * @param userId
     * @return
     */
    @Override
    public R combInPub(CompleteParam param, long userId) {
        if (Objects.isNull(param.getBarcode())) {
            throw new CoolException("托盘码不能为空!!");
        }
        if (Objects.isNull(param.getSourceStaNo())) {
            throw new CoolException("站点不能为空!!");
        }
        if (Objects.isNull(param.getLocType1())) {
            param.setLocType1((short) 1);
        }
 
        BasContainer container = basContainerService.selectOne(new EntityWrapper<BasContainer>().eq("barcode", param.getBarcode()));
        if (Objects.isNull(container)) {
            throw new CoolException("容器编码未维护,请维护后再操作!!");
        }
 
        WrkMast barcode = wrkMastService.selectOne(new EntityWrapper<WrkMast>().eq("barcode", param.getBarcode()));
        if (!Objects.isNull(barcode) && (barcode.getIoType() == 104 || barcode.getIoType() == 103 || barcode.getIoType() == 107) && barcode.getWrkSts() == 14L) {
            //
            workService.pickWrkMast(barcode.getWrkNo() + "", userId);
 
            Map<String, Object> response = new HashMap<>();
            response.put("taskNo", barcode.getWrkNo());
            response.put("locNo", barcode.getLocNo());
            response.put("taskPri", barcode.getIoPri());
 
            return R.ok("任务生成成功").add(response);
        } else {
            log.info("WCS任务生成请求参数:{}", JSONObject.toJSONString(param));
 
            BasDevp station = basDevpService.selectOne(new EntityWrapper<BasDevp>()
                    .eq("dev_no", param.getSourceStaNo()));
            if (Objects.isNull(station)) {
                throw new CoolException("站点不存在!!");
            }
            Map<String, Object> response = new HashMap<>();
 
            if (Objects.isNull(barcode)) {
                List<WaitPakin> waitPakins = waitPakinService
                        .selectList(new EntityWrapper<WaitPakin>()
                                .eq("io_status", "N")
                                .eq("zpallet", param.getBarcode()));
                if (Objects.isNull(waitPakins) || waitPakins.isEmpty()) {
                    throw new CoolException("数据错误,组拖档已不存在!!");
                }
 
                WrkMast wrkMast = generateCrnInTask(waitPakins, station, param.getLocType1(), userId);
 
                response.put("taskNo", wrkMast.getWrkNo());
                response.put("locNo", wrkMast.getLocNo());
                response.put("taskPri", wrkMast.getIoPri());
            } else {
                response.put("taskNo", barcode.getWrkNo());
                response.put("locNo", barcode.getLocNo());
                response.put("taskPri", barcode.getIoPri());
            }
 
            return R.ok("任务生成成功").add(response);
        }
    }
 
    /**
     * 生成堆垛机入库任务
     *
     * @param station
     * @author Ryan
     * @date 2025/12/9 13:54
     */
    @Transactional(rollbackFor = Exception.class)
    public WrkMast generateCrnInTask(List<WaitPakin> waitPakins, BasDevp station, Short locType, Long userId) {
        Date now = new Date();
 
        BasDevp sourceStaNo = basDevpService.checkSiteStatus(station.getDevNo(), true);
        sourceStaNo.setLocType1(locType);
        LocTypeDto locTypeDto = new LocTypeDto(sourceStaNo);
        WaitPakin pakin = waitPakins.stream().findFirst().get();
        FindLocNoAttributeVo findLocNoAttributeVo = new FindLocNoAttributeVo(pakin);
 
        WrkMast mast = wrkMastService.selectOne(new EntityWrapper<WrkMast>().eq("barcode", pakin.getZpallet()));
        WrkMast wrkMast = new WrkMast();
        String locNo;
        if (!Objects.isNull(mast)) {
            if (mast.getIoType().equals(TaskIOType.MERGE_OUT.type) || mast.getIoType().equals(TaskIOType.PICK_OUT.type) || mast.getIoType().equals(TaskIOType.CHECK_OUT.type)) {
                int ioType = mast.getIoType() - 50;
 
                List<WrkDetl> wrkDetls103 = wrkDetlService
                        .selectList(new EntityWrapper<WrkDetl>().eq("wrk_no", wrkMast.getWrkNo()));
                if (wrkDetls103.isEmpty()) {
                    ioType = 10;
                }
                List<LocDetl> locDetls = locDetlService.selectList(new EntityWrapper<LocDetl>().eq("loc_no", wrkMast.getSourceLocNo()));
                if (!Objects.isNull(locDetls) && !locDetls.isEmpty()) {
                    Double sum = wrkDetls103.stream().mapToDouble(WrkDetl::getAnfme).sum();
                    Double summed = locDetls.stream().mapToDouble(LocDetl::getAnfme).sum();
                    if (summed.compareTo(sum) <= 0) {
                        ioType = 10;
                    }
                }
                mast.setIoType(ioType);
                mast.setWrkSts(2L);
                mast.setSourceStaNo(mast.getSourceStaNo());
                mast.setStaNo(mast.getSourceStaNo());
                mast.setLocNo(mast.getSourceLocNo());
                boolean res = wrkMastService.updateById(mast);
                BeanUtils.copyProperties(mast, wrkMast);
                if (!res) {
                    throw new CoolException("保存工作档失败");
                }
                locNo = mast.getLocNo();
                // 更新源站点信息
                sourceStaNo.setWrkNo(mast.getWrkNo());
                sourceStaNo.setModiTime(now);
                if (!basDevpService.updateById(sourceStaNo)) {
                    throw new CoolException("更新源站失败");
                }
                // 更新目标库位状态
                LocMast locMast = locMastService.selectById(mast.getLocNo());
                if (locMast.getLocSts().equals("O")) {
                    locMast.setLocSts("S"); // S.入库预约
                    locMast.setModiTime(now);
                    if (!locMastService.updateById(locMast)) {
                        throw new CoolException("改变库位状态失败");
                    }
                } else {
                    throw new CoolException(mast.getLocNo() + "目标库位已被占用");
                }
            } else {
                throw new CoolException("托盘已存在任务档!!");
            }
        } else {
            StartupDto dto = commonService.getLocNo(1, sourceStaNo.getDevNo(), findLocNoAttributeVo, locTypeDto);
 
            int workNo = dto.getWorkNo();
            // 生成工作档
            wrkMast.setWrkNo(workNo);
            wrkMast.setIoTime(new Date());
            wrkMast.setWrkSts(2L); // 工作状态:生成入库ID
            wrkMast.setIoType(1); // 入出库状态:1.入库
            wrkMast.setCrnNo(dto.getCrnNo());
            wrkMast.setSourceStaNo(dto.getSourceStaNo() + "");
            wrkMast.setStaNo(!Objects.isNull(dto.getStaNo()) ? dto.getStaNo() + "" : null);
            wrkMast.setLocNo(dto.getLocNo());
            wrkMast.setIoPri(13D); // 优先级
            wrkMast.setBarcode(pakin.getZpallet()); // 托盘码
            wrkMast.setFullPlt("Y"); // 满板:Y
            wrkMast.setPicking("N"); // 拣料
            wrkMast.setExitMk("N"); // 退出
            wrkMast.setEmptyMk("N"); // 空板
            wrkMast.setLinkMis("Y");
            wrkMast.setModiUser(userId);
            wrkMast.setAppeUser(userId);
            wrkMast.setCtnType(sourceStaNo.getCtnType()); // 容器类型
            // 操作人员数据
            wrkMast.setAppeTime(now);
            wrkMast.setModiTime(now);
            boolean res = wrkMastService.insert(wrkMast);
            if (!res) {
                throw new CoolException("保存工作档失败");
            }
            locNo = dto.getLocNo();
            // 更新源站点信息
            sourceStaNo.setWrkNo(workNo);
            sourceStaNo.setModiTime(now);
            if (!basDevpService.updateById(sourceStaNo)) {
                throw new CoolException("更新源站失败");
            }
            // 更新目标库位状态
            LocMast locMast = locMastService.selectById(dto.getLocNo());
            if (locMast.getLocSts().equals("O")) {
                locMast.setLocSts("S"); // S.入库预约
                locMast.setModiTime(now);
                if (!locMastService.updateById(locMast)) {
                    throw new CoolException("改变库位状态失败");
                }
            } else {
                throw new CoolException(dto.getLocNo() + "目标库位已被占用");
            }
        }
 
        // 生成工作档明细
        waitPakins.forEach(waitPakin -> {
            if (!Objects.isNull(waitPakin.getMatnr())) {
                WrkDetl wrkDetl = new WrkDetl();
                wrkDetl.sync(waitPakin);
                wrkDetl.setWrkNo(wrkMast.getWrkNo());
                wrkDetl.setIoTime(wrkMast.getIoTime());
                wrkDetl.setAppeTime(now);
                wrkDetl.setModiTime(now);
                if (!wrkDetlService.insert(wrkDetl)) {
                    throw new CoolException("保存工作明细失败");
                }
            } else {
                wrkMast.setWrkSts(2L);
                wrkMast.setIoType(10);
                wrkMastService.updateById(wrkMast);
            }
        });
 
        // 更新入库通知档 ioStatus ===>> Y
        Wrapper<WaitPakin> wrapper = new EntityWrapper<WaitPakin>()
                .eq("zpallet", pakin.getZpallet());
 
        WaitPakin setParam = new WaitPakin();
        setParam.setLocNo(locNo);
        setParam.setIoStatus("Y");
        setParam.setModiTime(now);
        if (!waitPakinService.update(setParam, wrapper)) {
            throw new CoolException("更新通知档失败");
        }
 
        return wrkMast;
    }
 
    /**
     * WCS申请任务重新分配入库
     *
     * @param params
     * @return
     */
    @Override
    public R repeatLoc(CompleteParam params) {
        if (Objects.isNull(params.getTaskNo())) {
            return R.error("工作号不能为空!!");
        }
        WrkMast wrkMast = wrkMastService.selectOne(new EntityWrapper<WrkMast>().eq("wrk_no", params.getTaskNo()));
        if (Objects.isNull(wrkMast)) {
            WrkMastLog wrkLog = wrkMastLogService.selectOne(new EntityWrapper<WrkMastLog>()
                    .eq("loc_no", params.getLocNo())
                    .eq("wrk_no", params.getTaskNo()));
 
            wrkMast = new WrkMast();
            BeanUtils.copyProperties(wrkLog, wrkMast);
 
//            return R.error("任务档不存在,或已加入历史档");
        }
        LocMast locMast = locMastService.selectById(wrkMast.getLocNo());
        if (Objects.isNull(locMast)) {
            return R.error("库位信息不存在!!");
        }
 
        Integer whsType = Utils.GetWhsType(Integer.parseInt(wrkMast.getSourceStaNo()));
        if (whsType.equals(1)) {
            RowLastno lastno = rowLastnoService.selectById(whsType);
            if (Objects.isNull(lastno)) {
                return R.error("堆垛要策略不存在!!");
            }
            if (locMast.getRow1().equals(141)) {
                LocMast selectOne = locMastService.selectOne(new EntityWrapper<LocMast>()
                        .ne("row1", 141)
                        .ge("crn_no", lastno.getsCrnNo())
                        .le("crn_no", lastno.geteCrnNo())
                        .eq("loc_sts", LocStsType.LOC_STS_TYPE_O.type)
                        .orderAsc(Arrays.asList("row1", "bay1", "lev1")));
                if (!Objects.isNull(selectOne)) {
                    Map<String, Object> response = new HashMap<>();
                    response.put("locNo", selectOne.getLocNo());
                    List<LocDetl> locDetls = locDetlService.selectList(new EntityWrapper<LocDetl>().eq("loc_no", wrkMast.getLocNo()));
                    locDetls.forEach(locDetl -> {
                        String locNo = locDetl.getLocNo();
                        locDetl.setLocNo(selectOne.getLocNo());
                        if (!locDetlService.update(locDetl, new EntityWrapper<LocDetl>()
                                .eq("loc_no", locNo)
                                .eq("supp_code", locDetl.getSuppCode())
                                .eq("matnr", locDetl.getMatnr()))) {
                            throw new CoolException("库位更新失败!!");
                        }
                    });
 
                    //新库位预约入库
                    selectOne.setBarcode(locMast.getBarcode());
                    selectOne.setLocSts(LocStsType.LOC_STS_TYPE_F.type);
                    locMastService.updateById(selectOne);
 
                    wrkMast.setLocNo(selectOne.getLocNo());
                    wrkMast.setWrkSts(2L);
                    wrkMast.setBarcode(locMast.getBarcode());
                    wrkMastService.updateById(wrkMast);
 
 
                    //原库位置空
                    locMast.setLocSts(LocStsType.LOC_STS_TYPE_O.type);
                    locMast.setBarcode(null);
                    locMastService.updateById(locMast);
 
                    return R.ok().add(response);
                } else {
                    return R.error("暂无可用库位!!");
                }
            } else {
                LocMast selectOne = locMastService.selectOne(new EntityWrapper<LocMast>()
                        .eq("crn_no", wrkMast.getCrnNo())
                        .eq("loc_sts", LocStsType.LOC_STS_TYPE_O.type)
                        .orderAsc(Arrays.asList("row1", "bay1", "lev1")));
                if (!Objects.isNull(selectOne)) {
                    Map<String, Object> response = new HashMap<>();
                    response.put("locNo", selectOne.getLocNo());
 
                    List<LocDetl> locDetls = locDetlService.selectList(new EntityWrapper<LocDetl>().eq("loc_no", wrkMast.getLocNo()));
                    locDetls.forEach(locDetl -> {
                        String locNo = locDetl.getLocNo();
                        locDetl.setLocNo(selectOne.getLocNo());
                        if (!locDetlService.update(locDetl, new EntityWrapper<LocDetl>()
                                .eq("loc_no", locNo)
                                .eq("supp_code", locDetl.getSuppCode())
                                .eq("matnr", locDetl.getMatnr()))) {
                            throw new CoolException("库位更新失败!!");
                        }
                    });
 
                    //新库位预约入库
                    selectOne.setBarcode(locMast.getBarcode());
                    selectOne.setLocSts(LocStsType.LOC_STS_TYPE_F.type);
                    locMastService.updateById(selectOne);
 
                    wrkMast.setLocNo(selectOne.getLocNo());
                    wrkMast.setWrkSts(2L);
                    wrkMast.setBarcode(locMast.getBarcode());
                    wrkMastService.updateById(wrkMast);
 
 
                    //原库位置空
                    locMast.setLocSts(LocStsType.LOC_STS_TYPE_O.type);
                    locMast.setBarcode(null);
                    locMastService.updateById(locMast);
 
                    return R.ok().add(response);
                } else {
                    return R.error("暂无可用库位!!");
                }
            }
        } else {
            LocMast selectOne = locMastService.selectOne(new EntityWrapper<LocMast>()
                    .eq("crn_no", wrkMast.getCrnNo())
                    .eq("loc_sts", LocStsType.LOC_STS_TYPE_O.type)
                    .orderAsc(Arrays.asList("row1", "bay1", "lev1")));
            if (!Objects.isNull(selectOne)) {
                Map<String, Object> response = new HashMap<>();
                response.put("locNo", selectOne.getLocNo());
 
                List<LocDetl> locDetls = locDetlService.selectList(new EntityWrapper<LocDetl>().eq("loc_no", wrkMast.getLocNo()));
                locDetls.forEach(locDetl -> {
                    String locNo = locDetl.getLocNo();
                    locDetl.setLocNo(selectOne.getLocNo());
                    if (!locDetlService.update(locDetl, new EntityWrapper<LocDetl>()
                            .eq("loc_no", locNo)
                            .eq("supp_code", locDetl.getSuppCode())
                            .eq("matnr", locDetl.getMatnr()))) {
                        throw new CoolException("库位更新失败!!");
                    }
                });
 
                //新库位预约入库
                selectOne.setBarcode(locMast.getBarcode());
                selectOne.setLocSts(LocStsType.LOC_STS_TYPE_F.type);
                locMastService.updateById(selectOne);
 
                wrkMast.setLocNo(selectOne.getLocNo());
                wrkMast.setWrkSts(2L);
                wrkMast.setBarcode(locMast.getBarcode());
                wrkMastService.updateById(wrkMast);
 
 
                //原库位置空
                locMast.setLocSts(LocStsType.LOC_STS_TYPE_O.type);
                locMast.setBarcode(null);
                locMastService.updateById(locMast);
 
                return R.ok().add(response);
            } else {
                return R.error("暂无可用库位!!");
            }
        }
    }
}