自动化立体仓库 - WMS系统
chen.llin
5 小时以前 375f337b2a91e59beb0b41dc89353de1e92aa6a1
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
package com.zy.asrs.service.impl;
 
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.core.common.Cools;
import com.core.common.DateUtils;
import com.core.common.R;
import com.core.common.SnowflakeIdWorker;
import com.core.exception.CoolException;
import com.zy.asrs.entity.*;
import com.zy.asrs.entity.param.*;
import com.zy.asrs.entity.result.OpenOrderCompeteResult;
import com.zy.asrs.entity.result.StockVo;
import com.zy.asrs.mapper.TagMapper;
import com.zy.asrs.service.*;
import com.zy.asrs.task.handler.AgvHandler;
import com.zy.asrs.utils.MatUtils;
import com.zy.common.model.DetlDto;
import com.zy.common.utils.NodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
 
/**
 * Created by vincent on 2022/4/9
 */
@Slf4j
@Service
public class OpenServiceImpl implements OpenService {
 
    @Autowired
    private OrderPakinService orderPakinService;
    @Autowired
    private OrderDetlPakinService orderDetlPakinService;
    @Autowired
    private OrderPakoutService orderPakoutService;
    @Autowired
    private OrderDetlPakoutService orderDetlPakoutService;
    @Autowired
    private SnowflakeIdWorker snowflakeIdWorker;
    @Autowired
    private DocTypeService docTypeService;
    @Autowired
    private MatService matService;
    @Autowired
    private LocDetlService locDetlService;
    @Autowired
    private PackService packService;
    @Autowired
    private TagService tagService;
    @Autowired
    private TagMapper tagMapper;
 
    @Resource
    private TaskService taskService;
 
