服务端的TigerApi 框架,基于.NET6 2024 版本
Rodney Chen
2025-03-19 9e5eb7707c9a42e2515a583b40078d2d7685e2d0
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
using Rhea.Common;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
 
namespace Tiger.Business.WMS
{
    /// <summary>
    /// 条码基类
    /// </summary>
    public class Barcode
    {
        public Barcode(string metaSn)
        {
            SetAnalysisRule();
            Analyse(MetaSn = metaSn);
        }
 
        /// <summary>
        /// 设置条码分析验证规则
        /// </summary>
        public void SetAnalysisRule()
        {
            Analyses.Clear();
            Analyses.Add(new YadaBarcode());
            //Analyses.Add(new BarcodeAnalysis());
        }
 
        /// <summary>
        /// 条码(解析后的条码)
        /// </summary>
        private List<BarcodeAnalysis> Analyses { get; set; } = new();
        /// <summary>
        /// 条码(解析后的条码)
        /// </summary>
        public string SN { get; set; }
        /// <summary>
        /// 元条码(扫描到的条码)
        /// </summary>
        public string MetaSn { get; set; }
        /// <summary>
        /// 条码类型
        /// </summary>
        public Types Type { get; set; }
        /// <summary>
        /// 物料编码
        /// </summary>
        public ItemTypes ItemType { get; set; }
        /// <summary>
        /// 物料编码
        /// </summary>
        public string ItemCode { get; set; }
        /// <summary>
        /// 物料名称
        /// </summary>
        public string ItemName { get; set; }
        /// <summary>
        /// 生产日期编码
        /// </summary>
        public string DateCode { get; set; }
        /// <summary>
        /// 生产日期
        /// </summary>
        public string ProdDateStr { get; set; }
        /// <summary>
        /// 生产日期
        /// </summary>
        public DateTime? ProdDate { get => BarcodeAnalysis.GetDateTime(ProdDateStr) ?? BarcodeAnalysis.GetDateTime(PrintDateStr); }
        /// <summary>
        /// 打印日期
        /// </summary>
        public string PrintDateStr { get; set; }
        /// <summary>
        /// 供应商代码
        /// </summary>
        public string SupplierCode { get; set; }
        /// <summary>
        /// 批次号
        /// </summary>
        public string LotNo { get; set; }
        /// <summary>
        /// 组织代码
        /// </summary>
        public string Organization { get; set; }
        /// <summary>
        /// 订单号
        /// </summary>
        public string OrderNo { get; set; }
        /// <summary>
        /// 订单行号
        /// </summary>
        public string OrderLine { get; set; }
        /// <summary>
        /// 制造商/代工厂料号
        /// </summary>
        public string OEMItemCode { get; set; }
        /// <summary>
        /// 流水码
        /// </summary>
        public string SerialNoStr { get; set; }
        /// <summary>
        /// 流水码
        /// </summary>
        public int? SerialNo { get => BarcodeAnalysis.GetSerialNo(SerialNoStr); }
 
        /// <summary>
        /// 单位
        /// </summary>
        public string Unit { get; set; }
 
        /// <summary>
        /// 数量
        /// </summary>
        public string QtyStr { get; set; }
        /// <summary>
        /// 数量
        /// </summary>
        public double? Qty { get => BarcodeAnalysis.GetDouble(QtyStr); }
 
        /// <summary>
        /// 毛重
        /// </summary>
        public string GrossWeightStr { get; set; }
        /// <summary>
        /// 毛重
        /// </summary>
        public double? GrossWeight { get => BarcodeAnalysis.GetDouble(GrossWeightStr); }
        /// <summary>
        /// 仓库
        /// </summary>
        public string Warehouse { get; set; }
        /// <summary>
        /// 储位
        /// </summary>
        public string Location { get; set; }
 
        /// <summary>
        /// 条码分析异常信息
        /// </summary>
        public Exception AnalyseException { get; set; }
        /// <summary>
        /// 条码是否分析异常
        /// </summary>
        public bool IsException { get => !AnalyseException.IsNullOrEmpty(); }
 
        /// <summary>
        /// 条码是否合法
        /// </summary>
        public bool IsRegular { get => Type != Types.Illegal; }
 
        /// <summary>
        /// 条码是否原材料条码
        /// </summary>
        public bool IsMaterialSN { get => !(new List<ItemTypes>() { ItemTypes.RawMaterial }).Contains(ItemType); }
 
