203 lines
8.1 KiB
C#
203 lines
8.1 KiB
C#
|
using BeautySalonContracts.BindingModels;
|
|||
|
using BeautySalonContracts.BusinessLogicsContracts;
|
|||
|
using BeautySalonContracts.SearchModels;
|
|||
|
using BeautySalonDataModels.Models;
|
|||
|
using Microsoft.Extensions.Logging;
|
|||
|
|
|||
|
namespace BeautySalon
|
|||
|
{
|
|||
|
public partial class FormMaster : Form
|
|||
|
{
|
|||
|
private readonly ILogger _logger;
|
|||
|
private readonly IMasterLogic _logic;
|
|||
|
private int? _id;
|
|||
|
private Dictionary<int, (IServiceModel, int)> _masterServices;
|
|||
|
public int Id { set { _id = value; } }
|
|||
|
public FormMaster(ILogger<FormMaster> logger, IMasterLogic logic)
|
|||
|
{
|
|||
|
InitializeComponent();
|
|||
|
_logger = logger;
|
|||
|
_logic = logic;
|
|||
|
_masterServices = new Dictionary<int, (IServiceModel, int)>();
|
|||
|
}
|
|||
|
|
|||
|
private void LoadData()
|
|||
|
{
|
|||
|
_logger.LogInformation("Загрузка услуг мастера");
|
|||
|
try
|
|||
|
{
|
|||
|
if (_masterServices != null)
|
|||
|
{
|
|||
|
dataGridView.Rows.Clear();
|
|||
|
foreach (var ec in _masterServices)
|
|||
|
{
|
|||
|
dataGridView.Rows.Add(new object[] { ec.Key, ec.Value.Item1.ServiceName, ec.Value.Item2 });
|
|||
|
}
|
|||
|
textBoxWorkingHours.Text = CalcWorkingHours().ToString();
|
|||
|
}
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
_logger.LogError(ex, "Ошибка загрузки услуг мастера");
|
|||
|
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
private void ButtonAdd_Click(object sender, EventArgs e)
|
|||
|
{
|
|||
|
var service = Program.ServiceProvider?.GetService(typeof(FormMasterService));
|
|||
|
if (service is FormMasterService form)
|
|||
|
{
|
|||
|
if (form.ShowDialog() == DialogResult.OK)
|
|||
|
{
|
|||
|
if (form.ServiceModel == null)
|
|||
|
{
|
|||
|
return;
|
|||
|
}
|
|||
|
_logger.LogInformation("Добавление новой услуги: {ServiceName} - {Time}", form.ServiceModel.ServiceName, form.Time);
|
|||
|
if (_masterServices.ContainsKey(form.Id))
|
|||
|
{
|
|||
|
_masterServices[form.Id] = (form.ServiceModel, form.Time);
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
_masterServices.Add(form.Id, (form.ServiceModel, form.Time));
|
|||
|
}
|
|||
|
LoadData();
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
private void ButtonUpd_Click(object sender, EventArgs e)
|
|||
|
{
|
|||
|
if (dataGridView.SelectedRows.Count == 1)
|
|||
|
{
|
|||
|
var service = Program.ServiceProvider?.GetService(typeof(FormMasterService));
|
|||
|
if (service is FormMasterService form)
|
|||
|
{
|
|||
|
int id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells[0].Value);
|
|||
|
form.Id = id;
|
|||
|
form.Time = _masterServices[id].Item2;
|
|||
|
if (form.ShowDialog() == DialogResult.OK)
|
|||
|
{
|
|||
|
if (form.ServiceModel == null)
|
|||
|
{
|
|||
|
return;
|
|||
|
}
|
|||
|
_logger.LogInformation("Изменение услуги: { ServiceName} - { Time}", form.ServiceModel.ServiceName, form.Time);
|
|||
|
_masterServices[form.Id] = (form.ServiceModel, form.Time);
|
|||
|
LoadData();
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
private void ButtonDel_Click(object sender, EventArgs e)
|
|||
|
{
|
|||
|
if (dataGridView.SelectedRows.Count == 1)
|
|||
|
{
|
|||
|
if (MessageBox.Show("Удалить запись?", "Вопрос", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
_logger.LogInformation("Удаление услуги: { ServiceName} - { Time}", dataGridView.SelectedRows[0].Cells[1].Value);
|
|||
|
_masterServices?.Remove(Convert.ToInt32(dataGridView.SelectedRows[0].Cells[0].Value));
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|||
|
}
|
|||
|
LoadData();
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
private void ButtonRef_Click(object sender, EventArgs e)
|
|||
|
{
|
|||
|
LoadData();
|
|||
|
}
|
|||
|
|
|||
|
private void ButtonSave_Click(object sender, EventArgs e)
|
|||
|
{
|
|||
|
if (string.IsNullOrEmpty(textBoxName.Text))
|
|||
|
{
|
|||
|
MessageBox.Show("Заполните ФИО", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|||
|
return;
|
|||
|
}
|
|||
|
if (_masterServices == null || _masterServices.Count == 0)
|
|||
|
{
|
|||
|
MessageBox.Show("Заполните услуги", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|||
|
return;
|
|||
|
}
|
|||
|
if (string.IsNullOrEmpty(textBoxWorkingHours.Text))
|
|||
|
{
|
|||
|
MessageBox.Show("Заполните рабочее время", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|||
|
return;
|
|||
|
}
|
|||
|
_logger.LogInformation("Сохранение мастера");
|
|||
|
try
|
|||
|
{
|
|||
|
var model = new MasterBindingModel
|
|||
|
{
|
|||
|
Id = _id ?? 0,
|
|||
|
MasterFIO = textBoxName.Text,
|
|||
|
WorkingHours = Convert.ToDouble(textBoxWorkingHours.Text),
|
|||
|
MasterServices = _masterServices
|
|||
|
};
|
|||
|
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();
|
|||
|
}
|
|||
|
|
|||
|
private void FormMaster_Load(object sender, EventArgs e)
|
|||
|
{
|
|||
|
if (_id.HasValue)
|
|||
|
{
|
|||
|
_logger.LogInformation("Загрузка мастера");
|
|||
|
try
|
|||
|
{
|
|||
|
var view = _logic.ReadElement(new MasterSearchModel { Id = _id.Value });
|
|||
|
if (view != null)
|
|||
|
{
|
|||
|
textBoxName.Text = view.MasterFIO;
|
|||
|
textBoxWorkingHours.Text = view.WorkingHours.ToString();
|
|||
|
_masterServices = view.MasterServices ?? new Dictionary<int, (IServiceModel, int)>();
|
|||
|
LoadData();
|
|||
|
}
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
_logger.LogError(ex, "Ошибка загрузки мастера");
|
|||
|
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
private double CalcWorkingHours()
|
|||
|
{
|
|||
|
double workingHours = 0;
|
|||
|
foreach (var elem in _masterServices)
|
|||
|
{
|
|||
|
workingHours += elem.Value.Item1?.Cost ?? 0;
|
|||
|
}
|
|||
|
return Math.Round(workingHours, 2);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|