    @Resource
    private AgvHandler agvHandler;
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R agvCallback(AgvCallbackDto param) {
        log.debug("agv回调 param:{}", param);
        // 优先使用taskId,如果没有则使用id(向后兼容)
        String taskId = Cools.isEmpty(param.getTaskId()) ? param.getId() : param.getTaskId();
        if (Cools.isEmpty(taskId)) {
            return R.error("任务id不能为空");
        }
        
        log.info("收到AGV任务回调请求,taskId:{},fromBin:{},toBin:{},robotGroup:{},kind:{}", 
            taskId, param.getFromBin(), param.getToBin(), 
            Cools.isEmpty(param.getRobotGroup()) ? param.getRobotName() : param.getRobotGroup(), 
            param.getKind());
        
        // 根据taskId查询任务
        // taskId格式:T + 工作号(如"T5299")或 T + 任务ID(向后兼容)
        Task task = null;
        try {
            // 处理"T"前缀格式(如"T5299"或"T130")
            String numericId = taskId;
            if (taskId.startsWith("T") && taskId.length() > 1) {
                numericId = taskId.substring(1);
                    log.debug("检测到T前缀格式的taskId,提取数字:{}", numericId);
            }
            
            // 先尝试通过工作号(wrk_no)查询(优先,因为现在使用工作号作为taskId)
            try {
                Integer wrkNo = Integer.parseInt(numericId);
                task = taskService.selectOne(
                    new EntityWrapper<Task>().eq("wrk_no", wrkNo).eq("is_deleted", 0)
                );
                if (task != null) {
                    log.debug("通过工作号(wrk_no={})找到任务,taskId:{}", wrkNo, task.getId());
                }
            } catch (NumberFormatException e) {
                log.debug("无法解析为工作号,taskId:{}", taskId);
            }
            
            // 如果通过工作号没找到,尝试通过任务ID查询(向后兼容)
            if (task == null) {
                try {
                    Long taskIdLong = Long.parseLong(numericId);
                    task = taskService.selectById(taskIdLong);
                    if (task != null) {
                        log.debug("通过任务ID(id={})找到任务,taskId:{}", taskIdLong, taskId);
                    }
                } catch (NumberFormatException e) {
                    log.debug("无法解析为任务ID,taskId:{}", taskId);
                }
            }
        } catch (Exception e) {
            log.debug("解析taskId异常,taskId:{},异常:{}", taskId, e.getMessage());
        }
        
        // 如果还是没找到,尝试通过其他字段查询(如sheetNo等,向后兼容)
        if (task == null) {
            log.debug("通过工作号和任务ID都没找到,尝试通过其他字段查询,taskId:{}", taskId);
            task = taskService.selectOne(
                new EntityWrapper<Task>().eq("sheet_no", taskId).eq("is_deleted", 0)
            );
        }
        
        if (task == null) {
            log.warn("未找到对应的任务,taskId:{}", taskId);
            return R.error("任务不存在,taskId:" + taskId);
        }
        
        // 更新任务信息
        if (!Cools.isEmpty(param.getFromBin())) {
            task.setSourceStaNo(param.getFromBin());
        }
        if (!Cools.isEmpty(param.getToBin())) {
            task.setLocNo(param.getToBin());
        }
        
        // 优先使用robotGroup,如果没有则使用robotName(向后兼容)
        String robotGroup = Cools.isEmpty(param.getRobotGroup()) ? param.getRobotName() : param.getRobotGroup();
        if (!Cools.isEmpty(robotGroup)) {
            task.setInvWh(robotGroup);
        }
        
        task.setModiTime(new Date());
        
        // 标记任务是否已经被转移到历史表
        boolean taskMovedToHistory = false;
        
        // 如果有status字段,按原有逻辑处理(向后兼容)
        if (!Cools.isEmpty(param.getStatus())) {
            switch (param.getStatus()) {
                case "Assigned":
                    // AGV已派车,确认接收命令,记录确认时间
                    if (task.getPlcStrTime() == null) {
                        task.setPlcStrTime(new Date());
//                        log.info("AGV已确认接收命令,记录确认时间,taskId:{}", taskId);
                    }
                    if (Cools.isEmpty(robotGroup)) {
                        task.setInvWh(param.getRobotName());
                    }
                    break;
                case "Done":
                    // status="Done" 才算搬运完成,结束掉AGV任务(适用于所有任务类型:入库、出库、转移)
                    if (task.getWrkSts() != null && task.getWrkSts() == 8L) {
                        task.setWrkSts(9L);
                        task.setModiTime(new Date());
                        String taskTypeName = "";
                        if (!Cools.isEmpty(param.getKind())) {
                            taskTypeName = param.getKind();
                        }
                        log.info("AGV回调搬运完成(status=Done),完结AGV呼叫单,taskId:{},任务类型:{}", taskId, taskTypeName);
                        // 保存任务更新后,转移到历史表
                        if (taskService.updateById(task)) {
                            try {
                                agvHandler.moveTaskToHistory(Collections.singletonList(task));
                                taskMovedToHistory = true;
                                log.info("AGV回调搬运完成,已将任务转移到历史表,taskId:{}", taskId);
                            } catch (Exception e) {
                                log.error("AGV回调搬运完成,转移任务到历史表失败,taskId:{}", taskId, e);
                            }
                        }
                    } else {
                        log.debug("AGV回调status=Done,但任务状态不是8(当前状态:{}),不结束任务,taskId:{}", 
                            task.getWrkSts(), taskId);
                    }
                    break;
                case "Failed":
                case "Cancelled":
                default:
                    break;
            }
        }
        
        // 根据任务类型进行相应处理(记录日志,但不在此处结束任务,任务结束统一由status="Done"处理)
        if (!Cools.isEmpty(param.getKind())) {
            String kind = param.getKind();
            if ("货物转运".equals(kind)) {
                log.info("处理货物转运任务,taskId:{},status:{}", taskId, param.getStatus());
            } else if ("实托入库".equals(kind) || "空托入库".equals(kind)) {
                log.info("处理入库任务,taskId:{},status:{}", taskId, param.getStatus());
                // 注意:任务结束统一由 status="Done" 处理,此处不再重复处理
            } else if ("实托出库".equals(kind)) {
                log.info("处理实托出库任务,taskId:{},status:{}", taskId, param.getStatus());
                // 注意:任务结束统一由 status="Done" 处理,此处不再重复处理
            }
        }
        
        // 如果任务已经被转移到历史表,直接返回成功
        if (taskMovedToHistory) {
            log.info("AGV任务回调处理成功,任务已转移到历史表,taskId:{}", taskId);
            return R.ok();
        }
        
        // 保存任务更新
        if (!taskService.updateById(task)) {
            log.error("更新任务失败,taskId:{}", taskId);
            return R.error("更新任务失败");
        }
        
        log.info("AGV任务回调处理成功,taskId:{}", taskId);
        return R.ok();
    }
 
