некоторые изменения

This commit is contained in:
Ino 2023-03-13 13:25:55 +04:00
parent cff88300c0
commit c07a77636f
11 changed files with 170 additions and 182 deletions

View File

@ -10,6 +10,7 @@ using IceCreamShopContracts.SearchModels;
using IceCreamShopContracts.StoragesContracts; using IceCreamShopContracts.StoragesContracts;
using IceCreamShopContracts.ViewModels; using IceCreamShopContracts.ViewModels;
using AbstractIceCreamShopDataModels.Enums; using AbstractIceCreamShopDataModels.Enums;
using AbstractIceCreamShopDataModels.Models;
namespace IceCreamBusinessLogic.BusinessLogics namespace IceCreamBusinessLogic.BusinessLogics
{ {
@ -17,12 +18,14 @@ namespace IceCreamBusinessLogic.BusinessLogics
{ {
private readonly ILogger _logger; private readonly ILogger _logger;
private readonly IOrderStorage _orderStorage; private readonly IOrderStorage _orderStorage;
private readonly IShopStorage _shopStorage;
private readonly IShopLogic _shopLogic; private readonly IShopLogic _shopLogic;
private readonly IIceCreamStorage _iceCreamStorage; private readonly IIceCreamStorage _iceCreamStorage;
public OrderLogic(IOrderStorage orderStorage, IShopLogic shopLogic, IIceCreamStorage iceCreamStorage, ILogger<OrderLogic> logger) public OrderLogic(IOrderStorage orderStorage, IShopStorage shopStorage, IShopLogic shopLogic, IIceCreamStorage iceCreamStorage, ILogger<OrderLogic> logger)
{ {
_orderStorage = orderStorage; _orderStorage = orderStorage;
_shopStorage = shopStorage;
_logger = logger; _logger = logger;
_shopLogic = shopLogic; _shopLogic = shopLogic;
_iceCreamStorage = iceCreamStorage; _iceCreamStorage = iceCreamStorage;
@ -111,43 +114,109 @@ namespace IceCreamBusinessLogic.BusinessLogics
throw new ArgumentNullException(nameof(orderModel)); throw new ArgumentNullException(nameof(orderModel));
} }
if (viewModel.Status + 1 != newStatus) OrderBindingModel model = new OrderBindingModel
{
Id = viewModel.Id,
IceCreamId = viewModel.IceCreamId,
Status = viewModel.Status,
DateCreate = viewModel.DateCreate,
DateImplement = viewModel.DateImplement,
Count = viewModel.Count,
Sum = viewModel.Sum
};
CheckModel(model);
if (model.Status + 1 != newStatus)
{ {
_logger.LogWarning("Status update to " + newStatus.ToString() + " operation failed. Order status incorrect."); _logger.LogWarning("Status update to " + newStatus.ToString() + " operation failed. Order status incorrect.");
return false; return false;
} }
orderModel.Status = newStatus;
if (orderModel.Status == OrderStatus.Готов) if (newStatus == OrderStatus.Готов)
{ {
orderModel.DateImplement = DateTime.Now; var icecream = _iceCreamStorage.GetElement(new IceCreamSearchModel() { Id = model.IceCreamId });
if (icecream == null)
var iceCream = _iceCreamStorage.GetElement(new() { Id = viewModel.IceCreamId });
if (iceCream == null)
{ {
throw new ArgumentNullException(nameof(iceCream)); _logger.LogWarning("Status update to " + newStatus.ToString() + " operation failed. Document not found.");
return false;
} }
if (CheckSupply(icecream, model.Count) == false)
if (!_shopLogic.SupplyIceCreams(iceCream, viewModel.Count))
{ {
throw new Exception($"SupplyIceCreams operation failed. Store is full."); _logger.LogWarning("Status update to " + newStatus.ToString() + " operation failed. Shop supply error.");
return false;
} }
} }
else
{
orderModel.DateImplement = viewModel.DateImplement;
}
CheckModel(orderModel); model.Status = newStatus;
if (model.Status == OrderStatus.Выдан) model.DateImplement = DateTime.Now;
if (_orderStorage.Update(orderModel) == null) if (_orderStorage.Update(model) == null)
{ {
orderModel.Status--; model.Status--;
_logger.LogWarning("Update operation failed"); _logger.LogWarning("Update operation failed");
return false; return false;
} }
return true; return true;
} }
public bool CheckSupply(IIceCreamModel iceCream, int count)
{
if (count <= 0)
{
_logger.LogWarning("Check then supply operation error. IceCream count < 0.");
return false;
}
int freeSpace = 0;
foreach (var shop in _shopStorage.GetFullList())
{
freeSpace += shop.IceCreamMaxCount;
foreach (var doc in shop.ShopIceCreams)
{
freeSpace -= doc.Value.Item2;
}
}
if (freeSpace - count < 0)
{
_logger.LogWarning("Check then supply operation error. There's no place for new IceCream in shops.");
return false;
}
foreach (var shop in _shopStorage.GetFullList())
{
freeSpace = shop.IceCreamMaxCount;
foreach (var doc in shop.ShopIceCreams)
{
freeSpace -= doc.Value.Item2;
}
if (freeSpace == 0)
{
continue;
}
if (freeSpace - count >= 0)
{
if (_shopLogic.SupplyIceCreams(new() { Id = shop.Id }, iceCream, count)) count = 0;
else
{
_logger.LogWarning("Supply error");
return false;
}
}
if (freeSpace - count < 0)
{
if (_shopLogic.SupplyIceCreams(new() { Id = shop.Id }, iceCream, freeSpace)) count -= freeSpace;
else
{
_logger.LogWarning("Supply error");
return false;
}
}
if (count <= 0)
{
return true;
}
}
return false;
}
} }
} }

