skyouc
5 天以前 e046dba174365eb8934ee1e4206f09821145e876
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
package com.zy.asrs.wms.asrs.service.impl;
 
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.zy.asrs.framework.common.R;
import com.zy.asrs.framework.exception.CoolException;
import com.zy.asrs.wms.apis.wcs.entity.request.SlapLightControlParam;
import com.zy.asrs.wms.apis.wcs.entity.response.CommonReponse;
import com.zy.asrs.wms.apis.wcs.utils.HttpEssUtils;
import com.zy.asrs.wms.asrs.entity.*;
import com.zy.asrs.wms.asrs.entity.dto.MatnrDetlDto;
import com.zy.asrs.wms.asrs.entity.dto.PickSheetDetlDto;
import com.zy.asrs.wms.asrs.entity.dto.ShippingOrderDetlDto;
import com.zy.asrs.wms.asrs.entity.enums.*;
import com.zy.asrs.wms.asrs.entity.enums.OrderType;
import com.zy.asrs.wms.asrs.entity.param.BatchMergeOrdersParam;
import com.zy.asrs.wms.asrs.entity.param.GeneratePakInParam;
import com.zy.asrs.wms.asrs.entity.param.PakinOnShelvesParams;
import com.zy.asrs.wms.asrs.mapper.*;
import com.zy.asrs.wms.asrs.service.*;
import com.zy.asrs.wms.system.entity.Host;
import com.zy.asrs.wms.system.service.HostService;
import io.netty.util.internal.StringUtil;
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.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;
 
@Service
@Slf4j
public class MobileServiceImpl implements MobileService {
 
    @Autowired
    private WaitPakinService waitPakinService;
    @Autowired
    private WaitPakinLogService waitPakinLogService;
    @Autowired
    private WorkService workService;
    @Autowired
    private HostService hostService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderDetlService orderDetlService;
    @Autowired
    private OrderLogService orderLogService;
    @Autowired
    private LocService locService;
    @Autowired
    private LocDetlService locDetlService;
    @Autowired
    private LocAreaService locAreaService;
    @Autowired
    private LocAreaTypeService locAreaTypeService;
    @Autowired
    private PickSheetService pickSheetService;
    @Autowired
    private PickSheetDetlService pickSheetDetlService;
    @Autowired
    private MatService matService;
    @Autowired
    private WaveService waveService;
    @Autowired
    private WaveLogService waveLogService;
    @Autowired
    private CacheSiteService cacheSiteService;
    @Autowired
    WaveDetlLogService waveDetlLogService;
    @Autowired
    private WaveDetlService waveDetlService;
    @Autowired
    private CacheSiteMapper cacheSiteMapper;
 
    @Autowired
    private SeedSitesMapper seedSitesMapper;
    @Autowired
    private OrderDetlMapper orderDetlMapper;
    @Autowired
    private WaveSeedService waveSeedMapper;
    @Autowired
    private WaveSeedLogService waveSeedLogService;
    @Autowired
    private OrderDetlLogService orderDetlLogService;
    @Autowired
    private PlatformDetlService platformDetlService;
    @Autowired
    private PlatformDetlLogService platformDetlLogService;
 
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchMergeOrders(BatchMergeOrdersParam ordersParam) {
        Loc locServiceOne = locService.getOne(new LambdaQueryWrapper<Loc>()
                .eq(Loc::getLocStsId, 1)
                .eq(Loc::getBarcode, ordersParam.getMergeNo()), false);
        if (!Objects.isNull(locServiceOne)) {
            throw new CoolException("托盘已入库,不能再组托!!");
        }
 
        Order order = orderService.getOne(new LambdaQueryWrapper<Order>().eq(Order::getOrderNo, ordersParam.getOrderNo()));
        if (Objects.isNull(order)) {
            throw new CoolException("订单不存在!!");
        }
 
        ArrayList<WaitPakin> waitPakins = new ArrayList<>();
        ordersParam.getOrderDetls().forEach(orderdetl -> {
            WaitPakin waitPakin = new WaitPakin();
            waitPakin.setBatch(orderdetl.getBatch())
                    .setAnfme(orderdetl.getMergeNum())
                    .setBarcode(ordersParam.getMergeNo())
                    .setMatnr(orderdetl.getMatnr())
                    .setDetlId(orderdetl.getDetlId())
                    .setIoStatus(0)
                    .setOrderNo(orderdetl.getOrderNo())
                    .setOrderId(orderdetl.getOrderId())
                    .setStatus(1);
            waitPakins.add(waitPakin);
        });
 
        //组拖通知档
        waitPakins.forEach(pakin -> {
            waitPakinService.comb(pakin, ordersParam.getInType());
        });
 
 
        // UTC入库单据(非平库入库单据)
        if (!ordersParam.getInType().equals(OrderType.PK_IN_ORDER.id)) {
            /*** 项目下发流程 * 1. PDA组拖通知档* 2. 生成任务档* 3. 通过定时任务下发至ESS** */
            //生成任务档
            GeneratePakInParam generatePakInParam = new GeneratePakInParam();
            //当前没有起始站点,默认101, 高低位默认传低库位
            generatePakInParam.setBarcode(ordersParam.getMergeNo())
                    .setOriginSite("101")
                    .setTaskType(TaskStsType.GENERATE_IN.id).setLocTypeHeight(LocTypeHeightType.LOW.id);
            if (workService.generatePakIn(generatePakInParam)) {
                return true;
            }
        }
        return false;
    }
 