    @Override
    @Transactional
    public void pakinOrderCreate(OpenOrderPakinParam param) {
        String orderNo = param.getOrderNo();
        String orderType = param.getOrderType();
        String optType = param.getOptType();
        OrderPakin order = orderPakinService.selectByNo(param.getOrderNo());
        if (optType.equals("add")) {
            if (!Cools.isEmpty(order)) {
                throw new CoolException(param.getOrderNo() + "单据已存在,请勿重复提交");
            }
            DocType docType = docTypeService.selectOrAdd(orderType, Boolean.TRUE);
            Date now = new Date();
            // 单据主档
            order = new OrderPakin(
                    String.valueOf(snowflakeIdWorker.nextId()),    // 编号[非空]
                    param.getOrderNo(),    // 订单编号
                    DateUtils.convert(now),    // 单据日期
                    docType.getDocId(),    // 单据类型
                    null,    // 项目编号
                    null,    //
                    null,    // 调拨项目编号
                    null,    // 初始票据号
                    null,    // 票据号
                    null,    // 客户编号
                    null,    // 客户
                    null,    // 联系方式
                    null,    // 操作人员
                    null,    // 合计金额
                    null,    // 优惠率
                    null,    // 优惠金额
                    null,    // 销售或采购费用合计
                    null,    // 实付金额
                    null,    // 付款类型
                    null,    // 业务员
                    null,    // 结算天数
                    null,    // 邮费支付类型
                    null,    // 邮费
                    null,    // 付款时间
                    null,    // 发货时间
                    null,    // 物流名称
                    null,    // 物流单号
                    1L,    // 订单状态
                    1,    // 状态
                    null,    // 添加人员
                    now,    // 添加时间
                    null,    // 修改人员
                    now,    // 修改时间
                    null  // 备注
            );
            if (!orderPakinService.insert(order)) {
                throw new CoolException("生成单据主档失败,请联系管理员");
            }
            // 单据明细档
            List<DetlDto> list = new ArrayList<>();
            List<DetlDto> orderDetails = param.getOrderDetails();
            for (DetlDto detail : orderDetails) {
                DetlDto dto = new DetlDto(detail.getMatnr(), detail.getBatch(), detail.getBrand(), detail.getStandby1(), detail.getStandby2(), detail.getStandby3(), detail.getLineNumber(), detail.getBoxType1(), detail.getBoxType2(), detail.getBoxType3(), detail.getAnfme());
                if (DetlDto.hasLineNumber(list, dto)) {
                    DetlDto detlDto = DetlDto.findLineNumber(list, dto.getMatnr(), dto.getBatch(), dto.getBrand(), dto.getStandby1(), dto.getStandby2(), dto.getStandby3(), dto.getLineNumber(), dto.getBoxType1(), dto.getBoxType2(), dto.getBoxType3());
                    assert detlDto != null;
                    detlDto.setAnfme(detlDto.getAnfme() + detail.getAnfme());
                } else {
                    list.add(dto);
                }
            }
            for (DetlDto detlDto : list) {
                Mat mat = matService.selectByMatnr(detlDto.getMatnr());
                if (Cools.isEmpty(mat)) {
                    throw new CoolException(detlDto.getMatnr() + "编号商品检索失败,请先添加商品");
                }
                OrderDetlPakin orderDetl = new OrderDetlPakin();
                orderDetl.sync(mat);
                orderDetl.setLineNumber(detlDto.getLineNumber());
                orderDetl.setBatch(detlDto.getBatch());
                orderDetl.setAnfme(detlDto.getAnfme());
                orderDetl.setBrand(detlDto.getBrand());
                orderDetl.setStandby1(detlDto.getStandby1());
                orderDetl.setStandby2(detlDto.getStandby2());
                orderDetl.setStandby3(detlDto.getStandby3());
                orderDetl.setOrderId(order.getId());
                orderDetl.setOrderNo(order.getOrderNo());
                orderDetl.setCreateBy(null);
                orderDetl.setCreateTime(now);
                orderDetl.setUpdateBy(null);
                orderDetl.setUpdateTime(now);
                orderDetl.setStatus(1);
                orderDetl.setQty(0.0D);
                orderDetl.setPakinPakoutStatus(1);
                if (!orderDetlPakinService.insert(orderDetl)) {
                    throw new CoolException("生成单据明细失败,请联系管理员");
                }
            }
        } else if (optType.equals("delete")) {
            if (Cools.isEmpty(order)) {
                throw new CoolException(orderNo + "单据不存在!");
            }
 
            // 订单状态
            Long settle = order.getSettle();
            if (settle == 1) {
                // 删除订单明细
                orderDetlPakinService.delete(new EntityWrapper<OrderDetlPakin>().eq("order_id", order.getId()));
                // 删除订单主表
                orderPakinService.delete(new EntityWrapper<OrderPakin>().eq("id", order.getId()));
            } else {
                throw new CoolException(orderNo + ":订单已处理,无法取消");
            }
        } else {
            throw new CoolException(orderNo + ",操作类型不存在:" + optType);
        }
    }
 
