This commit is contained in:
Кашин Максим 2023-03-28 10:53:40 +04:00
parent 21e9d8ed5c
commit e78c13eb51
13 changed files with 180 additions and 36 deletions

View File

@ -116,7 +116,7 @@ namespace ConfectioneryView
textBoxAddress.Text = view.Address;
textBoxDateOpening.Text = view.DateOpening.ToString();
VolumeNumericUpDown.Value = view.ReinforcedMaxCount;
_listShops = view.ShopReinforcedies ?? new Dictionary<int, (IReinforcedModel, int)>();
_listShops = view.Reinforcedies ?? new Dictionary<int, (IReinforcedModel, int)>();
LoadData();
}
}

View File

@ -57,7 +57,7 @@ namespace PrecastConcretePlantBusinessLogic.BusinessLogic
public bool Create(ShopBindingModel model)
{
CheckModel(model);
model.ShopReinforcedies = new();
model.Reinforcedies = new();
if (_shopStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
@ -148,21 +148,21 @@ namespace PrecastConcretePlantBusinessLogic.BusinessLogic
return false;
}
if (element.ReinforcedMaxCount - element.ShopReinforcedies.Select(x => x.Value.Item2).Sum() < count)
if (element.ReinforcedMaxCount - element.Reinforcedies.Select(x => x.Value.Item2).Sum() < count)
{
throw new ArgumentNullException("Магазин переполнен", nameof(count));
}
_logger.LogInformation("AddReinforced find. Id:{Id}", element.Id);
if (element.ShopReinforcedies.TryGetValue(reinforced.Id, out var pair))
if (element.Reinforcedies.TryGetValue(reinforced.Id, out var pair))
{
element.ShopReinforcedies[reinforced.Id] = (reinforced, count + pair.Item2);
element.Reinforcedies[reinforced.Id] = (reinforced, count + pair.Item2);
_logger.LogInformation("AddReinforced. Added {count} {reinforced} to '{Name}' shop", count, reinforced.ReinforcedName, element.Name);
}
else
{
element.ShopReinforcedies[reinforced.Id] = (reinforced, count);
element.Reinforcedies[reinforced.Id] = (reinforced, count);
_logger.LogInformation("AddReinforced. Added {count} new reinforced {reinforced} to '{Name}' shop", count, reinforced.ReinforcedName, element.Name);
}
@ -173,7 +173,7 @@ namespace PrecastConcretePlantBusinessLogic.BusinessLogic
Name = element.Name,
DateOpening = element.DateOpening,
ReinforcedMaxCount = element.ReinforcedMaxCount,
ShopReinforcedies = element.ShopReinforcedies,
Reinforcedies = element.Reinforcedies,
});
return true;
@ -191,7 +191,7 @@ namespace PrecastConcretePlantBusinessLogic.BusinessLogic
}
var freePlaces = _shopStorage.GetFullList()
.Select(x => x.ReinforcedMaxCount - x.ShopReinforcedies
.Select(x => x.ReinforcedMaxCount - x.Reinforcedies
.Select(p => p.Value.Item2).Sum()).Sum() - count;
if (freePlaces < 0)
@ -202,7 +202,7 @@ namespace PrecastConcretePlantBusinessLogic.BusinessLogic
foreach (var shop in _shopStorage.GetFullList())
{
var temp = Math.Min(count, shop.ReinforcedMaxCount - shop.ShopReinforcedies.Select(x => x.Value.Item2).Sum());
var temp = Math.Min(count, shop.ReinforcedMaxCount - shop.Reinforcedies.Select(x => x.Value.Item2).Sum());
if (temp <= 0)
{

View File

@ -12,13 +12,13 @@ namespace PrecastConcretePlantContracts.BindingModels
public string Name { get; set; } = string.Empty;
public string Address { get; set; } = string.Empty;
public int ReinforcedMaxCount { get; set; }
public DateTime DateOpening { get; set; } = DateTime.Now;
public Dictionary<int, (IReinforcedModel, int)> ShopReinforcedies { get; set; } = new();
public Dictionary<int, (IReinforcedModel, int)> Reinforcedies { get; set; } = new();
public int Id { get; set; }
public int ReinforcedMaxCount { get; set; }
}
}

View File

@ -22,7 +22,7 @@ namespace PrecastConcretePlantContracts.ViewModels
[DisplayName("Вместимость магазина")]
public int ReinforcedMaxCount { get; set; }
public Dictionary<int, (IReinforcedModel, int)> ShopReinforcedies { get; set; } = new();
public Dictionary<int, (IReinforcedModel, int)> Reinforcedies { get; set; } = new();
public int Id { get; set; }

View File

@ -10,8 +10,9 @@ namespace PrecastConcretePlantDataModels.Models
{
string Name { get; }
string Address { get; }
DateTime DateOpening { get; }
Dictionary<int, (IReinforcedModel, int)> ShopReinforcedies { get; }
public int ReinforcedMaxCount { get; }
DateTime DateOpening { get; }
Dictionary<int, (IReinforcedModel, int)> Reinforcedies { get; }
}
}