    @Override
    public List<Host> getHosts() {
        return hostService.list();
    }
 
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean pakinToStock(PakinOnShelvesParams shelvesParams) {
        Loc loc = locService.getOne(new LambdaQueryWrapper<Loc>().eq(Loc::getLocNo, shelvesParams.getLoc()));
        if (Objects.isNull(loc)) {
            throw new CoolException("库位不存在!!");
        }
        if (loc.getLocStsId() != LocStsType.O.val()) {
            throw new CoolException("当前库位状态不是空库状态." + LocStsType.O.val());
        }
 
        //判断当前仓库是否为平库位
        List<LocArea> locAreas = locAreaService.list(new LambdaQueryWrapper<LocArea>().eq(LocArea::getLocId, loc.getId()));
        if (locAreas.isEmpty()) {
            throw new CoolException("库位没有分配所属仓库区域!!");
        }
        locAreas.forEach(locArea -> {
            LocAreaType typeServiceOne = locAreaTypeService.getOne(new LambdaQueryWrapper<LocAreaType>().eq(LocAreaType::getId, locArea.getTypeId()));
            if (typeServiceOne.getParentId() != LocAreaTypeSts.LOC_AREA_TYPE_FLAT.id && typeServiceOne.getId() != LocAreaTypeSts.LOC_AREA_TYPE_FLAT.id) {
                throw new CoolException("请选择平库区库位,再操作!!");
            }
        });
 
        loc.setBarcode(shelvesParams.getBarcode());
        loc.setUpdateTime(new Date());
        //库存状态修改为在库状态
        loc.setLocStsId(LocStsType.F.val());
        if (!locService.updateById(loc)) {
            throw new CoolException("库位更新失败!!");
        }
        List<WaitPakin> waitPakins = waitPakinService.list(new LambdaQueryWrapper<WaitPakin>().eq(WaitPakin::getBarcode, shelvesParams.getBarcode()));
        if (waitPakins.isEmpty()) {
            throw new CoolException("当前拖盘没有组拖档!!");
        }
 
        waitPakins.forEach(pakin -> {
            LocDetl locDetl = new LocDetl();
            locDetl.setAnfme(pakin.getAnfme());
            locDetl.setBatch(pakin.getBatch());
            locDetl.setMatId(pakin.getMatnrId$());
            locDetl.setCreateTime(new Date());
            locDetl.setOrderNo(pakin.getOrderNo());
            locDetl.setLocNo(loc.getLocNo());
            locDetl.setLocId(loc.getId());
            locDetl.setMatnr(pakin.getMatnr());
            Mat one = matService.getOne(new LambdaQueryWrapper<Mat>().eq(Mat::getMatnr, pakin.getMatnr()));
            if (Objects.isNull(one)) {
                throw new CoolException("当前物料不存在!!");
            }
            locDetl.setMatId(one.getId());
            locDetl.setMemo(pakin.getMemo());
            locDetl.setUpdateTime(new Date());
            if (!locDetlService.saveOrUpdate(locDetl)) {
                throw new CoolException("库存明细更新失败!!");
            }
            //修改状态为入库中
            pakin.setIoStatus(1);
 
            OrderDetl detl = orderDetlService.getById(pakin.getDetlId());
            if (Objects.isNull(detl)) {
                throw new CoolException("数据错误:单据明细不存在!!");
            }
            Double qty = Math.round((detl.getQty() + pakin.getAnfme()) * 1000) / 1000.0;
            detl.setQty(qty);
            if (!orderDetlService.updateById(detl)) {
                throw new CoolException("单据明细修改失败!!");
            }
        });
 
        //删除组拖档,加入历史组拖档
        waitPakins.forEach(waitPakin -> {
            WaitPakinLog pakinLog = new WaitPakinLog();
            BeanUtils.copyProperties(waitPakin, pakinLog);
            pakinLog.setIoStatus(1);
            pakinLog.setId(null);
            if (!waitPakinLogService.saveOrUpdate(pakinLog)) {
                throw new CoolException("组拖历史档更新失败");
            }
        });
 
        //通过组拖订单ID获取订单,并删除原单据,加入单据历史档
        Set<Long> list = waitPakins.stream().map(WaitPakin::getOrderId).collect(Collectors.toSet());
        List<Order> orders = orderService.list(new LambdaQueryWrapper<Order>().in(Order::getId, list));
        orders.forEach(order -> {
            List<OrderDetl> detls = orderDetlService.list(new LambdaQueryWrapper<OrderDetl>().eq(OrderDetl::getOrderId, order.getId()));
            Double sum = detls.stream().mapToDouble(OrderDetl::getQty).sum();
//            Double summed = detls.stream().mapToDouble(OrderDetl::getWorkQty).sum();
//            Double v = Math.round((sum ) * 1000) / 1000.0;
            Double anfme = detls.stream().mapToDouble(OrderDetl::getAnfme).sum();
            if (sum.compareTo(anfme) == 0.0) {
                order.setOrderSettle(OrderSettleType.COMPLETE.val());
                if (!orderService.updateById(order)) {
                    throw new CoolException("单据状态修改失败!!");
                }
            }
        });
 
//        //订单入历史档
//        orders.forEach(order -> {
////            if (order.getOrderType() != OrderType.PK_IN_ORDER.id) {
////                throw new CoolException("当前订单类型:" + order.getOrderType() + ",不可做平库上架操作!!");
////            }
//            //修改订单状态为已完成
//            order.setOrderSettle(OrderSettleType.COMPLETE.val());
//            OrderLog orderLog = new OrderLog();
//            BeanUtils.copyProperties(order, orderLog);
//            orderLog.setId(null);
//            if (!orderLogService.save(orderLog)) {
//                throw new CoolException("历史单据更新失败!!");
//            }
//        });
//        //删除订单数据
//        if (!orderService.removeBatchByIds(orders)) {
//            throw new CoolException("订单删除失败!!");
//        }
//        List<OrderDetl> detls = orderDetlMapper.selectList(new LambdaQueryWrapper<OrderDetl>().in(OrderDetl::getOrderId, list));
//        List<OrderDetlLog> detlLogs = new ArrayList<>();
//        detls.forEach(delt ->{
//            OrderDetlLog detlLog = new OrderDetlLog();
//            BeanUtils.copyProperties(delt, detlLog);
//            detlLog.setQty(delt.getWorkQty());
//            detlLog.setWorkQty(0.0);
//            detlLog.setId(null);
//            detlLogs.add(detlLog);
//        });
//        //订单明细转历史档
//        if (!orderDetlLogService.saveBatch(detlLogs)) {
//            throw new CoolException("订单明细转历史失败!!");
//        }
//
//        //删除订单明细数据
//        if (!orderDetlService.remove(new LambdaQueryWrapper<OrderDetl>().in(OrderDetl::getOrderId, list))) {
//            throw new CoolException("订单明细删除失败!!");
//        }
        if (!waitPakinService.removeBatchByIds(waitPakins)) {
            throw new CoolException("组拖档删除失败!!");
        }
        return true;
    }
 
