2024-06-21 13:48:15 +04:00
|
|
|
|
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
|
|
|
|
|
{
|
2024-06-21 14:01:04 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Сущность "Магазин"
|
|
|
|
|
/// </summary>
|
2024-06-21 13:48:15 +04:00
|
|
|
|
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<int, (IPlaneModel, int)>? _shopPlanes = null;
|
|
|
|
|
|
|
|
|
|
[NotMapped]
|
|
|
|
|
public Dictionary<int, (IPlaneModel, int)> 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<ShopPlane> 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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|