服务端的TigerApi 框架,基于.NET6 2024 版本
Rodney Chen
2024-12-19 eb0ad83719de660e5c4f7676aea4710625b6bd51
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
using Rhea.Common;
using Tiger.IBusiness;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Tiger.Model;
using System.Collections;
 
namespace Tiger.Business.WMS.Transaction
{
    /// <summary>
    /// 客供料入库清点事务
    /// </summary>
    public class CustSupplyCheckingNew : WMSTransactionBase, ICustSupplyCheckingNew
    {
        /// <summary>
        /// 事务初始化函数
        /// </summary>
        /// <param name="id"></param>
        /// <param name="userCode"></param>
        /// <param name="apiHost"></param>
        /// <param name="orgCode"></param>
        public ICustSupplyCheckingNew Init(string id, string userCode, string apiHost, string orgCode)
        {
            TransID = id;
            UserCode = userCode;
            ApiHost = apiHost;
            OrgCode = orgCode;
            Logger.Console.Info($"Start {this.GetType().Name} Transaction[ID: {TransID}]");
            return this;
        }
 
        #region Propertys & Variables
 
        public string UserCode { get; set; }
        public long UserId { get; set; }
        public string VenderCode { get; set; }
        public string LotNo { get; set; }
        public string SecondVenderCode { get; set; }
        public string WarehouseCode { get; set; }
        public string WarehouseName { get; set; }
        public string OrgCode { get; set; }
        public string CustomsNo { get; set; }
        public List<ParameterReturn> suppliers { get; set; }
        public bool T100Success { get; set; } = false;
 
        #endregion Propertys & Variables
 
        #region Functions
 
