начало

This commit is contained in:
GokaPek 2024-05-07 21:28:35 +04:00
parent 14822abb7d
commit acdae9d645
16 changed files with 859 additions and 502 deletions

View File

@ -4,6 +4,7 @@ using AbstractLawFirmContracts.SearchModels;
using AbstractLawFirmContracts.StoragesContracts;
using AbstractLawFirmContracts.ViewModels;
using AbstractLawFirmDataModels.Enums;
using AbstractLawFirmDataModels.Models;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
@ -17,12 +18,17 @@ namespace AbstractLawFirmBusinessLogic.BusinessLogic
{
private readonly ILogger _logger;
private readonly IOrderStorage _orderStorage;
public OrderLogic(ILogger<OrderLogic> logger, IOrderStorage orderStorage)
private readonly IShopStorage _shopStorage;
private readonly IShopLogic _shopLogic;
private readonly IDocumentStorage _documentStorage;
public OrderLogic(ILogger<OrderLogic> logger, IOrderStorage orderStorage, IShopLogic shopLogic, IDocumentStorage documentStorage, IShopStorage shopStorage)
{
_logger = logger;
_orderStorage = orderStorage;
}
_shopLogic = shopLogic;
_documentStorage = documentStorage;
_shopStorage = shopStorage;
}
public List<OrderViewModel>? ReadList(OrderSearchModel? model)
{
@ -45,7 +51,8 @@ namespace AbstractLawFirmBusinessLogic.BusinessLogic
model.Status = OrderStatus.Принят;
if (_orderStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
model.Status = OrderStatus.Неизвестен;
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
@ -53,8 +60,8 @@ namespace AbstractLawFirmBusinessLogic.BusinessLogic
public bool ChangeStatus(OrderBindingModel model, OrderStatus status)
{
CheckModel(model);
var element = _orderStorage.GetElement(new OrderSearchModel { Id = model.Id });
CheckModel(model, false);
var element = _orderStorage.GetElement(new OrderSearchModel { Id = model.Id });
if (element == null)
{
_logger.LogWarning("Read operation failed");
@ -65,10 +72,31 @@ namespace AbstractLawFirmBusinessLogic.BusinessLogic
_logger.LogWarning("Status change operation failed");
throw new InvalidOperationException("Текущий статус заказа не может быть переведен в выбранный");
}
model.Status = status;
if (status == OrderStatus.Готов)
{
var document = _documentStorage.GetElement(new DocumentSearchModel() { Id = model.DocumentId });
if (document == null)
{
_logger.LogWarning("Status change operation failed. Car not found.");
return false;
}
if (!CheckThenSupplyMany(document, model.Count))
{
_logger.LogWarning("Status change operation failed. Shop supply error.");
return false;
}
}
model.Status = status;
if (model.Status == OrderStatus.Выдан) model.DateImplement = DateTime.Now;
_orderStorage.Update(model);
return true;
if (_orderStorage.Update(model) == null)
{
model.Status--;
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public bool TakeOrderInWork(OrderBindingModel model)
@ -97,7 +125,11 @@ true)
{
return;
}
if (model.Sum <= 0)
if (model.DocumentId < 0)
{
throw new ArgumentNullException("Некорректный идентификатор документа", nameof(model.DocumentId));
}
if (model.Sum <= 0)
{
throw new ArgumentNullException("Цена заказа должна быть больше 0", nameof(model.Sum));
}
@ -106,6 +138,67 @@ true)
throw new ArgumentNullException("Количество элементов в заказе должно быть больше 0", nameof(model.Count));
}
_logger.LogInformation("Order. Sum:{ Cost}. Id: { Id}", model.Sum, model.Id);
}
public bool CheckThenSupplyMany(IDocumentModel document, int count)
{
if (count <= 0)
{
_logger.LogWarning("Check then supply operation error. Car count < 0.");
return false;
}
int freeSpace = 0;
foreach (var shop in _shopStorage.GetFullList())
{
freeSpace += shop.MaxCountDocuments;
foreach (var c in shop.ShopDocuments)
{
freeSpace -= c.Value.Item2;
}
}
if (freeSpace < count)
{
_logger.LogWarning("Check then supply operation error. There's no place for new cars in shops.");
return false;
}
foreach (var shop in _shopStorage.GetFullList())
{
freeSpace = shop.MaxCountDocuments;
foreach (var c in shop.ShopDocuments)
freeSpace -= c.Value.Item2;
if (freeSpace <= 0)
continue;
if (freeSpace >= count)
{
if (_shopLogic.SupplyDocuments(new ShopSearchModel() { Id = shop.Id }, document, count))
count = 0;
else
{
_logger.LogWarning("Supply error");
return false;
}
}
if (freeSpace < count)
{
if (_shopLogic.SupplyDocuments(new ShopSearchModel() { Id = shop.Id }, document, freeSpace))
count -= freeSpace;
else
{
_logger.LogWarning("Supply error");
return false;
}
}
if (count <= 0)
{
return true;
}
}
return false;
}
}
}
}