    @Override
    @Transactional
    public List<OpenOrderCompeteResult> pakinOrderComplete(OpenOrderCompleteParam param) {
        List<OpenOrderCompeteResult> results = new ArrayList<>();
        if (!Cools.isEmpty(param) && !Cools.isEmpty(param.getOrderNo())) {
            // 指定订单
            OrderPakin order = orderPakinService.selectByNo(param.getOrderNo());
 
            if (null != order) {
                OpenOrderCompeteResult result = new OpenOrderCompeteResult();
                results.add(result);
                result.setOrderNo(order.getOrderNo());
                result.setOrderTime(order.getOrderTime());
                result.setOrderType(order.getDocType$());
                List<OrderDetlPakin> orderDetls = orderDetlPakinService.selectByOrderId(order.getId());
                for (OrderDetlPakin orderDetl : orderDetls) {
                    result.getOrderDetails().add(new DetlDto(orderDetl.getOrderNo(), orderDetl.getMatnr(), orderDetl.getBatch(), orderDetl.getBrand(),
                            orderDetl.getStandby1(), orderDetl.getStandby2(), orderDetl.getStandby3(), orderDetl.getLineNumber(),
                            orderDetl.getBoxType1(), orderDetl.getBoxType2(), orderDetl.getBoxType3(), orderDetl.getQty()));
                }
                if (order.getSettle() == 4L) {
                    // 修改订单状态 4.完成 ===>> 6.已上报
                    if (!orderPakinService.updateSettle(order.getId(), 6L, null)) {
                        throw new CoolException("服务器内部错误,请联系管理员");
                    }
                }
            }
        } else {
            // 所有订单
            List<OrderPakin> orders = orderPakinService.selectBySettle(4L);
            if (orders.isEmpty()) {
                orders = new ArrayList<>();
            }
            for (OrderPakin order : orders) {
                OpenOrderCompeteResult result = new OpenOrderCompeteResult();
                results.add(result);
                result.setOrderNo(order.getOrderNo());
                result.setOrderTime(order.getOrderTime());
                result.setOrderType(order.getDocType$());
                List<OrderDetlPakin> orderDetls = orderDetlPakinService.selectByOrderId(order.getId());
 
                for (OrderDetlPakin orderDetl : orderDetls) {
                    result.getOrderDetails().add(new DetlDto(orderDetl.getOrderNo(), orderDetl.getMatnr(), orderDetl.getBatch(), orderDetl.getBrand(),
                            orderDetl.getStandby1(), orderDetl.getStandby2(), orderDetl.getStandby3(), orderDetl.getLineNumber(),
                            orderDetl.getBoxType1(), orderDetl.getBoxType2(), orderDetl.getBoxType3(),
                            orderDetl.getQty()));
                }
//                // 修改订单状态 4.完成 ===>> 6.已上报
                if (!orderPakinService.updateSettle(order.getId(), 6L, null)) {
                    throw new CoolException("服务器内部错误,请联系管理员");
                }
 
            }
        }
        return results;
    }
 
