Добавил поле с заполненностбю магазинов, баг так и не решил

This commit is contained in:
Никита Потапов 2024-05-20 09:57:39 +04:00
parent 131a107746
commit d51c8ddcab
5 changed files with 44 additions and 22 deletions

View File

@ -192,7 +192,7 @@ namespace SecuritySystemBusinessLogic.BusinessLogics
foreach (var shop in shops)
{
int shopFreeSpace = GetFreeSpace(shop.Id);
if (shopFreeSpace > 0)
if (shopFreeSpace > 0 && count > 0)
{
int min = Math.Min(count, shopFreeSpace);
count -= min;

View File

@ -12,6 +12,8 @@ namespace SecuritySystemContracts.ViewModels
public string Address { get; set; } = string.Empty;
[DisplayName("Вместимость")]
public int MaxSecuresCount { get; set; }
[DisplayName("Заполненность")]
public int UsedSpace { get; set; }
[DisplayName("Дата открытия")]
public DateTime OpeningDate { get; set; }
public Dictionary<int, (ISecureModel, int)> ShopSecures { get; set; } = new();

View File

@ -113,10 +113,9 @@ namespace SecuritySystemDatabaseImplement.Implements
}
shop.Update(model);
context.SaveChanges();
if (model.ShopSecures.Count > 0)
{
shop.UpdateSecures(context, model);
}
shop.UpdateSecures(context, model);
transaction.Commit();
return shop.GetViewModel;
}

View File

@ -3,7 +3,6 @@ using SecuritySystemContracts.ViewModels;
using SecuritySystemDataModels.Models;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Diagnostics;
namespace SecuritySystemDatabaseImplement.Models
{
@ -21,14 +20,31 @@ namespace SecuritySystemDatabaseImplement.Models
[ForeignKey("ShopId")]
public virtual List<ShopSecure> Secures { get; set; } = new();
private Dictionary<int, (ISecureModel, int)>? _shopSecures = null;
[NotMapped]
private int UsedSpace { get; set; }
[NotMapped]
public Dictionary<int, (ISecureModel, int)> ShopSecures
{
get
{
if (_shopSecures == null)
{
_shopSecures = Secures
.ToDictionary(shopSecure => shopSecure.SecureId, shopSecure => (shopSecure.Secure as ISecureModel, shopSecure.Count));
// TODO
try
{
_shopSecures = Secures
.ToDictionary(
shopSecure => shopSecure.SecureId,
shopSecure => (shopSecure.Secure as ISecureModel, shopSecure.Count)
);
}
catch (Exception)
{
_shopSecures = new();
throw;
}
UsedSpace = _shopSecures.Select(x => x.Value.Item2).Sum();
}
return _shopSecures;
}
@ -63,17 +79,22 @@ namespace SecuritySystemDatabaseImplement.Models
Address = Address,
OpeningDate = OpeningDate,
MaxSecuresCount = MaxSecuresCount,
ShopSecures = ShopSecures
ShopSecures = ShopSecures,
UsedSpace = UsedSpace
};
public void UpdateSecures(SecuritySystemDatabase context, ShopBindingModel model)
{
var shopSecures = context.ShopSecures.Where(rec => rec.SecureId == model.Id).ToList();
// TODO
//context.ShopSecures.RemoveRange(context.ShopSecures.ToList());
//context.SaveChanges();
var shopSecures = context.ShopSecures.Where(shopSecure => shopSecure.SecureId == model.Id).ToList();
if (shopSecures != null && shopSecures.Count > 0)
{
// удалили те, которых нет в модели
context.ShopSecures.RemoveRange(shopSecures.Where(rec => !model.ShopSecures.ContainsKey(rec.SecureId)));
context.ShopSecures.RemoveRange(shopSecures.Where(shopSecure => !model.ShopSecures.ContainsKey(shopSecure.SecureId) || shopSecure.Count == 0));
context.SaveChanges();
shopSecures = context.ShopSecures.Where(rec => rec.ShopId == model.Id).ToList();
// обновили количество у существующих записей
foreach (var updateSecure in shopSecures)
{

View File

@ -34,13 +34,13 @@
SecuresToolStripMenuItem = new ToolStripMenuItem();
магазиныToolStripMenuItem = new ToolStripMenuItem();
пополнениеМагазинаToolStripMenuItem = new ToolStripMenuItem();
продатьПродукциюToolStripMenuItem = new ToolStripMenuItem();
dataGridView = new DataGridView();
buttonCreateOrder = new Button();
buttonTakeOrderInWork = new Button();
buttonOrderReady = new Button();
button4 = new Button();
buttonRefresh = new Button();
продатьПродукциюToolStripMenuItem = new ToolStripMenuItem();
menuStrip.SuspendLayout();
((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit();
SuspendLayout();
@ -65,21 +65,21 @@
// ComponentsToolStripMenuItem
//
ComponentsToolStripMenuItem.Name = "ComponentsToolStripMenuItem";
ComponentsToolStripMenuItem.Size = new Size(182, 26);
ComponentsToolStripMenuItem.Size = new Size(224, 26);
ComponentsToolStripMenuItem.Text = "Компоненты";
ComponentsToolStripMenuItem.Click += ComponentsToolStripMenuItem_Click;
//
// SecuresToolStripMenuItem
//
SecuresToolStripMenuItem.Name = "SecuresToolStripMenuItem";
SecuresToolStripMenuItem.Size = new Size(182, 26);
SecuresToolStripMenuItem.Size = new Size(224, 26);
SecuresToolStripMenuItem.Text = "Изделия";
SecuresToolStripMenuItem.Click += SecuresToolStripMenuItem_Click;
//
// магазиныToolStripMenuItem
//
магазиныToolStripMenuItem.Name = агазиныToolStripMenuItem";
магазиныToolStripMenuItem.Size = new Size(182, 26);
магазиныToolStripMenuItem.Size = new Size(224, 26);
магазиныToolStripMenuItem.Text = "Магазины";
магазиныToolStripMenuItem.Click += ShopsToolStripMenuItem_Click;
//
@ -90,6 +90,13 @@
пополнениеМагазинаToolStripMenuItem.Text = "Пополнение магазина";
пополнениеМагазинаToolStripMenuItem.Click += SupplyShopToolStripMenuItem_Click;
//
// продатьПродукциюToolStripMenuItem
//
продатьПродукциюToolStripMenuItem.Name = "продатьПродукциюToolStripMenuItem";
продатьПродукциюToolStripMenuItem.Size = new Size(165, 24);
продатьПродукциюToolStripMenuItem.Text = "Продать продукцию";
продатьПродукциюToolStripMenuItem.Click += продатьПродукциюToolStripMenuItem_Click;
//
// dataGridView
//
dataGridView.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
@ -159,13 +166,6 @@
buttonRefresh.UseVisualStyleBackColor = true;
buttonRefresh.Click += ButtonRefresh_Click;
//
// продатьПродукциюToolStripMenuItem
//
продатьПродукциюToolStripMenuItem.Name = "продатьПродукциюToolStripMenuItem";
продатьПродукциюToolStripMenuItem.Size = new Size(165, 24);
продатьПродукциюToolStripMenuItem.Text = "Продать продукцию";
продатьПродукциюToolStripMenuItem.Click += продатьПродукциюToolStripMenuItem_Click;
//
// FormMain
//
AutoScaleDimensions = new SizeF(8F, 20F);