View File

@ -113,25 +113,37 @@ namespace AbstractLawFirmBusinessLogic.BusinessLogic
}
_logger.LogInformation("AddPlaneInShop find. Id:{Id}", element.Id);
if (element.ShopDocuments.TryGetValue(document.Id, out var pair))
{
element.ShopDocuments[document.Id] = (document, count + pair.Item2);
_logger.LogInformation("AddPlaneInShop. Added {count} {plane} to '{ShopName}' shop", count, document.DocumentName, element.ShopName);
}
int countDocuments = 0;
foreach (var c in element.ShopDocuments)
countDocuments += c.Value.Item2;
if (count > element.MaxCountDocuments - countDocuments)
{
_logger.LogWarning("Required shop will be overflowed");
return false;
}
else
{
element.ShopDocuments[document.Id] = (document, count);
_logger.LogInformation("AddPlaneInShop. Added {count} new plane {plane} to '{ShopName}' shop", count, document.DocumentName, element.ShopName);
}
if (element.ShopDocuments.TryGetValue(document.Id, out var pair))
{
element.ShopDocuments[document.Id] = (document, count + pair.Item2);
_logger.LogInformation("AddPlaneInShop. Added {count} {plane} to '{ShopName}' shop", count, document.DocumentName, element.ShopName);
}
else
{
element.ShopDocuments[document.Id] = (document, count);
_logger.LogInformation("AddPlaneInShop. Added {count} new plane {plane} to '{ShopName}' shop", count, document.DocumentName, element.ShopName);
}
}
_shopStorage.Update(new()
{
Id = element.Id,
Address = element.Address,
ShopName = element.ShopName,
OpeningDate = element.OpeningDate,
ShopDocuments = element.ShopDocuments
});
_shopStorage.Update(new()
{
Id = element.Id,
Address = element.Address,
ShopName = element.ShopName,
MaxCountDocuments = element.MaxCountDocuments,
OpeningDate = element.OpeningDate,
ShopDocuments = element.ShopDocuments
});
return true;
}
private void CheckModel(ShopBindingModel model, bool withParams = true)
@ -158,5 +170,9 @@ namespace AbstractLawFirmBusinessLogic.BusinessLogic
throw new InvalidOperationException("Магазин с таким названием уже есть");
}
}
}
public bool SellDocument(IDocumentModel document, int count)
{
return _shopStorage.SellDocument(document, count);
}
}
}

View File

@ -22,5 +22,6 @@ namespace AbstractLawFirmContracts.BindingModels
get;
set;
} = new();
}
public int MaxCountDocuments { get; set; }
}
}

View File

@ -18,5 +18,6 @@ namespace AbstractLawFirmContracts.BusinessLogicsContracts
bool Update(ShopBindingModel model);
bool Delete(ShopBindingModel model);
bool SupplyDocuments(ShopSearchModel model, IDocumentModel document, int count);
}
bool SellDocument(IDocumentModel document, int count);
}
}

View File

@ -1,6 +1,7 @@
using AbstractLawFirmContracts.BindingModels;
using AbstractLawFirmContracts.SearchModels;
using AbstractLawFirmContracts.ViewModels;
using AbstractLawFirmDataModels.Models;
using System;
using System.Collections.Generic;
using System.Linq;
@ -17,5 +18,6 @@ namespace AbstractLawFirmContracts.StoragesContracts
ShopViewModel? Insert(ShopBindingModel model);
ShopViewModel? Update(ShopBindingModel model);
ShopViewModel? Delete(ShopBindingModel model);
}
bool SellDocument(IDocumentModel model, int count);
}
}

View File

