schangxiang@126.com
2024-12-23 f0088fd96c74f68ba912593710dd9271bcee581d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
using Admin.NET.Core.Service;
using Admin.NET.Application.Entity;
using Microsoft.AspNetCore.Http;
using System.Data;
using System.Web;
using System.Text;
using DocumentFormat.OpenXml.Office.CustomUI;
using Microsoft.CodeAnalysis.Operations;
using Admin.NET.Application.Service.WmsTask.WmsRbLineTask.Dto;
using Furion.DatabaseAccessor;
using Admin.NET.Application.Service.WmsCommonnQuery.Dto;
using Admin.NET.Application.CommonHelper;
 
namespace Admin.NET.Application;
/// <summary>
/// 操作任务服务
/// </summary>
[ApiDescriptionSettings(ApplicationConst.WmsTaskGroupName, Order = 100)]
public class WmsOperationTaskService : IDynamicApiController, ITransient
{
    private static readonly SemaphoreSlim semaphore = new SemaphoreSlim(1, 1);
    private static readonly SemaphoreSlim semaphore2 = new SemaphoreSlim(1, 1);
 
    private readonly SqlSugarRepository<WmsRbLineTask> _rep;
    private readonly SqlSugarRepository<WmsStockQuan> _wmsStockQuanRep;
    private readonly SqlSugarRepository<Mes_Package_Gather> _mesPackageGatherRep;
    private readonly SqlSugarRepository<SysConfig> _sysConfigRep;
    private readonly SqlSugarRepository<Mes_BatchOrderUPI_New> _mesBatchOrderUpiRep;
    private readonly SqlSugarRepository<Mes_Order_Gather> _mesOrderGatherRep;
    private readonly SqlSugarRepository<WmsRecordUpiProcess> _wmsRecordUpiProcessRep;
    private readonly SqlSugarRepository<WmsRecordPackageProcess> _wmsRecordPackageProcessRep;
    private readonly SqlSugarRepository<Mes_Upi_LineQueue> _mes_Upi_LineQueueRep;
    private readonly SqlSugarRepository<Mes_Package_LineQueue> _mes_Package_LineQueueRep;
    private readonly SqlSugarRepository<WmsRecordUpiNg> _wmsRecordUpiNgRep;
    private readonly SqlSugarRepository<Mes_Package_UnLine_Record> _mes_Package_UnLine_RecordRep;
    private readonly WmsCommonnQueryService _wmsCommonnQueryService;
 
    public WmsOperationTaskService(
        SqlSugarRepository<Mes_Upi_LineQueue> mes_Upi_LineQueueRep,
        SqlSugarRepository<WmsRbLineTask> rep, SqlSugarRepository<WmsStockQuan> wmsStockQuanRep
        , SqlSugarRepository<Mes_Package_Gather> mesPackageGatherRep
        , SqlSugarRepository<SysConfig> sysConfigRep
        , SqlSugarRepository<Mes_BatchOrderUPI_New> mesBatchOrderUpiRep
        , SqlSugarRepository<Mes_Order_Gather> mesOrderGatherRep
        , SqlSugarRepository<WmsRecordUpiProcess> wmsRecordUpiProcessRep
        , SqlSugarRepository<WmsRecordPackageProcess> wmsRecordPackageProcessRep
        , SqlSugarRepository<WmsRecordUpiNg> wmsRecordUpiNgRep
        , WmsCommonnQueryService wmsCommonnQueryService
        , SqlSugarRepository<Mes_Package_LineQueue> mes_Package_LineQueueRep
        , SqlSugarRepository<Mes_Package_UnLine_Record> mes_Package_UnLine_RecordRep
        )
    {
        _mes_Package_LineQueueRep = mes_Package_LineQueueRep;
        _wmsCommonnQueryService = wmsCommonnQueryService;
        _wmsRecordUpiNgRep = wmsRecordUpiNgRep;
        _sysConfigRep = sysConfigRep;
        _mesPackageGatherRep = mesPackageGatherRep;
        _rep = rep;
        _wmsStockQuanRep = wmsStockQuanRep;
        _mesBatchOrderUpiRep = mesBatchOrderUpiRep;
        _mesOrderGatherRep = mesOrderGatherRep;
        _wmsRecordUpiProcessRep = wmsRecordUpiProcessRep;
        _wmsRecordPackageProcessRep = wmsRecordPackageProcessRep;
        _mes_Upi_LineQueueRep = mes_Upi_LineQueueRep;
        _mes_Package_UnLine_RecordRep = mes_Package_UnLine_RecordRep;
    }
 
 
 