        /// <summary>
        /// 扫描物料并复核,如果物料已经完成移库则货架上亮灯提醒储位
        /// </summary>
        public async Task<ApiAction> ScanItem(CustSupplyCheckingInput input)
        {
            var action = new ApiAction();
            try
            {
                //if (string.IsNullOrEmpty(input.WarehouseCode))
                //{
                //    action.IsSuccessed = false;
                //    action.Message = $"库位不能为空";
                //    return action;
                //}
                string[] _strVenderCodes = input.SecondSuppCode.Split(',');
                SecondVenderCode = _strVenderCodes[0];
                LotNo = input.LotNo;
                if (string.IsNullOrEmpty(VenderCode)) { VenderCode = _strVenderCodes[1]; }
                if (string.IsNullOrEmpty(input.SN))
                {
                    action.IsSuccessed = false;
                    action.LocaleMsg = Biz.L("WMS.CustSupChk.ScanItem.SnEmptyFailure", input.SN);
                    return action;
                }
 
                #region 报关单号判断
 
                if (input.AuthOption.CurOrg == "YTXC" && string.IsNullOrWhiteSpace(CustomsNo))
                {
                    if (string.IsNullOrWhiteSpace(input.SN))
                    {
                        action.IsSuccessed = false;
                        action.LocaleMsg = Biz.L($"盐田据点请扫描报关单号二维码!");
                        return action;
                    }
                    var customsNo = input.SN.Replace(",", ",").Trim();
                    if (customsNo.Split(",").Length != 2)
                    {
                        action.IsSuccessed = false;
                        action.LocaleMsg = Biz.L($"报关单号格式错误,请确认格式是否为【报关单号,核算清单号】!");
                        return action;
                    }
                    CustomsNo = input.SN;
                    action.Data = new IncomingMatStorage
                    {
                        sn = CustomsNo,
                        IsCustomsNo = true
                    };
                    action.IsSuccessed = true;
                    action.LocaleMsg = Biz.L($"报关单号二维码扫描成功");
                    return action;
                }
 
                #endregion 报关单号判断
 
                //判断是否扫描的仓库,并查询是否存在
                var query = await Biz.Db.Queryable<WMS_WAREHOUSE>().Where(t => t.WH_CODE == input.SN.Trim()).FirstAsync();
                if (query != null)
                {
                    //if (!string.IsNullOrEmpty(WarehouseCode))
                    //{
                    //    action.IsSuccessed = false;
                    //    action.Data = query;
                    //    action.LocaleMsg = Biz.L("WMS.CustSupChk.ScanItem.WhExist", input.SN);
                    //    return action;
                    //}
                    //else
                    {
                        WarehouseCode = query.WH_CODE;
                        WarehouseName = query.WH_NAME;
                        action.Data = query;
                        action.LocaleMsg = Biz.L("WMS.CustSupChk.ScanItem.GetWhSuccess", WarehouseCode);
                        return action;
                    }
                }
                if (string.IsNullOrEmpty(WarehouseCode))
                {
                    action.IsSuccessed = false;
                    action.LocaleMsg = Biz.L("WMS.CustSupChk.ScanItem.NotExist", input.SN);
                    return action;
                }
                input.AuthOption.OrgCode = input.AuthOption.OrgCode.IsNullOrEmpty() ? input.AuthOption.CurOrg : input.AuthOption.OrgCode;
                Result<IInventory> result = WMS_ITEM_Biz.WmsItem.GetK(input.SN, input.AuthOption, false);
                if (result.Flag != Result.Flags.Success)
                {
                    action.IsSuccessed = false;
                    action.LocaleMsg = result.LocaleMsg;
                    return action;
                }
                var curInv = result.Data as Inventory;
                if (!curInv.IsQRCode)
                {
                    action.IsSuccessed = false;
                    action.LocaleMsg = Biz.L("WMS.CustSupChk.ScanItem.NotQrCode", curInv.Barcode.MetaSn);
                    return action;
                }
                //判断条码是否为最少包装
                if (!new[] { Barcode.Types.Small, Barcode.Types.Other }.Contains(curInv.Barcode.Type))
                {
                    action.IsSuccessed = false;
                    action.LocaleMsg = Biz.L("WMS.CustSupChk.ScanItem.TypeError", curInv.SN);
                    return action;
                }
                if (curInv.Items.Any())
                {
                    action.IsSuccessed = false;
                    action.LocaleMsg = Biz.L("WMS.CustSupChk.ScanItem.ExistItem", curInv.ItemInfo?.ITEM_CODE);
                    return action;
                }
 
                if (string.IsNullOrEmpty(curInv.ItemInfo?.ITEM_CODE))
                {
                    action.IsSuccessed = false;
                    action.LocaleMsg = Biz.L("WMS.CustSupChk.ScanItem.NotFound", curInv.ItemInfo?.ITEM_CODE);
                    return action;
                }
 
                // 其它入库清点不能扫入拆包后带-1的标签
                if (curInv.isNormalStatus && curInv.Status == WMS_ITEM.STATUSs.WaitIn)
                {
                    action.IsSuccessed = false;
                    action.LocaleMsg = Biz.L("WMS.CustSupChk.ScanItem.NotAllowIn", curInv.ItemInfo?.ITEM_CODE);
                    return action;
                }
                //物料验证
                if (curInv.ItemInfo.IsNullOrEmpty() || curInv.ItemInfo.IS_ACTIVE == "N")
                {
                    action.IsSuccessed = false;
                    action.LocaleMsg = Biz.L("WMS.ProdMReq.ScanItem.ItemCodeNotExistsOrNotActive", curInv.ItemInfo.ITEM_CODE.IsNullOrEmpty(curInv.Barcode.ItemCode));
                    return action;
                }
                OrgCode = input.AuthOption.CurOrg;
                //string[] _strVenderCodes = input.SecondSuppCode.Split(',');
                //SecondVenderCode = _strVenderCodes[0];
                //LotNo = input.LotNo;
                //if (string.IsNullOrEmpty(VenderCode)) { VenderCode = _strVenderCodes[1]; }
                //暂时不判断
                //if (_strVenderCodes[1] != VenderCode)
                //{
                //    action.IsSuccessed = false;
                //    action.Message = $"二级供应商代码不符!请重新选择二级供应商!";
                //    return action;
 
                //}
 
                var scanResult = new BIZ_ERP_OTH_IN_SN
                {
                    ID = Guid.NewGuid().ToString(),
                    BUSINESSCODE = UserCode,
                    WAREHOUSECODE = WarehouseCode,
                    SN = curInv.SN,
                    ITEM_CODE = curInv.ItemInfo.ITEM_CODE,
                    QTY = curInv.Barcode.Qty ?? 0,
                    DATECODE = curInv.Barcode.ProdDate ?? DateTime.MinValue,
                    LOTNO = LotNo, //item.Barcode.LotNo,
                    STATUS = WMS_ITEM.STATUSs.Counted.GetValue(),
                    META_SN = input.SN,
                    AUTH_ORG = input.AuthOption.CurOrg
                };
                var snExt = new BIZ_ERP_OTH_IN_SNExt()
                {
                    BUSINESSCODE = scanResult.BUSINESSCODE,
                    WAREHOUSECODE = scanResult.WAREHOUSECODE,
                    SN = scanResult.SN,
                    ITEM_CODE = scanResult.ITEM_CODE,
                    QTY = scanResult.QTY,
                    DATECODE = scanResult.DATECODE,
                    DATECODESTR = scanResult.DATECODESTR,
                    LOTNO = scanResult.LOTNO,
                    STATUS = scanResult.STATUS,
                    WH_CODE = scanResult.WAREHOUSECODE,
                    WH_NAME = WarehouseName,
                    MName = curInv.ItemInfo.ITEM_NAME,
                    MDesc = curInv.ItemInfo.SPEC,
                    MUom = curInv.ItemInfo.UNIT
                };
 
                //验证条码是否已做过清点
                var nLogs = Biz.Db.Queryable<BIZ_ERP_OTH_IN_SN>().Where(t => t.SN.ToUpper() == curInv.SN.ToUpper()).First();
                if (nLogs != null)
                {
                    action.IsSuccessed = false;
                    action.LocaleMsg = Biz.L("WMS.CustSupChk.ScanItem.BarcodeCounted", curInv.SN);
                    return action;
                }
 
                //保存到数据库
                var db = Business.Biz.Db;
 
                var model = new BIZ_ERP_CUSTOMS()
                {
                    AUTH_ORG = input.AuthOption.CurOrg,
                    CREATE_USER = UserCode,
                    OrderNo = UserCode,
                    Barcodes = curInv.SN,
                    CustomsNo = CustomsNo.Replace(",", ",").Trim().Split(',')[0],
                    AccountingNo = CustomsNo.Replace(",", ",").Trim().Split(',')[1],
                    CountsType = BIZ_ERP_CUSTOMS.CountsEnum.Incoming
                };
 
                var dbTran = db.UseTran(() =>
                {
                    db.Insertable(scanResult, UserCode).ExecuteCommand();
                    //db.Insertable(wi, UserCode).ExecuteCommand();
                    //db.Insertable(his, UserCode).ExecuteCommand();
                    if (model != null)
                    {
                        db.Insertable(model).ExecuteCommand();
                    }
                });
                if (!dbTran.IsSuccess)
                {
                    Logger.Default.Fatal(dbTran.ErrorException, "Database transaction save exception");
                    this.Close(!dbTran.IsSuccess);
                    throw dbTran.ErrorException;
                }
                action.Data = snExt;
                action.LocaleMsg = Biz.L("WMS.CustSupChk.ScanItem.CountSuccess", input.SN);
            }
            catch (Exception ex)
            {
                //取消当前操作
 
                action.CatchExceptionWithLog(ex, $"扫描物料[{input.SN}]复核异常");
                action.LocaleMsg = Biz.L("WMS.CustSupChk.ScanItem.ScanException", input.SN);
            }
            return action;
        }
 