    @Override
    public PickSheetDetlDto outFlatSheet(String code) {
        PickSheetDetlDto pickDetlDto = new PickSheetDetlDto();
        PickSheet pickSheet = pickSheetService.getOne(new LambdaQueryWrapper<PickSheet>()
                .eq(PickSheet::getPickNo, code)
                .eq(PickSheet::getDeleted, 0)
                .eq(PickSheet::getStatus, 1)
        );
        if (Objects.isNull(pickSheet)) {
            throw new CoolException("拣货单不存在!!");
        }
        BeanUtils.copyProperties(pickSheet, pickDetlDto);
 
        List<PickSheetDetl> sheetDetls = pickSheetDetlService.list(new LambdaQueryWrapper<PickSheetDetl>()
                .eq(PickSheetDetl::getPickId, pickSheet.getId())
                .eq(PickSheetDetl::getStatus, 1)
                .eq(PickSheetDetl::getDeleted, 0));
        pickDetlDto.setPickSheetDetlList(sheetDetls);
        return pickDetlDto;
    }
 
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean confirmFlatSheet(String code) {
        PickSheet pickSheet = pickSheetService.getOne(new LambdaQueryWrapper<PickSheet>().eq(PickSheet::getPickNo, code));
        if (Objects.isNull(pickSheet)) {
            throw new CoolException("拣货单不存在!!");
        }
        if (pickSheet.getStatus() == 1) {
            //状态修改为: 已转历史档
            pickSheet.setStatus(3);
            if (!pickSheetService.updateById(pickSheet)) {
                throw new CoolException("转历史档失败!!");
            } else {
                if (!pickSheetDetlService.update(new LambdaUpdateWrapper<PickSheetDetl>().eq(PickSheetDetl::getPickId, pickSheet.getId()).set(PickSheetDetl::getStatus, 3))) {
                    throw new CoolException("拣货单明细转历史档失败!!");
                }
            }
        } else {
            throw new CoolException("请勿操作历史档数据!!");
        }
        List<PickSheetDetl> sheetDetls = pickSheetDetlService.list(new LambdaQueryWrapper<PickSheetDetl>().eq(PickSheetDetl::getPickId, pickSheet.getId()));
        Map<Long, List<PickSheetDetl>> listMap = sheetDetls.stream().collect(Collectors.groupingBy(PickSheetDetl::getLocId));
        listMap.keySet().forEach(key -> {
            Loc loc = locService.getOne(new LambdaQueryWrapper<Loc>().eq(Loc::getId, key));
            listMap.get(key).forEach(pickDetl -> {
                LocDetl detl = locDetlService.getOne(new LambdaQueryWrapper<LocDetl>().eq(LocDetl::getId, pickDetl.getLocDetlId()));
                if (Objects.isNull(detl)) {
                    throw new CoolException("数据错误!!");
                }
                //更新锁定库存
//                BigDecimal workQty = BigDecimal.valueOf(detl.getAnfme()).subtract(pickDetl.getAnfme());
                detl.setWorkQty(0.0);
                //计算结果小于等于零,移出库存明细
                BigDecimal qty = BigDecimal.valueOf(detl.getAnfme()).subtract(pickDetl.getAnfme());
                detl.setAnfme(qty.doubleValue());
                if (qty.compareTo(new BigDecimal("0.00")) <= 0) {
                    detl.setDeleted(1);
                    if (!locDetlService.removeById(detl)) {
                        throw new CoolException("库存明细删除失败!!");
                    }
                } else { //结果大于零,更新库存明细
                    if (!locDetlService.updateById(detl)) {
                        throw new CoolException("库存明细更新失败!!");
                    }
                }
            });
            /**明细更新完成后,判断是否为空,为空更新主单状态为空库,拖盘码清空,不为空不做更新**/
            List<LocDetl> list = locDetlService.list(new LambdaQueryWrapper<LocDetl>().eq(LocDetl::getLocId, loc.getId()).eq(LocDetl::getDeleted, 0));
            if (list.isEmpty()) {
                boolean update = locService.update(new LambdaUpdateWrapper<Loc>()
                        .eq(Loc::getId, loc.getId())
                        .set(Loc::getBarcode, null)
                        .set(Loc::getUpdateTime, new Date())
                        .set(Loc::getLocStsId, LocStsType.O.val()));
                if (!update) {
                    throw new CoolException("库存更新失败!! ");
                }
            }
        });
        
        //判断是否为波次单据
        if (pickSheet.getType().equals(1)) {
            //删除波次及对应明细信息
            if (!waveService.remove(new LambdaQueryWrapper<Wave>().eq(Wave::getId, pickSheet.getWaveId()))) {
                throw new CoolException("波次列表单据删除失败!!");
            }
 
            if (!waveDetlService.remove(new LambdaQueryWrapper<WaveDetl>().eq(WaveDetl::getWaveId, pickSheet.getWaveId()))) {
                throw new CoolException("波次明细列表删除失败!!");
            }
 
            List<Order> orders = orderService.list(new LambdaQueryWrapper<Order>().eq(Order::getWaveId, pickSheet.getWaveId()));
            if (orders.isEmpty()) {
                throw new CoolException("波次订单不存在!!");
            }
            List<Long> list = orders.stream().map(Order::getId).collect(Collectors.toList());
            boolean remove = orderDetlService.remove(new LambdaQueryWrapper<OrderDetl>().in(OrderDetl::getOrderId, list));
            if (!remove) {
                throw new CoolException("原始订单明细删除失败!!");
            }
            if (!orderService.remove(new LambdaQueryWrapper<Order>().in(Order::getId, list))) {
                throw new CoolException("原始订单删除失败!!");
            }
        }
 
        return true;
    }
 
