using AircraftPlantBusinessLogic.BusinessLogics; using AircraftPlantContracts.BindingModels; using AircraftPlantContracts.BusinessLogicsContracts; using AircraftPlantContracts.SearchModels; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace AircraftPlantBusinessLogic.MailWorker { /// /// Абстрактный класс для работы с письмами /// public abstract class AbstractMailWorker { private readonly ILogger _logger; /// /// Логин для доступа к почтовому сервису /// protected string _mailLogin = string.Empty; /// /// Пароль для доступа к почтовому сервису /// protected string _mailPassword = string.Empty; /// /// Хост SMTP-клиента /// protected string _smtpClientHost = string.Empty; /// /// Порт SMTP-клиента /// protected int _smtpClientPort; /// /// Хост протокола POP3 /// protected string _popHost = string.Empty; /// /// Порт протокола POP3 /// protected int _popPort; /// /// Бизнес-логика для писем /// private readonly IMessageInfoLogic _messageInfoLogic; /// /// Бизнес-логика для клиентов /// private readonly IClientLogic _clientLogic; /// /// Конструктор /// /// /// public AbstractMailWorker(ILogger logger, IMessageInfoLogic messageInfoLogic, IClientLogic clientLogic) { _logger = logger; _messageInfoLogic = messageInfoLogic; _clientLogic = clientLogic; } /// /// Настроить почтовый сервис /// /// public void MailConfig(MailConfigBindingModel config) { _mailLogin = config.MailLogin; _mailPassword = config.MailPassword; _smtpClientHost = config.SmtpClientHost; _smtpClientPort = config.SmtpClientPort; _popHost = config.PopHost; _popPort = config.PopPort; _logger.LogDebug("Config: {login}, {password}, {clientHost}, {clientPort}, {popHost}, {popPort}", _mailLogin, _mailPassword, _smtpClientHost, _smtpClientPort, _popHost, _popPort); } /// /// Проверить и отправить письмо /// /// public async void MailSendAsync(MailSendInfoBindingModel info) { if (string.IsNullOrEmpty(_mailLogin) || string.IsNullOrEmpty(_mailPassword)) { return; } if (string.IsNullOrEmpty(_smtpClientHost) || _smtpClientPort == 0) { return; } if (string.IsNullOrEmpty(info.MailAddress) || string.IsNullOrEmpty(info.Subject) || string.IsNullOrEmpty(info.Text)) { return; } _logger.LogDebug("Send Mail: {To}, {Subject}", info.MailAddress, info.Subject); await SendMailAsync(info); } /// /// Проверить новые письма /// public async void MailCheck() { if (string.IsNullOrEmpty(_mailLogin) || string.IsNullOrEmpty(_mailPassword)) { return; } if (string.IsNullOrEmpty(_popHost) || _popPort == 0) { return; } if (_messageInfoLogic == null) { return; } var list = await ReceiveMailAsync(); _logger.LogDebug("Check Mail: {Count} new mails", list.Count); foreach (var mail in list) { mail.ClientId = _clientLogic.ReadElement(new ClientSearchModel { Email = mail.SenderName })?.Id; _messageInfoLogic.Create(mail); } } /// /// Отправить письмо /// /// /// protected abstract Task SendMailAsync(MailSendInfoBindingModel info); /// /// Получить все письма /// /// protected abstract Task> ReceiveMailAsync(); } }