        /// <summary>
        /// 生成客供物料入库单据
        /// </summary>
        /// <returns></returns>
        public async Task<ApiAction> SubmitInvBill()
        {
            var action = new ApiAction();
            bool isIQCOk = true;
            try
            {
                //获取当前用户所有已清点、未制单的条码数据
                var nScannedBarcodes = await Biz.Db.Queryable<BIZ_ERP_OTH_IN_SN, BAS_ITEM>((t, m) =>
                                   new JoinQueryInfos(
                                       JoinType.Left, t.ITEM_CODE == m.ITEM_CODE
                                       ))
               .Where((t, m) => t.WAREHOUSECODE.ToUpper() == WarehouseCode.ToUpper()
                           && t.STATUS == WMS_ITEM.STATUSs.Counted.GetValue()
                           && t.BUSINESSCODE == UserCode
                           && t.AUTH_ORG == OrgCode)
               .Select((t, m) => new BIZ_ERP_OTH_IN_SNExt
               {
                   ID = t.ID,
                   SN = t.SN,
                   ITEM_CODE = t.ITEM_CODE,
                   QTY = t.QTY,
                   META_SN = t.META_SN,
                   BUSINESSCODE = t.BUSINESSCODE,
                   BUSINESSLINE = t.BUSINESSLINE,
                   UPDATE_TIME = t.UPDATE_TIME,
                   UPDATE_USER = t.UPDATE_USER,
                   CREATE_TIME = t.CREATE_TIME,
                   CREATE_USER = t.CREATE_USER,
                   DATECODE = t.DATECODE,
                   DATECODESTR = t.DATECODESTR,
                   LOTNO = t.LOTNO,
                   STATUS = t.STATUS,
                   WAREHOUSECODE = t.WAREHOUSECODE,
                   MUom = m.UNIT,
                   MName = m.ITEM_NAME,
                   MDesc = m.SPEC,
               })
               .ToListAsync();
                if (!nScannedBarcodes.Any())
                {
                    action.IsSuccessed = false;
                    action.Message = $"没有可以提交的清点数据";
                    action.LocaleMsg = Biz.L("WMS.CustSupChk.ScanItem.NotDataToSubmit");
                    return action;
                }
 
                // 查询条码清点表所有仓库代码
                //var nWarehouseCodes = nScannedBarcodes.Select(x => x.WarehouseCode).Distinct().ToList();
 
                //新建其他入库单据头 OtherInstockHeader
                Result ruleResult = Cache.CodeRule["CI001"].Generate("CI");  //iWMS.GetBillNoByType("CI");
                if (!ruleResult.IsSuccessed)
                {
                    throw new Exception(ruleResult.ExceptionMsg.Message);
                }
                var billcode = ruleResult.Data.ToString();
                var nBillHeader = new BIZ_ERP_OTH_IN
                {
                    BILLCODE = billcode,
                    BILLDATE = DateTime.Now.Date,
                    WAREHOUSECODE = WarehouseCode,
                    STATUS = BIZ_ERP_OTH_IN.STATUSs.WORKING.GetValue(),
                    REMARK = LotNo,
                    AUTH_ORG = OrgCode
                };
 
                List<WMS_ITEM> Items = new List<WMS_ITEM>();
                List<WMS_ITEM_PKG> ItemPkgs = new List<WMS_ITEM_PKG>();
                List<WMS_ITEM_HIS> ItemHistorys = new List<WMS_ITEM_HIS>();
                foreach (var wi in nScannedBarcodes)
                {
                    Barcode barcode = new Barcode(wi.META_SN);
                    WMS_ITEM wmsItem = new WMS_ITEM
                    {
                        SN = wi.SN,
                        ITEM_CODE = wi.ITEM_CODE,
                        QTY = wi.QTY,
                        PROD_DATE = barcode.ProdDate ?? DateTime.MinValue,
                        SUPP_CODE = VenderCode,
                        LOTNO = LotNo,
                        ERP_WH = WarehouseCode,
                        STATUS = WMS_ITEM.STATUSs.WaitTest.GetValue(),
                        TRANS_CODE = nameof(BIZ_ERP_OTH_IN),
                        TRANS_NO = billcode,
                        //TRANS_LINE = item.SOURCELINE,
                        //SOURCE_CODE = nameof(BIZ_ERP_IQC),
                        //SOURCE_ORDER = item.BILLCODE,
                        AUTH_ORG = OrgCode,
                    };
                    Items.Add(wmsItem);
                    WMS_ITEM_PKG pKG = new WMS_ITEM_PKG
                    {
                        SN = wi.SN,
                        ITEM_CODE = wi.ITEM_CODE,
                        PARENT_SN = "",
                        PKG_TYPE = WMS_ITEM_PKG.PKG_TYPEs.OnePiece.GetValue(),
                        QTY = wi.QTY,
                        SUPP_CODE = VenderCode,
                        LOTNO = LotNo,
                        ERP_WH = WarehouseCode,
                        TRANS_CODE = nameof(BIZ_ERP_OTH_IN),
                        TRANS_NO = billcode,
                        //TRANS_LINE = item.SOURCELINE,
                        //SOURCE_CODE = nameof(BIZ_ERP_IQC),
                        //SOURCE_ORDER = item.BILLCODE,
                        AUTH_ORG = OrgCode,
                    };
                    ItemPkgs.Add(pKG);
                }
 
                // 查找待处理记录料号列表
                var nMaterials = nScannedBarcodes.Select(x => x.ITEM_CODE).Distinct().ToList();
 
                // 单据行号变量,初始值1
                var nLineSeq = 1;
 
                List<BIZ_ERP_OTH_IN_SN> scanList = new List<BIZ_ERP_OTH_IN_SN>();
                List<BIZ_ERP_OTH_IN_DTL> detailList = new List<BIZ_ERP_OTH_IN_DTL>();
                List<BIZ_ERP_IQC> iqcHeaders = new List<BIZ_ERP_IQC>();
                List<Detail> details = new List<Detail>();
                // 按照料号列表处理单据明细
                foreach (var matCode in nMaterials)
                {
                    // 按照料号、计量单位汇总scanBarcode
                    var lineMaterials = (from codes in nScannedBarcodes.Where(x => x.ITEM_CODE.ToUpper() == matCode.ToUpper())
                                         group codes by new { codes.ITEM_CODE, codes.MUom } into g
                                         select new
                                         {
                                             g.Key.ITEM_CODE,
                                             g.Key.MUom,
                                             sumQty = g.Sum(x => x.QTY)
                                         }
                            ).ToList();
 
                    // 循环处理每一行
                    foreach (var matLine in lineMaterials)
                    {
                        //插入单据明细行
                        var billDetail = new BIZ_ERP_OTH_IN_DTL
                        {
                            WAREHOUSECODE = WarehouseCode,
                            BILLCODE = billcode,
                            BILLLINE = nLineSeq.ToString(),
                            LINESTATUS = BIZ_ERP_OTH_IN.STATUSs.WORKING.GetValue(),
                            ITEM_CODE = matLine.ITEM_CODE,
                            UNITCODE = matLine.MUom,
                            PRQTY = matLine.sumQty,
                            ERP_BILL_CODE = "",
                            AUTH_ORG = OrgCode,
                            QTY = 0
                        };
                        detailList.Add(billDetail);
 
                        details.Add(new Detail
                        {
                            pmdtseq = nLineSeq.ToString(),
                            pmdt001 = "",
                            pmdt002 = "",
                            pmdt003 = "",
                            pmdt004 = "",
                            pmdt006 = matLine.ITEM_CODE, //"K7015AA016141",
                            pmdt018 = "创米科技",
                            pmdt020 = matLine.sumQty.ToString(),  //"100.00000",
                            pmdtud011 = "0"
                        });
 
                        Result iqcResult = Cache.CodeRule["IQC001"].Generate("IQC");
                        if (!iqcResult.IsSuccessed)
                        {
                            throw new Exception(iqcResult.ExceptionMsg.Message);
                        }
                        //nBillHeader.SOURCECODE = iqcResult.Data.ToString();
                        var nIqcHeader = new BIZ_ERP_IQC
                        {
                            BILLCODE = iqcResult.Data.ToString(),
                            SOURCETYPE = (int)BIZ_ERP_IQC.BillType.CustSupplyInStock,
                            SOURCECODE = billcode,
                            SOURCELINE = nLineSeq.ToString(),
                            PONO = billcode,
                            POLINE = nLineSeq.ToString(),
                            MEASURE = "Y",
                            AUTH_ORG = OrgCode,
                            QCTIMES = 0,
                            QCSTATUS = (int)BIZ_ERP_IQC.QCSTATUSs.Finished, /*(int)IqcManagementSides.QCStatus.UnFinish,*/
                            QCRESULT = (int)BIZ_ERP_IQC.QCRESULTs.Wait,
                            QCREVIEW = (int)BIZ_ERP_IQC.QCReview.UnReview,
                            QCLEVEL = 11,
                            ITEM_CODE = matLine.ITEM_CODE
                            //ReceiveQty = detail.Qty, //生成送检单加上接收数量 2022/07/27 Ben Lin 2022/09/03 Ben Lin 注释
                        };
                        iqcHeaders.Add(nIqcHeader);
 
                        // 更新清点表,单据号
                        foreach (BIZ_ERP_OTH_IN_SN barcode in nScannedBarcodes.Where(x => x.ITEM_CODE == matLine.ITEM_CODE))
                        {
                            barcode.BUSINESSCODE = billcode;
                            barcode.BUSINESSLINE = nLineSeq.ToString();
                            barcode.STATUS = WMS_ITEM.STATUSs.WaitTest.GetValue();
                            barcode.AUTH_ORG = OrgCode;
                            barcode.UPDATE_TIME = DateTime.Now;
                            barcode.UPDATE_USER = UserCode;
                            scanList.Add(barcode);
                        }
 
                        // 单据明细行号 ++
                        nLineSeq++;
                    }
                }
 
                var sns = nScannedBarcodes.Select(i => i.SN).Distinct().ToList();
                var customsData = Biz.Db.Queryable<BIZ_ERP_CUSTOMS>().Where(i => sns.Contains(i.Barcodes)).First();
 
                //调用T100接口生成到货单
                var ret = ErpT100.GenerateCustReceipt(new CustReceiptInputParameter
                {
                    parameter = new Parameter<Detail>
                    {
                        pmds000 = "2",
                        pmdsdocno = "3453",
                        pmdsdocdt = DateTime.Now.ToString("yyyy-MM-dd"), //"2023-04-26", //当天时间
                        pmds002 = UserCode == "admin" ? "00000" : UserCode, //"L36783", //用户ID
                        pmds003 = "",
                        pmds007 = VenderCode, //"C0006", //供应商代码
                        pmdsud003 = customsData.CustomsNo, // 报关单号
                        pmdsud004 = customsData.AccountingNo,// 核放清单号
                        pmds010 = "", //"测试客供料无采购收货", //送货单号
                        pmdsud001 = SecondVenderCode, //"CM017", //二级供应编号
                        detail = details
                    },
                    datakey = new Datakey
                    {
                        EntId = "88",
                        CompanyId = OrgCode
                    }
                });
                if (ret.payload.std_data.execution.code == "0") //成功
                {
                    nBillHeader.ERP_BILL_CODE = ret.payload.std_data.parameter.docno;
                    action.Message += $"{action.Message.IsNullOrEmpty("", ";")}{ret.payload.std_data.execution.description},返回的收货单号:[{ret.payload.std_data.parameter.docno}]";
                    T100Success = true;
 
                    #region 生成送检单
 
                    foreach (var item in iqcHeaders)
                    {
                        var retIns = ErpT100.GenerateInspection(new InsInputParameter
                        {
                            parameter = new InsParameter
                            {
                                pmdsdocno = ret.payload.std_data.parameter.docno,
                                pmdsdocdt = DateTime.Now.ToString("yyyy-MM-dd"),
                                pmdtseq = item.SOURCELINE.ToString(),
                                pmdt020 = item.QCQTY.ToString(),
                                qcbadocno = "3701",
                            },
                            datakey = new Datakey
                            {
                                EntId = "88",
                                CompanyId = OrgCode
                            }
                        });
                        isIQCOk &= retIns.payload.std_data.execution.code == "0";
                        if (retIns.payload.std_data.execution.code != "0")
                        {
                            //isIQCOk = false;
                            action.IsSuccessed = false;
                            action.Message = $"到货单[{ret.payload.std_data.parameter.docno}],当前用户[{UserCode}],供应商编号[{VenderCode}],二级供应商编号[{SecondVenderCode}],返回的信息:{retIns.payload.std_data.execution.description}"; ;
                            //return action;
                        }
                        else
                        {
                            item.ERP_BILL_CODE = retIns.payload.std_data.parameter.docno;
                            //更新库存表
                            foreach (var wi in Items.Where(x => x.TRANS_NO == billcode && x.ITEM_CODE == item.ITEM_CODE))
                            {
                                wi.STATUS = WMS_ITEM.STATUSs.WaitTest.GetValue();
                                wi.TRANS_CODE = nameof(BIZ_ERP_OTH_IN);
                                wi.TRANS_NO = billcode;
                                wi.TRANS_LINE = item.SOURCELINE;
                                wi.SOURCE_CODE = nameof(item);
                                wi.ERP_WH = WarehouseCode;
                                wi.SOURCE_ORDER = item.BILLCODE;
                                wi.UPDATE_USER = UserCode;
                                wi.UPDATE_TIME = DateTime.Now;
                                wi.AUTH_ORG = OrgCode;
 
                                WMS_ITEM_HIS his = new WMS_ITEM_HIS(wi, $"客供来料清点,到货单[{billcode}][{string.Join(",", detailList.Where(s => s.ITEM_CODE == item.ITEM_CODE).Select(q => q.BILLLINE))}],送检单[{item.BILLCODE}]");
                                ItemHistorys.Add(his);
                            }
                            foreach (var pkg in ItemPkgs.Where(x => x.TRANS_NO == billcode && x.ITEM_CODE == item.ITEM_CODE))
                            {
                                pkg.PARENT_SN = "";
                                pkg.PKG_TYPE = WMS_ITEM_PKG.PKG_TYPEs.OnePiece.GetValue();
                                pkg.SUPP_CODE = VenderCode;
                                pkg.LOTNO = LotNo;
                                pkg.ERP_WH = WarehouseCode;
                                pkg.TRANS_LINE = item.SOURCELINE;
                                pkg.SOURCE_CODE = nameof(item);
                                pkg.SOURCE_ORDER = item.BILLCODE;
                                pkg.UPDATE_USER = UserCode;
                                pkg.UPDATE_TIME = DateTime.Now;
                                pkg.AUTH_ORG = OrgCode;
                            }
                        }
                    }
 
                    #endregion 生成送检单
                }
                else //失败
                {
                    action.IsSuccessed = false;
                    action.Message = $"当前用户[{UserCode}],供应商编号[{VenderCode}],二级供应商编号[{SecondVenderCode}],返回的信息:{ret.payload.std_data.execution.description}";
                    return action;
                }
 
                //保存到数据库
                var db = Business.Biz.Db;
                var dbTran = db.UseTran(() =>
                {
                    // 插入单据表头
                    var headerId = Biz.Db.Insertable(nBillHeader, UserCode).ExecuteReturnBigIdentity();
                    var x = db.Storageable(detailList, UserCode).ToStorage();
                    x.AsInsertable.ExecuteCommand();//不存在插入
                    x.AsUpdateable.IgnoreColumns(z => new { z.CREATE_TIME, z.CREATE_USER }).ExecuteCommand();//存在更新,更新时忽略字段CreationTime、CreatorUserId
 
                    //db.Updateable(scanList, UserCode).ExecuteCommand();
                    db.Fastest<BIZ_ERP_OTH_IN_SN>().BulkUpdate(scanList);
                    if (action.IsSuccessed && isIQCOk)
                    {
                        db.Insertable(iqcHeaders, UserCode).ExecuteCommand();
                        action.Message += $"{action.Message.IsNullOrEmpty("", ";")}生成客供物料入库单据和送检单成功";
                    }
                    //var ret = Biz.GenerateCustReceipt("", "");
 
                    if (Items.Count > 100)
                    {
                        db.Fastest<WMS_ITEM>().BulkCopy(Items);
                    }
                    else
                    {
                        db.Insertable(Items, UserCode).ExecuteCommand();
                    }
                    if (ItemPkgs.Count > 100)
                    {
                        db.Fastest<WMS_ITEM_PKG>().BulkCopy(ItemPkgs);
                    }
                    else
                    {
                        db.Insertable(ItemPkgs, UserCode).ExecuteCommand();
                    }
                    if (ItemHistorys.Count > 100)
                    {
                        db.Fastest<WMS_ITEM_HIS>().BulkCopy(ItemHistorys);
                    }
                    else
                    {
                        db.Insertable(ItemHistorys, UserCode).ExecuteCommand();
                    }
                });
                if (!dbTran.IsSuccess)
                {
                    Logger.Default.Fatal(dbTran.ErrorException, "Database transaction save exception");
                    this.Close(!dbTran.IsSuccess);
                    throw dbTran.ErrorException;
                }
            }
            catch (System.Exception ex)
            {
                return action.CatchExceptionWithLog(ex, Biz.L(""));
            }
            return action;
        }
 
