我正在開發一個讀取電子郵件、從中提取資料并將其列印出來的應用程式DataGrid(作為警報或通知)。資料存盤在資料庫中。
場景如下:
如果同一客戶在特定時間段內(例如 5 分鐘內)未收到新警報,則會收到一封帶有資料的新電子郵件,結果相反(例如,第一個警報失敗,第二個成功 => 否定), 中的警報Datagrid剛剛更新。
我的問題在哪里: 在我要更新警報的部分中,添加了一個新警報,這導致重復并且表格混亂。并且當新的否定警報到達時它不會更新。
注意:我的應用程式更大,為了更好地展示我的問題,示例在 consol 應用程式中。 但是經過一些修改,代碼是一樣的
這是我的代碼:
讀取和提取資料的方法
public void MailKitLib(EmailParser emailParser)
{
using (var client = new ImapClient())
{
using (var cancel = new CancellationTokenSource())
{
client.Connect(emailParser.ServerName, emailParser.Port, emailParser.isSSLuse,
cancel.Token);
client.Authenticate(emailParser.Username, emailParser.Password, cancel.Token);
var inbox = client.Inbox;
inbox.Open(FolderAccess.ReadOnly, cancel.Token);
Console.WriteLine("Total messages: {0}", inbox.Count);
Console.WriteLine("Recent messages: {0}", inbox.Unread);
for (int i = 0; i < inbox.Count; i )
{
var message = inbox.GetMessage(i, cancel.Token);
MyAlert alert = new MyAlert(message.MessageId, message.Date.DateTime, message.Subject, message.Subject);
if (!MyAlert.alerts.Any(x => x.Id.Equals(alert.Id)))
{
MyAlert.alerts.Add(alert);\\Think of it as a method that saves the object to the database
}
CheckForUpdates(MyAlert.alerts[i]);
}
}
client.Disconnect(true);
}
}
檢查更新的方法
public void CheckForUpdates(MyAlert alert)
{
double result = 0;
foreach (var item in MyAlert.alerts.Select(x => x.NameOfCustomer.Equals(alert.NameOfCustomer)))
{
if (item)
{
for (int i = 0; i < MyAlert.alerts.Count(); i )
{
result = MyAlert.alerts[i].Date.Minute - alert.Date.Minute;
Console.WriteLine("Result of:" MyAlert.alerts[i].NameOfCustomer " "
"and" " " alert.NameOfCustomer " " "is:" result);
if (result > 0 && result <= 5)
{
Console.WriteLine("You HAVE TO UPDATE this" " " MyAlert.alerts[i].NameOfAlert);
MyAlert.alerts[i].NameOfAlert = "UPDATED";
break;
}
else
{
Console.WriteLine("You DON'T have to UPDATE this:" " " MyAlert.alerts[i].NameOfAlert);
}
}
}
}
Console.WriteLine("Wave:" MyAlert.alerts.Count);
}
uj5u.com熱心網友回復:
這就是我用 DB 解決這個問題的方法
public double GetInterval(DateTime a, DateTime b)
{
return a.Subtract(b).TotalMinutes;
}
public void FindUpdate(Alert newAlert, Alert matchAlert)
{
//If new Alerts arrives from same customer with same problem till five 5 minutes
if (dAOAlert.GetAll().Any(x => x.Email.Equals(newAlert.Email) && x.Problem.NameOfAlert.Equals(newAlert.Problem.NameOfAlert))
&&
GetInterval(newAlert.Date, matchAlert.Date) > 0 && GetInterval(newAlert.Date, matchAlert.Date) <= 5)
{
//Find that Alert, and update his variables from alert, else save new alert
var item = dAOAlert.GetAll().FirstOrDefault(x => x.Email.Equals(newAlert.Email) && x.Problem.NameOfAlert.Equals(newAlert.Problem.NameOfAlert));
dAOAlert.Update(item, newAlert);
dAOAlert.Delete(newAlert);
LoadAlertGrid();
}
}
這就是我在 Main 方法中使用它的方式
for (int i = 0; i < inbox.Count; i )
{
var message = inbox.GetMessage(i, cancel.Token);
GetBodyText = message.TextBody;
Problem problem = new Problem(message.MessageId);
if (!dAOProblem.GetAll().Any(x => x.Message_Id.Equals(problem.Message_Id)))
{
dAOProblem.Save(problem);
Alert alert = new Alert(message.MessageId, message.Date.DateTime, message.From.ToString(), 1, problem.Id);
if (!dAOAlert.GetAll().Any(x => x.Id_MimeMessage.Equals(alert.Id_MimeMessage)))
{
dAOAlert.Save(alert);
foreach (var item in dAOAlert.GetAll())
{
FindUpdate(alert, item);
}
LoadAlertGrid();
}
else
{
MessageBox.Show("Duplicate");
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/504303.html