    @Override
    @Transactional
    public void pakoutOrderCreate(OpenOrderPakoutParam param) {
        String orderNo = param.getOrderNo();
        String optType = param.getOptType();
        String orderType = param.getOrderType();
        OrderPakout order = orderPakoutService.selectByNo(param.getOrderNo());
        // 如果单据不存在则添加;如果单据存在,作业中无法修改,反之则修改单据
        if (optType.equals("add")) {
            if (!Cools.isEmpty(order)) {
                if (order.getSettle() > 1L) {
                    throw new CoolException(param.getOrderNo() + "订单已存在");
                }
                orderPakoutService.remove(order.getId());
            }
            DocType docType = docTypeService.selectOrAdd(orderType, Boolean.FALSE);
            Date now = new Date();
            // 单据主档
            order = new OrderPakout(
                    String.valueOf(snowflakeIdWorker.nextId()),    // 编号[非空]
                    param.getOrderNo(),    // 订单编号
                    DateUtils.convert(now),    // 单据日期
                    docType.getDocId(),    // 单据类型
                    null,    // 项目编号
                    null,    //
                    null,    // 调拨项目编号
                    null,    // 初始票据号
                    null,    // 票据号
                    null,    // 客户编号
                    null,    // 客户
                    null,    // 联系方式
                    null,    // 操作人员
                    null,    // 合计金额
                    null,    // 优惠率
                    null,    // 优惠金额
                    null,    // 销售或采购费用合计
                    null,    // 实付金额
                    null,    // 付款类型
                    null,    // 业务员
                    null,    // 结算天数
                    null,    // 邮费支付类型
                    null,    // 邮费
                    null,    // 付款时间
                    null,    // 发货时间
                    null,    // 物流名称
                    null,    // 物流单号
                    1L,    // 订单状态
                    1,    // 状态
                    null,    // 添加人员
                    now,    // 添加时间
                    null,    // 修改人员
                    now,    // 修改时间
                    null    // 备注
            );
            if (!orderPakoutService.insert(order)) {
                throw new CoolException("生成单据主档失败,请联系管理员");
            }
            // 单据明细档
            List<DetlDto> list = new ArrayList<>();
            List<DetlDto> orderDetails = param.getOrderDetails();
            for (DetlDto detail : orderDetails) {
                DetlDto dto = new DetlDto(detail.getMatnr(), detail.getBatch(), detail.getBrand(), detail.getStandby1(), detail.getStandby2(), detail.getStandby3(), detail.getLineNumber(),
                        detail.getBoxType1(), detail.getBoxType2(), detail.getBoxType3(), detail.getAnfme());
                if (DetlDto.hasLineNumber(list, dto)) {
                    DetlDto detlDto = DetlDto.findLineNumber(list, dto.getMatnr(), dto.getBatch(), dto.getBrand(), dto.getStandby1(), dto.getStandby2(), dto.getStandby3(), dto.getLineNumber(),
                            dto.getBoxType1(), dto.getBoxType2(), dto.getBoxType3());
                    assert detlDto != null;
                    detlDto.setAnfme(detlDto.getAnfme() + detail.getAnfme());
                } else {
                    list.add(dto);
                }
            }
            for (DetlDto detlDto : list) {
                Mat mat = matService.selectByMatnr(detlDto.getMatnr());
                if (Cools.isEmpty(mat)) {
                    throw new CoolException(detlDto.getMatnr() + "编号商品检索失败,请先添加商品");
                }
                OrderDetlPakout orderDetl = new OrderDetlPakout();
                orderDetl.sync(mat);
                orderDetl.setLineNumber(detlDto.getLineNumber());
                orderDetl.setBatch(detlDto.getBatch());
                orderDetl.setAnfme(detlDto.getAnfme());
                orderDetl.setBrand(detlDto.getBrand());
                orderDetl.setStandby1(detlDto.getStandby1());
                orderDetl.setStandby2(detlDto.getStandby2());
                orderDetl.setStandby3(detlDto.getStandby3());
                orderDetl.setOrderId(order.getId());
                orderDetl.setOrderNo(order.getOrderNo());
                orderDetl.setCreateBy(null);
                orderDetl.setCreateTime(now);
                orderDetl.setUpdateBy(null);
                orderDetl.setUpdateTime(now);
                orderDetl.setStatus(1);
                orderDetl.setQty(0.0D);
                orderDetl.setPakinPakoutStatus(2);
                if (!orderDetlPakoutService.insert(orderDetl)) {
                    throw new CoolException("生成单据明细失败,请联系管理员");
                }
            }
        } else if (optType.equals("delete")) {
            if (Cools.isEmpty(order)) {
                throw new CoolException(orderNo + "单据不存在!");
            }
            // 订单状态
            Long settle = order.getSettle();
            if (settle == 1) {
                // 删除订单明细
                orderDetlPakoutService.delete(new EntityWrapper<OrderDetlPakout>().eq("order_id", order.getId()));
                // 删除订单主表
                orderPakoutService.delete(new EntityWrapper<OrderPakout>().eq("id", order.getId()));
            } else {
                throw new CoolException(orderNo + ":订单已处理,无法取消");
            }
        } else {
            throw new CoolException(orderNo + ",操作类型不存在:" + optType);
        }
    }
 
