158 lines
5.2 KiB
C#
158 lines
5.2 KiB
C#
|
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
|
|||
|
{
|
|||
|
/// <summary>
|
|||
|
/// Сущность "Магазин"
|
|||
|
/// </summary>
|
|||
|
public class Shop : IShopModel
|
|||
|
{
|
|||
|
/// <summary>
|
|||
|
/// Идентификатор
|
|||
|
/// </summary>
|
|||
|
public int Id { get; set; }
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Название магазина
|
|||
|
/// </summary>
|
|||
|
[Required]
|
|||
|
public string ShopName { get; set; } = string.Empty;
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Адрес магазина
|
|||
|
/// </summary>
|
|||
|
[Required]
|
|||
|
public string Address { get; set; } = string.Empty;
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Дата открытия магазина
|
|||
|
/// </summary>
|
|||
|
[Required]
|
|||
|
public DateTime DateOpening { get; set; }
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Коллекция изделий в магазине
|
|||
|
/// </summary>
|
|||
|
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;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Максимальное количество изделий
|
|||
|
/// </summary>
|
|||
|
public int MaxPlanes { get; set; }
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Связь с классом связи магазина и изделий
|
|||
|
/// </summary>
|
|||
|
[ForeignKey("ShopId")]
|
|||
|
public List<ShopPlane> Planes { get; set; } = new();
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Создание модели магазина
|
|||
|
/// </summary>
|
|||
|
/// <param name="context"></param>
|
|||
|
/// <param name="model"></param>
|
|||
|
/// <returns></returns>
|
|||
|
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()
|
|||
|
};
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Изменение модели магазина
|
|||
|
/// </summary>
|
|||
|
/// <param name="model"></param>
|
|||
|
public void Update(ShopBindingModel model)
|
|||
|
{
|
|||
|
ShopName = model.ShopName;
|
|||
|
Address = model.Address;
|
|||
|
DateOpening = model.DateOpening;
|
|||
|
MaxPlanes = model.MaxPlanes;
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Получение модели магазина
|
|||
|
/// </summary>
|
|||
|
public ShopViewModel GetViewModel => new()
|
|||
|
{
|
|||
|
Id = Id,
|
|||
|
ShopName = ShopName,
|
|||
|
Address = Address,
|
|||
|
DateOpening = DateOpening,
|
|||
|
ShopPlanes = ShopPlanes,
|
|||
|
MaxPlanes = MaxPlanes
|
|||
|
};
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Метод обновления списка связей
|
|||
|
/// </summary>
|
|||
|
/// <param name="context"></param>
|
|||
|
/// <param name="model"></param>
|
|||
|
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;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|