PIbd-21_MasenkinMS_Aircraft.../AircraftPlant/AircraftPlantView/FormMessage.cs
2024-05-06 03:07:09 +04:00

143 lines
4.5 KiB
C#

using AircraftPlantBusinessLogic.MailWorker;
using AircraftPlantContracts.BindingModels;
using AircraftPlantContracts.BusinessLogicsContracts;
using AircraftPlantContracts.SearchModels;
using AircraftPlantContracts.ViewModels;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace AircraftPlantView
{
/// <summary>
/// Форма для отправки письма
/// </summary>
public partial class FormMessage : Form
{
/// <summary>
/// Логгер
/// </summary>
private readonly ILogger _logger;
/// <summary>
/// Бизнес-логика для писем
/// </summary>
private readonly IMessageInfoLogic _messageLogic;
/// <summary>
/// Бизнес-логика для отправки писем
/// </summary>
private readonly AbstractMailWorker _mailWorker;
/// <summary>
/// Письмо
/// </summary>
private MessageInfoViewModel? _message;
/// <summary>
/// Идентификатор сообщения
/// </summary>
public string MessageId { get; set; } = string.Empty;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="logger"></param>
/// <param name="messageLogic"></param>
/// <param name="mailWorker"></param>
public FormMessage(ILogger<FormMessage> logger, IMessageInfoLogic messageLogic, AbstractMailWorker mailWorker)
{
InitializeComponent();
_logger = logger;
_messageLogic = messageLogic;
_mailWorker = mailWorker;
}
/// <summary>
/// Загрузка данных
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void FormMessage_Load(object sender, EventArgs e)
{
try
{
_message = _messageLogic.ReadElement(new MessageInfoSearchModel
{
MessageId = MessageId
});
if (_message != null)
{
textBoxSubject.Text = _message.Subject;
textBoxBody.Text = _message.Reply;
if (!_message.IsChecked)
{
_messageLogic.Update(new MessageInfoBindingModel
{
MessageId = MessageId,
IsChecked = true,
Reply = _message.Reply
});
}
}
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения собщения");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
/// <summary>
/// Кнопка "Отправить"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonSend_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBoxBody.Text))
{
MessageBox.Show("Заполните ответ", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
_mailWorker.MailSendAsync(new()
{
MailAddress = _message!.SenderName,
Subject = _message.Subject,
Text = textBoxBody.Text,
});
_messageLogic.Update(new MessageInfoBindingModel
{
MessageId = MessageId,
IsChecked = true,
Reply = textBoxBody.Text
});
MessageBox.Show("Отправка прошла успешно", "Сообщение", MessageBoxButtons.OK);
DialogResult = DialogResult.OK;
Close();
}
/// <summary>
/// Кнопка "Отмена"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonCancel_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel;
Close();
}
}
}