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

153 lines
4.6 KiB
C#

using AircraftPlantContracts.BusinessLogicsContracts;
using AircraftPlantContracts.SearchModels;
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 FormMail : Form
{
/// <summary>
/// Логгер
/// </summary>
private readonly ILogger _logger;
/// <summary>
/// Бизнес-логика для писем
/// </summary>
private readonly IMessageInfoLogic _logic;
/// <summary>
/// Размер страницы пагинации
/// </summary>
private int PageSize = 3;
/// <summary>
/// Текущая страница пагинации
/// </summary>
private int CurrentPage = 1;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="logger"></param>
/// <param name="logic"></param>
public FormMail(ILogger<FormMail> logger, IMessageInfoLogic logic)
{
InitializeComponent();
_logger = logger;
_logic = logic;
}
/// <summary>
/// Загрузка писем
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void FormMail_Load(object sender, EventArgs e)
{
LoadData();
}
/// <summary>
/// Кнопка переключения на предыдущую страницу
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonPrev_Click(object sender, EventArgs e)
{
if (CurrentPage - 1 <= 0)
{
return;
}
CurrentPage--;
LoadData();
}
/// <summary>
/// Кнопка переключения на следующую страницу
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonNext_Click(object sender, EventArgs e)
{
CurrentPage++;
LoadData();
}
/// <summary>
/// Кнопка "Отправить"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonReply_Click(object sender, EventArgs e)
{
if (dataGridView.SelectedRows.Count == 1)
{
var service = Program.ServiceProvider?.GetService(typeof(FormMessage));
if (service is FormMessage form)
{
form.MessageId = Convert.ToString(dataGridView.SelectedRows[0].Cells["MessageId"].Value);
if (form.ShowDialog() == DialogResult.OK)
{
LoadData();
}
}
}
}
/// <summary>
/// Метод загрузки писем
/// </summary>
private void LoadData()
{
try
{
var list = _logic.ReadList(new MessageInfoSearchModel
{
PageSize = PageSize,
CurrentPage = CurrentPage
});
if (list != null && list.Count == 0 && CurrentPage != 1)
{
CurrentPage--;
list = _logic.ReadList(new MessageInfoSearchModel
{
PageSize = PageSize,
CurrentPage = CurrentPage
});
}
if (list != null)
{
dataGridView.DataSource = list;
dataGridView.Columns["MessageId"].Visible = false;
dataGridView.Columns["ClientId"].Visible = false;
dataGridView.Columns["Body"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
}
labelPage.Text = $"Страница {CurrentPage}";
_logger.LogInformation("Загрузка писем");
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка загрузки писем");
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}