    @Override
    public List<OpenOrderCompeteResult> pakoutOrderComplete(OpenOrderCompleteParam param) {
        List<OpenOrderCompeteResult> results = new ArrayList<>();
        if (!Cools.isEmpty(param) && !Cools.isEmpty(param.getOrderNo())) {
            // 指定订单
            OrderPakout order = orderPakoutService.selectByNo(param.getOrderNo());
            if (null != order) {
                OpenOrderCompeteResult result = new OpenOrderCompeteResult();
                results.add(result);
                result.setOrderNo(order.getOrderNo());
                result.setOrderTime(order.getOrderTime());
                result.setOrderType(order.getDocType$());
                List<OrderDetlPakout> orderDetls = orderDetlPakoutService.selectByOrderId(order.getId());
//                List<OrderDetl> orderDetls = OrderInAndOutUtil.selectByOrderId(order.getPakinPakoutStatus$(),order.getId());
                for (OrderDetlPakout orderDetl : orderDetls) {
                    result.getOrderDetails().add(new DetlDto(orderDetl.getOrderNo(), orderDetl.getMatnr(), orderDetl.getBatch(), orderDetl.getBrand(),
                            orderDetl.getStandby1(), orderDetl.getStandby2(), orderDetl.getStandby3(), orderDetl.getLineNumber(),
                            orderDetl.getBoxType1(), orderDetl.getBoxType2(), orderDetl.getBoxType3(), orderDetl.getQty()));
                }
                if (order.getSettle() == 4L) {
                    // 修改订单状态 4.完成 ===>> 6.已上报
                    if (!orderPakoutService.updateSettle(order.getId(), 6L, null)) {
                        throw new CoolException("服务器内部错误,请联系管理员");
                    }
                }
            }
        } else {
            // 所有订单
            List<OrderPakout> orders = orderPakoutService.selectList(new EntityWrapper<OrderPakout>().eq("settle", 4L));
            for (OrderPakout order : orders) {
                OpenOrderCompeteResult result = new OpenOrderCompeteResult();
                results.add(result);
                result.setOrderNo(order.getOrderNo());
                result.setOrderTime(order.getOrderTime());
                result.setOrderType(order.getDocType$());
                List<OrderDetlPakout> orderDetls = orderDetlPakoutService.selectByOrderId(order.getId());
                for (OrderDetlPakout orderDetl : orderDetls) {
                    result.getOrderDetails().add(new DetlDto(orderDetl.getOrderNo(), orderDetl.getMatnr(), orderDetl.getBatch(), orderDetl.getBrand(),
                            orderDetl.getStandby1(), orderDetl.getStandby2(), orderDetl.getStandby3(), orderDetl.getLineNumber(),
                            orderDetl.getBoxType1(), orderDetl.getBoxType2(), orderDetl.getBoxType3(), orderDetl.getQty()));
                }
                // 修改订单状态 4.完成 ===>> 6.已上报
                if (!orderPakoutService.updateSettle(order.getId(), 6L, null)) {
                    throw new CoolException("服务器内部错误,请联系管理员");
                }
            }
        }
        return results;
    }
 