        /// <summary>
        /// 标签重打流水码字符
        /// </summary>
        public string ReprintNoStr { get; set; }
        /// <summary>
        /// 标签重打流水码
        /// </summary>
        public int? ReprintNo => BarcodeAnalysis.GetReprintNo(ReprintNoStr);
        /// <summary>
        /// 标签重打一级业务编码
        /// </summary>
        public string ReprintBizCode1 { get; set; }
        /// <summary>
        /// 标签重打二级业务编码
        /// </summary>
        public string ReprintBizCode2 { get; set; }
        /// <summary>
        /// 是否重打标签
        /// </summary>
        public bool IsReprintSn => SN.Contains("_");
 
        /// <summary>
        /// 是否截料重打标签
        /// </summary>
        public bool IsCutSn => ReprintBizCode1 == "C";
 
        /// <summary>
        /// 是否退料重打标签
        /// </summary>
        public bool IsReturnSn => ReprintBizCode1 == "T";
        /// <summary>
        /// 是否二维码
        /// </summary>
        public bool IsQRCode => MetaSn.Contains(",");
 
 
        public enum Types
        {
            /// <summary>
            /// 非法条码
            /// </summary>
            [Description("非法条码")]
            Illegal,
            /// <summary>
            /// 唯一ID
            /// </summary>
            [Description("最小包装标签")]
            MiniPackage,
            /// <summary>
            /// 中包标签
            /// </summary>
            [Description("内包装标签")]
            InnerPackage,
            /// <summary>
            /// 外包标签
            /// </summary>
            [Description("外包装标签")]
            OuterPackage,
            /// <summary>
            /// 栈板条码
            /// </summary>
            [Description("栈板条码")]
            Pallet,
            /// <summary>
            /// 其他标签条码
            /// </summary>
            [Description("其他标签")]
            Other,
        }
 
        public enum ItemTypes
        {
            [Description("未知类型")]
            Unknown,
            [Description("湿敏元件")]
            HumidityMaterial,
            [Description("半成品")]
            SemiProduct,
            [Description("SMT半成品")]
            SmtSemiProduct,
            [Description("DIP半成品")]
            DipSemiProduct,
            [Description("成品")]
            Product,
            [Description("PCB")]
            PCB,
            [Description("辅料")]
            SubsidiaryMaterial,
            [Description("原材料")]
            RawMaterial,
        }
 