        /// <summary>
        /// 删除当前用户的已扫记录(缓存表)
        /// </summary>
        /// <returns></returns>
        public async Task<ApiAction> DeleteAllScannedBarcode()
        {
            var action = new ApiAction();
            try
            {
                if (WarehouseCode.IsNullOrEmpty())
                {
                    action.IsSuccessed = false;
                    action.Message = $"空,无法删除已清点条码";
                    return action;
                }
                else
                {
                    await Biz.Db.Deleteable<BIZ_ERP_OTH_IN_SN>(x => x.CREATE_USER == UserCode
                        && x.WAREHOUSECODE == WarehouseCode
                        && x.STATUS == WMS_ITEM.STATUSs.Counted.GetValue()
                        && x.BUSINESSCODE == UserCode).ExecuteCommandAsync();
                }
            }
            catch (System.Exception ex)
            {
            }
            return action;
        }
 
        /// <summary>
        /// 删除当前用户的已扫单个记录
        /// </summary>
        /// <returns></returns>
        public async Task<ApiAction> DeleteScannedBarcode(CustSupplyInInput input)
        {
            var action = new ApiAction();
            try
            {
                if (WarehouseCode.IsNullOrEmpty())
                {
                    action.IsSuccessed = false;
                    action.Message = $"空,无法删除已清点条码";
                    return action;
                }
                else
                {
                    await Biz.Db.Deleteable<BIZ_ERP_OTH_IN_SN>(x => x.SN == input.SN).ExecuteCommandAsync();
                }
            }
            catch (System.Exception ex)
            {
            }
            return action;
        }
 