View File

@ -11,6 +11,7 @@ using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Reflection.Metadata; using System.Reflection.Metadata;
using System.Xml.Linq;
namespace IceCreamBusinessLogic.BusinessLogics namespace IceCreamBusinessLogic.BusinessLogics
{ {
@ -118,56 +119,9 @@ namespace IceCreamBusinessLogic.BusinessLogics
return true; return true;
} }
public bool SupplyIceCreams(IIceCreamModel iceCream, int count) public bool SellIceCreams(IIceCreamModel iceCream, int count)
{ {
if (iceCream == null) return _shopStorage.SellIceCreams(iceCream, count);
{
throw new ArgumentNullException(nameof(iceCream));
}
if (count <= 0)
{
throw new ArgumentException("Count of icecreams in supply must be more than 0", nameof(count));
}
var freePlaces = _shopStorage.GetFullList()
.Select(x => x.IceCreamMaxCount - x.ShopIceCreams
.Select(p => p.Value.Item2).Sum()).Sum() - count;
if (freePlaces < 0)
{
_logger.LogInformation("SupplyIceCreams. Failed to add icecreams to store. It's full.");
return false;
}
foreach (var shop in _shopStorage.GetFullList())
{
var temp = Math.Min(count, shop.IceCreamMaxCount - shop.ShopIceCreams.Select(x => x.Value.Item2).Sum());
if (temp <= 0)
{
continue;
}
if (!SupplyIceCreams(new() { Id = shop.Id }, iceCream, temp))
{
_logger.LogWarning("An error occurred while adding iceCream to stores");
return false;
}
count -= temp;
if (count == 0)
{
return true;
}
}
return true;
}
public bool SellIceCreams(IIceCreamModel package, int quantity)
{
return _shopStorage.SellIceCreams(package, quantity);
} }
public bool Create(ShopBindingModel model) public bool Create(ShopBindingModel model)

View File

@ -67,6 +67,7 @@
this.buttonEdit.TabIndex = 8; this.buttonEdit.TabIndex = 8;
this.buttonEdit.Text = "Изменить"; this.buttonEdit.Text = "Изменить";
this.buttonEdit.UseVisualStyleBackColor = true; this.buttonEdit.UseVisualStyleBackColor = true;
this.buttonEdit.Click += new System.EventHandler(this.buttonEdit_Click);
// //
// buttonAdd // buttonAdd
// //

