2024-02-27 19:44:44 +04:00
|
|
|
|
using AbstractLawFirmDataModels.Models;
|
2024-02-11 18:24:54 +04:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2024-02-27 19:44:44 +04:00
|
|
|
|
using AbstractLawFirmContracts.BusinessLogicsContracts;
|
|
|
|
|
using AbstractLawFirmContracts.SearchModels;
|
|
|
|
|
using AbstractLawFirmContracts.BindingModels;
|
|
|
|
|
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;
|
2024-02-11 18:24:54 +04:00
|
|
|
|
|
2024-02-14 00:11:21 +04:00
|
|
|
|
namespace LawFirmView
|
2024-02-11 18:24:54 +04:00
|
|
|
|
{
|
2024-02-14 00:11:21 +04:00
|
|
|
|
public partial class FormDocument : Form
|
2024-02-11 18:24:54 +04:00
|
|
|
|
{
|
|
|
|
|
private readonly ILogger _logger;
|
2024-02-14 00:11:21 +04:00
|
|
|
|
private readonly IDocumentLogic _logic;
|
2024-02-11 18:24:54 +04:00
|
|
|
|
private int? _id;
|
2024-02-27 19:44:44 +04:00
|
|
|
|
private Dictionary<int, (IComponentModel, int)> _documentComponents;
|
2024-02-11 18:24:54 +04:00
|
|
|
|
public int Id { set { _id = value; } }
|
2024-02-14 00:11:21 +04:00
|
|
|
|
public FormDocument(ILogger<FormDocument> logger, IDocumentLogic logic)
|
2024-02-11 18:24:54 +04:00
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_logic = logic;
|
2024-02-27 19:44:44 +04:00
|
|
|
|
_documentComponents = new Dictionary<int, (IComponentModel, int)>();
|
2024-02-11 18:24:54 +04:00
|
|
|
|
}
|
|
|
|
|
|
2024-02-27 19:44:44 +04:00
|
|
|
|
private void FormDocument_Load(object sender, EventArgs e)
|
2024-02-11 18:24:54 +04:00
|
|
|
|
{
|
|
|
|
|
if (_id.HasValue)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation("Загрузка изделия");
|
|
|
|
|
try
|
|
|
|
|
{
|
2024-02-27 19:44:44 +04:00
|
|
|
|
var view = _logic.ReadElement(new DocumentSearchModel
|
|
|
|
|
{
|
|
|
|
|
Id =
|
|
|
|
|
_id.Value
|
|
|
|
|
});
|
2024-02-11 18:24:54 +04:00
|
|
|
|
if (view != null)
|
|
|
|
|
{
|
2024-02-14 00:11:21 +04:00
|
|
|
|
textBoxName.Text = view.DocumentName;
|
2024-02-11 18:24:54 +04:00
|
|
|
|
textBoxPrice.Text = view.Price.ToString();
|
2024-02-27 19:44:44 +04:00
|
|
|
|
_documentComponents = view.DocumentComponents ?? new
|
|
|
|
|
Dictionary<int, (IComponentModel, int)>();
|
2024-02-11 18:24:54 +04:00
|
|
|
|
LoadData();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(ex, "Ошибка загрузки изделия");
|
2024-02-27 19:44:44 +04:00
|
|
|
|
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK,
|
|
|
|
|
MessageBoxIcon.Error);
|
2024-02-11 18:24:54 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-27 19:44:44 +04:00
|
|
|
|
}
|
2024-02-11 18:24:54 +04:00
|
|
|
|
private void LoadData()
|
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation("Загрузка компонент изделия");
|
|
|
|
|
try
|
|
|
|
|
{
|
2024-02-27 19:44:44 +04:00
|
|
|
|
if (_documentComponents != null)
|
2024-02-11 18:24:54 +04:00
|
|
|
|
{
|
|
|
|
|
dataGridView.Rows.Clear();
|
2024-02-27 19:44:44 +04:00
|
|
|
|
foreach (var pc in _documentComponents)
|
2024-02-11 18:24:54 +04:00
|
|
|
|
{
|
2024-02-27 19:44:44 +04:00
|
|
|
|
dataGridView.Rows.Add(new object[] { pc.Key,
|
|
|
|
|
pc.Value.Item1.ComponentName, pc.Value.Item2 });
|
2024-02-11 18:24:54 +04:00
|
|
|
|
}
|
|
|
|
|
textBoxPrice.Text = CalcPrice().ToString();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(ex, "Ошибка загрузки компонент изделия");
|
2024-02-27 19:44:44 +04:00
|
|
|
|
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK,
|
|
|
|
|
MessageBoxIcon.Error);
|
2024-02-11 18:24:54 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-27 19:44:44 +04:00
|
|
|
|
private void buttonAdd_Click(object sender, EventArgs e)
|
2024-02-11 18:24:54 +04:00
|
|
|
|
{
|
2024-02-27 19:44:44 +04:00
|
|
|
|
var service =
|
|
|
|
|
Program.ServiceProvider?.GetService(typeof(FormDocumentComponent));
|
2024-02-14 00:11:21 +04:00
|
|
|
|
if (service is FormDocumentComponent form)
|
2024-02-11 18:24:54 +04:00
|
|
|
|
{
|
2024-02-28 09:04:02 +04:00
|
|
|
|
if (form.ShowDialog() == DialogResult.OK)
|
2024-02-11 18:24:54 +04:00
|
|
|
|
{
|
|
|
|
|
if (form.ComponentModel == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-02-27 19:44:44 +04:00
|
|
|
|
_logger.LogInformation("Добавление нового компонента:{ ComponentName}- { Count}", form.ComponentModel.ComponentName, form.Count);
|
|
|
|
|
if (_documentComponents.ContainsKey(form.Id))
|
2024-02-11 18:24:54 +04:00
|
|
|
|
{
|
2024-02-27 19:44:44 +04:00
|
|
|
|
_documentComponents[form.Id] = (form.ComponentModel,
|
|
|
|
|
form.Count);
|
2024-02-11 18:24:54 +04:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-02-27 19:44:44 +04:00
|
|
|
|
_documentComponents.Add(form.Id, (form.ComponentModel,
|
|
|
|
|
form.Count));
|
2024-02-11 18:24:54 +04:00
|
|
|
|
}
|
|
|
|
|
LoadData();
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-02-27 19:44:44 +04:00
|
|
|
|
|
2024-02-11 18:24:54 +04:00
|
|
|
|
}
|
|
|
|
|
|
2024-02-27 19:44:44 +04:00
|
|
|
|
private void buttonUpd_Click(object sender, EventArgs e)
|
2024-02-11 18:24:54 +04:00
|
|
|
|
{
|
|
|
|
|
if (dataGridView.SelectedRows.Count == 1)
|
|
|
|
|
{
|
2024-02-27 19:44:44 +04:00
|
|
|
|
var service =
|
|
|
|
|
Program.ServiceProvider?.GetService(typeof(FormDocumentComponent));
|
2024-02-14 00:11:21 +04:00
|
|
|
|
if (service is FormDocumentComponent form)
|
2024-02-11 18:24:54 +04:00
|
|
|
|
{
|
2024-02-27 19:44:44 +04:00
|
|
|
|
int id =
|
|
|
|
|
Convert.ToInt32(dataGridView.SelectedRows[0].Cells[0].Value);
|
2024-02-11 18:24:54 +04:00
|
|
|
|
form.Id = id;
|
2024-02-27 19:44:44 +04:00
|
|
|
|
form.Count = _documentComponents[id].Item2;
|
2024-02-11 18:24:54 +04:00
|
|
|
|
if (form.ShowDialog() == DialogResult.OK)
|
|
|
|
|
{
|
|
|
|
|
if (form.ComponentModel == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-02-27 19:44:44 +04:00
|
|
|
|
_logger.LogInformation("Изменение компонента:{ ComponentName - { Count}", form.ComponentModel.ComponentName, form.Count);
|
2024-02-28 09:04:02 +04:00
|
|
|
|
_documentComponents[form.Id] = (form.ComponentModel, form.Count);
|
2024-02-11 18:24:54 +04:00
|
|
|
|
LoadData();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-02-27 19:44:44 +04:00
|
|
|
|
|
2024-02-11 18:24:54 +04:00
|
|
|
|
}
|
|
|
|
|
|
2024-02-27 19:44:44 +04:00
|
|
|
|
private void buttonDel_Click(object sender, EventArgs e)
|
2024-02-11 18:24:54 +04:00
|
|
|
|
{
|
|
|
|
|
if (dataGridView.SelectedRows.Count == 1)
|
|
|
|
|
{
|
2024-02-27 19:44:44 +04:00
|
|
|
|
if (MessageBox.Show("Удалить запись?", "Вопрос",
|
|
|
|
|
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
|
2024-02-11 18:24:54 +04:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2024-02-27 19:44:44 +04:00
|
|
|
|
_logger.LogInformation("Удаление компонента: { ComponentName} - { Count} ", dataGridView.SelectedRows[0].Cells[1].Value);
|
2024-02-28 09:04:02 +04:00
|
|
|
|
|
2024-02-27 19:44:44 +04:00
|
|
|
|
_documentComponents?.Remove(Convert.ToInt32(dataGridView.SelectedRows[0].Cells[0].Value));
|
2024-02-11 18:24:54 +04:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2024-02-27 19:44:44 +04:00
|
|
|
|
MessageBox.Show(ex.Message, "Ошибка",
|
|
|
|
|
MessageBoxButtons.OK, MessageBoxIcon.Error);
|
2024-02-11 18:24:54 +04:00
|
|
|
|
}
|
|
|
|
|
LoadData();
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-02-27 19:44:44 +04:00
|
|
|
|
|
2024-02-11 18:24:54 +04:00
|
|
|
|
}
|
|
|
|
|
|
2024-02-27 19:44:44 +04:00
|
|
|
|
private void buttonRef_Click(object sender, EventArgs e)
|
2024-02-11 18:24:54 +04:00
|
|
|
|
{
|
|
|
|
|
LoadData();
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-27 19:44:44 +04:00
|
|
|
|
private void buttonSave_Click(object sender, EventArgs e)
|
2024-02-11 18:24:54 +04:00
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(textBoxName.Text))
|
|
|
|
|
{
|
2024-02-27 19:44:44 +04:00
|
|
|
|
MessageBox.Show("Заполните название", "Ошибка",
|
|
|
|
|
MessageBoxButtons.OK, MessageBoxIcon.Error);
|
2024-02-11 18:24:54 +04:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (string.IsNullOrEmpty(textBoxPrice.Text))
|
|
|
|
|
{
|
2024-02-27 19:44:44 +04:00
|
|
|
|
MessageBox.Show("Заполните цену", "Ошибка", MessageBoxButtons.OK,
|
|
|
|
|
MessageBoxIcon.Error);
|
2024-02-11 18:24:54 +04:00
|
|
|
|
return;
|
|
|
|
|
}
|
2024-02-27 19:44:44 +04:00
|
|
|
|
if (_documentComponents == null || _documentComponents.Count == 0)
|
2024-02-11 18:24:54 +04:00
|
|
|
|
{
|
2024-02-27 19:44:44 +04:00
|
|
|
|
MessageBox.Show("Заполните компоненты", "Ошибка",
|
|
|
|
|
MessageBoxButtons.OK, MessageBoxIcon.Error);
|
2024-02-11 18:24:54 +04:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
_logger.LogInformation("Сохранение изделия");
|
|
|
|
|
try
|
|
|
|
|
{
|
2024-02-14 00:11:21 +04:00
|
|
|
|
var model = new DocumentBindingModel
|
2024-02-11 18:24:54 +04:00
|
|
|
|
{
|
|
|
|
|
Id = _id ?? 0,
|
2024-02-14 00:11:21 +04:00
|
|
|
|
DocumentName = textBoxName.Text,
|
2024-02-11 18:24:54 +04:00
|
|
|
|
Price = Convert.ToDouble(textBoxPrice.Text),
|
2024-02-27 19:44:44 +04:00
|
|
|
|
DocumentComponents = _documentComponents
|
2024-02-11 18:24:54 +04:00
|
|
|
|
};
|
2024-02-27 19:44:44 +04:00
|
|
|
|
var operationResult = _id.HasValue ? _logic.Update(model) :
|
|
|
|
|
_logic.Create(model);
|
2024-02-11 18:24:54 +04:00
|
|
|
|
if (!operationResult)
|
|
|
|
|
{
|
|
|
|
|
throw new Exception("Ошибка при сохранении. Дополнительная информация в логах.");
|
|
|
|
|
}
|
2024-02-27 19:44:44 +04:00
|
|
|
|
MessageBox.Show("Сохранение прошло успешно", "Сообщение",
|
|
|
|
|
MessageBoxButtons.OK, MessageBoxIcon.Information);
|
2024-02-11 18:24:54 +04:00
|
|
|
|
DialogResult = DialogResult.OK;
|
|
|
|
|
Close();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(ex, "Ошибка сохранения изделия");
|
|
|
|
|
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
|
|
|
}
|
2024-02-27 19:44:44 +04:00
|
|
|
|
|
2024-02-11 18:24:54 +04:00
|
|
|
|
}
|
|
|
|
|
|
2024-02-27 19:44:44 +04:00
|
|
|
|
private void buttonCancel_Click(object sender, EventArgs e)
|
2024-02-11 18:24:54 +04:00
|
|
|
|
{
|
|
|
|
|
DialogResult = DialogResult.Cancel;
|
|
|
|
|
Close();
|
|
|
|
|
}
|
|
|
|
|
private double CalcPrice()
|
|
|
|
|
{
|
|
|
|
|
double price = 0;
|
2024-02-27 19:44:44 +04:00
|
|
|
|
foreach (var elem in _documentComponents)
|
2024-02-11 18:24:54 +04:00
|
|
|
|
{
|
|
|
|
|
price += ((elem.Value.Item1?.Cost ?? 0) * elem.Value.Item2);
|
|
|
|
|
}
|
|
|
|
|
return Math.Round(price * 1.1, 2);
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-02-27 19:44:44 +04:00
|
|
|
|
}
|