using AircraftPlantContracts.BindingModels;
using AircraftPlantContracts.ViewModels;
using AircraftPlantDataModels.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
namespace AircraftPlantDatabaseImplement.Models
{
///
/// Сущность "Магазин"
///
public class Shop : IShopModel
{
///
/// Идентификатор
///
public int Id { get; set; }
///
/// Название магазина
///
[Required]
public string ShopName { get; set; } = string.Empty;
///
/// Адрес магазина
///
[Required]
public string Address { get; set; } = string.Empty;
///
/// Дата открытия магазина
///
[Required]
public DateTime DateOpening { get; set; }
///
/// Коллекция изделий в магазине
///
private Dictionary? _shopPlanes = null;
[NotMapped]
public Dictionary ShopPlanes
{
get
{
if (_shopPlanes == null)
{
_shopPlanes = Planes
.ToDictionary(recSP => recSP.PlaneId, recSP => (recSP.Plane as IPlaneModel, recSP.Count));
}
return _shopPlanes;
}
}
///
/// Максимальное количество изделий
///
public int MaxPlanes { get; set; }
///
/// Связь с классом связи магазина и изделий
///
[ForeignKey("ShopId")]
public List Planes { get; set; } = new();
///
/// Создание модели магазина
///
///
///
///
public static Shop Create(AircraftPlantDatabase context, ShopBindingModel model)
{
return new Shop
{
Id = model.Id,
ShopName = model.ShopName,
Address = model.Address,
DateOpening = model.DateOpening,
MaxPlanes = model.MaxPlanes,
Planes = model.ShopPlanes.Select(x => new ShopPlane
{
Plane = context.Planes.First(y => y.Id == x.Key),
Count = x.Value.Item2
}).ToList()
};
}
///
/// Изменение модели магазина
///
///
public void Update(ShopBindingModel model)
{
ShopName = model.ShopName;
Address = model.Address;
DateOpening = model.DateOpening;
MaxPlanes = model.MaxPlanes;
}
///
/// Получение модели магазина
///
public ShopViewModel GetViewModel => new()
{
Id = Id,
ShopName = ShopName,
Address = Address,
DateOpening = DateOpening,
ShopPlanes = ShopPlanes,
MaxPlanes = MaxPlanes
};
///
/// Метод обновления списка связей
///
///
///
public void UpdatePlanes(AircraftPlantDatabase context, ShopBindingModel model)
{
var shopPlanes = context.ShopPlanes.Where(rec => rec.ShopId == model.Id).ToList();
if (shopPlanes != null && shopPlanes.Count > 0)
{
// Удаление изделий, которых нет в магазине
context.ShopPlanes.RemoveRange(shopPlanes.Where(rec => !model.ShopPlanes.ContainsKey(rec.PlaneId)));
context.SaveChanges();
// Обновление количества у существующих записей
foreach (var updatePlanes in shopPlanes)
{
updatePlanes.Count = model.ShopPlanes[updatePlanes.PlaneId].Item2;
model.ShopPlanes.Remove(updatePlanes.PlaneId);
}
context.SaveChanges();
}
var shop = context.Shops.First(x => x.Id == Id);
foreach (var sp in model.ShopPlanes)
{
context.ShopPlanes.Add(new ShopPlane
{
Shop = shop,
Plane = context.Planes.First(x => x.Id == sp.Key),
Count = sp.Value.Item2
});
context.SaveChanges();
}
_shopPlanes = null;
}
}
}