    /**
     * 获取播种墙站点
     *
     * @return
     */
    @Override
    public List<CacheSite> getSeedLocs() {
        return cacheSiteMapper.selectList(new LambdaQueryWrapper<>());
    }
 
    /**
     * 订单绑定播种墙站点+
     *
     * @param param
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean bindOrderBySite(Map<String, Object> param) {
        CacheSite siteNo = cacheSiteMapper.selectOne(new LambdaQueryWrapper<CacheSite>()
                .eq(CacheSite::getSiteNo, param.get("siteNo")), false);
        if (Objects.isNull(siteNo)) {
            throw new CoolException("站点异常!!");
        }
        if (param.get("type").equals("bind")) {
            if (!StringUtil.isNullOrEmpty(siteNo.getOrderNo())) {
                throw new CoolException("当前播种站点已绑定订单!!");
            }
            Order order = orderService.getOne(new LambdaQueryWrapper<Order>()
                    .eq(Order::getOrderNo, param.get("orderNo").toString()));
            if (Objects.isNull(order)) {
                throw new CoolException("绑定订单不存在!!");
            }
            siteNo.setOrderNo(order.getOrderNo())
                    .setOrderId(order.getId())
                    .setSiteStatus(CacheSiteStatusType.R.id)
                    .setBarcode(param.get("barcode").toString());
            if (!cacheSiteService.saveOrUpdate(siteNo)) {
                throw new CoolException("订单播种位绑定失败!!");
            }
 
//            if (!waveSeedMapper.update(new LambdaUpdateWrapper<WaveSeed>()
//                    .eq(WaveSeed::getOrderNo, order.getOrderNo())
//                    .set(WaveSeed::getSiteId, siteNo.getId())
//                    .set(WaveSeed::getSiteNo, siteNo.getSiteNo()))) {
//                throw new CoolException("播种站点更新失败!!");
//            }
            //绑定成功,播种墙亮灯
            sowLightMange(siteNo.getSiteNo(), order, "LIGHT");
        } else {
            if (StringUtil.isNullOrEmpty(siteNo.getOrderNo())) {
                throw new CoolException("当前播种站点未绑定订单!!");
            }
            siteNo.setOrderId(null).setOrderNo(null).setBarcode(null).setSiteNo("0");
            if (cacheSiteMapper.updateById(siteNo) < 1) {
                throw new CoolException("订单与播种位解绑失败!!");
            }
        }
        return true;
    }
 
    /**
     * @author Ryan
     * @date 2025/7/11
     * @description: 绑定播种墙亮灯
     * @version 1.0
     */
    public void sowLightMange(String siteNo, Order order, String light) {
        // 到达拣选位,控制播种墙亮灯
        CacheSite cacheSite = cacheSiteService.getOne(new LambdaQueryWrapper<CacheSite>()
                .eq(CacheSite::getSiteNo, siteNo));
        if (Objects.isNull(cacheSite)) {
            log.error("条码:{},未找到播种墙亮灯", siteNo);
            throw new CoolException("未找到播种墙");
        }
        List<OrderDetl> detls = orderDetlService.list(new LambdaQueryWrapper<OrderDetl>().eq(OrderDetl::getOrderId, order.getId()));
        if (Objects.isNull(detls)) {
            throw new CoolException("数据错误,订单明细不存在!!");
        }
        int anfme = (int) detls.stream().mapToDouble(OrderDetl::getAnfme).sum();
        SlapLightControlParam slapLightControlParam = new SlapLightControlParam();
        slapLightControlParam
                .setControllerCode(cacheSite.getMemo())
                .setTagCode(cacheSite.getSiteNo())
                .setColor("GREEN")
                .setIndex(cacheSite.getIndex())
//                .setMode("LIGHT")
                .setMode(light)
                .setDisplay(anfme + "");
        // 发起亮灯请求
        log.info("播种墙亮灯下发", JSONObject.toJSONString(slapLightControlParam));
        CommonReponse response = HttpEssUtils.post("到达拣选位,播种墙亮灯", HttpEssUtils.PLT_SEND_COMMAND, slapLightControlParam);
        if (response.getCode().equals(0)) {
            log.info("播种墙{}亮灯成功", cacheSite.getSiteNo());
        } else {
            log.info("播种墙{}亮灯失败", cacheSite.getSiteNo());
        }
    }
 
 
    @Override
    public R selectShippingDetl(Map<String, Object> params) {
        //判断订单是否为空
        if (StringUtil.isNullOrEmpty((String) params.get("orderNo"))) {
            throw new CoolException("订单编码号不能为空!!");
        }
        String orderNo = params.get("orderNo").toString();
        Order order = orderService.getOne(new LambdaQueryWrapper<Order>().eq(Order::getOrderNo, orderNo));
        if (Objects.isNull(order)) {
            throw new CoolException("订单不存在!!");
        }
        if (!order.getOrderType().equals(OrderType.UTC_OUT_ORDER.id)) {
            throw new CoolException("订单为入库单据,不能发货!!");
        }
 
        List<ShippingOrderDetlDto> waveSeeds = waveSeedMapper.selectShippingOrderDtel(orderNo);
 
        return R.ok(waveSeeds);
    }
 