View File

@ -102,6 +102,7 @@
this.Controls.Add(this.IceCreamLabel); this.Controls.Add(this.IceCreamLabel);
this.Name = "FormSellIceCream"; this.Name = "FormSellIceCream";
this.Text = "Продать мороженое"; this.Text = "Продать мороженое";
this.Load += new System.EventHandler(this.FormSellIceCream_Load);
this.ResumeLayout(false); this.ResumeLayout(false);
this.PerformLayout(); this.PerformLayout();

View File

@ -1,44 +1,27 @@
using IceCreamShopContracts.SearchModels; using IceCreamShopContracts.SearchModels;
using IceCreamShopContracts.BusinessLogicsContracts; using IceCreamShopContracts.BusinessLogicsContracts;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using IceCreamShopContracts.BindingModels;
namespace IceCreamShopView namespace IceCreamShopView
{ {
public partial class FormSellIceCream : Form public partial class FormSellIceCream : Form
{ {
private readonly ILogger _logger; private readonly ILogger _logger;
private readonly IIceCreamLogic _logicPackage; private readonly IIceCreamLogic _logicI;
private readonly IShopLogic _logicStore; private readonly IShopLogic _logicS;
public FormSellIceCream(ILogger<FormSellIceCream> logger, IIceCreamLogic logicIceCream, IShopLogic logicShop) public FormSellIceCream(ILogger<FormSellIceCream> logger, IIceCreamLogic logicIceCream, IShopLogic logicShop)
{ {
InitializeComponent(); InitializeComponent();
_logger = logger; _logger = logger;
_logicPackage = logicIceCream; _logicI = logicIceCream;
_logicStore = logicShop; _logicS = logicShop;
LoadData(); LoadData();
} }
private void LoadData() private void LoadData()
{ {
_logger.LogInformation("Loading icecream for sale.");
try
{
var list = _logicPackage.ReadList(null);
if (list != null)
{
IceCreamСomboBox.DisplayMember = "IceCreamName";
IceCreamСomboBox.ValueMember = "Id";
IceCreamСomboBox.DataSource = list;
IceCreamСomboBox.SelectedItem = null;
}
}
catch (Exception ex)
{
_logger.LogError(ex, "List loading error.");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
} }
private void SaveButton_Click(object sender, EventArgs e) private void SaveButton_Click(object sender, EventArgs e)
@ -59,10 +42,13 @@ namespace IceCreamShopView
try try
{ {
var operationResult = _logicStore.SellIceCreams(_logicPackage.ReadElement(new IceCreamSearchModel() var operationResult = _logicS.SellIceCreams(
{ new IceCreamBindingModel
Id = Convert.ToInt32(IceCreamСomboBox.SelectedValue) {
})!, Convert.ToInt32(QuantityTextBox.Text)); Id = Convert.ToInt32(IceCreamСomboBox.SelectedValue)
},
Convert.ToInt32(QuantityTextBox.Text)
);
if (!operationResult) if (!operationResult)
{ {
@ -86,5 +72,27 @@ namespace IceCreamShopView
DialogResult = DialogResult.Cancel; DialogResult = DialogResult.Cancel;
Close(); Close();
} }
private void FormSellIceCream_Load(object sender, EventArgs e)
{
_logger.LogInformation("Loading icecream for sale.");
try
{
var list = _logicI.ReadList(null);
if (list != null)
{
IceCreamСomboBox.DisplayMember = "IceCreamName";
IceCreamСomboBox.ValueMember = "Id";
IceCreamСomboBox.DataSource = list;
IceCreamСomboBox.SelectedItem = null;
}
}
catch (Exception ex)
{
_logger.LogError(ex, "List loading error.");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
} }
} }

View File

@ -31,7 +31,7 @@
this.buttonCancel = new System.Windows.Forms.Button(); this.buttonCancel = new System.Windows.Forms.Button();
this.buttonSave = new System.Windows.Forms.Button(); this.buttonSave = new System.Windows.Forms.Button();
this.textBoxCount = new System.Windows.Forms.TextBox(); this.textBoxCount = new System.Windows.Forms.TextBox();
this.comboBoxDocument = new System.Windows.Forms.ComboBox(); this.comboBoxIceCream = new System.Windows.Forms.ComboBox();
this.comboBoxShop = new System.Windows.Forms.ComboBox(); this.comboBoxShop = new System.Windows.Forms.ComboBox();
this.labelDocumentCount = new System.Windows.Forms.Label(); this.labelDocumentCount = new System.Windows.Forms.Label();
this.labelDocument = new System.Windows.Forms.Label(); this.labelDocument = new System.Windows.Forms.Label();
@ -68,14 +68,14 @@
this.textBoxCount.Size = new System.Drawing.Size(110, 23); this.textBoxCount.Size = new System.Drawing.Size(110, 23);
this.textBoxCount.TabIndex = 15; this.textBoxCount.TabIndex = 15;
// //
// comboBoxDocument // comboBoxIceCream
// //
this.comboBoxDocument.FormattingEnabled = true; this.comboBoxIceCream.FormattingEnabled = true;
this.comboBoxDocument.Location = new System.Drawing.Point(115, 49); this.comboBoxIceCream.Location = new System.Drawing.Point(115, 49);
this.comboBoxDocument.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); this.comboBoxIceCream.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2);
this.comboBoxDocument.Name = "comboBoxDocument"; this.comboBoxIceCream.Name = "comboBoxIceCream";
this.comboBoxDocument.Size = new System.Drawing.Size(220, 23); this.comboBoxIceCream.Size = new System.Drawing.Size(220, 23);
this.comboBoxDocument.TabIndex = 14; this.comboBoxIceCream.TabIndex = 14;
// //
// comboBoxShop // comboBoxShop
// //
@ -122,7 +122,7 @@
this.Controls.Add(this.buttonCancel); this.Controls.Add(this.buttonCancel);
this.Controls.Add(this.buttonSave); this.Controls.Add(this.buttonSave);
this.Controls.Add(this.textBoxCount); this.Controls.Add(this.textBoxCount);
this.Controls.Add(this.comboBoxDocument); this.Controls.Add(this.comboBoxIceCream);
this.Controls.Add(this.comboBoxShop); this.Controls.Add(this.comboBoxShop);
this.Controls.Add(this.labelDocumentCount); this.Controls.Add(this.labelDocumentCount);
this.Controls.Add(this.labelDocument); this.Controls.Add(this.labelDocument);
@ -140,7 +140,7 @@
private Button buttonCancel; private Button buttonCancel;
private Button buttonSave; private Button buttonSave;
private TextBox textBoxCount; private TextBox textBoxCount;
private ComboBox comboBoxDocument; private ComboBox comboBoxIceCream;
private ComboBox comboBoxShop; private ComboBox comboBoxShop;
private Label labelDocumentCount; private Label labelDocumentCount;
private Label labelDocument; private Label labelDocument;

View File

@ -33,7 +33,7 @@ namespace IceCreamShopView
MessageBox.Show("Заполните поле Количество", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); MessageBox.Show("Заполните поле Количество", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return; return;
} }
if (comboBoxDocument.SelectedValue == null) if (comboBoxIceCream.SelectedValue == null)
{ {
MessageBox.Show("Выберите мороженое", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); MessageBox.Show("Выберите мороженое", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return; return;
@ -54,8 +54,8 @@ namespace IceCreamShopView
}, },
new IceCreamBindingModel new IceCreamBindingModel
{ {
Id = Convert.ToInt32(comboBoxDocument.SelectedValue), Id = Convert.ToInt32(comboBoxIceCream.SelectedValue),
IceCreamName = comboBoxDocument.Text IceCreamName = comboBoxIceCream.Text
}, },
Convert.ToInt32(textBoxCount.Text) Convert.ToInt32(textBoxCount.Text)
); );
@ -88,10 +88,10 @@ namespace IceCreamShopView
var list = _logicI.ReadList(null); var list = _logicI.ReadList(null);
if (list != null) if (list != null)
{ {
comboBoxDocument.DisplayMember = "IceCreamName"; comboBoxIceCream.DisplayMember = "IceCreamName";
comboBoxDocument.ValueMember = "Id"; comboBoxIceCream.ValueMember = "Id";
comboBoxDocument.DataSource = list; comboBoxIceCream.DataSource = list;
comboBoxDocument.SelectedItem = null; comboBoxIceCream.SelectedItem = null;
} }
} }

