This commit is contained in:
Danil Markov 2023-04-21 14:46:06 +04:00
commit 66ac668be4
5 changed files with 382 additions and 382 deletions

View File

@ -3,53 +3,53 @@ using System.Xml.Linq;
namespace LawFirmFileImplement namespace LawFirmFileImplement
{ {
internal class DataFileSingleton internal class DataFileSingleton
{ {
private static DataFileSingleton? instance; private static DataFileSingleton? instance;
private readonly string BlankFileName = "Blank.xml"; private readonly string BlankFileName = "Blank.xml";
private readonly string OrderFileName = "Order.xml"; private readonly string OrderFileName = "Order.xml";
private readonly string DocumentFileName = "Document.xml"; private readonly string DocumentFileName = "Document.xml";
public List<Blank> Blanks { get; private set; } public List<Blank> Blanks { get; private set; }
public List<Order> Orders { get; private set; } public List<Order> Orders { get; private set; }
public List<Document> Documents { get; private set; } public List<Document> Documents { get; private set; }
public static DataFileSingleton GetInstance() public static DataFileSingleton GetInstance()
{ {
if (instance == null) if (instance == null)
{ {
instance = new DataFileSingleton(); instance = new DataFileSingleton();
} }
return instance; return instance;
} }
public void SaveBlanks() => SaveData(Blanks, BlankFileName, "Blanks", x => x.GetXElement); public void SaveBlanks() => SaveData(Blanks, BlankFileName, "Blanks", x => x.GetXElement);
public void SaveDocuments() => SaveData(Documents, DocumentFileName, "Documents", x => x.GetXElement); public void SaveDocuments() => SaveData(Documents, DocumentFileName, "Documents", x => x.GetXElement);
public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x => x.GetXElement); public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x => x.GetXElement);
private DataFileSingleton() private DataFileSingleton()
{ {
Blanks = LoadData(BlankFileName, "Blank", x => Blank.Create(x)!)!; Blanks = LoadData(BlankFileName, "Blank", x => Blank.Create(x)!)!;
Documents = LoadData(DocumentFileName, "Document", x => Document.Create(x)!)!; Documents = LoadData(DocumentFileName, "Document", x => Document.Create(x)!)!;
Orders = LoadData(OrderFileName, "Order", x => Order.Create(x)!)!; Orders = LoadData(OrderFileName, "Order", x => Order.Create(x)!)!;
} }
private static List<T>? LoadData<T>(string filename, string xmlNodeName, private static List<T>? LoadData<T>(string filename, string xmlNodeName,
Func<XElement, T> selectFunction) Func<XElement, T> selectFunction)
{ {
if (File.Exists(filename)) if (File.Exists(filename))
{ {
return XDocument.Load(filename)?. return XDocument.Load(filename)?.
Root?. Root?.
Elements(xmlNodeName)?. Elements(xmlNodeName)?.
Select(selectFunction)?. Select(selectFunction)?.
ToList(); ToList();
} }
return new List<T>(); return new List<T>();
} }
private static void SaveData<T>(List<T> data, string filename, private static void SaveData<T>(List<T> data, string filename,
string xmlNodeName, Func<T, XElement> selectFunction) string xmlNodeName, Func<T, XElement> selectFunction)
{ {
if (data != null) if (data != null)
{ {
new XDocument(new XElement(xmlNodeName, new XDocument(new XElement(xmlNodeName,
data.Select(selectFunction).ToArray())).Save(filename); data.Select(selectFunction).ToArray())).Save(filename);
} }
} }
} }
} }

View File