    /**
     * 发货单明细确认
     * 1. 出库修改订单完成状态,判断订单是否完成,完成加入历史档,未完成修改订单已完成数量
     * 2. 删除订单已完成播种明细信息
     * 3. 清除集货区绑定数据
     *
     * @param params
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R confirmShippingDetl(List<ShippingOrderDetlDto> params) {
        if (params.isEmpty()) {
            throw new CoolException("发货单据明细为空,不可执行发货操作!!");
        }
 
        //获取当前上传所有订单号
        Set<String> orderNoSet = params.stream().map(ShippingOrderDetlDto::getOrderNo).collect(Collectors.toSet());
        if (orderNoSet.isEmpty()) {
            throw new CoolException("主单信息不存在,请核对出库订单!!");
        }
 
        List<Order> orderList = orderService.list(new LambdaQueryWrapper<Order>().in(Order::getOrderNo, orderNoSet));
        if (orderList.isEmpty()) {
            throw new CoolException("订单不存在!!");
        }
 
        params.forEach(order -> {
            OrderDetl byId = orderDetlService.getById(order.getId());
            if (Objects.isNull(byId)) {
                throw new CoolException("订单明细不存在!!");
            }
            OrderDetlLog orderDetlLog = new OrderDetlLog();
            BeanUtils.copyProperties(byId, orderDetlLog);
            orderDetlLog.setId(null);
            orderDetlLog.setQty(order.getWorkQty());
            orderDetlLog.setWorkQty(0.0);
            if (!orderDetlLogService.save(orderDetlLog)) {
                throw new CoolException("订单明细转历史档失败!!");
            }
 
            //完全拣货,订单完成删除原始订单明细
            //new BigDecimal(order.getAnfme()).compareTo(new BigDecimal(order.getWorkQty())) == 0
            if (new BigDecimal(order.getAnfme()).compareTo(new BigDecimal(order.getWorkQty())) == 0) {
                if (!orderDetlService.removeById(byId)) {
                    throw new CoolException("订单明细不存在!!");
                }
            } else { //部分拣货
                byId.setQty(Math.round((order.getWorkQty() + byId.getQty()) * 1000) / 1000.0);
                byId.setWorkQty(0.0);
                if (!orderDetlService.updateById(byId)) {
                    throw new CoolException("部分拣料明细更新失败!!");
                }
            }
 
            List<WaveSeed> waveSeeds = waveSeedMapper.list(new LambdaQueryWrapper<WaveSeed>().eq(WaveSeed::getOrderDetlId, order.getId()));
            //fixme 订单明细未播种,默认可发货
            if (!waveSeeds.isEmpty()) {
                ArrayList<WaveSeedLog> waveSeedLogs = new ArrayList<>();
                waveSeeds.forEach(seed -> {
                    WaveSeedLog waveSeedLog = new WaveSeedLog();
                    BeanUtils.copyProperties(seed, waveSeedLog);
                    waveSeedLog.setId(null);
                    waveSeedLogs.add(waveSeedLog);
                });
                if (!waveSeedLogService.saveBatch(waveSeedLogs)) {
                    throw new CoolException("波明历史档保存失败!!");
                }
 
                List<Long> list = waveSeeds.stream().map(WaveSeed::getId).collect(Collectors.toList());
                //删除播种明细信息
                if (!waveSeedMapper.removeByIds(list)) {
                    throw new CoolException("播种明细删除携程!!");
                }
 
            }
 
        });
 
        orderList.forEach(one -> {
            OrderLog orderLog = new OrderLog();
            BeanUtils.copyProperties(one, orderLog);
            orderLog.setId(null);
            orderLog.setOrderSettle(OrderSettleType.COMPLETE.val());
 
            if (!orderLogService.save(orderLog)) {
                throw new CoolException("原始订单转历史档失败!!");
            }
            //查询当前订单下否还有明细存在
            List<OrderDetl> orders = orderDetlService.list(new LambdaQueryWrapper<OrderDetl>().in(OrderDetl::getOrderNo, one.getOrderNo()));
            //订单明细为空,则删除主单
            if (orders.isEmpty()) {
                if (!orderService.remove(new LambdaQueryWrapper<Order>().eq(Order::getOrderNo, one.getOrderNo()))) {
                    throw new CoolException("原始订单删除失败!!");
                }
            } else { //不为空,修改订单状态,可重新组拖
                one.setOrderSettle(OrderSettleType.INIT.val());
                if (orderService.updateById(one)) ;
            }
        });
 
        Set<Long> ordersSet = orderList.stream().map(Order::getId).collect(Collectors.toSet());
        List<PlatformDetl> platDetls = platformDetlService.list(new LambdaQueryWrapper<PlatformDetl>().in(PlatformDetl::getOrderId, ordersSet));
        if (platDetls.isEmpty()) {
            throw new CoolException("订单信息未进集货区!!");
        }
        ArrayList<PlatformDetlLog> platformDetls = new ArrayList<>();
        platDetls.forEach(plat -> {
            PlatformDetlLog detl = new PlatformDetlLog();
            BeanUtils.copyProperties(plat, detl);
            detl.setId(null);
            platformDetls.add(detl);
        });
        //集货区转历史档
        if (!platformDetlLogService.saveBatch(platformDetls)) {
            throw new CoolException("集货区订单转历史档失败!!");
        }
 
        return R.ok("发货完成!!");
    }
 
    /**
     * @author Ryan
     * @date 2025/6/28
     * @description: 获取平库推荐库位
     * @version 1.0
     */
    @Override
    public R getRecommendLocs() {
        return locService.getRecommengLocs();
    }
 
