116 lines
4.3 KiB
C#
116 lines
4.3 KiB
C#
using PrecastConcretePlantContracts.BindingModels;
|
||
using PrecastConcretePlantContracts.ViewModels;
|
||
using PrecastConcretePlantDataModels;
|
||
using System.ComponentModel.DataAnnotations.Schema;
|
||
using System.ComponentModel.DataAnnotations;
|
||
using PrecastConcretePlantDataModels.Models;
|
||
|
||
namespace PrecastConcretePlantDatabaseImplement
|
||
{
|
||
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 MaxCountReinforceds { get; private set; }
|
||
|
||
public DateTime DateOpening { get; private set; }
|
||
|
||
private Dictionary<int, (IReinforcedModel, int)>? _cachedReinforceds = null;
|
||
[NotMapped]
|
||
public Dictionary<int, (IReinforcedModel, int)> Reinforceds
|
||
{
|
||
get
|
||
{
|
||
if (_cachedReinforceds == null)
|
||
{
|
||
using var context = new PrecastConcretePlantDatabase();
|
||
_cachedReinforceds = ShopReinforceds
|
||
.ToDictionary(x => x.ReinforcedId, x => (context.Reinforceds
|
||
.FirstOrDefault(y => y.Id == x.ReinforcedId)! as IReinforcedModel, x.Count));
|
||
}
|
||
return _cachedReinforceds;
|
||
}
|
||
}
|
||
|
||
public int Id { get; private set; }
|
||
|
||
[ForeignKey("ShopId")]
|
||
public virtual List<ShopReinforced> ShopReinforceds { 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,
|
||
MaxCountReinforceds = model.MaxCountReinforceds,
|
||
ShopReinforceds = model.Reinforceds.Select(x => new ShopReinforced
|
||
{
|
||
Reinforced = context.Reinforceds.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;
|
||
MaxCountReinforceds = model.MaxCountReinforceds;
|
||
}
|
||
public ShopViewModel GetViewModel => new()
|
||
{
|
||
Id = Id,
|
||
Name = Name,
|
||
Address = Address,
|
||
Reinforceds = Reinforceds,
|
||
DateOpening = DateOpening,
|
||
MaxCountReinforceds = MaxCountReinforceds,
|
||
};
|
||
|
||
public void UpdateReinforceds(PrecastConcretePlantDatabase context, ShopBindingModel model)
|
||
{
|
||
var shopReinforceds = context.ShopReinforceds
|
||
.Where(rec => rec.ShopId == model.Id)
|
||
.ToList();
|
||
// удалили те, которых нет в модели
|
||
if (shopReinforceds != null && shopReinforceds.Count > 0)
|
||
{
|
||
context.ShopReinforceds
|
||
.RemoveRange(shopReinforceds
|
||
.Where(rec => !model.Reinforceds
|
||
.ContainsKey(rec.ReinforcedId)));
|
||
// обновили количество у существующих записей
|
||
foreach (var updateReinforced in shopReinforceds.Where(x => model.Reinforceds.ContainsKey(x.ReinforcedId)))
|
||
{
|
||
updateReinforced.Count = model.Reinforceds[updateReinforced.ReinforcedId].Item2;
|
||
model.Reinforceds.Remove(updateReinforced.ReinforcedId);
|
||
}
|
||
}
|
||
var shop = context.Shops.First(x => x.Id == model.Id);
|
||
shop.ShopReinforceds.AddRange(model.Reinforceds.Select(x => new ShopReinforced
|
||
{
|
||
Reinforced = context.Reinforceds.First(y => y.Id == x.Key),
|
||
Count = x.Value.Item2,
|
||
}).Except(shopReinforceds ?? new()));
|
||
context.SaveChanges();
|
||
_cachedReinforceds = null;
|
||
}
|
||
}
|
||
}
|