        /// <summary>
        /// 条码分析
        /// </summary>
        /// <param name="metaSn"></param>
        public void Analyse(string metaSn)
        {
            SN = metaSn;
            Type = Types.Illegal;
            ItemType = ItemTypes.Unknown;
            AnalyseException = null;
            try
            {
                foreach (var rule in Analyses)
                {
                    var result = rule.Analyse(this);
                    if (result.IsSuccessed)
                    {
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                AnalyseException = ex;
            }
        }
 
        /// <summary>
        /// 是否与传入的条码相同的生产日期
        /// </summary>
        /// <param name="sn"></param>
        /// <returns></returns>
        public bool IsSameProdDate(string sn)
        {
            return (ProdDate ?? DateTime.MaxValue).Date == ((new Barcode(sn)).ProdDate ?? DateTime.MinValue).Date;
        }
 
        /// <summary>
        /// 是否小于等于传入的条码的生产日期
        /// </summary>
        /// <param name="sn"></param>
        /// <returns></returns>
        public bool IsPriorProdDate(string sn)
        {
            return (ProdDate ?? DateTime.MaxValue).Date <= ((new Barcode(sn)).ProdDate ?? DateTime.MinValue).Date;
        }
 
        ///// <summary>
        ///// 条码分析
        ///// </summary>
        ///// <param name="metaSn"></param>
        //public void Analyse(string metaSn)
        //{
        //    SN = metaSn;
        //    Type = Types.Illegal;
        //    ItemType = ItemTypes.Unknown;
        //    AnalyseException = null;
        //    try
        //    {
        //        if (!string.IsNullOrEmpty(SN))
        //        {
        //            string[] parts = (metaSn ?? "").Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
        //            if (parts.Length > 1 && parts.Any(q => q.StartsWith("S")))
        //            {
        //                foreach (var part in parts.OrderBy(q => q.FirstOrDefault()))
        //                {
        //                    var param = part.First().ToString().ToUpper();
        //                    var content = part.Substring(1);
        //                    switch (param)
        //                    {
        //                        case "S"://PAG ID (SN)
        //                            var snParts = (content ?? "").Split(new string[] { "_" }, StringSplitOptions.RemoveEmptyEntries);
        //                            SN = content;
        //                            Type = Types.Other;
        //                            if (SN.StartsWith("C") && SN.Length > 14)
        //                            {
        //                                SupplierCode = SN.Substring(1, 5);
        //                                PrintDateStr = SN.Substring(6, 8);
        //                                SerialNoStr = SN.Substring(14);
        //                                if (ProdDate != null && SerialNo != null)
        //                                {
        //                                    Type = Types.Middle;
        //                                    ItemType = ItemTypes.RawMaterial;
        //                                }
        //                            }
        //                            else if (SN.StartsWith("P") && SN.Length > 14)
        //                            {
        //                                SupplierCode = SN.Substring(1, 5);
        //                                PrintDateStr = SN.Substring(6, 8);
        //                                SerialNoStr = SN.Substring(14);
        //                                if (ProdDate != null && SerialNo != null)
        //                                {
        //                                    Type = Types.Outer;
        //                                    ItemType = ItemTypes.RawMaterial;
        //                                }
        //                            }
        //                            else if (SN.Length > 13)
        //                            {
        //                                SupplierCode = SN.Substring(0, 5);
        //                                PrintDateStr = SN.Substring(5, 8);
        //                                SerialNoStr = SN.Substring(13);
        //                                if (ProdDate != null && SerialNo != null)
        //                                {
        //                                    Type = Types.Small;
        //                                    ItemType = ItemTypes.RawMaterial;
        //                                }
        //                            }
        //                            //是否重打判断
        //                            if(snParts.Count() >1 && snParts[1].Length > 2)
        //                            {
        //                                ReprintBizCode1 = snParts[1][0].ToString();
        //                                ReprintBizCode2 = snParts[1][1].ToString();
        //                                ReprintNoStr = snParts[1].Substring(2);
        //                            }
        //                            break;
        //                        case "V"://送货单号
        //                            OrderNo = content;
        //                            break;
        //                        case "P"://物料编码
        //                            ItemCode = content;
        //                            break;
        //                        case "N"://物料编码
        //                            ItemName = content;
        //                            break;
        //                        case "Q"://物料数量
        //                            QtyStr = content;
        //                            break;
        //                        case "U"://物料单位
        //                            Unit = content;
        //                            break;
        //                        case "M"://制造商料号
        //                            OEMItemCode = content;
        //                            break;
        //                        case "D"://生产日期
        //                            DateCode = content;
        //                            ProdDateStr = content;
        //                            break;
        //                        case "L"://生产批次
        //                            LotNo = content;
        //                            break;
        //                        case "G"://物料毛重
        //                            GrossWeightStr = content;
        //                            break;
        //                        default:
        //                            break;
        //                    }
        //                }
        //            }
        //            else if(SN.Length > 10)
        //            {
        //                var snParts = (SN ?? "").Split(new string[] { "_" }, StringSplitOptions.RemoveEmptyEntries);
        //                Type = Types.Other;
        //                if (SN.StartsWith("C") && SN.Length > 14)
        //                {
        //                    SupplierCode = SN.Substring(1, 5);
        //                    PrintDateStr = SN.Substring(6, 8);
        //                    SerialNoStr = SN.Substring(14);
        //                    if (ProdDate != null && SerialNo != null)
        //                    {
        //                        Type = Types.Middle;
        //                        ItemType = ItemTypes.RawMaterial;
        //                    }
        //                }
        //                else if (SN.StartsWith("P") && SN.Length > 14)
        //                {
        //                    SupplierCode = SN.Substring(1, 5);
        //                    PrintDateStr = SN.Substring(6, 8);
        //                    SerialNoStr = SN.Substring(14);
        //                    if (ProdDate != null && SerialNo != null)
        //                    {
        //                        Type = Types.Outer;
        //                        ItemType = ItemTypes.RawMaterial;
        //                    }
        //                }
        //                else if (SN.Length > 13)
        //                {
        //                    SupplierCode = SN.Substring(0, 5);
        //                    PrintDateStr = SN.Substring(5, 8);
        //                    SerialNoStr = SN.Substring(13);
        //                    if (ProdDate != null && SerialNo != null)
        //                    {
        //                        Type = Types.Small;
        //                        ItemType = ItemTypes.RawMaterial;
        //                    }
        //                }
        //                //是否重打判断
        //                if (snParts.Count() > 1 && snParts[1].Length > 2)
        //                {
        //                    ReprintBizCode1 = snParts[1][0].ToString();
        //                    ReprintBizCode2 = snParts[1][1].ToString();
        //                    ReprintNoStr = snParts[1].Substring(2);
        //                }
        //            }
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        AnalyseException = ex;
        //    }
        //}
 
        ///// <summary>
        ///// 根据字符串返回流水号
        ///// </summary>
        ///// <param name="str"></param>
        ///// <returns></returns>
        //private int? GetSerialNo(string str)
        //{
        //    int serialNo;
        //    try
        //    {
        //        serialNo = Convert.ToInt32(new Regex(@"[^\d]*").Replace(str, ""));
        //    }
        //    catch
        //    {
        //        return null;
        //    }
        //    return serialNo;
        //}
 
        ///// <summary>
        ///// 根据字符串返回重打流水号
        ///// </summary>
        ///// <param name="str"></param>
        ///// <returns></returns>
        //private int? GetReprintNo(string str)
        //{
        //    int qty;
        //    try
        //    {
        //        qty = Convert.ToInt32(str, 16);
        //    }
        //    catch
        //    {
        //        return null;
        //    }
        //    return qty;
        //}
 
        ///// <summary>
        ///// 根据字符串返回数量
        ///// </summary>
        ///// <param name="str"></param>
        ///// <returns></returns>
        //private int? GetInt32(string str)
        //{
        //    int qty;
        //    try
        //    {
        //        qty = Convert.ToInt32(str);
        //    }
        //    catch
        //    {
        //        return null;
        //    }
        //    return qty;
        //}
 
        ///// <summary>
        ///// 根据字符串返回数量
        ///// </summary>
        ///// <param name="str"></param>
        ///// <returns></returns>
        //private double? GetDouble(string str)
        //{
        //    double qty;
        //    try
        //    {
        //        qty = Convert.ToDouble(str);
        //    }
        //    catch
        //    {
        //        return null;
        //    }
        //    return qty;
        //}
 
        ///// <summary>
        ///// 根据日期编码返回生产日期
        ///// </summary>
        ///// <param name="dateStr"></param>
        ///// <returns></returns>
        //public static DateTime? GetDateTime(string dateStr)
        //{
        //    DateTime proddate;
        //    dateStr = dateStr ?? "";
        //    if (dateStr.Length == 8)
        //    {
        //        try
        //        {
        //            //年月日
        //            proddate = DateTime.ParseExact(dateStr, "yyyyMMdd", new CultureInfo("zh-CN", true));
        //        }
        //        catch
        //        {
        //            return null;
        //        }
        //    }
        //    else if (dateStr.Length == 6)
        //    {
        //     try
        //     {
        //         //年月日
        //         proddate = DateTime.ParseExact(dateStr, "yyMMdd", new CultureInfo("zh-CN", true));
        //     }
        //     catch
        //     {
        //         try
        //         {
        //             //年周
        //             var firstDate = Convert.ToDateTime(dateStr.Substring(0, 4) + "-1-1");
        //             int weekNum = Convert.ToInt32(dateStr.Substring(4, 2));
 
        //                int daysOffset = DayOfWeek.Monday - firstDate.DayOfWeek;
        //                DateTime firstSunday = firstDate.AddDays(daysOffset);
        //                var calendar = CultureInfo.CurrentCulture.Calendar;
        //                int firstWeek = calendar.GetWeekOfYear(firstDate, CalendarWeekRule.FirstDay, DayOfWeek.Sunday);
        //                if (firstWeek <= 1)
        //                {
        //                    weekNum -= 1;
        //                }
        //                proddate = firstSunday.AddDays(weekNum * 7 - 1);
        //                if (proddate.Year < firstDate.Year)
        //                {
        //                    proddate = firstDate;
        //                }
        //                if (proddate.Year > firstDate.Year)
        //                {
        //                    proddate = firstDate.AddYears(1).AddDays(-1);
        //                }
        //            }
        //         catch
        //         {
        //             try
        //             {
        //                 //年月
        //                 proddate = DateTime.ParseExact(dateStr, "yyyyMM", new CultureInfo("zh-CN", true));
        //             }
        //             catch
        //             {
        //                    try
        //                    {
        //                        proddate = Convert.ToDateTime(dateStr);
        //                    }
        //                    catch
        //                    {
        //                        //proddate = DateTime.Now;
        //                        return null;
        //                    }
        //                }
        //         }
        //     }
        //    }
        //    else if (dateStr.Length == 4)
        //    {
        //        try
        //        {
        //            //年周
        //            var firstDate = DateTime.ParseExact(dateStr.Substring(0, 2), "yy", CultureInfo.CurrentCulture);
        //            int weekNum = Convert.ToInt32(dateStr.Substring(2, 2));
 
        //            int daysOffset = DayOfWeek.Monday - firstDate.DayOfWeek;
        //            DateTime firstSunday = firstDate.AddDays(daysOffset);
        //            var calendar = CultureInfo.CurrentCulture.Calendar;
        //            int firstWeek = calendar.GetWeekOfYear(firstDate, CalendarWeekRule.FirstDay, DayOfWeek.Sunday);
        //            if (firstWeek <= 1)
        //            {
        //                weekNum -= 1;
        //            }
        //            proddate = firstSunday.AddDays(weekNum * 7 - 1);
        //            if (proddate.Year < firstDate.Year)
        //            {
        //                proddate = firstDate;
        //            }
        //            if (proddate.Year > firstDate.Year)
        //            {
        //                proddate = firstDate.AddYears(1).AddDays(-1);
        //            }
        //        }
        //        catch
        //        {
        //            try
        //            {
        //                //年月
        //                proddate = DateTime.ParseExact(dateStr, "yyMM", new CultureInfo("zh-CN", true));
        //            }
        //            catch
        //            {
        //                try
        //                {
        //                    //年
        //                    proddate = DateTime.ParseExact(dateStr, "yyyy", new CultureInfo("zh-CN", true));
        //                }
        //                catch
        //                {
        //                    try
        //                    {
        //                        proddate = Convert.ToDateTime(dateStr);
        //                    }
        //                    catch
        //                    {
        //                        //无法识别则使用当前时间
        //                        //proddate = DateTime.Now;
        //                        return null;
        //                    }
        //                }
        //            }
        //        }
        //    }
        //    else
        //    {
        //        try
        //        {
        //            proddate = Convert.ToDateTime(dateStr);
        //        }
        //        catch
        //        {
        //            //proddate = DateTime.Now;
        //            return null;
        //        }
        //    }
        //    return proddate;
        //}
 
        ///// <summary>
        ///// 是否与传入的条码相同的生产日期
        ///// </summary>
        ///// <param name="sn"></param>
        ///// <returns></returns>
        //public bool IsSameProdDate(string sn)
        //{
        //    return (ProdDate ?? DateTime.MaxValue).Date == ((new Barcode(sn)).ProdDate ?? DateTime.MinValue).Date;
        //}
 
        ///// <summary>
        ///// 是否小于等于传入的条码的生产日期
        ///// </summary>
        ///// <param name="sn"></param>
        ///// <returns></returns>
        //public bool IsPriorProdDate(string sn)
        //{
        //    return (ProdDate ?? DateTime.MaxValue).Date <= ((new Barcode(sn)).ProdDate ?? DateTime.MinValue).Date;
        //}
 
        ///// <summary>
        ///// 是否非法条码
        ///// </summary>
        ///// <param name="sn"></param>
        ///// <returns></returns>
        //public static bool IsIllegal(string sn)
        //{
        //    return !new Barcode(sn).IsRegular;
        //}
 
        ///// <summary>
        ///// 获取条码类型
        ///// </summary>
        ///// <param name="sn"></param>
        ///// <returns></returns>
        //public static Types GetType(string sn)
        //{
        //    return new Barcode(sn).Type;
        //}
 
        ///// <summary>
        ///// 判断是否重打的条码
        ///// </summary>
        ///// <param name="sn"></param>
        ///// <returns></returns>
        //public static bool IsReprint(string sn)
        //{
        //    return new Barcode(sn).IsReprintSn;
        //}
    }//endClass
}