Exemple #1
0
 private void btAdd_Click(object sender, EventArgs e)
 {
     var inp = new InputSingleLine {Text = @"Добавление нового списка рассылки"};
     if (inp.ShowDialog(this)==DialogResult.OK)
     {
         gvReceivers.EndEdit();
         var notifyList = new NotifyReceiversList() {Id = Guid.NewGuid(), Description = inp.InputLine.Text};
         _receiversLists.Add(notifyList);
         cbReceiversLists.DataSource = _receiversLists.ToList();
         cbReceiversLists.SelectedValue = notifyList.Id;
     }
 }
        public void SendNotify(Event notifiedEvent, RequestAction action,NotifyReceiversList receiversList,object tag=null)
        {
            MiniSplash_TF sp0 = new MiniSplash_TF(Properties.Resources.load4) {Text=@"Отправка сообщения" , StatusText = @"Отправка почтового уведомления"};
            sp0.WorkingFunction = () =>
            {
                ClearHeaders();
                List<string> fullReceivers = new List<string>();
                List<string> shortReceivers = new List<string>();
                if (receiversList != null)
                {
                    List<NotifyReceiver> receivers = receiversList.NotifyReceivers.ToList();
                    receivers.ForEach(r => { if (r.Required) r.Selected = true; });
                    var selectedReceivers = receivers.Where(r => r.Selected).ToList();
                    if (selectedReceivers.Count == 0)
                        return;
                    fullReceivers = selectedReceivers.Where(r => !r.IsSms).Select(r => r.Receiver).ToList();
                    shortReceivers = selectedReceivers.Where(r => r.IsSms).Select(r => r.Receiver).ToList();
                }
                else
                {
                    if (tag != null)
                    {
                        var data = tag as string[];
                        if (data != null)
                        {
                            fullReceivers.Add(data[0]);
                            if (data.Length == 2)
                                shortReceivers.Add(data[1]);
                        }
                    }

                }
                string shortDepartmentId = string.Empty;
                if (notifiedEvent.Access_Users.Department != null)
                    if (notifiedEvent.Access_Users.Department.ShortId != string.Empty)
                        shortDepartmentId = notifiedEvent.Access_Users.Department.ShortId.Trim() + ";";
                string eventNumber = @"#" + notifiedEvent.Number;
                string eventTypeDescription = ((EventType) notifiedEvent.Type).GetAttributeValue<DescriptionAttribute, string>(x => x.Description);
                string eventIndex = notifiedEvent.EventIndex.ToString();
                int customerId = notifiedEvent.CustomerId.HasValue ? notifiedEvent.CustomerId.Value : 0;
                int firmId = notifiedEvent.FirmId.HasValue ? notifiedEvent.FirmId.Value : 0;
                string[] customerInfo = _dataContexts.GetCustomerDescription2(firmId, customerId);
                string eventCustomerFull = string.Join(",", customerInfo);
                string fullEventText = string.Empty;
                string shortEventText = string.Empty;
                string fullEmailSubject = string.Empty;
                string shortEmailSubject = string.Empty;
                string eventCustomerSide = string.Empty;
                string eventCustomerPort = string.Empty;
                string eventCustomerPortShort = string.Empty;
                if (notifiedEvent.CustomerSide != null)
                {
                    eventCustomerSide = notifiedEvent.CustomerSide.Description;
                    if (notifiedEvent.EquipmentPort != null)
                    {
                        eventCustomerPort = GetPortFullDescription(notifiedEvent.EquipmentPort);
                        eventCustomerPortShort = GetPortShortDescription(notifiedEvent.EquipmentPort);
                    }
                }
                else
                {
                    if (notifiedEvent.EquipmentPort != null)
                    {
                        if (notifiedEvent.EquipmentPort.SideLink.LinkType == (int) SideLinkType.toEquipmentPort)
                            eventCustomerPort = GetPortFullDescription(notifiedEvent.EquipmentPort.SideLink.EquipmentPort);
                        eventCustomerSide = notifiedEvent.EquipmentPort.Equipment.Area.Description;
                        eventCustomerPortShort = GetPortShortDescription(notifiedEvent.EquipmentPort);
                    }
                }
                string eventCustomerContact = notifiedEvent.CustomerInfo;
                string eventDescription = notifiedEvent.Description;
                switch (action)
                {
                    case RequestAction.Transfer:
                    {
                        var user = notifiedEvent.Access_Users;
                        if (user == null)
                            return;
                        fullEmailSubject = @"Номер заявки: " + eventNumber + @" изменена";
                        fullEventText = @"Заявка: " + eventNumber + " передана в департамент [" + user.Department.Description + "]";
                        //+ @"Тип заявки: " + eventTypeDescription + Environment.NewLine
                        //+ @"Вес заявки: " + eventIndex + Environment.NewLine
                        //+ @"Абонент: " + eventCustomerFull + Environment.NewLine
                        //+ (!string.IsNullOrEmpty(eventCustomerSide) ? @"Площадка абонента: " + eventCustomerSide + Environment.NewLine : "")
                        //+ (!string.IsNullOrEmpty(eventCustomerPort) ? @"Порт абонента: " + eventCustomerPort + Environment.NewLine : "")
                        //+ @"Контакт абонента: " + eventCustomerContact + Environment.NewLine
                        //+ @"Текст заявки: " + eventDescription;
                        _headers.Add(new KeyValuePair<string, string>("Message-ID", "<" + notifiedEvent.Id.ToString() + "@transfer.event>"));
                        break;
                    }
                    case RequestAction.Renew:
                    case RequestAction.New:
                    {

                        fullEventText = @"Номер заявки: " + eventNumber + @" Новая заявка" + Environment.NewLine
                                        + @"Тип заявки: " + eventTypeDescription + Environment.NewLine
                                        + @"Вес заявки: " + eventIndex + Environment.NewLine
                                        + @"Абонент: " + eventCustomerFull + Environment.NewLine
                                        + (!string.IsNullOrEmpty(eventCustomerSide) ? @"Площадка абонента: " + eventCustomerSide + Environment.NewLine : "")
                                        + (!string.IsNullOrEmpty(eventCustomerPort) ? @"Порт абонента: " + eventCustomerPort + Environment.NewLine : "")
                                        + @"Контакт абонента: " + eventCustomerContact + Environment.NewLine
                                        + @"Текст заявки: " + eventDescription;

                        fullEmailSubject = eventNumber + ", " + eventCustomerFull + (!string.IsNullOrEmpty(eventCustomerSide) ? ", " + eventCustomerSide : "");
                        shortEventText = eventNumber + ";NEW;" + shortDepartmentId + customerId + ";" + eventIndex + ";" + eventCustomerSide + ";" + eventDescription + ";" + customerInfo.Last();
                        shortEmailSubject = "";
                        _headers.Add(new KeyValuePair<string, string>("Message-ID", "<" + notifiedEvent.Id.ToString() + "@open.event>"));

                        break;
                    }
                    case RequestAction.Edit:
                        break;
                    case RequestAction.Remedy:
                    {
                        string remedyPerson = string.Empty;
                        string remedyData = string.Empty;
                        if (tag != null)
                        {
                            var data = tag as string[];
                            if (data != null)
                            {
                                remedyPerson = data[0];
                                remedyData = data[1];
                            }
                        }
                        fullEventText = @"Номер заявки: " + eventNumber + @" Изменено состояние" + Environment.NewLine
                                        + @"Тип заявки: " + eventTypeDescription + Environment.NewLine
                                        + @"Вес заявки: " + eventIndex + Environment.NewLine
                                        + @"Устранил: " + remedyPerson + @" Выполнены работы: " + remedyData + Environment.NewLine
                                        + @"Абонент: " + eventCustomerFull + Environment.NewLine
                                        + (!string.IsNullOrEmpty(eventCustomerSide) ? @"Площадка абонента: " + eventCustomerSide + Environment.NewLine : "")
                                        + (!string.IsNullOrEmpty(eventCustomerPort) ? @"Порт абонента: " + eventCustomerPort + Environment.NewLine : "")
                                        + @"Текст заявки: " + eventDescription;

                        fullEmailSubject = eventNumber + ", " + eventCustomerFull + (!string.IsNullOrEmpty(eventCustomerSide) ? ", " + eventCustomerSide : "");
                        shortEventText = eventNumber + ";REMEDY;" + shortDepartmentId + customerId + ";" + eventIndex + ";" + (eventCustomerSide) + ";" + remedyPerson + ";" + remedyData + ";" +
                                         eventDescription + ";" + customerInfo.Last();
                        shortEmailSubject = "";
                        _headers.Add(new KeyValuePair<string, string>("Message-ID", "<" + notifiedEvent.Id.ToString() + "@remedy.event>"));
                        _headers.Add(new KeyValuePair<string, string>("References", "<" + notifiedEvent.Id.ToString() + "@open.event>"));
                        _headers.Add(new KeyValuePair<string, string>("In-Reply-To", "<" + notifiedEvent.Id.ToString() + "@open.event>"));
                        break;
                    }
                    case RequestAction.Close:
                    {
                        string closedPerson = notifiedEvent.Access_Users2 != null ? notifiedEvent.Access_Users2.Description : string.Empty;
                        string checkPerson = notifiedEvent.Access_Users1 != null ? notifiedEvent.Access_Users1.Description : string.Empty;
                        string closedDate = notifiedEvent.CloseDate.HasValue ? notifiedEvent.CloseDate.Value.ToShortDateString() : string.Empty;
                        string closedFixer = notifiedEvent.ClosePerson;
                        string closedCheckDate = notifiedEvent.CheckDate.HasValue ? notifiedEvent.CheckDate.Value.ToShortDateString() : string.Empty;
                        fullEmailSubject = eventNumber + ", " + eventCustomerFull + (!string.IsNullOrEmpty(eventCustomerSide) ? ", " + eventCustomerSide : "");
                        fullEventText = @"Номер заявки: " + eventNumber + @" Закрытие заявки" + Environment.NewLine
                                        + @"Тип заявки: " + eventTypeDescription + Environment.NewLine
                                        + @"Абонент: " + eventCustomerFull + Environment.NewLine
                                        + (!string.IsNullOrEmpty(eventCustomerSide) ? @"Площадка абонента: " + eventCustomerSide + Environment.NewLine : "")
                                        + (!string.IsNullOrEmpty(eventCustomerPort) ? @"Порт абонента: " + eventCustomerPort + Environment.NewLine : "")
                                        + @"Закрыл: " + closedPerson + " " + closedDate + Environment.NewLine
                                        + @"Проверил: " + checkPerson + " " + closedDate + Environment.NewLine
                                        + @"Исполнитель: " + closedFixer + " " + closedCheckDate + Environment.NewLine
                                        + @"Отчет: " + notifiedEvent.CloseInfo;
                        shortEventText = eventNumber + ";CLOSE;" + shortDepartmentId + customerId + ";" + eventIndex + ";" + eventCustomerSide + ";" + closedPerson + ";" + eventDescription + ";" +
                                         customerInfo.Last();
                        _headers.Add(new KeyValuePair<string, string>("Message-ID", "<" + notifiedEvent.Id.ToString() + "@close.event>"));
                        _headers.Add(new KeyValuePair<string, string>("References", "<" + notifiedEvent.Id.ToString() + "@open.event>"));
                        _headers.Add(new KeyValuePair<string, string>("In-Reply-To", "<" + notifiedEvent.Id.ToString() + "@open.event>"));
                        break;
                    }
                    case RequestAction.Unknown:
                        break;
                    default:
                        throw new ArgumentOutOfRangeException("action");
                }
                if (fullReceivers.Any())
                {
                    try
                    {
                        if (!string.IsNullOrEmpty(fullEventText))
                        {
                            string emailFrom = @"*****@*****.**";
                            if (SharedAppData.LoggedUser.Email != null && !string.IsNullOrEmpty(SharedAppData.LoggedUser.Email))
                            {
                                emailFrom = SharedAppData.LoggedUser.Email;
                            }
                            SendEmail(_headers, fullReceivers, emailFrom, fullEmailSubject, fullEventText);
                        }
                    }
                    catch (Exception)
                    {
                        throw;
                    }
                }
                if (shortReceivers.Any())
                {
                    try
                    {
                        if (!string.IsNullOrEmpty(shortEventText))
                        {
                            shortEventText = Translator.ToTranslite(shortEventText);
                            shortReceivers.ForEach(receiver =>
                            {
                                MailMessage mailMessage = new MailMessage {From = new MailAddress("*****@*****.**"), Subject = shortEmailSubject, Body = shortEventText};
                                mailMessage.Headers.Add("Return-Path", "*****@*****.**");
                                mailMessage.Headers.Add("X-Original-To", "*****@*****.**");
                                mailMessage.Headers.Add("X-Mailer", "event registrator");
                                mailMessage.ReplyToList.Add("*****@*****.**");
                                mailMessage.To.Add(receiver);
                                SmtpClient client = new SmtpClient("212.90.160.3") {EnableSsl = false, DeliveryMethod = SmtpDeliveryMethod.Network};
                                client.Send(mailMessage);
                                mailMessage.Dispose();
                            });
                        }
                    }
                    catch (Exception)
                    {
                        throw;
                    }
                }
            };
            sp0.ShowDialog(_parentForm);

        }