        /// <summary>
        /// 删除当前用户的已扫记录(缓存表)
        /// </summary>
        /// <returns></returns>
        public async Task<ApiAction> CheckingT100Staut()
        {
            var action = new ApiAction();
            try
            {
                if (T100Success)
                {
                    action.IsSuccessed = true;
                    T100Success = false;
                }
                else
                {
                    action.IsSuccessed = false;
                }
            }
            catch (System.Exception ex)
            {
            }
            return action;
        }
 
        /// <summary>
        ///  获取已清点的物料汇总
        /// </summary>
        /// <returns></returns>
        public async Task<PageAble<Model.Minsun.CustSupplyCheckList>> GetScannedMaterialSummarys()
        {
            var query = Biz.Db.Queryable<BIZ_ERP_OTH_IN_SN, BAS_ITEM>((t, m) =>
                                    new JoinQueryInfos(
                                        JoinType.Left, t.ITEM_CODE == m.ITEM_CODE
                                        ))
                .Where((t, m) => t.CREATE_USER == UserCode
                    && t.WAREHOUSECODE.ToUpper() == WarehouseCode.ToUpper()
                    && t.STATUS == WMS_ITEM.STATUSs.Counted.GetValue()
                    && t.AUTH_ORG == OrgCode.ToUpper()
                    && t.BUSINESSCODE == UserCode)
                .GroupBy((t, m) => new { t.WAREHOUSECODE, t.ITEM_CODE })
                .Select((t, m) => new Model.Minsun.CustSupplyCheckList
                {
                    WarehouseCode = t.WAREHOUSECODE,
                    MaterialCode = t.ITEM_CODE,
                    MaterialName = SqlFunc.AggregateMax(m.ITEM_NAME),
                    MaterialStandard = SqlFunc.AggregateMax(m.SPEC),
                    InventoryQty = SqlFunc.AggregateSum(t.QTY),
                    Unit = SqlFunc.AggregateMax(m.UNIT)
                })
                .ToList();
            return await Task.FromResult(new PageAble<Model.Minsun.CustSupplyCheckList>()
            {
                data = query,
                totals = query.Count(),
            });
        }
 
