服务端的TigerApi 框架,基于.NET6 2024 版本
Rodney Chen
2024-12-06 9719a7f0ccbb70e4e51a93cbe1733d1424c16f6d
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
using Tiger.Model;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Rhea.Common;
using System.Net;
using System.Linq;
using Newtonsoft.Json;
using Tiger.Business.DbCache;
using System.Text.RegularExpressions;
using System.Diagnostics;
using System.Threading;
using System.Data;
using Tiger.IBusiness;
 
namespace Tiger.Business
{
    /// <summary>
    /// BAS_CODE_RULE 扩展类
    /// </summary>
    public static class BAS_CODE_RULEExtension
    {
        /// <summary>
        /// 规则验证
        /// </summary>
        /// <param name="rule"></param>
        /// <param name="sn"></param>
        /// <returns></returns>
        public static Result Verify(this BAS_CODE_RULE rule, string sn)
        {
            return Cache.CodeRule.Verify(sn, rule);
        }
 
        /// <summary>
        /// 生成条码
        /// </summary>
        /// <param name="rule"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        public static Result Generate(this BAS_CODE_RULE rule, params object?[] args)
        {
            return Cache.CodeRule.Generate(rule.RULE_CODE, args);
        }
 
        /// <summary>
        /// 尝试生成条码,不保存到数据库
        /// </summary>
        /// <param name="rule"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        public static Result TryGenerate(this BAS_CODE_RULE rule, params object?[] args)
        {
            return Cache.CodeRule.TryGenerate(rule.RULE_CODE, args);
        }
    }//endClass
 
    /// <summary>
    /// 条码规则验证类
    /// </summary>
    public class RuleVerifier
    {
        public RuleVerifier(BAS_CODE_RULE rule)
        {
            Rule = rule;
        }
 
        public BAS_CODE_RULE Rule { get; set; }
        /// <summary>
        /// 是否完成验证
        /// </summary>
        public bool isFinish { get; set; } = false;
        /// <summary>
        /// 是否匹配成功
        /// </summary>
        public bool IsMatch { get; set; } = false;
        /// <summary>
        /// 是否验证异常
        /// </summary>
        public bool IsException { get; set; } = false;
        /// <summary>
        /// 验证异常
        /// </summary>
        public Exception MatchException { get; set; }
 
        /// <summary>
        /// 数据库关联验证
        /// </summary>
        public static bool CheckDB(string key, BAS_CODE_DTL ruleDtl)
        {
            return ruleDtl.DATA_TYPE == BAS_CODE_DTL.DATA_TYPEs.DbCheck.GetValue() ? Biz.Db.Queryable(ruleDtl.CHECK_TABLE, "t").Where($"{ruleDtl.CHECK_FIELD} = '{key}'").Any() : true;
        }
 
        /// <summary>
        /// 规则验证
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public RuleVerifier Verify(string code)
        {
            //Debug.WriteLine($"{Rule.RULE_CODE} verify begin");
            IsMatch = true;
            IsException = false;
            isFinish = false;
            try
            {
                var match = Rule.GroupRegex.Match(code);
                IsMatch = match.Success;
                if (IsMatch)
                {
                    foreach (var item in Rule.Details)
                    {
                        item.CodeValue = match.Groups[item.RULE_SEQ.ToString()].Value;
                        //判断流水号是否符合规则
                        if (IsMatch && item.DATA_TYPE == BAS_CODE_DTL.DATA_TYPEs.SerialCode.GetValue())
                        {
                            //按取值范围判断当前流水号是否在范围内
                            IsMatch &= item.SERIAL_MIN <= item.CodeValue.ToInt32() && item.CodeValue.ToInt32() <= item.SERIAL_MAX;
                            //当前流水号是否按设置间隔固定数值
                            IsMatch &= (item.CodeValue.ToInt32() - item.SERIAL_MIN) % item.SERIAL_INTERVAL == 0;
                            //判断当前流水号是否跳过以设置号码结尾的流水号
                            if (!item.SERIAL_IGNORE.IsNullOrEmpty())
                            {
                                var ignoreList = item.SERIAL_IGNORE.Split(',');
                                foreach (var num in ignoreList)
                                {
                                    IsMatch &= !item.CodeValue.EndsWith(num);
                                }
                            }
                        }
                    }
                    //判断关联字段是否符合规则
                    if (IsMatch)
                    {
                        foreach (var item in Rule.Details.Where(q => q.DATA_TYPE == BAS_CODE_DTL.DATA_TYPEs.DbCheck.GetValue()))
                        {
                            IsMatch &= CheckDB(item.CodeValue, item);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                IsMatch = false;
                IsException = true;
                MatchException = ex;
            }
            finally
            {
                isFinish = true;
            }
            //var sleep = new Random().Next(1, 5);
            //Thread.Sleep(sleep * 1000);
            //Debug.WriteLine($"{Rule.RULE_CODE} verify end, verify {(isFinish ? "Finish" : "not finish")}, match {(IsMatch ? "success" : "failed")}, sleep {sleep} seconds");
 
            return this;
        }
    }
 
    /// <summary>
    /// 条码验证类
    /// </summary>
    public class CodeVerifier
    {
        /// <summary>
        /// 条码验证类
        /// </summary>
        /// <param name="code"></param>
        /// <param name="predicate"></param>
        public CodeVerifier(string code, Func<BAS_CODE_RULE, bool> predicate = null)
        {
            Code = code;
            Predicate = predicate;
        }
 
        public string Code { get; set; }
        public Func<BAS_CODE_RULE, bool> Predicate { get; set; }
        public Expression<Func<BAS_CODE_RULE, bool>> Expression { get; set; }
        public Exception VerifyException { get; set; }
        private List<RuleVerifier> _Verifiers = new();
        public List<RuleVerifier> Verifiers => _Verifiers.IsNullOrEmpty() ? Cache.CodeRule.Rules.WhereIF(!Predicate.IsNullOrEmpty(), Predicate).Select(q => new RuleVerifier(q)).ToList() : _Verifiers;
        /// <summary>
        /// 是否完成验证
        /// </summary>
        public bool isFinish => !Verifiers.Any(q => !q.isFinish);
        /// <summary>
        /// 是否匹配成功
        /// </summary>
        public bool IsMatch => Verifiers.Any(q => q.IsMatch);
        /// <summary>
        /// 耗费时间
        /// </summary>
        public double ElapsedTime { get; set; }
        /// <summary>
        /// 匹配成功的规则
        /// </summary>
        public List<RuleVerifier> Matches => Verifiers.Where(q => q.IsMatch).OrderBy(q => q.Rule.PRIORITY).ToList();
 
        /// <summary>
        /// 验证所有规则
        /// </summary>
        public void DoVerify()
        {
            var begin = DateTime.Now;
            ElapsedTime = 0;
            //Debug.WriteLine($"{Code} verify begin at {begin:T}");
            _Verifiers = Cache.CodeRule.Rules.WhereIF(!Predicate.IsNullOrEmpty(), Predicate).Select(q => new RuleVerifier(q)).ToList();
            foreach (var item in Verifiers)
            {
                Work.DoAsync(() => { item.Verify(Code); });
                //item.Verify(Code);
            }
            while (!isFinish) ;
            ElapsedTime = (DateTime.Now - begin).TotalSeconds;
            //Debug.WriteLine($"{Code} verify end, elapsed total {ElapsedTime} seconds");
        }
    }
}