@ -25,5 +25,7 @@ namespace AbstractLawFirmContracts.ViewModels
get;
set;
} = new();
}
[DisplayName("Максимальное количество пакетов документов в магазине")]
public int MaxCountDocuments { get; set; }
}
}

View File

@ -12,5 +12,6 @@ namespace AbstractLawFirmDataModels.Models
String Address { get; }
DateTime OpeningDate { get; }
Dictionary<int, (IDocumentModel, int)> ShopDocuments { get; }
}
int MaxCountDocuments { get; }
}
}

View File

@ -8,55 +8,55 @@ using AbstractLawFirmFileImplement.Models;
namespace AbstractLawFirmFileImpliment
{
internal class DataFileSingleton
{
private static DataFileSingleton? instance;
private readonly string ComponentFileName = "Component.xml";
private readonly string OrderFileName = "Order.xml";
private readonly string ProductFileName = "Product.xml";
public List<Component> Components { get; private set; }
public List<Order> Orders { get; private set; }
public List<Document> Documents { get; private set; }
public static DataFileSingleton GetInstance()
{
if (instance == null)
{
instance = new DataFileSingleton();
}
return instance;
}
public void SaveComponents() => SaveData(Components, ComponentFileName,
"Components", x => x.GetXElement);
public void SaveDocuments() => SaveData(Documents, ProductFileName,
"Documents", x => x.GetXElement);
public void SaveOrders() => SaveData(Orders, OrderFileName,
"Orders", x => x.GetXElement);
private DataFileSingleton()
{
Components = LoadData(ComponentFileName, "Component", x => Component.Create(x)!)!;
Documents = LoadData(ProductFileName, "Document", x => Document.Create(x)!)!;
Orders = LoadData(OrderFileName, "Order", x => Order.Create(x)!)!;
internal class DataFileSingleton
{
private static DataFileSingleton? instance;
private readonly string ComponentFileName = "Component.xml";
private readonly string OrderFileName = "Order.xml";
private readonly string DocumentFileName = "Document.xml";
private readonly string ShopFileName = "Shop.xml";
}
private static List<T>? LoadData<T>(string filename, string xmlNodeName,
Func<XElement, T> selectFunction)
{
if (File.Exists(filename))
{
return
XDocument.Load(filename)?.Root?.Elements(xmlNodeName)?.Select(selectFunction)?.ToList();
}
return new List<T>();
}
private static void SaveData<T>(List<T> data, string filename, string
xmlNodeName, Func<T, XElement> selectFunction)
{
if (data != null)
{
new XDocument(new XElement(xmlNodeName,
data.Select(selectFunction).ToArray())).Save(filename);
}
}
public List<Component> Components { get; private set; }
public List<Order> Orders { get; private set; }
public List<Document> Documents { get; private set; }
public List<Shop> Shops { get; private set; }
public static DataFileSingleton GetInstance()
{
if (instance == null)
{
instance = new DataFileSingleton();
}
return instance;
}
public void SaveComponents() => SaveData(Components, ComponentFileName, "Components", 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 SaveShops() => SaveData(Shops, ShopFileName, "Shops", x => x.GetXElement);
private DataFileSingleton()
{
Components = LoadData(ComponentFileName, "Component", x => Component.Create(x)!)!;
Documents = LoadData(DocumentFileName, "Document", x => Document.Create(x)!)!;
Orders = LoadData(OrderFileName, "Order", x => Order.Create(x)!)!;
Shops = LoadData(ShopFileName, "Shop", x => Shop.Create(x)!)!;
}
private static List<T>? LoadData<T>(string filename, string xmlNodeName,
Func<XElement, T> selectFunction)
{
if (File.Exists(filename))
{
return
XDocument.Load(filename)?.Root?.Elements(xmlNodeName)?.Select(selectFunction)?.ToList();
}
return new List<T>();
}
private static void SaveData<T>(List<T> data, string filename, string
xmlNodeName, Func<T, XElement> selectFunction)
{
if (data != null)
{
new XDocument(new XElement(xmlNodeName, data.Select(selectFunction).ToArray())).Save(filename);
}
}
}
}
}

View File

@ -12,80 +12,69 @@ using System.Threading.Tasks;
namespace AbstractLawFirmFileImplement.Implements
{
public class ComponentStorage: IComponentStorage
{
private readonly DataFileSingleton source;
public ComponentStorage()
{
source = DataFileSingleton.GetInstance();
}
public List<ComponentViewModel> GetFullList()
{
return source.Components
.Select(x => x.GetViewModel)
.ToList();
}
public List<ComponentViewModel> GetFilteredList(ComponentSearchModel
model)
{
if (string.IsNullOrEmpty(model.ComponentName))
{
return new();
}
return source.Components
.Where(x => x.ComponentName.Contains(model.ComponentName))
.Select(x => x.GetViewModel)
.ToList();
}
public ComponentViewModel? GetElement(ComponentSearchModel model)
{
if (string.IsNullOrEmpty(model.ComponentName) && !model.Id.HasValue)
{
return null;
}
return source.Components
.FirstOrDefault(x =>
(!string.IsNullOrEmpty(model.ComponentName) && x.ComponentName ==
model.ComponentName) ||
(model.Id.HasValue && x.Id == model.Id))
?.GetViewModel;
}
public ComponentViewModel? Insert(ComponentBindingModel model)
{
model.Id = source.Components.Count > 0 ? source.Components.Max(x =>
x.Id) + 1 : 1;
var newComponent = Component.Create(model);
if (newComponent == null)
{
return null;
}
source.Components.Add(newComponent);
source.SaveComponents();
return newComponent.GetViewModel;
}
public ComponentViewModel? Update(ComponentBindingModel model)
{
var component = source.Components.FirstOrDefault(x => x.Id ==
model.Id);
if (component == null)
{
return null;
}
component.Update(model);
source.SaveComponents();
return component.GetViewModel;
}
public ComponentViewModel? Delete(ComponentBindingModel model)
{
var element = source.Components.FirstOrDefault(x => x.Id ==
model.Id);
if (element != null)
{
source.Components.Remove(element);
source.SaveComponents();
return element.GetViewModel;
}
return null;
}
}
public class ComponentStorage : IComponentStorage
{
private readonly DataFileSingleton source;
public ComponentStorage()
{
source = DataFileSingleton.GetInstance();
}
public List<ComponentViewModel> GetFullList()
{
return source.Components.Select(x => x.GetViewModel).ToList();
}
public List<ComponentViewModel> GetFilteredList(ComponentSearchModel model)
{
if (string.IsNullOrEmpty(model.ComponentName))
{
return new();
}
return source.Components.Where(x => x.ComponentName.Contains(model.ComponentName)).Select(x => x.GetViewModel).ToList();
}
public ComponentViewModel? GetElement(ComponentSearchModel model)
{
if (string.IsNullOrEmpty(model.ComponentName) && !model.Id.HasValue)
{
return null;
}
return source.Components.FirstOrDefault(x =>
(!string.IsNullOrEmpty(model.ComponentName) && x.ComponentName ==
model.ComponentName) ||
(model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
}
public ComponentViewModel? Insert(ComponentBindingModel model)
{
model.Id = source.Components.Count > 0 ? source.Components.Max(x => x.Id) + 1 : 1;
var newComponent = Component.Create(model);
if (newComponent == null)
{
return null;
}
source.Components.Add(newComponent);
source.SaveComponents();
return newComponent.GetViewModel;
}
public ComponentViewModel? Update(ComponentBindingModel model)
{
var component = source.Components.FirstOrDefault(x => x.Id == model.Id);
if (component == null)
{
return null;
}
component.Update(model);
source.SaveComponents();
return component.GetViewModel;
}
public ComponentViewModel? Delete(ComponentBindingModel model)
{
var element = source.Components.FirstOrDefault(x => x.Id == model.Id);
if (element != null)
{
source.Components.Remove(element);
source.SaveComponents();
return element.GetViewModel;
}
return null;
}
}
}

View File

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

View File

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

View File

@ -0,0 +1,137 @@
using AbstractLawFirmContracts.BindingModels;
using AbstractLawFirmContracts.SearchModels;
using AbstractLawFirmContracts.StoragesContracts;
using AbstractLawFirmContracts.ViewModels;
using AbstractLawFirmDataModels.Models;
using AbstractLawFirmFileImpliment;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AbstractLawFirmFileImplement.Implements
{
public class ShopStorage : IShopStorage
{
private readonly DataFileSingleton source;
public ShopStorage()
{
source = DataFileSingleton.GetInstance();
}
public ShopViewModel? GetElement(ShopSearchModel model)
{
if (!model.Id.HasValue)
{
return null;
}
return source.Shops.FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)?.GetViewModel;
}
public List<ShopViewModel> GetFilteredList(ShopSearchModel model)
{
if (string.IsNullOrEmpty(model.ShopName))
{
return new();
}
return source.Shops
.Select(x => x.GetViewModel)
.Where(x => x.ShopName.Contains(model.ShopName ?? string.Empty))
.ToList();
}
public List<ShopViewModel> GetFullList()
{
return source.Shops.Select(shop => shop.GetViewModel).ToList();
}
public ShopViewModel? Insert(ShopBindingModel model)
{
model.Id = source.Shops.Count > 0 ? source.Shops.Max(x => x.Id) + 1 : 1;
var newShop = Shop.Create(model);
if (newShop == null)
{
return null;
}
source.Shops.Add(newShop);
source.SaveShops();
return newShop.GetViewModel;
}
public ShopViewModel? Update(ShopBindingModel model)
{
var shop = source.Shops.FirstOrDefault(x => x.Id == model.Id);
if (shop == null)
{
return null;
}
shop.Update(model);
source.SaveShops();
return shop.GetViewModel;
}
public ShopViewModel? Delete(ShopBindingModel model)
{
var shop = source.Shops.FirstOrDefault(x => x.Id == model.Id);
if (shop == null)
{
return null;
}
source.Shops.Remove(shop);
source.SaveShops();
return shop.GetViewModel;
}
public bool SellDocument(IDocumentModel model, int count)
{
var document = source.Documents.FirstOrDefault(x => x.Id == model.Id);
if (document == null)
{
return false;
}
var shopDocuments = source.Shops.SelectMany(shop => shop.ShopDocuments.Where(c => c.Value.Item1.Id == document.Id));
int countStore = 0;
foreach (var it in shopDocuments)
countStore += it.Value.Item2;
if (count > countStore)
return false;
foreach (var shop in source.Shops)
{
var documents = shop.ShopDocuments;
foreach (var c in documents.Where(x => x.Value.Item1.Id == document.Id))
{
int min = Math.Min(c.Value.Item2, count);
documents[c.Value.Item1.Id] = (c.Value.Item1, c.Value.Item2 - min);
count -= min;
if (count <= 0)
break;
}
shop.Update(new ShopBindingModel
{
Id = shop.Id,
ShopName = shop.ShopName,
Address = shop.Address,
MaxCountDocuments = shop.MaxCountDocuments,
OpeningDate = shop.OpeningDate,
ShopDocuments = documents
});
source.SaveShops();
if (count <= 0)
return true;
}
return true;
}
}
}

View File

@ -10,55 +10,55 @@ using System.Xml.Linq;
namespace AbstractLawFirmFileImplement.Models
{
public class Component : IComponentModel
{
public int Id { get; private set; }
public string ComponentName { get; private set; } = string.Empty;
public double Cost { get; set; }
public static Component? Create(ComponentBindingModel model)
{
if (model == null)
{
return null;
}
return new Component()
{
Id = model.Id,
ComponentName = model.ComponentName,
Cost = model.Cost
};
}
public static Component? Create(XElement element)
{
if (element == null)
{
return null;
}
return new Component()
{
Id = Convert.ToInt32(element.Attribute("Id")!.Value),
ComponentName = element.Element("ComponentName")!.Value,
Cost = Convert.ToDouble(element.Element("Cost")!.Value)
};
}
public void Update(ComponentBindingModel model)
{
if (model == null)
{
return;
}
ComponentName = model.ComponentName;
Cost = model.Cost;
}
public ComponentViewModel GetViewModel => new()
{
Id = Id,
ComponentName = ComponentName,
Cost = Cost
};
public XElement GetXElement => new("Component",
new XAttribute("Id", Id),
new XElement("ComponentName", ComponentName),
new XElement("Cost", Cost.ToString()));
}
public class Component : IComponentModel
{
public int Id { get; private set; }
public string ComponentName { get; private set; } = string.Empty;
public double Cost { get; set; }
public static Component? Create(ComponentBindingModel model)
{
if (model == null)
{
return null;
}
return new Component()
{
Id = model.Id,
ComponentName = model.ComponentName,
Cost = model.Cost
};
}
public static Component? Create(XElement element)
{
if (element == null)
{
return null;
}
return new Component()
{
Id = Convert.ToInt32(element.Attribute("Id")!.Value),
ComponentName = element.Element("ComponentName")!.Value,
Cost = Convert.ToDouble(element.Element("Cost")!.Value)
};
}
public void Update(ComponentBindingModel model)
{
if (model == null)
{
return;
}
ComponentName = model.ComponentName;
Cost = model.Cost;
}
public ComponentViewModel GetViewModel => new()
{
Id = Id,
ComponentName = ComponentName,
Cost = Cost
};
public XElement GetXElement => new("Component",
new XAttribute("Id", Id),
new XElement("ComponentName", ComponentName),
new XElement("Cost", Cost.ToString()));
}
}

View File

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

View File

@ -12,83 +12,88 @@ using System.Xml.Linq;
namespace AbstractLawFirmFileImplement.Models
{
public class Order : IOrderModel
{
public int Id { get; private set; }
public int DocumentId { get; private set; }
public int Count { get; private set; }
public double Sum { get; private set; }
public OrderStatus Status { get; private set; } = OrderStatus.Неизвестен;
public DateTime DateCreate { get; private set; }
public DateTime? DateImplement { get; private set; }
public static Order? Create(OrderBindingModel? model)
{
if (model == null)
{
return null;
}
return new Order()
{
Id = model.Id,
DocumentId = model.DocumentId,
Count = model.Count,
Sum = model.Sum,
Status = model.Status,
DateCreate = model.DateCreate,
DateImplement = model.DateImplement,
};
}
public static Order? Create(XElement element)
{
if (element == null)
{
return null;
}
return new Order()
{
Id = Convert.ToInt32(element.Attribute("Id")!.Value),
DocumentId = Convert.ToInt32(element.Element("DocumentId")!.Value),
Sum = Convert.ToDouble(element.Element("Sum")!.Value),
Count = Convert.ToInt32(element.Element("Count")!.Value),
Status = (OrderStatus)Enum.Parse(typeof(OrderStatus), element.Element("Status")!.Value),
DateCreate = Convert.ToDateTime(element.Element("DateCreate")!.Value),
DateImplement = string.IsNullOrEmpty(element.Element("DateImplement")!.Value) ? null : Convert.ToDateTime(element.Element("DateImplement")!.Value)
};
}
public void Update(OrderBindingModel? model)
{
if (model == null)
{
return;
}
Id = model.Id;
DocumentId = model.DocumentId;
Count = model.Count;
Sum = model.Sum;
Status = model.Status;
DateCreate = model.DateCreate;
DateImplement = model.DateImplement;
}
public OrderViewModel GetViewModel => new()
{
Id = Id,
DocumentId = DocumentId,
Count = Count,
Sum = Sum,
Status = Status,
DateCreate = DateCreate,
DateImplement = DateImplement,
};
public class Order : IOrderModel
{
public int DocumentId { get; private set; }
public XElement GetXElement => new(
"Order",
new XAttribute("Id", Id),
new XElement("DocumentId", DocumentId.ToString()),
new XElement("Count", Count.ToString()),
new XElement("Sum", Sum.ToString()),
new XElement("Status", Status.ToString()),
new XElement("DateCreate", DateCreate.ToString()),
new XElement("DateImplement", DateImplement.ToString())
);
}
public int Count { get; private set; }
public double Sum { get; private set; }
public OrderStatus Status { get; private set; } = OrderStatus.Неизвестен;
public DateTime DateCreate { get; private set; } = DateTime.Now;
public DateTime? DateImplement { get; private set; }
public int Id { get; private set; }
public static Order? Create(OrderBindingModel? model)
{
if (model == null)
{
return null;
}
return new Order
{
DocumentId = model.DocumentId,
Count = model.Count,
Sum = model.Sum,
Status = model.Status,
DateCreate = model.DateCreate,
DateImplement = model.DateImplement,
Id = model.Id,
};
}
public static Order? Create(XElement element)
{
if (element == null)
{
return null;
}
return new Order()
{
Id = Convert.ToInt32(element.Attribute("Id")!.Value),
DocumentId = Convert.ToInt32(element.Element("DocumentId")!.Value),
Sum = Convert.ToDouble(element.Element("Sum")!.Value),
Count = Convert.ToInt32(element.Element("Count")!.Value),
Status = (OrderStatus)Enum.Parse(typeof(OrderStatus), element.Element("Status")!.Value),
DateCreate = Convert.ToDateTime(element.Element("DateCreate")!.Value),
DateImplement = string.IsNullOrEmpty(element.Element("DateImplement")!.Value) ? null : Convert.ToDateTime(element.Element("DateImplement")!.Value)
};
}
public void Update(OrderBindingModel? model)
{
if (model == null)
{
return;
}
Status = model.Status;
DateImplement = model.DateImplement;
}
public OrderViewModel GetViewModel => new()
{
DocumentId = DocumentId,
Count = Count,
Sum = Sum,
DateCreate = DateCreate,
DateImplement = DateImplement,
Id = Id,
Status = Status,
};
public XElement GetXElement => new(
"Order",
new XAttribute("Id", Id),
new XElement("DocumentId", DocumentId.ToString()),
new XElement("Count", Count.ToString()),
new XElement("Sum", Sum.ToString()),
new XElement("Status", Status.ToString()),
new XElement("DateCreate", DateCreate.ToString()),
new XElement("DateImplement", DateImplement.ToString())
);
}
}

View File

@ -0,0 +1,115 @@
using AbstractLawFirmContracts.BindingModels;
using AbstractLawFirmContracts.ViewModels;
using AbstractLawFirmDataModels.Models;
using AbstractLawFirmFileImpliment;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace AbstractLawFirmFileImplement.Models
{
public class Shop : IShopModel
{
public int Id { get; private set; }
public string ShopName { get; private set; } = string.Empty;
public string Address { get; private set; } = string.Empty;
public int MaxCountDocuments { get; private set; }
public DateTime OpeningDate { get; private set; }
public Dictionary<int, int> Documents { get; private set; } = new();
private Dictionary<int, (IDocumentModel, int)>? _shopDocuments = null;
public Dictionary<int, (IDocumentModel, int)> ShopDocuments
{
get
{
if (_shopDocuments == null)
{
var source = DataFileSingleton.GetInstance();
_shopDocuments = Documents.ToDictionary(
x => x.Key,
y => ((source.Documents.FirstOrDefault(z => z.Id == y.Key) as IDocumentModel)!, y.Value)
);
}
return _shopDocuments;
}
}
public static Shop? Create(ShopBindingModel? model)
{
if (model == null)
{
return null;
}
return new Shop()
{
Id = model.Id,
ShopName = model.ShopName,
Address = model.Address,
MaxCountDocuments = model.MaxCountDocuments,
OpeningDate = model.OpeningDate,
Documents = model.ShopDocuments.ToDictionary(x => x.Key, x => x.Value.Item2)
};
}
public static Shop? Create(XElement element)
{
if (element == null)
{
return null;
}
return new Shop()
{
Id = Convert.ToInt32(element.Attribute("Id")!.Value),
ShopName = element.Element("ShopName")!.Value,
Address = element.Element("Address")!.Value,
MaxCountDocuments = Convert.ToInt32(element.Element("MaxCountDocuments")!.Value),
OpeningDate = Convert.ToDateTime(element.Element("OpeningDate")!.Value),
Documents = element.Element("ShopDocuments")!.Elements("ShopDocument").ToDictionary(
x => Convert.ToInt32(x.Element("Key")?.Value),
x => Convert.ToInt32(x.Element("Value")?.Value)
)
};
}
public void Update(ShopBindingModel? model)
{
if (model == null)
{
return;
}
ShopName = model.ShopName;
Address = model.Address;
MaxCountDocuments = model.MaxCountDocuments;
OpeningDate = model.OpeningDate;
if (model.ShopDocuments.Count > 0)
{
Documents = model.ShopDocuments.ToDictionary(x => x.Key, x => x.Value.Item2);
_shopDocuments = null;
}
}
public ShopViewModel GetViewModel => new()
{
Id = Id,
ShopName = ShopName,
Address = Address,
MaxCountDocuments = MaxCountDocuments,
OpeningDate = OpeningDate,
ShopDocuments = ShopDocuments,
};
public XElement GetXElement => new(
"Shop",
new XAttribute("Id", Id),
new XElement("ShopName", ShopName),
new XElement("Address", Address),
new XElement("MaxCountDocuments", MaxCountDocuments),
new XElement("OpeningDate", OpeningDate.ToString()),
new XElement("ShopDocuments", Documents.Select(x =>
new XElement("ShopDocument",
new XElement("Key", x.Key),
new XElement("Value", x.Value)))
.ToArray()));
}
}
}