private IncomeSMS ChangeIncomeSMSStatus(IncomeSMS incomeSMS, IncomeSMS.IncomeSMSStatus Status, SMSContext context) { try { incomeSMS.Status = Status; context.Entry(incomeSMS).State = System.Data.Entity.EntityState.Modified; context.SaveChanges(); } catch (Exception e) { logger.Error("Ошибка записи в базу данных. Не удалось сохранить изменение статуса входящего сообщения.\r\nТекст ошибки:", e); } return incomeSMS; }
private int AddAnswer(int MessageId, int AnswerMessageId, Answer.AnswerSource Source) { Answer answer = new Answer(); try { using (SMSContext context = new SMSContext()) { IncomeSMS queryResult = context.IncomeSMS.FirstOrDefault(x => x.MessageId == MessageId); if (queryResult == null) throw new Exception("Не найдено сообщения с номером" + MessageId); queryResult.IsAnswered = true; answer.IncomeSMS = queryResult; answer.AnswerMessageId = AnswerMessageId; answer.Source = Source; context.Answer.Add(answer); context.SaveChanges(); queryResult.AnswerId = answer.AnswerId; context.Entry(queryResult).State = System.Data.Entity.EntityState.Modified; context.SaveChanges(); } } catch (Exception e) { logger.Error("Ошибка записи в базу данных.Не удалось сохранить ответ на сообщение." + "\r\nТекст ошибки:", e); } return answer.AnswerId; }
private void ChangeIncomeClientSMS(IncomeClientSMS incomeClientSMS, SMSContext context) { try { context.Entry(incomeClientSMS).State = System.Data.Entity.EntityState.Modified; context.SaveChanges(); } catch (Exception e) { logger.Error("Ошибка записи в базу данных.Не удалось сохранить изменения сообщения.\r\nТекст ошибки:", e); } }
public void SetChekedMessages(GsmModem modem) { try { IList<IncomeSMS> incomeSMSs = null; IList<IncomeClientSMS> IncomeClientSMSs = null; using (SMSContext context = new SMSContext()) { incomeSMSs = context.IncomeSMS.Where(x => x.Status == IncomeSMS.IncomeSMSStatus.Received && x.RecipientNumber.Equals(modem.ServicePhone.PhoneNumber)).ToList(); IncomeClientSMSs = context.IncomeClientSMS.Where(x => x.Status == IncomeClientSMS.IncomeClientSMSStatus.Received && x.RecipientNumber.Equals(modem.ServicePhone.PhoneNumber)).ToList(); } if (incomeSMSs != null) { using (SMSContext context = new SMSContext()) { foreach (IncomeSMS incomeSMS in incomeSMSs) { incomeSMS.Status = IncomeSMS.IncomeSMSStatus.Checked; context.Entry(incomeSMS).State = System.Data.Entity.EntityState.Modified; } context.SaveChanges(); } } using (SMSContext context = new SMSContext()) { foreach (IncomeClientSMS incomeClientSMS in IncomeClientSMSs) { incomeClientSMS.Status = IncomeClientSMS.IncomeClientSMSStatus.Checked; context.Entry(incomeClientSMS).State = System.Data.Entity.EntityState.Modified; } context.SaveChanges(); } } catch (Exception e) { logger.Error("Ошибка обработки базы данных. Не удалось обновить статусы сообщений.\r\nТекст ошибки:", e); } }
public ShortMessageCollection ProcessingNewIncomeSMSMessages(ShortMessageCollection messages, GsmModem modem) { IncomeSMSSaveMutex.WaitOne(); ShortMessageCollection notSend = new ShortMessageCollection(); try { using (SMSContext context = new SMSContext()) { ServicePhone ServicePhone = GetServicePhoneById(modem.ServicePhone.ServicePhoneId, context); foreach (ShortMessage message in messages) { bool NotSaved = true; bool isGarbage = true; try { int firstWordLength = message.Text.Contains(" ") ? message.Text.Substring(0, message.Text.IndexOf(" ")).Length : 0; string firstWord = message.Text.Contains(" ") ? message.Text.Substring(0, message.Text.IndexOf(" ")).Replace("\n", "").Replace("\r", "") : null; Client client = GetClientByPhone(message.Sender, context); bool haveFirstWord = !String.IsNullOrEmpty(firstWord); bool ClientSMSAndNotAnswer = false; if (client != null && haveFirstWord) { isGarbage = false; IncomeSMS incomeSMS = GetIncomeSMS(firstWord, client.ClientId, context); if (incomeSMS == null) ClientSMSAndNotAnswer = true; else { message.Recipient = incomeSMS.SenderNumber; message.Sender = ServicePhone.PhoneNumber; message.Text = message.Text.Substring(firstWordLength + 1); message.IsRead = true; if (ClientIsNotBlocked(client)) { IncomeClientSMS incomeClientSMS = SaveIncomeClientSMS(message, ServicePhone, incomeSMS, client, context); if (incomeClientSMS != null) NotSaved = false; int answerId = AddAnswer(incomeSMS.MessageId, incomeClientSMS.IncomeClientSMSId, Answer.AnswerSource.SMS); } } } if ((client == null || ClientSMSAndNotAnswer) && message.Sender != null && message.Sender.Length > 10) { isGarbage = false; client = haveFirstWord ? GetClientByShortKey(firstWord, ServicePhone, context) : null; string text; if (ServicePhone.Type == ServicePhone.PhoneType.Private) { client = GetClientByServicePhone(ServicePhone, context); text = message.Text; } else { text = message.Text.Substring(firstWord.Length + 1); } IncomeSMS incomeSMS = null; incomeSMS = context.IncomeSMS.FirstOrDefault(x => x.SenderNumber.Equals(message.Sender) && x.RecipientNumber.Equals(ServicePhone.PhoneNumber) && DbFunctions.DiffSeconds(x.DateTime, message.SentDate) <= 30 && x.Status != IncomeSMS.IncomeSMSStatus.Sent); if (incomeSMS != null) { incomeSMS.Text += message.Text; context.SaveChanges(); NotSaved = false; message.IsRead = true; } else if (incomeSMS == null && client != null) { if (ClientIsNotBlocked(client)) { incomeSMS = SaveIncomeSMS(client, text, message.Sender, ServicePhone.PhoneNumber, message.SentDate, firstWord, context); if (incomeSMS != null) NotSaved = false; } message.IsRead = true; } } if (NotSaved & !isGarbage) { notSend.Add(message); message.IsRead = true; } } catch { if (NotSaved & !isGarbage) { notSend.Add(message); message.IsRead = true; } } } foreach (ShortMessage message in messages.Where(x => x.IsRead == false)) { bool Saved = false; try { Client client = GetClientByPhone(message.Sender, context); if (client != null) { IncomeClientSMS incomeClientSMS = null; incomeClientSMS = context.IncomeClientSMS.FirstOrDefault(x => x.Client.ClientId == client.ClientId && DbFunctions.DiffSeconds(x.DateTime, message.SentDate) <= 15 && x.Status != IncomeClientSMS.IncomeClientSMSStatus.Sent); if (incomeClientSMS != null) { incomeClientSMS.Text += message.Text; context.Entry(incomeClientSMS).State = System.Data.Entity.EntityState.Modified; context.SaveChanges(); Saved = true; } } else { IncomeSMS incomeSMS = null; incomeSMS = context.IncomeSMS.FirstOrDefault(x => x.SenderNumber.Equals(message.Sender) && x.RecipientNumber.Equals(ServicePhone.PhoneNumber) && DbFunctions.DiffSeconds(x.DateTime, message.SentDate) <= 30 && x.Status != IncomeSMS.IncomeSMSStatus.Sent); if (incomeSMS != null) { incomeSMS.Text += message.Text; context.Entry(incomeSMS).State = System.Data.Entity.EntityState.Modified; context.SaveChanges(); Saved = true; } } } catch { if (!Saved) notSend.Add(message); } } } return notSend; } catch (Exception e) { notSend = messages; return notSend; } finally { IncomeSMSSaveMutex.ReleaseMutex(); } }
private IncomeSMS SaveIncomeSms(IncomeSMS incomeSMS, SMSContext context) { try { if (incomeSMS.MessageId == 0) context.IncomeSMS.Add(incomeSMS); else context.Entry(incomeSMS).State = System.Data.Entity.EntityState.Modified; context.SaveChanges(); } catch (Exception e) { logger.Error("Ошибка чтения из базы данных.Не удалось сохранить входящее сообщения.\r\nТекст ошибки:", e); } return incomeSMS; }
private void SaveIncomeClientWebSendResult(IncomeClientWeb incomeClientWeb, SMSContext context) { try { incomeClientWeb.IsSent = true; context.Entry(incomeClientWeb).State = System.Data.Entity.EntityState.Modified; context.SaveChanges(); } catch (Exception e) { logger.Error("Ошибка записи в базу данных. Не удалось сохранить результат отправки сообщения с сайта.\r\nТекст ошибки:", e); } }
private IncomeClientEmail SaveIncomeClientEmail(IncomeClientEmail incomeClientEmail, SMSContext context) { try { if (incomeClientEmail.IncomeClientEmailId == 0) context.IncomeClientEmail.Add(incomeClientEmail); else context.Entry(incomeClientEmail).State = System.Data.Entity.EntityState.Modified; context.SaveChanges(); return incomeClientEmail; } catch (Exception e) { logger.Error("Ошибка записи в базу данных. Не удалось добавить письмо клиента с ответом.\r\nТекст ошибки:", e); return null; } }