    /// <summary>
    /// 强制出库
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "ForceOutbound")]
    [Description("WmsOperationTask/ForceOutbound")]
    [UnitOfWork]
    public async Task ForceOutbound(ForceOutboundInput input)
    {
        if (input == null || input.PackageCodeList?.Count == 0)
        {
            throw Oops.Oh("参数不能为空");
        }
        //验证传递的包是否都在齐包之内。
        List<WmsPackListOutput> packList = await _wmsCommonnQueryService.PackList(new Service.WmsCommonnQuery.Dto.PackListInput() { IsPack = false });
 
        List<WmsStockQuan> updateQuanList = new List<WmsStockQuan>();
        foreach (var item in input.PackageCodeList)
        {
            var isExistCount = packList.Where(x => x.PackageCode == item).Count();
            if (isExistCount == 0)
            {
                throw Oops.Oh($"包{item}不是缺包状态,请检查!");
            }
 
            var quanList = await _wmsStockQuanRep.AsQueryable().Where(x => x.PackageCode == item).ToListAsync();
            var isExist = quanList.Where(x => x.StockStatus == StockStatusEnum.齐包待出库 || x.StockStatus == StockStatusEnum.人工齐包出库任务已创建).Count();
            if (isExist > 0)
            {
                throw Oops.Oh($"包{item}中其中有板状态是'{StockStatusEnum.齐包待出库.ToString()}' 或'{StockStatusEnum.人工齐包出库任务已创建.ToString()}',不允许强制出库");
            }
 
            //更新状态 
            foreach (var quan in quanList)
            {
                if (quan.StockStatus != StockStatusEnum.在库 && quan.StockStatus != StockStatusEnum.已冻结)
                {
                    throw Oops.Oh($"部件条码{quan.Upi}的库存状态不是'{StockStatusEnum.在库.ToString()}或{StockStatusEnum.已冻结.ToString()}'状态");
                }
 
                quan.StockStatus = StockStatusEnum.人工强制待出库;
                quan.StockStatusName = StockStatusEnum.人工强制待出库.ToString();
                quan.UpdateTime = DateTime.Now;
                quan.OperReason = "人工强制待出库";
            }
            updateQuanList.AddRange(quanList);
 
            var packageObj = await _mesPackageGatherRep.GetFirstAsync(x => x.PackageCode == item);
            packageObj.UpiFlag = UpiFlagEnum.NG;
            packageObj.UpiStatus = UpiStatusEnum.已下线;
            await _mesPackageGatherRep.UpdateAsync(packageObj);
 
 
            //下线更新 订单表数据 _mesOrderGatherRep
            await UpdateOrderGather(packageObj, true);
 
            foreach (var quan in quanList)
            {
                //清理 队列表
                var removeUpiList = await _mes_Upi_LineQueueRep.AsQueryable().Where(x => x.Upi == quan.Upi).ToListAsync();
                if (removeUpiList?.Count > 0)
                {
                    await _mes_Upi_LineQueueRep.DeleteAsync(removeUpiList);
                }
 
                var removePackageList = await _mes_Package_LineQueueRep.AsQueryable().Where(x => x.PackageCode == quan.PackageCode).ToListAsync();
                if (removePackageList?.Count > 0)
                {
                    await _mes_Package_LineQueueRep.DeleteAsync(removePackageList);
                }
            }
        }
 
        //模式判断
        var client = await WCFServiceHelper.GetWCFService_SAPCC_Client(_sysConfigRep);
        var res = await client.ValidateModeAsync(1);
        if (res.result == false)
        {
            throw Oops.Oh(res.resMsg);
        }
 
        await _wmsStockQuanRep.UpdateRangeAsync(updateQuanList);
 
    }
 
 
    /// <summary>
    /// 人工齐包出库
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "PersonOutbound")]
    [Description("WmsOperationTask/PersonOutbound")]
    [UnitOfWork]
    public async Task PersonOutbound(PersonOutboundInput input)
    {
        if (input == null || input.PackageCodeList?.Count == 0)
        {
            throw Oops.Oh("参数不能为空");
        }
        //验证传递的包是否都在齐包之内。
        List<WmsPackListOutput> packList = await _wmsCommonnQueryService.PackList(new Service.WmsCommonnQuery.Dto.PackListInput() { IsPack = true });
 
        List<WmsStockQuan> updateQuanList = new List<WmsStockQuan>();
        foreach (var item in input.PackageCodeList)
        {
            var isExistCount = packList.Where(x => x.PackageCode == item).Count();
            if (isExistCount == 0)
            {
                throw Oops.Oh($"包{item}不是齐包状态,请检查!");
            }
 
            var quanList = await _wmsStockQuanRep.AsQueryable().Where(x => x.PackageCode == item).ToListAsync();
            var isExist = quanList.Where(x => x.StockStatus == StockStatusEnum.人工强制待出库 || x.StockStatus == StockStatusEnum.人工强制任务已创建).Count();
            if (isExist > 0)
            {
                throw Oops.Oh($"包{item}中其中有板状态是'{StockStatusEnum.人工强制待出库.ToString()}'或'{StockStatusEnum.人工强制任务已创建.ToString()}',不允许人工齐包出库");
            }
 
            //更新状态 
            foreach (var quan in quanList)
            {
                if (quan.StockStatus != StockStatusEnum.在库 && quan.StockStatus != StockStatusEnum.已冻结)
                {
                    throw Oops.Oh($"部件条码{quan.Upi}的库存状态不是'{StockStatusEnum.在库.ToString()}或{StockStatusEnum.已冻结.ToString()}'状态");
                }
                quan.StockStatus = StockStatusEnum.齐包待出库;
                quan.StockStatusName = StockStatusEnum.齐包待出库.ToString();
                quan.UpdateTime = DateTime.Now;
                quan.OperReason = "人工齐包出库";
            }
            updateQuanList.AddRange(quanList);
 
            var packageObj = await _mesPackageGatherRep.GetFirstAsync(x => x.PackageCode == item);
            packageObj.UpiStatus = UpiStatusEnum.已齐包;
            packageObj.IsQiBao = true;
            await _mesPackageGatherRep.UpdateAsync(packageObj);
        }
 
        //模式判断
        var client = await WCFServiceHelper.GetWCFService_SAPCC_Client(_sysConfigRep);
        var res = await client.ValidateModeAsync(2);
        if (res.result == false)
        {
            throw Oops.Oh(res.resMsg);
        }
 
        await _wmsStockQuanRep.UpdateRangeAsync(updateQuanList);
 
    }
 
    /// <summary>
    /// 下线放行
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "ValdateQiTao")]
    [Description("WmsOperationTask/ValdateQiTao")]
    [UnitOfWork]
    public async Task<ValidateQiTaoOutput> ValdateQiTao(ValidateQiTaoInput input)
    {
        await semaphore2.WaitAsync();
        try
        {
            ValidateQiTaoOutput output = new ValidateQiTaoOutput();
            if (input == null || input.PackageCode == null)
            {
                throw Oops.Oh("参数不能为空");
            }
 
            var package = await _mesPackageGatherRep.AsQueryable().Where(x => x.PackageCode == input.PackageCode).FirstAsync();
            if (package == null)
            {
                throw Oops.Oh($"没有找到包号{input.PackageCode}的汇总数据");
            }
 
            if (package.UpiStatus == UpiStatusEnum.已下线)
            {
                throw Oops.Oh($"包{input.PackageCode}已下线");
            }
 
            var unlineObj = await _mes_Package_UnLine_RecordRep.AsQueryable().Where(x => x.PackageCode == input.PackageCode).FirstAsync();
            if (unlineObj != null)
            {
                throw Oops.Oh($"包号{input.PackageCode}已经下线");
            }
 
            //判断是否是新生产订单
            var _QiTaoReuslt = "";
            bool isNewOrder = false;
            string _location = "BZ-33";
            var lastPackageUnline = await _mes_Package_UnLine_RecordRep.AsQueryable().Where(x => x.UpiFlag == UpiFlagEnum.正常 && x.Location != "人工").OrderByDescending(x => x.Id).FirstAsync();
            if (lastPackageUnline == null)
            {//认为是新生产订单
                isNewOrder = true;
            }
            else
            {
                if (lastPackageUnline.Info5 != package.Info5)
                {//认为是新生产订单
                    isNewOrder = true;
                    if (lastPackageUnline.Location == "BZ-33")
                    {
                        _location = "BZ-32";
                    }
                    else
                    {
                        _location = "BZ-33";
                    }
                }
                else
                {
                    _location = lastPackageUnline.Location;
                }
            }
            if (string.IsNullOrEmpty(_location))
            {//默认
                _location = "BZ-33";
            }
 
            _QiTaoReuslt = (bool)isNewOrder ? "新订单" : "当前订单";
 
 
            //记录工作时间和工作人
            var curUserName = App.User.FindFirst(ClaimConst.RealName)?.Value;
 
            var sysConfig_wms_unline_oper = await _sysConfigRep.GetFirstAsync(x => x.Code == CommonConst.wms_unline_oper);
            if (sysConfig_wms_unline_oper == null)
            {
                throw Oops.Oh($"没有配置 包装下线操作人 值");
            }
            if (sysConfig_wms_unline_oper.Value != curUserName)
            {
                sysConfig_wms_unline_oper.Value = curUserName;
                await _sysConfigRep.UpdateAsync(sysConfig_wms_unline_oper);
 
                var sysConfig_wms_unline_time = await _sysConfigRep.GetFirstAsync(x => x.Code == CommonConst.wms_unline_time);
                if (sysConfig_wms_unline_time == null)
                {
                    throw Oops.Oh($"没有配置 包装下线登陆时间 值");
                }
                sysConfig_wms_unline_time.Value = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                await _sysConfigRep.UpdateAsync(sysConfig_wms_unline_time);
            }
 
 
 
            //请求PLC
            //判断方向
            bool myLocation = false;
            if (_location == "BZ-33")
            {
                myLocation = false;
            }
            else
            {
                myLocation = true;
            }
            var client = await WCFServiceHelper.GetWCFService_SAPCC_Client(_sysConfigRep);
            var res = await client.WriteQiTaoInfoAsync(myLocation);
            if (res.result == false)
            {
                throw Oops.Oh(res.resMsg);
            }
 
 
            //更新数据
            package.UpiStatus = UpiStatusEnum.已下线;
            package.AreaCode = AreaCodeEnum.下线区域;
            await _mesPackageGatherRep.UpdateAsync(package);
 
            var upiList = await _mesBatchOrderUpiRep.AsQueryable().Where(x => x.PackageCode == input.PackageCode).ToListAsync();
            foreach (var item in upiList)
            {
                item.UpiStatus = UpiStatusEnum.已下线;
                item.AreaCode = AreaCodeEnum.下线区域;
                item.UpdateTime = DateTime.Now;
            }
            await _mesBatchOrderUpiRep.UpdateRangeAsync(upiList);
            //删除队列信息
            var lineQueue = await _mes_Package_LineQueueRep.AsQueryable().Where(x => x.PackageCode == input.PackageCode).FirstAsync();
            if (lineQueue != null)
            {
                await _mes_Package_LineQueueRep.DeleteAsync(lineQueue);
            }
            //处理表  mes_order_gather
            await UpdateOrderGather(package, false);
 
            var _cretaorName = App.User.FindFirst(ClaimConst.RealName)?.Value;
 
            //新增下线记录表
            var all_upiList = await _mesBatchOrderUpiRep.AsQueryable().Where(x => x.Info5 == package.Info5).ToListAsync();
            var all_unlineList = await _mes_Package_UnLine_RecordRep.AsQueryable().Where(x => x.Info5 == package.Info5).ToListAsync();
            Mes_Package_UnLine_Record record = new Mes_Package_UnLine_Record();
            record = package.Adapt<Mes_Package_UnLine_Record>();
            record.Id = Yitter.IdGenerator.YitIdHelper.NextId();
            record.Location = _location;
            record.AllPackageNum = all_upiList.GroupBy(x => x.PackageCode).Count();
            record.CurUnlineIndexPackageNum = all_unlineList.GroupBy(x => x.PackageCode).Count() + 1;
            record.CreateTime = DateTime.Now;
            record.CreateUserName = _cretaorName;
            await _mes_Package_UnLine_RecordRep.InsertAsync(record);
 
            //记录包履历
            WmsRecordPackageProcess wmsRecordPackage = new WmsRecordPackageProcess();
            wmsRecordPackage = package.Adapt<WmsRecordPackageProcess>();
            wmsRecordPackage.Id = Yitter.IdGenerator.YitIdHelper.NextId();
            wmsRecordPackage.Location = "";
            wmsRecordPackage.OperRemark = "正常下线";
            wmsRecordPackage.CreateTime = DateTime.Now;
            wmsRecordPackage.CreateUserName = _cretaorName;
            await _wmsRecordPackageProcessRep.InsertAsync(wmsRecordPackage);
 
 
            output = package.Adapt<ValidateQiTaoOutput>();
            output.QiTaoReuslt = _QiTaoReuslt + $",第{record.CurUnlineIndexPackageNum}包下线,共{record.AllPackageNum}包";
            output.IsNewOrder = isNewOrder;
            //output.IsNewOrder = false;
            return output;
        }
        catch (Exception)
        {
 
            throw;
        }
        finally
        {
            semaphore2.Release();
        }
 
    }
 
 
    /// <summary>
    /// 核对标签
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "ValdateLabel")]
    [Description("WmsOperationTask/ValdateLabel")]
    [UnitOfWork]
    public async Task<Mes_Package_Gather> ValdateLabel(ValidateLabelInput input)
    {
        if (input == null || input.PackageCode == null || input.Upi == null)
        {
            throw Oops.Oh("参数不能为空");
        }
 
        var singleUpi = await _mesBatchOrderUpiRep.AsQueryable().Where(x => x.UPI == input.Upi).FirstAsync();
        if (singleUpi == null)
        {
            throw Oops.Oh($"没有找到部件条码{input.Upi}的数据");
        }
        if (singleUpi.PackageCode != input.PackageCode)
        {
            throw Oops.Oh($"部件条码{input.Upi}所属包是{singleUpi.PackageCode},跟扫描的包号{input.PackageCode}不符");
        }
 
        var singlePackage = await _mesPackageGatherRep.AsQueryable().Where(x => x.PackageCode == input.PackageCode).FirstAsync();
        if (singlePackage == null)
        {
            throw Oops.Oh($"没有找到包号{input.PackageCode}的汇总数据");
        }
 
        var _cretaorName = App.User.FindFirst(ClaimConst.RealName)?.Value;
 
        WmsRecordPackageProcess wmsRecordPackage = new WmsRecordPackageProcess();
        wmsRecordPackage = singlePackage.Adapt<WmsRecordPackageProcess>();
        wmsRecordPackage.Id = Yitter.IdGenerator.YitIdHelper.NextId();
        wmsRecordPackage.CreateTime = DateTime.Now;
        wmsRecordPackage.CreateUserName = _cretaorName;
        wmsRecordPackage.Location = "BZ29";
        wmsRecordPackage.OperRemark = "核对标签";
        await _wmsRecordPackageProcessRep.InsertAsync(wmsRecordPackage);
 
        WmsRecordUpiProcess wmsRecordUpi = new WmsRecordUpiProcess();
        wmsRecordUpi = singleUpi.Adapt<WmsRecordUpiProcess>();
        wmsRecordUpi.Id = Yitter.IdGenerator.YitIdHelper.NextId();
        wmsRecordUpi.CreateTime = DateTime.Now;
        wmsRecordUpi.CreateUserName = _cretaorName;
        wmsRecordUpi.Location = "BZ29";
        wmsRecordUpi.OperRemark = "核对标签";
        await _wmsRecordUpiProcessRep.InsertAsync(wmsRecordUpi);
 
        return singlePackage;
    }
 
 
    /// <summary>
    /// NG包下线或 正常包 下线 -扫描包号
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "UnlineForNGPackage")]
    [Description("WmsOperationTask/UnlineForNGPackage")]
    [UnitOfWork]
    public async Task UnlineForNGPackage(UnlineForNGPackageInput input)
    {
        await semaphore.WaitAsync();
        try
        {
            if (input == null)
            {
                throw Oops.Oh("参数不能为空");
            }
            if (string.IsNullOrEmpty(input.PackageCode))
            {
                throw Oops.Oh("包号不能为空");
            }
            //if (string.IsNullOrEmpty(input.Reason))
            //{
            //    throw Oops.Oh("原因不能为空");
            //}
            string _OperRemark = "正常人工下线";
            UpiFlagEnum upiFlagEnum = UpiFlagEnum.正常;
            if (input.IsNG)
            {
                upiFlagEnum = UpiFlagEnum.NG;
                _OperRemark = "NG下线";
            }
 
            var singlePackage = await _mesPackageGatherRep.AsQueryable().Where(x => x.PackageCode == input.PackageCode).FirstAsync();
            if (singlePackage == null)
            {
                throw Oops.Oh($"没有找到包号{input.PackageCode}的汇总数据");
            }
 
            if (singlePackage.UpiStatus == UpiStatusEnum.已下线)
            {
                throw Oops.Oh($"包号{input.PackageCode}已经下线");
            }
 
            singlePackage.UpiFlag = upiFlagEnum;
            singlePackage.UpdateTime = DateTime.Now;
            singlePackage.UpiStatus = UpiStatusEnum.已下线;
            singlePackage.AreaCode = AreaCodeEnum.下线区域;
 
            var upiLineQueueList = await _mes_Upi_LineQueueRep.AsQueryable().Where(x => x.PackageCode == input.PackageCode).ToListAsync();
            if (upiLineQueueList?.Count > 0)
            {
                await _mes_Upi_LineQueueRep.DeleteAsync(upiLineQueueList);
            }
 
            var upiList = await _mesBatchOrderUpiRep.AsQueryable().Where(x => x.PackageCode == input.PackageCode).ToListAsync();
            if (upiList == null)
            {
                throw Oops.Oh($"没有找到包号{input.PackageCode}的板件数据");
            }
            var _cretaorName = App.User.FindFirst(ClaimConst.RealName)?.Value;
 
            foreach (var item in upiList)
            {
                item.UpiFlag = UpiFlagEnum.NG;
                item.UpdateTime = DateTime.Now;
                item.UpiStatus = UpiStatusEnum.已下线;
                item.AreaCode = AreaCodeEnum.下线区域;
 
                WmsRecordUpiProcess wmsRecordUpi = new WmsRecordUpiProcess();
                wmsRecordUpi = item.Adapt<WmsRecordUpiProcess>();
                wmsRecordUpi.Id = Yitter.IdGenerator.YitIdHelper.NextId();
                wmsRecordUpi.CreateTime = DateTime.Now;
                wmsRecordUpi.CreateUserName = _cretaorName;
                wmsRecordUpi.Location = "";
                wmsRecordUpi.OperRemark = _OperRemark;
                await _wmsRecordUpiProcessRep.InsertAsync(wmsRecordUpi);
 
                WmsRecordUpiNg wmsRecordUpiNg = new WmsRecordUpiNg();
                wmsRecordUpiNg = item.Adapt<WmsRecordUpiNg>();
                wmsRecordUpiNg.Id = Yitter.IdGenerator.YitIdHelper.NextId();
                wmsRecordUpiNg.CreateTime = DateTime.Now;
                wmsRecordUpiNg.CreateUserName = _cretaorName;
                wmsRecordUpiNg.OperRemark = _OperRemark;
                await _wmsRecordUpiNgRep.InsertAsync(wmsRecordUpiNg);
 
 
 
                //清理 队列表
                var removeUpiList = await _mes_Upi_LineQueueRep.AsQueryable().Where(x => x.Upi == item.UPI).ToListAsync();
                if (removeUpiList?.Count > 0)
                {
                    await _mes_Upi_LineQueueRep.DeleteAsync(removeUpiList);
                }
 
                var removePackageList = await _mes_Package_LineQueueRep.AsQueryable().Where(x => x.PackageCode == item.PackageCode).ToListAsync();
                if (removePackageList?.Count > 0)
                {
                    await _mes_Package_LineQueueRep.DeleteAsync(removePackageList);
                }
 
 
            }
 
            //下线更新 订单表数据 _mesOrderGatherRep
            await UpdateOrderGather(singlePackage, input.IsNG);
 
 
            WmsRecordPackageProcess wmsRecordPackage = new WmsRecordPackageProcess();
            wmsRecordPackage = singlePackage.Adapt<WmsRecordPackageProcess>();
            wmsRecordPackage.Id = Yitter.IdGenerator.YitIdHelper.NextId();
            wmsRecordPackage.Location = "";
            wmsRecordPackage.OperRemark = _OperRemark;
            wmsRecordPackage.CreateTime = DateTime.Now;
            wmsRecordPackage.CreateUserName = _cretaorName;
            await _wmsRecordPackageProcessRep.InsertAsync(wmsRecordPackage);
 
 
            //新增下线记录表
            var all_upiList = await _mesBatchOrderUpiRep.AsQueryable().Where(x => x.Info5 == singlePackage.Info5).ToListAsync();
            var all_unlineList = await _mes_Package_UnLine_RecordRep.AsQueryable().Where(x => x.Info5 == singlePackage.Info5).ToListAsync();
            Mes_Package_UnLine_Record record = new Mes_Package_UnLine_Record();
            record = singlePackage.Adapt<Mes_Package_UnLine_Record>();
            record.Id = Yitter.IdGenerator.YitIdHelper.NextId();
            record.Location = "人工";
            record.AllPackageNum = all_upiList.GroupBy(x => x.PackageCode).Count();
            record.CurUnlineIndexPackageNum = all_unlineList.GroupBy(x => x.PackageCode).Count() + 1;
            record.CreateTime = DateTime.Now;
            record.CreateUserName = _cretaorName;
            await _mes_Package_UnLine_RecordRep.InsertAsync(record);
 
 
            await _mesPackageGatherRep.UpdateAsync(singlePackage);
            await _mesBatchOrderUpiRep.UpdateRangeAsync(upiList);
        }
        catch (Exception)
        {
 
            throw;
        }
        finally
        {
            semaphore.Release();
        }
    }
 
    /// <summary>
    /// 公共-下线更新 订单表数据
    /// </summary>
    /// <param name="singlePackage"></param>
    /// <returns></returns>
    private async Task UpdateOrderGather(Mes_Package_Gather singlePackage, bool isNG)
    {
        //下线更新 订单表数据 _mesOrderGatherRep
        bool isAdd = false;
        var order = await _mesOrderGatherRep.AsQueryable().Where(x => x.Info5 == singlePackage.Info5).FirstAsync();
        if (order == null)
        {
            isAdd = true;
            order = new Mes_Order_Gather()
            {
                Info5 = singlePackage.Info5,
                OrderId = singlePackage.OrderId,
                PlanNo = singlePackage.PlanNo,
            };
        }
        var packageList = await _mesPackageGatherRep.AsQueryable().Where(x => x.Info5 == singlePackage.Info5).ToListAsync();
        order.PackageNum = packageList.Count();
        order.UnLinePackageNum = packageList.Where(x => x.UpiStatus == UpiStatusEnum.已下线).Count() + 1;
        if (order.UnLinePackageNum > order.PackageNum)
        {
            order.UnLinePackageNum = order.PackageNum;
        }
        order.NgPackageNum = packageList.Where(x => x.UpiStatus == UpiStatusEnum.已下线 && x.UpiFlag == UpiFlagEnum.NG).Count();
        if (isNG)
        {
            order.NgPackageNum = order.NgPackageNum + 1;
        }
        if (order.NgPackageNum > order.PackageNum)
        {
            order.NgPackageNum = order.PackageNum;
        }
 
        order.NoUnLinePackageNum = order.PackageNum - order.UnLinePackageNum;
 
        order.AllPackageArea = packageList.Sum(x => Convert.ToDecimal(x.Info13));
        order.ScanPackageArea = packageList.Where(x => x.UpiStatus == UpiStatusEnum.已下线 && x.UpiFlag == (int)UpiFlagEnum.正常).Sum(x => Convert.ToDecimal(x.Info13));
        if (isNG == false)
        {
            order.ScanPackageArea = order.ScanPackageArea + Convert.ToDecimal(singlePackage.Info13);
        }
 
        order.NoScanPackageArea = order.AllPackageArea - order.ScanPackageArea;
 
        //判断该订单是否已下线
        if (order.NoUnLinePackageNum == 0)
        {
            order.IsUnline = true;
            order.UnlineTime = DateTime.Now;
            //order.UnlinePerson = _UnlinePerson;
        }
 
        if (isAdd)
        {
            await _mesOrderGatherRep.InsertAsync(order);
        }
        else
        {
            await _mesOrderGatherRep.UpdateAsync(order);
        }
    }
 
    /// <summary>
    /// 标记NG-扫描件号
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "SetNG")]
    [Description("WmsOperationTask/SetNG")]
    [UnitOfWork]
    public async Task SetNG(SetNGInput input)
    {
        if (input == null)
        {
            throw Oops.Oh("参数不能为空");
        }
        if (string.IsNullOrEmpty(input.Upi))
        {
            throw Oops.Oh("部件条码不能为空");
        }
        //if (string.IsNullOrEmpty(input.Reason))
        //{
        //    throw Oops.Oh("原因不能为空");
        //}
 
        var singleUpi = await _mesBatchOrderUpiRep.AsQueryable().Where(x => x.UPI == input.Upi).FirstAsync();
        if (singleUpi == null)
        {
            throw Oops.Oh($"没有找到部件条码{input.Upi}的数据");
        }
        var packageCode = singleUpi.PackageCode;
        var singlePackage = await _mesPackageGatherRep.AsQueryable().Where(x => x.PackageCode == packageCode).FirstAsync();
        if (singlePackage == null)
        {
            throw Oops.Oh($"没有找到包号{packageCode}的汇总数据");
        }
 
        var _cretaorName = App.User.FindFirst(ClaimConst.RealName)?.Value;
 
        singlePackage.UpiFlag = UpiFlagEnum.NG;
        singlePackage.UpdateTime = DateTime.Now;
        singlePackage.UpdateUserName = _cretaorName;
 
        var upiList = await _mesBatchOrderUpiRep.AsQueryable().Where(x => x.PackageCode == packageCode).ToListAsync();
        if (upiList == null)
        {
            throw Oops.Oh($"没有找到包号{packageCode}的板件数据");
        }
        foreach (var item in upiList)
        {
            item.UpiFlag = UpiFlagEnum.NG;
            item.UpdateTime = DateTime.Now;
            item.UpdateUserName = _cretaorName;
 
            WmsRecordUpiProcess wmsRecordUpi = new WmsRecordUpiProcess();
            wmsRecordUpi = item.Adapt<WmsRecordUpiProcess>();
            wmsRecordUpi.Id = Yitter.IdGenerator.YitIdHelper.NextId();
            wmsRecordUpi.CreateTime = DateTime.Now;
            wmsRecordUpi.CreateUserName = _cretaorName;
            wmsRecordUpi.Location = "";
            wmsRecordUpi.OperRemark = "标记NG";
            await _wmsRecordUpiProcessRep.InsertAsync(wmsRecordUpi);
 
            WmsRecordUpiNg wmsRecordUpiNg = new WmsRecordUpiNg();
            wmsRecordUpiNg = item.Adapt<WmsRecordUpiNg>();
            wmsRecordUpiNg.Id = Yitter.IdGenerator.YitIdHelper.NextId();
            wmsRecordUpiNg.CreateTime = DateTime.Now;
            wmsRecordUpiNg.CreateUserName = _cretaorName;
            wmsRecordUpiNg.OperRemark = "标记NG";
            await _wmsRecordUpiNgRep.InsertAsync(wmsRecordUpiNg);
        }
 
 
        WmsRecordPackageProcess wmsRecordPackage = new WmsRecordPackageProcess();
        wmsRecordPackage = singlePackage.Adapt<WmsRecordPackageProcess>();
        wmsRecordPackage.Id = Yitter.IdGenerator.YitIdHelper.NextId();
        wmsRecordPackage.Location = "";
        wmsRecordPackage.OperRemark = "标记NG";
        wmsRecordPackage.CreateTime = DateTime.Now;
        wmsRecordPackage.CreateUserName = _cretaorName;
        await _wmsRecordPackageProcessRep.InsertAsync(wmsRecordPackage);
 
 
        await _mesPackageGatherRep.UpdateAsync(singlePackage);
        await _mesBatchOrderUpiRep.UpdateRangeAsync(upiList);
 
    }
 
 
    /// <summary>
    /// 取消NG-扫描件号
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "CancelNG")]
    [Description("WmsOperationTask/CancelNG")]
    [UnitOfWork]
    public async Task CancelNG(CancelNGInput input)
    {
        if (input == null)
        {
            throw Oops.Oh("参数不能为空");
        }
        if (string.IsNullOrEmpty(input.Upi))
        {
            throw Oops.Oh("部件条码不能为空");
        }
        //if (string.IsNullOrEmpty(input.Reason))
        //{
        //    throw Oops.Oh("原因不能为空");
        //}
 
        var singleUpi = await _mesBatchOrderUpiRep.AsQueryable().Where(x => x.UPI == input.Upi).FirstAsync();
        if (singleUpi == null)
        {
            throw Oops.Oh($"没有找到部件条码{input.Upi}的数据");
        }
        var packageCode = singleUpi.PackageCode;
        var singlePackage = await _mesPackageGatherRep.AsQueryable().Where(x => x.PackageCode == packageCode).FirstAsync();
        if (singlePackage == null)
        {
            throw Oops.Oh($"没有找到包号{packageCode}的汇总数据");
        }
 
        var _cretaorName = App.User.FindFirst(ClaimConst.RealName)?.Value;
 
        singlePackage.UpiFlag = UpiFlagEnum.正常;
        singlePackage.UpdateTime = DateTime.Now;
        singlePackage.UpdateUserName = _cretaorName;
 
        var upiList = await _mesBatchOrderUpiRep.AsQueryable().Where(x => x.PackageCode == packageCode).ToListAsync();
        if (upiList == null)
        {
            throw Oops.Oh($"没有找到包号{packageCode}的板件数据");
        }
        foreach (var item in upiList)
        {
            item.UpiFlag = UpiFlagEnum.正常;
            item.UpdateTime = DateTime.Now;
            item.UpdateUserName = _cretaorName;
 
            WmsRecordUpiProcess wmsRecordUpi = new WmsRecordUpiProcess();
            wmsRecordUpi = item.Adapt<WmsRecordUpiProcess>();
            wmsRecordUpi.Id = Yitter.IdGenerator.YitIdHelper.NextId();
            wmsRecordUpi.CreateTime = DateTime.Now;
            wmsRecordUpi.CreateUserName = _cretaorName;
            wmsRecordUpi.Location = "";
            wmsRecordUpi.OperRemark = "取消NG";
            await _wmsRecordUpiProcessRep.InsertAsync(wmsRecordUpi);
 
            WmsRecordUpiNg wmsRecordUpiNg = new WmsRecordUpiNg();
            wmsRecordUpiNg = item.Adapt<WmsRecordUpiNg>();
            wmsRecordUpiNg.Id = Yitter.IdGenerator.YitIdHelper.NextId();
            wmsRecordUpiNg.CreateTime = DateTime.Now;
            wmsRecordUpiNg.CreateUserName = _cretaorName;
            wmsRecordUpiNg.OperRemark = "取消NG";
            await _wmsRecordUpiNgRep.InsertAsync(wmsRecordUpiNg);
        }
 
 
        WmsRecordPackageProcess wmsRecordPackage = new WmsRecordPackageProcess();
        wmsRecordPackage = singlePackage.Adapt<WmsRecordPackageProcess>();
        wmsRecordPackage.Id = Yitter.IdGenerator.YitIdHelper.NextId();
        wmsRecordPackage.Location = "";
        wmsRecordPackage.OperRemark = "取消NG";
        wmsRecordPackage.CreateTime = DateTime.Now;
        wmsRecordPackage.CreateUserName = _cretaorName;
        await _wmsRecordPackageProcessRep.InsertAsync(wmsRecordPackage);
 
 
        await _mesPackageGatherRep.UpdateAsync(singlePackage);
        await _mesBatchOrderUpiRep.UpdateRangeAsync(upiList);
 
    }
 
 
    /// <summary>
    /// 扫码记录清除
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "PartClear")]
    [Description("WmsOperationTask/PartClear")]
    public async Task PartClear()
    {
        //模式判断
        var client = await WCFServiceHelper.GetWCFService_SAPCC_Client(_sysConfigRep);
        var res = await client.WriteStation_PartClearAsync();
        if (res.result == false)
        {
            throw Oops.Oh(res.resMsg);
        }
    }
 
 
    /// <summary>
    /// 一键清理板件队列数据
    /// </summary>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "OneKeyClearUpiQueue")]
    [Description("WmsOperationTask/OneKeyClearUpiQueue")]
    [UnitOfWork]
    public async Task OneKeyClearUpiQueue()
    {
        var upiList = await _mes_Upi_LineQueueRep.AsQueryable().ToListAsync();
        if (upiList?.Count == 0)
        {
            throw Oops.Oh($"没有板件数据");
        }
        await _mes_Upi_LineQueueRep.DeleteAsync(upiList);
 
    }
}