CourseWork_CompShop/ComputerShopProvider/ComputerShopDatabaseImplement/Models/Supply.cs

136 lines
4.8 KiB
C#
Raw Normal View History

2023-04-07 16:10:59 +04:00
using ComputerShopContracts.BindingModels;
using ComputerShopContracts.ViewModels;
using ComputerShopDataModels.Enums;
using ComputerShopDataModels.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 ComputerShopDatabaseImplement.Models
{
internal class Supply : ISupplyModel
{
public int Id { get; set; }
[Required]
public SupplyStatus Status { get; private set; } = SupplyStatus.Неизвестен;
[Required]
public DateTime DateCreate { get; private set; } = DateTime.Now;
public DateTime? DateImplement { get; private set; }
2023-05-20 08:34:33 +04:00
public int ClientId { get; set; }
2023-04-07 16:10:59 +04:00
2023-05-20 07:41:13 +04:00
public int? ReceivingId { get; set; }
2023-05-20 00:08:47 +04:00
public virtual EquipmentReceiving Receiving { get; set; }
2023-05-19 18:26:38 +04:00
private Dictionary<int, IOrderModel>? _supplyOrders =
2023-04-07 16:10:59 +04:00
null;
2023-05-24 18:51:13 +04:00
private Dictionary<int, IComponentModel>? _supplyComponents = null;
2023-04-07 16:10:59 +04:00
[NotMapped]
2023-05-19 18:26:38 +04:00
public Dictionary<int, IOrderModel> SupplyOrders
2023-04-07 16:10:59 +04:00
{
get
{
if (_supplyOrders == null)
{
_supplyOrders = Orders
2023-05-19 18:26:38 +04:00
.ToDictionary(recPC => recPC.SupplyId, recPC => recPC.Order as IOrderModel);
2023-04-07 16:10:59 +04:00
}
return _supplyOrders;
}
}
2023-05-24 18:51:13 +04:00
[NotMapped]
public Dictionary<int, IComponentModel> SupplyComponents
{
get
{
if (_supplyComponents == null)
{
_supplyComponents = Components
.ToDictionary(recPC => recPC.SupplyId, recPC => recPC.Component as IComponentModel);
}
return _supplyComponents;
}
}
2023-04-07 16:10:59 +04:00
[ForeignKey("SupplyId")]
public virtual List<SupplyOrder> Orders { get; set; } = new();
[ForeignKey("SupplyId")]
2023-05-24 18:51:13 +04:00
public virtual List<ComponentSupply> Components { get; set; } = new();
2023-04-07 16:53:49 +04:00
2023-04-07 16:10:59 +04:00
public static Supply Create(ComputerShopDatabase context, SupplyBindingModel model)
{
return new Supply
{
Id = model.Id,
2023-05-20 08:34:33 +04:00
ClientId = model.ClientId,
2023-04-07 16:10:59 +04:00
Status = model.Status,
DateCreate = model.DateCreate,
DateImplement = model.DateImplement,
2023-05-24 18:51:13 +04:00
ReceivingId = model.ReceivingId,
2023-04-07 16:10:59 +04:00
Orders = model.SupplyOrders.Select(x => new
SupplyOrder
{
Order = context.Orders.First(y => y.Id == x.Key),
2023-05-24 18:51:13 +04:00
}).ToList(),
Components = model.SupplyComponents.Select(x => new
ComponentSupply
{
Component = context.Components.First(y => y.Id == x.Key),
2023-04-07 16:10:59 +04:00
}).ToList()
};
}
public void Update(SupplyBindingModel model)
{
if (model == null)
{
return;
}
Status = model.Status;
DateImplement = model.DateImplement;
}
public SupplyViewModel GetViewModel => new()
{
Id = Id,
Status = Status,
DateCreate = DateCreate,
DateImplement = DateImplement,
2023-05-20 08:34:33 +04:00
SupplyOrders = SupplyOrders,
2023-05-24 18:51:13 +04:00
SupplyComponents = SupplyComponents,
ReceivingId = ReceivingId,
2023-05-20 08:34:33 +04:00
ClientId = ClientId
2023-04-07 16:10:59 +04:00
};
public void UpdateOrders(ComputerShopDatabase context, SupplyBindingModel model)
{
var SupplyOrders = context.SupplyOrders.Where(rec =>
rec.Id == model.Id).ToList();
if (SupplyOrders != null && SupplyOrders.Count > 0)
{ // удалили те, которых нет в модели
context.SupplyOrders.RemoveRange(SupplyOrders.Where(rec
=> !model.SupplyOrders.ContainsKey(rec.OrderId)));
context.SaveChanges();
// обновили количество у существующих записей
foreach (var updateOrder in SupplyOrders)
{
model.SupplyOrders.Remove(updateOrder.OrderId);
}
context.SaveChanges();
}
var supply = context.Supplies.First(x => x.Id == Id);
foreach (var pc in model.SupplyOrders)
{
context.SupplyOrders.Add(new SupplyOrder
{
Supply = supply,
2023-05-18 23:41:42 +04:00
Order = context.Orders.First(x => x.Id == pc.Key)
2023-04-07 16:10:59 +04:00
});
context.SaveChanges();
}
_supplyOrders = null;
}
}
}