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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using AutoCheckSMS.Common;
 
namespace AutoCheckSMS
{
    class Program
    {
        /// <summary>
        /// 模板列表
        /// </summary>
        static List<A_template> _Templates;
        static Thread _auditThread;//审核线程
        static Thread _templateThread;//模板线程
        static List<int> tempTaskList=new List<int>();//不处理的列表
        
        static void Main(string[] args)
        {
            //设置控制台前景/背景色
            Console.ForegroundColor = ConsoleColor.White;
            Console.BackgroundColor = ConsoleColor.DarkBlue;
 
            Console.Title = "【自动审核】AutoCheckSMS";
            Console.SetBufferSize(1000, 1000);
 
 
            System.Timers.Timer timer = new System.Timers.Timer(1000 * 60 * 5);
            timer.Elapsed += new System.Timers.ElapsedEventHandler(DealClean);
            timer.AutoReset = true;
            timer.Enabled = true;
 
            //多线程处理,一个线程处理审核,一个线程处理模板录入等,注意线程间通信
            _Templates = SQLHelper.Instance.GetTemplates();
            Log4netService.Debug($"加载模板...{_Templates.Count}");
 
            if (_auditThread != null)
            {
                _auditThread.Abort();
                _auditThread = null;
            }
            if (_auditThread == null || _auditThread.ThreadState == ThreadState.Aborted || _auditThread.ThreadState == ThreadState.Stopped)
            {
                ThreadStart thr_start_func = new ThreadStart(AuditThread);
                _auditThread = new Thread(thr_start_func);
                _auditThread.Start();
            }
 
 
            if (_templateThread != null)
            {
                _templateThread.Abort();
                _templateThread = null;
            }
            if (_templateThread == null || _templateThread.ThreadState == ThreadState.Aborted || _templateThread.ThreadState == ThreadState.Stopped)
            {
                ThreadStart thr_start_func = new ThreadStart(TemplateThread);
                _templateThread = new Thread(thr_start_func);
                _templateThread.Start();
            }
 
 
 
 
        }
 
 
        /// <summary>
        /// 审核线程
        /// </summary>
        public static void AuditThread()
        {
            Console.WriteLine($"审核处理线程启动...线程ID:{Thread.CurrentThread.ManagedThreadId}");
            while(true)
            {
                List<tbl_sms_task> list = SQLHelper.Instance.GetSmsConent();
                Log4netService.Debug($"获取待审核的记录:{list.Count}");
                if (list.Count > 0)
                {
                    foreach (tbl_sms_task sms in list)
                    {
                        //表示内容已经处理过了
                        if (tempTaskList!=null && tempTaskList.Contains(sms.TaskID))
                        {
                            continue;
                        }
 
                        Log4netService.Debug($"[调试] 当前处理的taskid={sms.TaskID}");
 
                        //线程间通信,如果模板列表清空或正在更新中,这里暂停处理
                        if(_Templates.Count<=0)
                        {
                            Log4netService.Debug($"[模板更新中]-不处理,直至模板线程完成更新...");
                            continue;
                        }
 
                        bool result = Match.Instance.IsMatchByWildcard(_Templates,sms.MsgContent);
                        if (result)
                        {
                            Log4netService.Debug($"{sms.TaskID}-匹配通过:{sms.MsgContent}");
                            //todo更新审核状态
                            SQLHelper.Instance.UpdateSmsTask(2, sms.TaskID);
                            Log4netService.Debug($"审核通过=内容:{sms.MsgContent}");
                        }
                        else
                        {
                            tempTaskList.Add(sms.TaskID);//匹配不到模板,下次不再处理
                        }
 
                    }
                }
                Thread.Sleep(1000*10);
            }
        }
 
 
        /// <summary>
        /// 模板处理
        /// </summary>
        public static void TemplateThread()
        {
            Log4netService.Debug($"模板处理线程启动...线程ID:{Thread.CurrentThread.ManagedThreadId}");
            while (true)
            {
                
                Log4netService.Debug($"模板处理");
                List<string> tmpList = new List<string>();
                List<tbl_sms_task> smsList = SQLHelper.Instance.GetSmsLogContent();
                if (smsList.Count > 0)
                {
                    Log4netService.Debug($"获取到审核内容:{smsList.Count}");
                    foreach (tbl_sms_task sms in smsList)
                    {
                        if (tempTaskList.Contains(sms.TaskID)) continue;//已经处理过了
 
                        //匹配模板-判断模板是否存在
                        if (Match.Instance.IsMatchByWildcard(_Templates, sms.MsgContent))
                        {
                            Console.WriteLine($"模板已经存在-不处理-{sms.MsgContent}");
                            tempTaskList.Add(sms.TaskID);
                            continue;
                        }
 
                        //生成模板
                        string _template = GenerateTemplate.Instance.GenerateTemplates(sms.MsgContent);
                        tmpList.Add(_template);
                    }
                }
 
                //模板入库
                if (tmpList.Count > 0)
                {
                    tmpList = tmpList.Distinct().ToList();
                    foreach (string str in tmpList)
                    {
                        //Log4netService.Debug($"[模板]-内容:{str}");
                        int num = _Templates.FindIndex(c => c.Template == str);
                        if (num >= 0)
                        {
                            continue;
                        }
 
                        //插入数据库
                        int numAdd = SQLHelper.Instance.AddTemplate(str, 0);
                        
                        if (numAdd > 0)
                        {
                            Log4netService.Debug($"[模板插入成功]-内容:{str}");
                        }
                        else
                        {
                            Log4netService.Debug($"[模板插入失败]-内容:{str}");
                        }
                    }
                }
 
 
 
                //模板更新处理
                List<A_template> a_Templates = SQLHelper.Instance.GetTemplates();
                if (a_Templates.Count != _Templates.Count)
                {
                    Log4netService.Debug($"模板更新成功-{_Templates.Count}");
                    _Templates = a_Templates;
                }
 
                Thread.Sleep(1000 * 60);
            }
 
        }
 
 
        /// <summary>
        /// 清理列表
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void DealClean(object sender, System.Timers.ElapsedEventArgs e)
        {
            tempTaskList.Clear();
            Log4netService.Debug("清理列表");
        }
    }
}