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
{
///
/// 模板列表
///
static List _Templates;
static Thread _auditThread;//审核线程
static Thread _templateThread;//模板线程
static List tempTaskList=new List();//不处理的列表
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();
}
}
///
/// 审核线程
///
public static void AuditThread()
{
Console.WriteLine($"审核处理线程启动...线程ID:{Thread.CurrentThread.ManagedThreadId}");
while(true)
{
List 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);
}
}
///
/// 模板处理
///
public static void TemplateThread()
{
Log4netService.Debug($"模板处理线程启动...线程ID:{Thread.CurrentThread.ManagedThreadId}");
while (true)
{
Log4netService.Debug($"模板处理");
List tmpList = new List();
List 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_Templates = SQLHelper.Instance.GetTemplates();
if (a_Templates.Count != _Templates.Count)
{
Log4netService.Debug($"模板更新成功-{_Templates.Count}");
_Templates = a_Templates;
}
Thread.Sleep(1000 * 60);
}
}
///
/// 清理列表
///
///
///
private static void DealClean(object sender, System.Timers.ElapsedEventArgs e)
{
tempTaskList.Clear();
Log4netService.Debug("清理列表");
}
}
}