服务端的TigerApi 框架,基于.NET6 2024 版本
Rodney Chen
2025-03-24 4ee6d2449e47c49ca31c3083e2a08cbb88e0935b
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
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;
using static Tiger.Business.WMS.Barcode;
 
namespace Tiger.Business.WMS
{
    /// <summary>
    /// 条码分析基类
    /// </summary>
    public class BarcodeAnalysis
    {
        public Barcode Barcode;
        /// <summary>
        /// 条码分析
        /// </summary>
        /// <param name="metaSn"></param>
        public virtual Result<Barcode> Analyse(Barcode barcode)
        {
            var result = new Result<Barcode>(Result.Flags.Failed);
            Barcode = barcode;
 
            if (!string.IsNullOrEmpty(Barcode.MetaSn))
            {
                Barcode.QtyStr = "1";
                Barcode.Type = Types.Other;
                Barcode.ItemType = ItemTypes.Unknown;
                result.Flag = Result.Flags.Success;
            }
 
            result.Data = Barcode;
            return result;
        }
 
        /// <summary>
        /// 根据字符串返回流水号
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static 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>
        public static 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>
        public static int? GetInt32(string str)
        {
            int qty;
            try
            {
                qty = Convert.ToInt32(str);
            }
            catch
            {
                return null;
            }
            return qty;
        }
 
        /// <summary>
        /// 根据字符串返回数量
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static 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 static bool IsIllegal(string sn)
        {
            return !new Barcode(sn).IsRegular;
        }
 
        /// <summary>
        /// 获取条码类型
        /// </summary>
        /// <param name="sn"></param>
        /// <returns></returns>
        public static Barcode.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
}