2024-04-12 20:16:02 +04:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using PlumbingRepairContracts.BindingModels;
|
|
|
|
|
using PlumbingRepairContracts.BusinessLogicsContracts;
|
|
|
|
|
using PlumbingRepairContracts.SearchModels;
|
|
|
|
|
using PlumbingRepairDataModels.Models;
|
|
|
|
|
|
|
|
|
|
namespace PlumbingRepairView
|
|
|
|
|
{
|
|
|
|
|
public partial class FormShop : Form
|
|
|
|
|
{
|
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
|
|
|
|
|
private readonly IShopLogic _logic;
|
|
|
|
|
|
|
|
|
|
private int? _id;
|
|
|
|
|
|
|
|
|
|
private Dictionary<int, (IWorkModel, int)> _shopWorks;
|
|
|
|
|
|
|
|
|
|
public int Id { set { _id = value; } }
|
|
|
|
|
|
|
|
|
|
public FormShop(ILogger<FormShop> logger, IShopLogic logic)
|
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_logic = logic;
|
|
|
|
|
_shopWorks = new Dictionary<int, (IWorkModel, int)>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void FormShop_Load(object sender, EventArgs e)
|
2024-04-12 20:55:05 +04:00
|
|
|
|
{
|
2024-04-12 20:16:02 +04:00
|
|
|
|
if (_id.HasValue)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation("Загрузка изделия");
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var view = _logic.ReadElement(new ShopSearchModel { Id = _id.Value });
|
|
|
|
|
if (view != null)
|
|
|
|
|
{
|
|
|
|
|
textBoxName.Text = view.ShopName;
|
|
|
|
|
textBoxAddress.Text = view.Address;
|
2024-04-12 20:55:05 +04:00
|
|
|
|
textBoxMaxCount.Text = view.maxCountWorks.ToString();
|
2024-04-12 20:16:02 +04:00
|
|
|
|
dateTimePickerOpening.Value = view.DateOpening;
|
|
|
|
|
_shopWorks = view.ShopWorks ?? new Dictionary<int, (IWorkModel, int)>();
|
|
|
|
|
LoadData();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(ex, "Ошибка загрузки магазина");
|
|
|
|
|
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void LoadData()
|
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation("Загрузка изделий магазина");
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (_shopWorks != null)
|
|
|
|
|
{
|
|
|
|
|
dataGridView.Rows.Clear();
|
|
|
|
|
foreach (var pc in _shopWorks)
|
|
|
|
|
{
|
|
|
|
|
dataGridView.Rows.Add(new object[] { pc.Key, pc.Value.Item1.WorkName, pc.Value.Item2 });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2024-04-12 20:55:05 +04:00
|
|
|
|
_logger.LogError(ex, "Ошибка загрузки работ магазина");
|
2024-04-12 20:16:02 +04:00
|
|
|
|
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
private void ButtonSave_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(textBoxName.Text))
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show("Заполните название", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (string.IsNullOrEmpty(textBoxAddress.Text))
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show("Заполните адрес", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-04-12 20:55:05 +04:00
|
|
|
|
if (string.IsNullOrEmpty(textBoxMaxCount.Text))
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show("Заполните максимальное количество работ", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-04-12 20:16:02 +04:00
|
|
|
|
if (string.IsNullOrEmpty(dateTimePickerOpening.Text))
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show("Заполните дату", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
_logger.LogInformation("Сохранение магазина");
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var model = new ShopBindingModel
|
|
|
|
|
{
|
|
|
|
|
Id = _id ?? 0,
|
|
|
|
|
ShopName = textBoxName.Text,
|
|
|
|
|
Address = textBoxAddress.Text,
|
|
|
|
|
DateOpening= dateTimePickerOpening.Value,
|
2024-04-12 20:55:05 +04:00
|
|
|
|
maxCountWorks = Convert.ToInt32(textBoxMaxCount.Text),
|
2024-04-12 20:16:02 +04:00
|
|
|
|
ShopWorks = _shopWorks
|
|
|
|
|
};
|
|
|
|
|
var operationResult = _id.HasValue ? _logic.Update(model) : _logic.Create(model);
|
|
|
|
|
if (!operationResult)
|
|
|
|
|
{
|
|
|
|
|
throw new Exception("Ошибка при сохранении. Дополнительная информация в логах.");
|
|
|
|
|
}
|
|
|
|
|
MessageBox.Show("Сохранение прошло успешно", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
|
|
|
|
DialogResult = DialogResult.OK;
|
|
|
|
|
Close();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(ex, "Ошибка сохранения магазина");
|
|
|
|
|
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ButtonCancel_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
DialogResult = DialogResult.Cancel;
|
|
|
|
|
Close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|