        /// <summary>
        /// 查询物料对应的条码
        /// </summary>
        /// <param name="MaterialCode"></param>
        /// <returns></returns>
        public async Task<PageAble<Model.Minsun.CustSupplyCheckDetail>> GetScannedMaterialDetailBarcodes(string MaterialCode)
        {
            var query = await Biz.Db.Queryable<BIZ_ERP_OTH_IN_SN, BAS_ITEM>((t, m) =>
                                    new JoinQueryInfos(
                                        JoinType.Left, t.ITEM_CODE == m.ITEM_CODE
                                        ))
                .Where((t, m) => t.CREATE_USER == UserCode
                    && t.WAREHOUSECODE.ToUpper() == WarehouseCode.ToUpper()
                    && t.STATUS == WMS_ITEM.STATUSs.Counted.GetValue()
                    && t.ITEM_CODE.ToUpper() == MaterialCode.ToUpper()
                    && t.BUSINESSCODE == UserCode)
                .Select((t, m) => new Model.Minsun.CustSupplyCheckDetail
                {
                    Barcode = t.SN,
                    BarcodeQty = t.QTY,
                    Unit = m.UNIT
                })
                .ToListAsync();
            return new PageAble<Model.Minsun.CustSupplyCheckDetail>()
            {
                data = query,
                totals = query.Count(),
            };
        }
 
        #endregion Functions
 
        public override bool Close(bool needSaveHistoryLog = false)
        {
            //保存操作日志
 
            this.IsFinished = true;
            return IsFinished ? base.Close(needSaveHistoryLog) : IsFinished;
        }
    }//endClass
}