View File

@ -18,7 +18,6 @@ namespace IceCreamShopContracts.BusinessLogicsContracts
bool Update(ShopBindingModel model); bool Update(ShopBindingModel model);
bool Delete(ShopBindingModel model); bool Delete(ShopBindingModel model);
bool SupplyIceCreams(ShopSearchModel model, IIceCreamModel iceCream, int count); bool SupplyIceCreams(ShopSearchModel model, IIceCreamModel iceCream, int count);
bool SupplyIceCreams(IIceCreamModel iceCream, int count);
bool SellIceCreams(IIceCreamModel iceCream, int count); bool SellIceCreams(IIceCreamModel iceCream, int count);
} }
} }

View File

@ -60,14 +60,14 @@ namespace IceCreamShopFileImplement.Implements
public IceCreamViewModel? Update(IceCreamBindingModel model) public IceCreamViewModel? Update(IceCreamBindingModel model)
{ {
var document = source.IceCreams.FirstOrDefault(x => x.Id == model.Id); var iceCream = source.IceCreams.FirstOrDefault(x => x.Id == model.Id);
if (document == null) if (iceCream == null)
{ {
return null; return null;
} }
document.Update(model); iceCream.Update(model);
source.SaveIceCreams(); source.SaveIceCreams();
return document.GetViewModel; return iceCream.GetViewModel;
} }
public IceCreamViewModel? Delete(IceCreamBindingModel model) public IceCreamViewModel? Delete(IceCreamBindingModel model)
{ {

View File

@ -4,6 +4,7 @@ using IceCreamShopContracts.SearchModels;
using IceCreamShopContracts.ViewModels; using IceCreamShopContracts.ViewModels;
using AbstractIceCreamShopDataModels.Models; using AbstractIceCreamShopDataModels.Models;
using IceCreamShopFileImplement.Models; using IceCreamShopFileImplement.Models;
using System.Reflection.Metadata;
namespace IceCreamShopFileImplement.Implements namespace IceCreamShopFileImplement.Implements
{ {
@ -19,13 +20,13 @@ namespace IceCreamShopFileImplement.Implements
public ShopViewModel? Delete(ShopBindingModel model) public ShopViewModel? Delete(ShopBindingModel model)
{ {
var element = source.Shops.FirstOrDefault(x => x.Id == model.Id); var element = source.Shops.FirstOrDefault(x => x.Id == model.Id);
if (element != null) if (element == null)
{ {
source.Shops.Remove(element); return null;
source.SaveShops();
return element.GetViewModel;
} }
return null; source.Shops.Remove(element);
source.SaveShops();
return element.GetViewModel;
} }
public ShopViewModel? GetElement(ShopSearchModel model) public ShopViewModel? GetElement(ShopSearchModel model)
@ -79,52 +80,24 @@ namespace IceCreamShopFileImplement.Implements
return store.GetViewModel; return store.GetViewModel;
} }
/* public bool SellIceCreams(IIceCreamModel model, int count)
{
if (source.Shops.Select(x => x.ShopIceCreams.FirstOrDefault(y => y.Key == model.Id).Value.Item2).Sum() < count)
{
return false;
}
foreach (var store in source.Shops.Where(x => x.ShopIceCreams.ContainsKey(model.Id)))
{
int QuantityInCurrentShop = store.ShopIceCreams[model.Id].Item2;
if (QuantityInCurrentShop <= count)
{
store.ShopIceCreams.Remove(model.Id);
count -= QuantityInCurrentShop;
}
else
{
store.ShopIceCreams[model.Id] = (store.ShopIceCreams[model.Id].Item1, QuantityInCurrentShop - count);
count = 0;
}
if (count == 0)
{
return true;
}
}
return false;
}*/
public bool SellIceCreams(IIceCreamModel model, int count) public bool SellIceCreams(IIceCreamModel model, int count)
{ {
var icecream = source.IceCreams.FirstOrDefault(x => x.Id == model.Id); var iceCream = source.IceCreams.FirstOrDefault(x => x.Id == model.Id);
var countStore = count; var countStore = count;
if (icecream == null) if (iceCream == null)
{ {
return false; return false;
} }
foreach (var shop in source.Shops) foreach (var shop in source.Shops)
{ {
foreach (var doc in shop.ShopIceCreams) foreach (var icecream in shop.ShopIceCreams)
{ {
if (doc.Value.Item1.Id == icecream.Id) if (icecream.Value.Item1.Id == iceCream.Id)
{ {
count -= doc.Value.Item2; count -= icecream.Value.Item2;
} }
if (count <= 0) if (count <= 0)
{ {
@ -145,33 +118,15 @@ namespace IceCreamShopFileImplement.Implements
var shop = source.Shops[i]; var shop = source.Shops[i];
var icecreams = shop.ShopIceCreams; var icecreams = shop.ShopIceCreams;
for (int j = 1; j < icecreams.Count + 1; j++) foreach (var icecream in icecreams.Where(x => x.Value.Item1.Id == iceCream.Id))
{ {
if (icecreams[j].Item1.Id == icecream.Id) var min = Math.Min(icecream.Value.Item2, count);
{ icecreams[icecream.Value.Item1.Id] = (icecream.Value.Item1, icecream.Value.Item2 - min);
while (icecreams[j].Item2 > 0 && count > 0) count -= min;
{
int tempItem2 = icecreams[j].Item2;
tempItem2--;
count--;
icecreams[j] = (icecreams[j].Item1, tempItem2);
}
}
if (count <= 0) if (count <= 0)
{ {
shop.Update(new ShopBindingModel break;
{
Id = shop.Id,
Name = shop.Name,
Adress = shop.Adress,
OpeningDate = shop.OpeningDate,
IceCreamMaxCount = shop.IceCreamMaxCount,
ShopIceCreams = icecreams
});
source.SaveShops();
return true;
} }
} }

View File

@ -2,6 +2,7 @@
using IceCreamShopContracts.ViewModels; using IceCreamShopContracts.ViewModels;
using AbstractIceCreamShopDataModels.Models; using AbstractIceCreamShopDataModels.Models;
using System.Xml.Linq; using System.Xml.Linq;
using System.Reflection.Metadata;
namespace IceCreamShopFileImplement.Models namespace IceCreamShopFileImplement.Models
@ -20,7 +21,7 @@ namespace IceCreamShopFileImplement.Models
public Dictionary<int, int> IceCreams { get; private set; } = new(); public Dictionary<int, int> IceCreams { get; private set; } = new();
public Dictionary<int, (IIceCreamModel, int)>? _shopIceCreams = null; private Dictionary<int, (IIceCreamModel, int)>? _shopIceCreams = null;
public Dictionary<int, (IIceCreamModel, int)> ShopIceCreams public Dictionary<int, (IIceCreamModel, int)> ShopIceCreams
{ {
@ -86,7 +87,7 @@ namespace IceCreamShopFileImplement.Models
if(model.ShopIceCreams.Count > 0) if(model.ShopIceCreams.Count > 0)
{ {
IceCreams = model.ShopIceCreams.ToDictionary(x => x.Key, x => x.Value.Item2); IceCreams = model.ShopIceCreams.ToDictionary(x => x.Key, x => x.Value.Item2);
_shopIceCreams = null; _shopIceCreams = model.ShopIceCreams;
} }
} }