@ -6,69 +6,69 @@ using LawFirmFileImplement.Models;
namespace LawFirmFileImplement.Implements namespace LawFirmFileImplement.Implements
{ {
public class DocumentStorage : IDocumentStorage public class DocumentStorage : IDocumentStorage
{ {
private readonly DataFileSingleton source; private readonly DataFileSingleton source;
public DocumentStorage() public DocumentStorage()
{ {
source = DataFileSingleton.GetInstance(); source = DataFileSingleton.GetInstance();
} }
public List<DocumentViewModel> GetFullList() public List<DocumentViewModel> GetFullList()
{ {
return source.Documents.Select(x => x.GetViewModel).ToList(); return source.Documents.Select(x => x.GetViewModel).ToList();
} }
public List<DocumentViewModel> GetFilteredList(DocumentSearchModel model) public List<DocumentViewModel> GetFilteredList(DocumentSearchModel model)
{ {
if (string.IsNullOrEmpty(model.DocumentName)) if (string.IsNullOrEmpty(model.DocumentName))
{ {
return new(); return new();
} }
return source.Documents.Where(x => return source.Documents.Where(x =>
x.DocumentName.Contains(model.DocumentName)).Select(x => x.GetViewModel).ToList(); x.DocumentName.Contains(model.DocumentName)).Select(x => x.GetViewModel).ToList();
} }
public DocumentViewModel? GetElement(DocumentSearchModel model) public DocumentViewModel? GetElement(DocumentSearchModel model)
{ {
if (string.IsNullOrEmpty(model.DocumentName) && !model.Id.HasValue) if (string.IsNullOrEmpty(model.DocumentName) && !model.Id.HasValue)
{ {
return null; return null;
} }
return source.Documents.FirstOrDefault(x => return source.Documents.FirstOrDefault(x =>
(!string.IsNullOrEmpty(model.DocumentName) && x.DocumentName == (!string.IsNullOrEmpty(model.DocumentName) && x.DocumentName ==
model.DocumentName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; model.DocumentName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
} }
public DocumentViewModel? Insert(DocumentBindingModel model) public DocumentViewModel? Insert(DocumentBindingModel model)
{ {
model.Id = source.Documents.Count > 0 ? source.Documents.Max(x => x.Id) + 1 : 1; model.Id = source.Documents.Count > 0 ? source.Documents.Max(x => x.Id) + 1 : 1;
var newDocument = Document.Create(model); var newDocument = Document.Create(model);
if (newDocument == null) if (newDocument == null)
{ {
return null; return null;
} }
source.Documents.Add(newDocument); source.Documents.Add(newDocument);
source.SaveDocuments(); source.SaveDocuments();
return newDocument.GetViewModel; return newDocument.GetViewModel;
} }
public DocumentViewModel? Update(DocumentBindingModel model) public DocumentViewModel? Update(DocumentBindingModel model)
{ {
var document = source.Documents.FirstOrDefault(x => x.Id == model.Id); var document = source.Documents.FirstOrDefault(x => x.Id == model.Id);
if (document == null) if (document == null)
{ {
return null; return null;
} }
document.Update(model); document.Update(model);
source.SaveDocuments(); source.SaveDocuments();
return document.GetViewModel; return document.GetViewModel;
} }
public DocumentViewModel? Delete(DocumentBindingModel model) public DocumentViewModel? Delete(DocumentBindingModel model)
{ {
var element = source.Documents.FirstOrDefault(x => x.Id == model.Id); var element = source.Documents.FirstOrDefault(x => x.Id == model.Id);
if (element != null) if (element != null)
{ {
source.Documents.Remove(element); source.Documents.Remove(element);
source.SaveDocuments(); source.SaveDocuments();
return element.GetViewModel; return element.GetViewModel;
} }
return null; return null;
} }
} }
} }

View File

@ -6,78 +6,78 @@ using LawFirmFileImplement.Models;
namespace LawFirmFileImplement.Implements namespace LawFirmFileImplement.Implements
{ {
public class OrderStorage : IOrderStorage public class OrderStorage : IOrderStorage
{ {
private readonly DataFileSingleton source; private readonly DataFileSingleton source;
public OrderStorage() public OrderStorage()
{ {
source = DataFileSingleton.GetInstance(); source = DataFileSingleton.GetInstance();
} }
public List<OrderViewModel> GetFullList() public List<OrderViewModel> GetFullList()
{ {
return source.Orders.Select(x => GetViewModel(x)).ToList(); return source.Orders.Select(x => GetViewModel(x)).ToList();
} }
public List<OrderViewModel> GetFilteredList(OrderSearchModel model) public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
{ {
if (!model.Id.HasValue) if (!model.Id.HasValue)
{ {
return new(); return new();
} }
return source.Orders.Where(x => x.Id.Equals(model.Id)).Select(x => GetViewModel(x)).ToList(); return source.Orders.Where(x => x.Id.Equals(model.Id)).Select(x => GetViewModel(x)).ToList();
} }
public OrderViewModel? GetElement(OrderSearchModel model) public OrderViewModel? GetElement(OrderSearchModel model)
{ {
if (!model.Id.HasValue) if (!model.Id.HasValue)
{ {
return null; return null;
} }
return source.Orders.FirstOrDefault(x => return source.Orders.FirstOrDefault(x =>
(model.Id.HasValue && x.Id == model.Id))?.GetViewModel; (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
} }
private OrderViewModel GetViewModel(Order order) private OrderViewModel GetViewModel(Order order)
{ {
var viewModel = order.GetViewModel; var viewModel = order.GetViewModel;
var document = source.Documents.FirstOrDefault(x => x.Id == order.DocumentId); var document = source.Documents.FirstOrDefault(x => x.Id == order.DocumentId);
if (document != null) if (document != null)
{ {
viewModel.DocumentName = document.DocumentName; viewModel.DocumentName = document.DocumentName;
} }
return viewModel; return viewModel;
} }
public OrderViewModel? Insert(OrderBindingModel model) public OrderViewModel? Insert(OrderBindingModel model)
{ {
model.Id = source.Orders.Count > 0 ? source.Orders.Max(x => x.Id) + 1 : 1; model.Id = source.Orders.Count > 0 ? source.Orders.Max(x => x.Id) + 1 : 1;
var newOrder = Order.Create(model); var newOrder = Order.Create(model);
if (newOrder == null) if (newOrder == null)
{ {
return null; return null;
} }
source.Orders.Add(newOrder); source.Orders.Add(newOrder);
source.SaveOrders(); source.SaveOrders();
return GetViewModel(newOrder); return GetViewModel(newOrder);
} }
public OrderViewModel? Update(OrderBindingModel model) public OrderViewModel? Update(OrderBindingModel model)
{ {
var order = source.Orders.FirstOrDefault(x => x.Id == model.Id); var order = source.Orders.FirstOrDefault(x => x.Id == model.Id);
if (order == null) if (order == null)
{ {
return null; return null;
} }
order.Update(model); order.Update(model);
source.SaveOrders(); source.SaveOrders();
return GetViewModel(order); return GetViewModel(order);
} }
public OrderViewModel? Delete(OrderBindingModel model) public OrderViewModel? Delete(OrderBindingModel model)
{ {
var element = source.Orders.FirstOrDefault(x => x.Id == model.Id); var element = source.Orders.FirstOrDefault(x => x.Id == model.Id);
if (element != null) if (element != null)
{ {
source.Orders.Remove(element); source.Orders.Remove(element);
source.SaveOrders(); source.SaveOrders();
return GetViewModel(element); return GetViewModel(element);
} }
return null; return null;
} }
} }
} }

View File

@ -5,86 +5,86 @@ using System.Xml.Linq;
namespace LawFirmFileImplement.Models namespace LawFirmFileImplement.Models
{ {
public class Document : IDocumentModel public class Document : IDocumentModel
{ {
public int Id { get; private set; } public int Id { get; private set; }
public string DocumentName { get; private set; } = string.Empty; public string DocumentName { get; private set; } = string.Empty;
public double Price { get; private set; } public double Price { get; private set; }
public Dictionary<int, int> Blanks { get; private set; } = new(); public Dictionary<int, int> Blanks { get; private set; } = new();
private Dictionary<int, (IBlankModel, int)>? _documentBlanks = null; private Dictionary<int, (IBlankModel, int)>? _documentBlanks = null;
public Dictionary<int, (IBlankModel, int)> DocumentBlanks public Dictionary<int, (IBlankModel, int)> DocumentBlanks
{ {
get get
{ {
if (_documentBlanks == null) if (_documentBlanks == null)
{ {
var source = DataFileSingleton.GetInstance(); var source = DataFileSingleton.GetInstance();
_documentBlanks = Blanks.ToDictionary(x => x.Key, y => _documentBlanks = Blanks.ToDictionary(x => x.Key, y =>
((source.Blanks.FirstOrDefault(z => z.Id == y.Key) as IBlankModel)!, ((source.Blanks.FirstOrDefault(z => z.Id == y.Key) as IBlankModel)!,
y.Value)); y.Value));
} }
return _documentBlanks; return _documentBlanks;
} }
} }
public static Document? Create(DocumentBindingModel model) public static Document? Create(DocumentBindingModel model)
{ {
if (model == null) if (model == null)
{ {
return null; return null;
} }
return new Document() return new Document()
{ {
Id = model.Id, Id = model.Id,
DocumentName = model.DocumentName, DocumentName = model.DocumentName,
Price = model.Price, Price = model.Price,
Blanks = model.DocumentBlanks.ToDictionary(x => x.Key, x Blanks = model.DocumentBlanks.ToDictionary(x => x.Key, x
=> x.Value.Item2) => x.Value.Item2)
}; };
} }
public static Document? Create(XElement element) public static Document? Create(XElement element)
{ {
if (element == null) if (element == null)
{ {
return null; return null;
} }
return new Document() return new Document()
{ {
Id = Convert.ToInt32(element.Attribute("Id")!.Value), Id = Convert.ToInt32(element.Attribute("Id")!.Value),
DocumentName = element.Element("DocumentName")!.Value, DocumentName = element.Element("DocumentName")!.Value,
Price = Convert.ToDouble(element.Element("Price")!.Value), Price = Convert.ToDouble(element.Element("Price")!.Value),
Blanks = Blanks =
element.Element("DocumentBlanks")!.Elements("DocumentBlank") element.Element("DocumentBlanks")!.Elements("DocumentBlank")
.ToDictionary(x => .ToDictionary(x =>
Convert.ToInt32(x.Element("Key")?.Value), x => Convert.ToInt32(x.Element("Key")?.Value), x =>
Convert.ToInt32(x.Element("Value")?.Value)) Convert.ToInt32(x.Element("Value")?.Value))
}; };
} }
public void Update(DocumentBindingModel model) public void Update(DocumentBindingModel model)
{ {
if (model == null) if (model == null)
{ {
return; return;
} }
DocumentName = model.DocumentName; DocumentName = model.DocumentName;
Price = model.Price; Price = model.Price;
Blanks = model.DocumentBlanks.ToDictionary(x => x.Key, x => Blanks = model.DocumentBlanks.ToDictionary(x => x.Key, x =>
x.Value.Item2); x.Value.Item2);
_documentBlanks = null; _documentBlanks = null;
} }
public DocumentViewModel GetViewModel => new() public DocumentViewModel GetViewModel => new()
{ {
Id = Id, Id = Id,
DocumentName = DocumentName, DocumentName = DocumentName,
Price = Price, Price = Price,
DocumentBlanks = DocumentBlanks DocumentBlanks = DocumentBlanks
}; };
public XElement GetXElement => new("Document", public XElement GetXElement => new("Document",
new XAttribute("Id", Id), new XAttribute("Id", Id),
new XElement("DocumentName", DocumentName), new XElement("DocumentName", DocumentName),
new XElement("Price", Price.ToString()), new XElement("Price", Price.ToString()),
new XElement("DocumentBlanks", Blanks.Select(x => new XElement("DocumentBlanks", Blanks.Select(x =>
new XElement("DocumentBlank", new XElement("DocumentBlank",
new XElement("Key", x.Key), new XElement("Key", x.Key),
new XElement("Value", x.Value))).ToArray())); new XElement("Value", x.Value))).ToArray()));
} }
} }

View File

@ -5,113 +5,113 @@ using Microsoft.Extensions.Logging;
namespace LawFirmView namespace LawFirmView
{ {
public partial class FormCreateOrder : Form public partial class FormCreateOrder : Form
{ {
private readonly ILogger _logger; private readonly ILogger _logger;
private readonly IDocumentLogic _logicD; private readonly IDocumentLogic _logicD;
private readonly IOrderLogic _logicO; private readonly IOrderLogic _logicO;
public FormCreateOrder(ILogger<FormCreateOrder> logger, IDocumentLogic logicD, IOrderLogic logicO) public FormCreateOrder(ILogger<FormCreateOrder> logger, IDocumentLogic logicD, IOrderLogic logicO)
{ {
InitializeComponent(); InitializeComponent();
_logger = logger; _logger = logger;
_logicD = logicD; _logicD = logicD;
_logicO = logicO; _logicO = logicO;
} }
private void FormCreateOrder_Load(object sender, EventArgs e) private void FormCreateOrder_Load(object sender, EventArgs e)
{ {
_logger.LogInformation("Загрузка документов для заказа"); _logger.LogInformation("Загрузка документов для заказа");
try try
{ {
var list = _logicD.ReadList(null); var list = _logicD.ReadList(null);
if (list != null) if (list != null)
{ {
comboBoxDocument.DisplayMember = "DocumentName"; comboBoxDocument.DisplayMember = "DocumentName";
comboBoxDocument.ValueMember = "Id"; comboBoxDocument.ValueMember = "Id";
comboBoxDocument.DataSource = list; comboBoxDocument.DataSource = list;
comboBoxDocument.SelectedItem = null; comboBoxDocument.SelectedItem = null;
} }
} }
catch (Exception ex) catch (Exception ex)
{ {
_logger.LogError(ex, "Ошибка загрузки документов"); _logger.LogError(ex, "Ошибка загрузки документов");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
} }
} }
private void CalcSum() private void CalcSum()
{ {
if (comboBoxDocument.SelectedValue != null && if (comboBoxDocument.SelectedValue != null &&
!string.IsNullOrEmpty(textBoxCount.Text)) !string.IsNullOrEmpty(textBoxCount.Text))
{ {
try try
{ {
int id = Convert.ToInt32(comboBoxDocument.SelectedValue); int id = Convert.ToInt32(comboBoxDocument.SelectedValue);
var document = _logicD.ReadElement(new DocumentSearchModel var document = _logicD.ReadElement(new DocumentSearchModel
{ {
Id = id Id = id
}); });
int count = Convert.ToInt32(textBoxCount.Text); int count = Convert.ToInt32(textBoxCount.Text);
textBoxSum.Text = Math.Round(count * (document?.Price ?? 0), 2).ToString(); textBoxSum.Text = Math.Round(count * (document?.Price ?? 0), 2).ToString();
_logger.LogInformation("Расчет суммы заказа"); _logger.LogInformation("Расчет суммы заказа");
} }
catch (Exception ex) catch (Exception ex)
{ {
_logger.LogError(ex, "Ошибка расчета суммы заказа"); _logger.LogError(ex, "Ошибка расчета суммы заказа");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK,
MessageBoxIcon.Error); MessageBoxIcon.Error);
} }
} }
} }
private void TextBoxCount_TextChanged(object sender, EventArgs e) private void TextBoxCount_TextChanged(object sender, EventArgs e)
{ {
CalcSum(); CalcSum();
} }
private void ComboBoxDocument_SelectedIndexChanged(object sender, EventArgs e) private void ComboBoxDocument_SelectedIndexChanged(object sender, EventArgs e)
{ {
CalcSum(); CalcSum();
} }
private void ButtonSave_Click(object sender, EventArgs e) private void ButtonSave_Click(object sender, EventArgs e)
{ {
if (string.IsNullOrEmpty(textBoxCount.Text)) if (string.IsNullOrEmpty(textBoxCount.Text))
{ {
MessageBox.Show("Заполните поле Количество", "Ошибка", MessageBox.Show("Заполните поле Количество", "Ошибка",
MessageBoxButtons.OK, MessageBoxIcon.Error); MessageBoxButtons.OK, MessageBoxIcon.Error);
return; return;
} }
if (comboBoxDocument.SelectedValue == null) if (comboBoxDocument.SelectedValue == null)
{ {
MessageBox.Show("Выберите документ", "Ошибка", MessageBox.Show("Выберите документ", "Ошибка",
MessageBoxButtons.OK, MessageBoxIcon.Error); MessageBoxButtons.OK, MessageBoxIcon.Error);
return; return;
} }
_logger.LogInformation("Создание заказа"); _logger.LogInformation("Создание заказа");
try try
{ {
var operationResult = _logicO.CreateOrder(new OrderBindingModel var operationResult = _logicO.CreateOrder(new OrderBindingModel
{ {
DocumentId = Convert.ToInt32(comboBoxDocument.SelectedValue), DocumentId = Convert.ToInt32(comboBoxDocument.SelectedValue),
Count = Convert.ToInt32(textBoxCount.Text), Count = Convert.ToInt32(textBoxCount.Text),
Sum = Convert.ToDouble(textBoxSum.Text) Sum = Convert.ToDouble(textBoxSum.Text)
}); });
if (!operationResult) if (!operationResult)
{ {
throw new Exception("Ошибка при создании заказа. Дополнительная информация в логах."); throw new Exception("Ошибка при создании заказа. Дополнительная информация в логах.");
} }
MessageBox.Show("Сохранение прошло успешно", "Сообщение", MessageBox.Show("Сохранение прошло успешно", "Сообщение",
MessageBoxButtons.OK, MessageBoxIcon.Information); MessageBoxButtons.OK, MessageBoxIcon.Information);
DialogResult = DialogResult.OK; DialogResult = DialogResult.OK;
Close(); Close();
} }
catch (Exception ex) catch (Exception ex)
{ {
_logger.LogError(ex, "Ошибка создания заказа"); _logger.LogError(ex, "Ошибка создания заказа");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK,
MessageBoxIcon.Error); MessageBoxIcon.Error);
} }
} }
private void ButtonCancel_Click(object sender, EventArgs e) private void ButtonCancel_Click(object sender, EventArgs e)
{ {
DialogResult = DialogResult.Cancel; DialogResult = DialogResult.Cancel;
Close(); Close();
} }
} }
} }