View File

@ -19,11 +19,11 @@ namespace PrecastConcretePlantDatabaseImplement.Implements
{
using var context = new PrecastConcretePlantDataBase();
var element = context.Reinforceds.Include(x => x.Components).FirstOrDefault(rec => rec.Id == model.Id);
var element = context.Reinforcedies.Include(x => x.Components).FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Reinforceds.Remove(element);
context.Reinforcedies.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
@ -40,7 +40,7 @@ namespace PrecastConcretePlantDatabaseImplement.Implements
using var context = new PrecastConcretePlantDataBase();
return context.Reinforceds.Include(x => x.Components).ThenInclude(x => x.Component).FirstOrDefault(x => (!string.IsNullOrEmpty(model.ReinforcedName) && x.ReinforcedName == model.ReinforcedName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
return context.Reinforcedies.Include(x => x.Components).ThenInclude(x => x.Component).FirstOrDefault(x => (!string.IsNullOrEmpty(model.ReinforcedName) && x.ReinforcedName == model.ReinforcedName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
}
public List<ReinforcedViewModel> GetFilteredList(ReinforcedSearchModel model)
@ -52,14 +52,14 @@ namespace PrecastConcretePlantDatabaseImplement.Implements
using var context = new PrecastConcretePlantDataBase();
return context.Reinforceds.Include(x => x.Components).ThenInclude(x => x.Component).Where(x => x.ReinforcedName.Contains(model.ReinforcedName)).ToList().Select(x => x.GetViewModel).ToList();
return context.Reinforcedies.Include(x => x.Components).ThenInclude(x => x.Component).Where(x => x.ReinforcedName.Contains(model.ReinforcedName)).ToList().Select(x => x.GetViewModel).ToList();
}
public List<ReinforcedViewModel> GetFullList()
{
using var context = new PrecastConcretePlantDataBase();
return context.Reinforceds.Include(x => x.Components).ThenInclude(x => x.Component).ToList().Select(x => x.GetViewModel).ToList();
return context.Reinforcedies.Include(x => x.Components).ThenInclude(x => x.Component).ToList().Select(x => x.GetViewModel).ToList();
}
public ReinforcedViewModel? Insert(ReinforcedBindingModel model)
@ -73,7 +73,7 @@ namespace PrecastConcretePlantDatabaseImplement.Implements
return null;
}
context.Reinforceds.Add(newReinforced);
context.Reinforcedies.Add(newReinforced);
context.SaveChanges();
return newReinforced.GetViewModel;
@ -87,7 +87,7 @@ namespace PrecastConcretePlantDatabaseImplement.Implements
try
{
var reinforced = context.Reinforceds.FirstOrDefault(rec => rec.Id == model.Id);
var reinforced = context.Reinforcedies.FirstOrDefault(rec => rec.Id == model.Id);
if (reinforced == null)
{

View File

@ -87,7 +87,7 @@ namespace PrecastConcretePlantDatabaseImplement.Models
context.SaveChanges();
}
var reinforced = context.Reinforceds.First(x => x.Id == Id);
var reinforced = context.Reinforcedies.First(x => x.Id == Id);
foreach (var pc in model.ReinforcedComponents)
{

View File

@ -0,0 +1,118 @@
using PrecastConcretePlantContracts.BindingModels;
using PrecastConcretePlantContracts.ViewModels;
using PrecastConcretePlantDataModels.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PrecastConcretePlantDatabaseImplement.Models
{
public class Shop : IShopModel
{
[Required]
public string Name { get; private set; } = string.Empty;
[Required]
public string Address { get; private set; } = string.Empty;
[Required]
public int ReinforcedMaxCount { get; private set; }
public DateTime DateOpening { get; private set; }
private Dictionary<int, (IReinforcedModel, int)>? _cachedReinforcedies = null;
[NotMapped]
public Dictionary<int, (IReinforcedModel, int)> Reinforcedies
{
get
{
if (_cachedReinforcedies == null)
{
using var context = new PrecastConcretePlantDataBase();
_cachedReinforcedies = ShopReinforcedies
.ToDictionary(x => x.ReinforcedId, x => (context.Reinforcedies
.FirstOrDefault(y => y.Id == x.ReinforcedId)! as IReinforcedModel, x.Count));
}
return _cachedReinforcedies;
}
}
public int Id { get; private set; }
[ForeignKey("ShopId")]
public virtual List<ShopReinforced> ShopReinforcedies { get; set; } = new();
public static Shop? Create(PrecastConcretePlantDataBase context, ShopBindingModel? model)
{
if (model == null)
{
return null;
}
return new Shop()
{
Id = model.Id,
Name = model.Name,
Address = model.Address,
DateOpening = model.DateOpening,
ReinforcedMaxCount = model.ReinforcedMaxCount,
ShopReinforcedies = model.Reinforcedies.Select(x => new ShopReinforced
{
Reinforced = context.Reinforcedies.FirstOrDefault(y => y.Id == x.Key)!,
Count = x.Value.Item2,
}).ToList()
};
}
public void Update(ShopBindingModel? model)
{
if (model == null)
{
return;
}
Name = model.Name;
Address = model.Address;
DateOpening = model.DateOpening;
}
public ShopViewModel GetViewModel => new()
{
Id = Id,
Name = Name,
Address = Address,
Reinforcedies = Reinforcedies,
DateOpening = DateOpening,
ReinforcedMaxCount = ReinforcedMaxCount,
};
public void UpdateReinforcedies(PrecastConcretePlantDataBase context, ShopBindingModel model)
{
var shopReinforcedies = context.ShopReinforcedies
.Where(rec => rec.ShopId == model.Id)
.ToList();
if (shopReinforcedies != null && shopReinforcedies.Count > 0)
{
context.ShopReinforcedies
.RemoveRange(shopReinforcedies
.Where(rec => !model.Reinforcedies
.ContainsKey(rec.ReinforcedId)));
foreach (var updateReinforced in shopReinforcedies.Where(x => model.Reinforcedies.ContainsKey(x.ReinforcedId)))
{
updateReinforced.Count = model.Reinforcedies[updateReinforced.ReinforcedId].Item2;
model.Reinforcedies.Remove(updateReinforced.ReinforcedId);
}
}
var shop = context.Shops.First(x => x.Id == model.Id);
shop.ShopReinforcedies.AddRange(model.Reinforcedies.Select(x => new ShopReinforced
{
Reinforced = context.Reinforcedies.First(y => y.Id == x.Key),
Count = x.Value.Item2,
}).Except(shopReinforcedies ?? new()));
context.SaveChanges();
_cachedReinforcedies = null;
}
}
}

View File

@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PrecastConcretePlantDatabaseImplement.Models
{
public class ShopReinforced
{
public int Id { get; set; }
[Required]
public int ReinforcedId { get; set; }
[Required]
public int ShopId { get; set; }
[Required]
public int Count { get; set; }
public virtual Shop Shop { get; set; } = new();
public virtual Reinforced Reinforced { get; set; } = new();
}
}

View File

@ -19,8 +19,10 @@ namespace PrecastConcretePlantDatabaseImplement
base.OnConfiguring(optionsBuilder);
}
public virtual DbSet<Component> Components { set; get; }
public virtual DbSet<Reinforced> Reinforceds { set; get; }
public virtual DbSet<Reinforced> Reinforcedies { set; get; }
public virtual DbSet<ReinforcedComponent> ReinforcedComponents { set; get; }
public virtual DbSet<Order> Orders { set; get; }
public virtual DbSet<Shop> Shops { set; get; }
public virtual DbSet<ShopReinforced> ShopReinforcedies { set; get; }
}
}

View File

@ -72,21 +72,21 @@ namespace PrecastConcretePlantFileImplement.Implements
public bool SellReinforced(IReinforcedModel model, int quantity)
{
if (source.Shops.Select(x => x.ShopReinforcedies.FirstOrDefault(y => y.Key == model.Id).Value.Item2).Sum() < quantity)
if (source.Shops.Select(x => x.Reinforcedies.FirstOrDefault(y => y.Key == model.Id).Value.Item2).Sum() < quantity)
{
return false;
}
foreach (var shop in source.Shops.Where(x => x.ShopReinforcedies.ContainsKey(model.Id)))
foreach (var shop in source.Shops.Where(x => x.Reinforcedies.ContainsKey(model.Id)))
{
int QuantityInCurrentShop = shop.ShopReinforcedies[model.Id].Item2;
int QuantityInCurrentShop = shop.Reinforcedies[model.Id].Item2;
if (QuantityInCurrentShop <= quantity)
{
shop.ShopReinforcedies.Remove(model.Id);
shop.Reinforcedies.Remove(model.Id);
quantity -= QuantityInCurrentShop;
}
else
{
shop.ShopReinforcedies[model.Id] = (shop.ShopReinforcedies[model.Id].Item1, QuantityInCurrentShop - quantity);
shop.Reinforcedies[model.Id] = (shop.Reinforcedies[model.Id].Item1, QuantityInCurrentShop - quantity);
quantity = 0;
}
if (quantity == 0)

View File

@ -19,7 +19,7 @@ namespace PrecastConcretePlantFileImplement.Models
public Dictionary<int, int> Reinforcedies { get; private set; } = new();
public Dictionary<int, (IReinforcedModel, int)> _shopReinforcedies = null;
public Dictionary<int, (IReinforcedModel, int)> ShopReinforcedies
public Dictionary<int, (IReinforcedModel, int)> Reinforcedies
{
get
{
@ -49,7 +49,7 @@ namespace PrecastConcretePlantFileImplement.Models
Address = model.Address,
ReinforcedMaxCount = model.ReinforcedMaxCount,
DateOpening = model.DateOpening,
Reinforcedies = model.ShopReinforcedies.ToDictionary(x => x.Key, x => x.Value.Item2)
Reinforcedies = model.Reinforcedies.ToDictionary(x => x.Key, x => x.Value.Item2)
};
}
public static Shop? Create(XElement element)
@ -80,7 +80,7 @@ namespace PrecastConcretePlantFileImplement.Models
Address = model.Address;
DateOpening = model.DateOpening;
ReinforcedMaxCount = model.ReinforcedMaxCount;
Reinforcedies = model.ShopReinforcedies.ToDictionary(x => x.Key, x => x.Value.Item2);
Reinforcedies = model.Reinforcedies.ToDictionary(x => x.Key, x => x.Value.Item2);
_shopReinforcedies = null;
}
public ShopViewModel GetViewModel => new()
@ -88,7 +88,7 @@ namespace PrecastConcretePlantFileImplement.Models
Id = Id,
Name = Name,
Address = Address,
ShopReinforcedies = ShopReinforcedies,
Reinforcedies = Reinforcedies,
DateOpening = DateOpening,
ReinforcedMaxCount = ReinforcedMaxCount,
};

View File

@ -17,7 +17,7 @@ namespace PrecastConcretePlantListImplement.Models
public DateTime DateOpening { get; private set; }
public Dictionary<int, (IReinforcedModel, int)> ShopReinforcedies { get; private set; } = new();
public Dictionary<int, (IReinforcedModel, int)> Reinforcedies { get; private set; } = new();
public int Id { get; private set; }
@ -33,7 +33,7 @@ namespace PrecastConcretePlantListImplement.Models
Name = model.Name,
Address = model.Address,
DateOpening = model.DateOpening,
ShopReinforcedies = new()
Reinforcedies = new()
};
}
public void Update(ShopBindingModel? model)
@ -45,14 +45,14 @@ namespace PrecastConcretePlantListImplement.Models
Name = model.Name;
Address = model.Address;
DateOpening = model.DateOpening;
ShopReinforcedies = model.ShopReinforcedies;
Reinforcedies = model.Reinforcedies;
}
public ShopViewModel GetViewModel => new()
{
Id = Id,
Name = Name,
Address = Address,
ShopReinforcedies = ShopReinforcedies,
Reinforcedies = Reinforcedies,
DateOpening = DateOpening,
};
public int ReinforcedMaxCount => throw new NotImplementedException();