PIbd-21_MasenkinMS_Aircraft.../AircraftPlant/AircraftPlantBusinessLogic/MailWorker/AbstractMailWorker.cs

161 lines
4.2 KiB
C#
Raw Normal View History

2024-05-04 22:30:27 +04:00
using AircraftPlantBusinessLogic.BusinessLogics;
using AircraftPlantContracts.BindingModels;
using AircraftPlantContracts.BusinessLogicsContracts;
2024-05-06 03:46:17 +04:00
using AircraftPlantContracts.SearchModels;
2024-05-04 22:30:27 +04:00
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AircraftPlantBusinessLogic.MailWorker
{
/// <summary>
/// Абстрактный класс для работы с письмами
/// </summary>
public abstract class AbstractMailWorker
{
private readonly ILogger _logger;
/// <summary>
/// Логин для доступа к почтовому сервису
/// </summary>
protected string _mailLogin = string.Empty;
/// <summary>
/// Пароль для доступа к почтовому сервису
/// </summary>
protected string _mailPassword = string.Empty;
/// <summary>
/// Хост SMTP-клиента
/// </summary>
protected string _smtpClientHost = string.Empty;
/// <summary>
/// Порт SMTP-клиента
/// </summary>
protected int _smtpClientPort;
/// <summary>
/// Хост протокола POP3
/// </summary>
protected string _popHost = string.Empty;
/// <summary>
/// Порт протокола POP3
/// </summary>
protected int _popPort;
/// <summary>
/// Бизнес-логика для писем
/// </summary>
private readonly IMessageInfoLogic _messageInfoLogic;
2024-05-06 03:46:17 +04:00
/// <summary>
/// Бизнес-логика для клиентов
/// </summary>
private readonly IClientLogic _clientLogic;
2024-05-04 22:30:27 +04:00
/// <summary>
/// Конструктор
/// </summary>
/// <param name="logger"></param>
/// <param name="messageInfoLogic"></param>
2024-05-06 03:46:17 +04:00
public AbstractMailWorker(ILogger<AbstractMailWorker> logger, IMessageInfoLogic messageInfoLogic, IClientLogic clientLogic)
2024-05-04 22:30:27 +04:00
{
_logger = logger;
_messageInfoLogic = messageInfoLogic;
2024-05-06 03:46:17 +04:00
_clientLogic = clientLogic;
2024-05-04 22:30:27 +04:00
}
/// <summary>
/// Настроить почтовый сервис
/// </summary>
/// <param name="config"></param>
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);
}
/// <summary>
/// Проверить и отправить письмо
/// </summary>
/// <param name="info"></param>
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);
}
/// <summary>
/// Проверить новые письма
/// </summary>
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)
{
2024-05-06 03:46:17 +04:00
mail.ClientId = _clientLogic.ReadElement(new ClientSearchModel
{
Email = mail.SenderName
})?.Id;
2024-05-04 22:30:27 +04:00
_messageInfoLogic.Create(mail);
}
}
/// <summary>
/// Отправить письмо
/// </summary>
/// <param name="info"></param>
/// <returns></returns>
protected abstract Task SendMailAsync(MailSendInfoBindingModel info);
/// <summary>
/// Получить все письма
/// </summary>
/// <returns></returns>
protected abstract Task<List<MessageInfoBindingModel>> ReceiveMailAsync();
}
}