    @Override
    @Transactional
    public List<StockVo> queryStock() {
        return locDetlService.queryStockTotal();
    }
 
    @Override
    @Transactional
    public void packageUp(PackParam param) {
        if (Cools.isEmpty(param.getBarcode())) {
            throw new CoolException("barcode不能为空");
        }
        Mat analyse = MatUtils.analyseMat(param.getBarcode());
        Pack pack = packService.selectByBarcode(param.getBarcode());
        if (null != pack) {
            throw new CoolException(param.getBarcode() + "重复提交");
        }
        Date now = new Date();
        pack = new Pack(
                param.getBarcode(),    // 条码[非空]
                analyse.getMatnr(),    // 商品编号
                analyse.getBarcode(),    // 序列码
                1L,    // 订单状态
                1,    // 状态
                now,    // 添加时间
                null,    // 添加人员
                now,    // 修改时间
                null,    // 修改人员
                null    // 备注
        );
        if (!packService.insert(pack)) {
            throw new CoolException("服务器内部错误,请联系管理员");
        }
 
        Mat mat = matService.selectByMatnr(analyse.getMatnr());
        if (mat == null) {
            mat = new Mat();
            // 分类
            Long tagId;
            // 一级分类
            if (!Cools.isEmpty(param.getGroupCode()) && !Cools.isEmpty(param.getGroupName())) {
                Tag priTag = tagService.selectByName(param.getGroupCode(), 2);
                if (priTag == null) {
                    Tag top = tagService.getTop();
                    NodeUtils nodeUtils = new NodeUtils();
                    nodeUtils.executePath(top.getId());
                    priTag = new Tag(
                            null,    // 编号
                            param.getGroupCode(),    // 名称
                            top.getId(),    // 父级
                            top.getName(),    // 父级名称
                            nodeUtils.path.toString(),    // 关联路径
                            nodeUtils.pathName.toString(),    // 关联路径名
                            0,    // 类型
                            null,    // 负责人
                            null,    // 图片
                            null,    // 简要描述
                            null,    // 数量
                            2,    // 等级
                            null,    // 排序
                            1,    // 状态
                            now,    // 添加时间
                            null,    // 添加人员
                            now,    // 修改时间
                            null,    // 修改人员
                            null    // 备注
                    );
                    if (tagMapper.insert(priTag) == 0) {
                        throw new CoolException("服务器内部错误,请联系管理员");
                    }
                }
                // 二级分类
                Tag secTag = tagService.selectByName(param.getGroupName(), 3);
                if (secTag == null) {
                    NodeUtils nodeUtils = new NodeUtils();
                    nodeUtils.executePath(priTag.getId());
                    secTag = new Tag(
                            null,    // 编号
                            param.getGroupName(),    // 名称
                            priTag.getId(),    // 父级
                            priTag.getName(),    // 父级名称
                            nodeUtils.path.toString(),    // 关联路径
                            nodeUtils.pathName.toString(),    // 关联路径名
                            0,    // 类型
                            null,    // 负责人
                            null,    // 图片
                            null,    // 简要描述
                            null,    // 数量
                            3,    // 等级
                            null,    // 排序
                            1,    // 状态
                            now,    // 添加时间
                            null,    // 添加人员
                            now,    // 修改时间
                            null,    // 修改人员
                            null    // 备注
                    );
                    if (tagMapper.insert(secTag) == 0) {
                        throw new CoolException("服务器内部错误,请联系管理员");
                    }
                }
                tagId = secTag.getId();
            } else {
                tagId = tagService.getTop().getId();
            }
            mat.setTagId(tagId);
            mat.setLocType(tagId);
            mat.setMatnr(analyse.getMatnr());
            mat.setMaktx(param.getMaterialName());
            mat.setSpecs(param.getConfigureDesc());
            mat.setModel(analyse.getModel());
            mat.setStatus(1);
            mat.setCreateTime(now);
            mat.setUpdateTime(now);
            if (!matService.insert(mat)) {
                throw new CoolException("服务器内部错误,请联系管理员");
            } else {
                log.info("打包上线添加新物料[商品编号:{}]", mat.getMatnr());
            }
        }
    }
 