    /**
     * @author Ryan
     * @date 2025/6/28
     * @description: 查询物料信息
     * @version 1.0
     */
    @Override
    public R getMatsByCode(String matnr) {
        QueryWrapper<LocDetl> wrapper = new QueryWrapper<>();
        wrapper.eq("matnr", matnr).select("matnr, SUM(anfme) anfme");
        LocDetl detl = locDetlService.getOne(wrapper);
 
        Mat one = matService.getOne(new LambdaQueryWrapper<Mat>().eq(Mat::getMatnr, detl.getMatnr()), false);
        if (Objects.isNull(one)) {
            throw new CoolException("物料不存在!!");
        }
        one.setAnfme(detl.getAnfme());
        return R.ok().add(Arrays.asList(one));
    }
 
    /**
     * @author Ryan
     * @date 2025/7/10
     * @description: 获取库位明细
     * @version 1.0
     */
    @Override
    public List<LocDetl> getDetlsByCode(String barcode) {
        Loc loc = locService.getOne(new LambdaQueryWrapper<Loc>().eq(Loc::getLocNo, barcode));
        if (Objects.isNull(loc)) {
            throw new CoolException("库位不存在!!");
        }
        LocArea locArea = locAreaService.getOne(new LambdaQueryWrapper<LocArea>()
                .eq(LocArea::getLocId, loc.getId())
                .eq(LocArea::getTypeId, LocAreaTypeSts.LOC_AREA_TYPE_FLAT.id));
        if (Objects.isNull(locArea)) {
            throw new CoolException("非平库库位不可执行此操作!!");
        }
        List<LocDetl> detls = locDetlService.list(new LambdaQueryWrapper<LocDetl>()
                .eq(LocDetl::getLocNo, barcode));
        for (int i = 0; i < detls.size(); i++) {
            Mat mat = matService.getOne(new LambdaQueryWrapper<Mat>().eq(Mat::getId, detls.get(i).getMatId()));
            MatnrDetlDto detlDto = new MatnrDetlDto();
            detlDto.setTspec(mat.getSpecs())
                    .setType(mat.getModel())
                    .setMatnrName(mat.getMaktx())
                    .setMatnrNo(mat.getMatnr());
            detls.get(i).setMats(detlDto);
        }
        return detls;
    }
 
