начало

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,11 +18,16 @@ 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,6 +51,7 @@ namespace AbstractLawFirmBusinessLogic.BusinessLogic
model.Status = OrderStatus.Принят;
if (_orderStorage.Insert(model) == null)
{
model.Status = OrderStatus.Неизвестен;
_logger.LogWarning("Insert operation failed");
return false;
}
@ -53,7 +60,7 @@ namespace AbstractLawFirmBusinessLogic.BusinessLogic
public bool ChangeStatus(OrderBindingModel model, OrderStatus status)
{
CheckModel(model);
CheckModel(model, false);
var element = _orderStorage.GetElement(new OrderSearchModel { Id = model.Id });
if (element == null)
{
@ -64,10 +71,31 @@ namespace AbstractLawFirmBusinessLogic.BusinessLogic
{
_logger.LogWarning("Status change operation failed");
throw new InvalidOperationException("Текущий статус заказа не может быть переведен в выбранный");
}
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);
if (_orderStorage.Update(model) == null)
{
model.Status--;
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
@ -96,6 +124,10 @@ true)
if (!withParams)
{
return;
}
if (model.DocumentId < 0)
{
throw new ArgumentNullException("Некорректный идентификатор документа", nameof(model.DocumentId));
}
if (model.Sum <= 0)
{
@ -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,6 +113,16 @@ namespace AbstractLawFirmBusinessLogic.BusinessLogic
}
_logger.LogInformation("AddPlaneInShop find. Id:{Id}", element.Id);
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
{
if (element.ShopDocuments.TryGetValue(document.Id, out var pair))
{
element.ShopDocuments[document.Id] = (document, count + pair.Item2);
@ -123,12 +133,14 @@ namespace AbstractLawFirmBusinessLogic.BusinessLogic
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,
MaxCountDocuments = element.MaxCountDocuments,
OpeningDate = element.OpeningDate,
ShopDocuments = element.ShopDocuments
});
@ -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

@ -13,10 +13,13 @@ namespace AbstractLawFirmFileImpliment
private static DataFileSingleton? instance;
private readonly string ComponentFileName = "Component.xml";
private readonly string OrderFileName = "Order.xml";
private readonly string ProductFileName = "Product.xml";
private readonly string DocumentFileName = "Document.xml";
private readonly string ShopFileName = "Shop.xml";
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)
@ -25,18 +28,16 @@ namespace AbstractLawFirmFileImpliment
}
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);
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(ProductFileName, "Document", x => Document.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)
@ -53,10 +54,9 @@ namespace AbstractLawFirmFileImpliment
{
if (data != null)
{
new XDocument(new XElement(xmlNodeName,
data.Select(selectFunction).ToArray())).Save(filename);
}
new XDocument(new XElement(xmlNodeName, data.Select(selectFunction).ToArray())).Save(filename);
}
}
}
}

View File

@ -21,21 +21,15 @@ namespace AbstractLawFirmFileImplement.Implements
}
public List<ComponentViewModel> GetFullList()
{
return source.Components
.Select(x => x.GetViewModel)
.ToList();
return source.Components.Select(x => x.GetViewModel).ToList();
}
public List<ComponentViewModel> GetFilteredList(ComponentSearchModel
model)
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();
return source.Components.Where(x => x.ComponentName.Contains(model.ComponentName)).Select(x => x.GetViewModel).ToList();
}
public ComponentViewModel? GetElement(ComponentSearchModel model)
{
@ -43,17 +37,14 @@ namespace AbstractLawFirmFileImplement.Implements
{
return null;
}
return source.Components
.FirstOrDefault(x =>
return source.Components.FirstOrDefault(x =>
(!string.IsNullOrEmpty(model.ComponentName) && x.ComponentName ==
model.ComponentName) ||
(model.Id.HasValue && x.Id == model.Id))
?.GetViewModel;
(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;
model.Id = source.Components.Count > 0 ? source.Components.Max(x => x.Id) + 1 : 1;
var newComponent = Component.Create(model);
if (newComponent == null)
{
@ -65,8 +56,7 @@ namespace AbstractLawFirmFileImplement.Implements
}
public ComponentViewModel? Update(ComponentBindingModel model)
{
var component = source.Components.FirstOrDefault(x => x.Id ==
model.Id);
var component = source.Components.FirstOrDefault(x => x.Id == model.Id);
if (component == null)
{
return null;
@ -77,8 +67,7 @@ namespace AbstractLawFirmFileImplement.Implements
}
public ComponentViewModel? Delete(ComponentBindingModel model)
{
var element = source.Components.FirstOrDefault(x => x.Id ==
model.Id);
var element = source.Components.FirstOrDefault(x => x.Id == model.Id);
if (element != null)
{
source.Components.Remove(element);

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

@ -58,9 +58,7 @@ namespace AbstractLawFirmFileImplement.Models
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 =>
Components = element.Element("DocumentComponents")!.Elements("DocumentComponent").ToDictionary(x =>
Convert.ToInt32(x.Element("Key")?.Value), x =>
Convert.ToInt32(x.Element("Value")?.Value))
};
@ -73,8 +71,7 @@ namespace AbstractLawFirmFileImplement.Models
}
DocumentName = model.DocumentName;
Price = model.Price;
Components = model.DocumentComponents.ToDictionary(x => x.Key, x =>
x.Value.Item2);
Components = model.DocumentComponents.ToDictionary(x => x.Key, x => x.Value.Item2);
_documentComponents = null;
}
public DocumentViewModel GetViewModel => new()
@ -89,8 +86,6 @@ namespace AbstractLawFirmFileImplement.Models
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()));
new XElement("DocumentComponent", new XElement("Key", x.Key), new XElement("Value", x.Value))).ToArray()));
}
}

View File

@ -14,30 +14,38 @@ 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 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()
return new Order
{
Id = model.Id,
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)
@ -55,29 +63,26 @@ namespace AbstractLawFirmFileImplement.Models
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,
Id = Id,
Status = Status,
};
public XElement GetXElement => new(

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()));
}
}
}