    @Override
    @Transactional
    public void syncMat(MatSyncParam param) {
        Date now = new Date();
        for (MatSyncParam.MatParam matParam : param.getMatDetails()) {
            String logInfo = "optType:" + matParam.getOptType() + ",matnr:" + matParam.getMatnr() + ",maktx:" + matParam.getMaktx() + ",specs:" + matParam.getSpecs();
            if (Cools.isEmpty(matParam.getOptType(), matParam.getMatnr(), matParam.getMaktx())) {
                throw new CoolException("商品信息不能为空[" + logInfo + "]");
            }
            Mat mat = matService.selectByMatnr(matParam.getMatnr());
            switch (matParam.getOptType()) {
                case "add":
                    if (mat != null) {
                        throw new CoolException("商品信息已经存在[" + logInfo + "]");
                    }
                    mat = new Mat();
                    // 分类
                    Long tagId;
                    // 一级分类
                    if (!Cools.isEmpty(matParam.getGroupName())) {
                        Tag priTag = tagService.selectByName(matParam.getGroupName(), 2);
                        if (priTag == null) {
                            Tag top = tagService.getTop();
                            NodeUtils nodeUtils = new NodeUtils();
                            nodeUtils.executePath(top.getId());
                            priTag = new Tag(
                                    null,    // 编号
                                    matParam.getGroupName(),    // 名称
                                    top.getId(),    // 父级
                                    top.getName(),    // 父级名称
                                    nodeUtils.path.toString(),    // 关联路径
                                    nodeUtils.pathName.toString(),    // 关联路径名
                                    0,    // 类型
                                    null,    // 负责人
                                    null,    // 图片
                                    null,    // 简要描述
                                    null,    // 数量
                                    2,    // 等级
                                    null,    // 排序
                                    1,    // 状态
                                    now,    // 添加时间
                                    null,    // 添加人员
                                    now,    // 修改时间
                                    null,    // 修改人员
                                    null    // 备注
                            );
                            if (tagMapper.insert(priTag) == 0) {
                                throw new CoolException("服务器内部错误,请联系管理员");
                            }
                        }
                        tagId = priTag.getId();
                    } else {
                        tagId = tagService.getTop().getId();
                    }
                    mat.sync(matParam);
                    mat.setTagId(tagId);
                    mat.setStatus(1);
                    mat.setCreateTime(now);
                    mat.setUpdateTime(now);
                    if (!matService.insert(mat)) {
                        throw new CoolException("同步新物料错误[" + logInfo + "]");
                    } else {
                        log.info("同步新物料[{}]", logInfo);
                    }
                    break;
                case "delete":
                    if (mat == null) {
                        throw new CoolException("商品信息不存在[" + logInfo + "]");
                    }
                    if (matService.delete(new EntityWrapper<Mat>().eq("matnr", matParam.getMatnr()))) {
                        log.info("删除物料[{}]", logInfo);
                    } else {
                        throw new CoolException("删除已存在商品信息失败[" + logInfo + "]");
                    }
                    break;
                case "update":
                    if (mat == null) {
                        throw new CoolException("商品信息不存在[" + logInfo + "]");
                    }
                    mat.sync(matParam);
                    if (!matService.update(mat, new EntityWrapper<Mat>().eq("matnr", matParam.getMatnr()))) {
                        throw new CoolException("更新已存在商品信息失败[" + logInfo + "]");
                    } else {
                        log.info("更新物料[:{}]", logInfo);
                    }
                    break;
                default:
                    throw new CoolException("商品信息操作类型不存在[" + logInfo + "]");
            }
        }
    }
 
}