    /**
     * @author Ryan
     * @date 2025/7/11
     * @description: 平库库位转移
     * @version 1.0
     */
    @Override
    public R transferLocs(Map<String, String> param) {
        Loc barcode = locService.getOne(new LambdaQueryWrapper<Loc>()
                .eq(Loc::getLocStsId, LocStsType.F.val())
                .eq(Loc::getLocNo, param.get("barcode")));
        if (Objects.isNull(barcode)) {
            throw new CoolException(param.get("barcode") +  "库位状态不可执行此操作!!");
        }
        Loc tarCode = locService.getOne(new LambdaQueryWrapper<Loc>()
                .eq(Loc::getLocStsId, LocStsType.O.val())
                .eq(Loc::getLocNo, param.get("tarCode")));
        if (Objects.isNull(tarCode)) {
            throw new CoolException("请检查目标为唯位是否存在或是否为空库!!");
        }
 
        List<Long> ids = Arrays.asList(barcode.getId(), tarCode.getId());
 
        List<LocArea> locAreas = locAreaService.list(new LambdaQueryWrapper<LocArea>()
                .in(LocArea::getLocId, ids)
                .eq(LocArea::getTypeId, LocAreaTypeSts.LOC_AREA_TYPE_FLAT.id));
        if (!locAreas.isEmpty() && locAreas.size() != ids.size()) {
            throw new CoolException("非平库不可执行此操作!!");
        }
 
        List<LocDetl> locDetls = locDetlService.list(new LambdaQueryWrapper<LocDetl>().eq(LocDetl::getLocNo, barcode.getLocNo()));
        if (locDetls.isEmpty()) {
            return R.error("库位明细不存在!!");
        }
 
        if (!locService.update(new LambdaUpdateWrapper<Loc>()
                .eq(Loc::getLocNo, barcode.getLocNo())
                .set(Loc::getBarcode, null)
                .set(Loc::getLocStsId, LocStsType.O.val()))) {
            throw new CoolException("源库位状态修改失败!!");
        }
 
        if (!locService.update(new LambdaUpdateWrapper<Loc>().eq(Loc::getLocNo, tarCode.getLocNo())
                .set(Loc::getBarcode, barcode.getBarcode())
                .set(Loc::getLocStsId, LocStsType.F.val()))) {
            throw new CoolException("目标库位修改失败!!");
        }
 
        if (!locDetlService.update(new LambdaUpdateWrapper<LocDetl>()
                .eq(LocDetl::getLocId, barcode.getId())
                .set(LocDetl::getLocId, tarCode.getId())
                .set(LocDetl::getLocNo, tarCode.getLocNo()))) {
            throw new CoolException("库位明细修改失败!!");
        }
 
        return R.ok();
    }
 
 
}