153 lines
4.3 KiB
C#
153 lines
4.3 KiB
C#
using ComputerShopContracts.BindingModels;
|
||
using ComputerShopContracts.ViewModels;
|
||
using ComputerShopDataModels.Models;
|
||
using System.ComponentModel.DataAnnotations;
|
||
using System.ComponentModel.DataAnnotations.Schema;
|
||
|
||
namespace ComputerShopDatabaseImplement.Models
|
||
{
|
||
public class Assembly : IAssemblyModel
|
||
{
|
||
public int Id { get; private set; }
|
||
|
||
[Required]
|
||
public int UserId { get; private set; }
|
||
|
||
[Required]
|
||
public string AssemblyName { get; private set; } = string.Empty;
|
||
|
||
[Required]
|
||
public double Price { get; private set; }
|
||
|
||
[Required]
|
||
public string Category { get; private set; } = string.Empty;
|
||
|
||
[ForeignKey("AssemblyId")]
|
||
public virtual List<Request> Requests { get; set; } = new();
|
||
|
||
[ForeignKey("AssemblyId")]
|
||
public virtual List<AssemblyComponent> Components { get; set; } = new();
|
||
|
||
private Dictionary<int, IComponentModel>? _assemblyComponents;
|
||
|
||
[NotMapped]
|
||
public Dictionary<int, IComponentModel> AssemblyComponents
|
||
{
|
||
get
|
||
{
|
||
if (_assemblyComponents == null)
|
||
{
|
||
_assemblyComponents = Components.ToDictionary(
|
||
AsmComp => AsmComp.ComponentId,
|
||
AsmComp => AsmComp.Component as IComponentModel
|
||
);
|
||
}
|
||
|
||
return _assemblyComponents;
|
||
}
|
||
}
|
||
|
||
public static Assembly Create(ComputerShopDatabase Context, AssemblyBindingModel Model)
|
||
{
|
||
var Components = Model.AssemblyComponents
|
||
.Select(x => new AssemblyComponent
|
||
{
|
||
Component = Context.Components.First(y => y.Id == x.Key)
|
||
})
|
||
.ToList();
|
||
double Price = Components.Sum(x => x.Component.Cost);
|
||
|
||
return new Assembly()
|
||
{
|
||
Id = Model.Id,
|
||
UserId = Model.UserId,
|
||
AssemblyName = Model.AssemblyName,
|
||
Price = Price,
|
||
Category = Model.Category,
|
||
Components = Components,
|
||
};
|
||
}
|
||
|
||
public void Update(AssemblyBindingModel Model)
|
||
{
|
||
if (!string.IsNullOrEmpty(Model.AssemblyName))
|
||
{
|
||
AssemblyName = Model.AssemblyName;
|
||
}
|
||
|
||
if (!string.IsNullOrEmpty(Model.Category))
|
||
{
|
||
Category = Model.Category;
|
||
}
|
||
}
|
||
|
||
public AssemblyViewModel ViewModel => new()
|
||
{
|
||
Id = Id,
|
||
UserId = UserId,
|
||
AssemblyName = AssemblyName,
|
||
Price = Price,
|
||
Category = Category,
|
||
AssemblyComponents = AssemblyComponents,
|
||
};
|
||
|
||
public void UpdateComponents(ComputerShopDatabase Context, AssemblyBindingModel Model)
|
||
{
|
||
// Сначала подсчитывается новая цена, т.к. Model.AssemblyComponents далее может измениться
|
||
double NewPrice = Context.Components
|
||
.Where(x => Model.AssemblyComponents.ContainsKey(x.Id))
|
||
.Sum(x => x.Cost);
|
||
|
||
var AssemblyComponents = Context.AssemblyComponents.Where(x => x.AssemblyId == Model.Id).ToList();
|
||
if (AssemblyComponents != null && AssemblyComponents.Count > 0)
|
||
{
|
||
// Удаление записей из таблицы AssemblyComponents тех компонентов, которых нет в модели
|
||
Context.AssemblyComponents
|
||
.RemoveRange(AssemblyComponents.Where(x => !Model.AssemblyComponents.ContainsKey(x.ComponentId)));
|
||
Context.SaveChanges();
|
||
|
||
// После этого в Model.AssemblyComponents останутся только те компоненты, записей о которых еще нет в БД
|
||
foreach (var ComponentToUpdate in AssemblyComponents)
|
||
{
|
||
Model.AssemblyComponents.Remove(ComponentToUpdate.ComponentId);
|
||
}
|
||
|
||
Context.SaveChanges();
|
||
}
|
||
|
||
var CurrentAssembly = Context.Assemblies.First(x => x.Id == Id);
|
||
foreach (var AssemblyComponent in Model.AssemblyComponents)
|
||
{
|
||
Context.AssemblyComponents.Add(new AssemblyComponent
|
||
{
|
||
Assembly = CurrentAssembly,
|
||
Component = Context.Components.First(x => x.Id == AssemblyComponent.Key),
|
||
});
|
||
|
||
Context.SaveChanges();
|
||
}
|
||
|
||
Price = NewPrice;
|
||
Context.SaveChanges();
|
||
|
||
_assemblyComponents = null;
|
||
}
|
||
|
||
private void CalculatePrice(
|
||
ComputerShopDatabase Context,
|
||
Dictionary<int, IComponentModel> ModelComponents,
|
||
out List<AssemblyComponent> OutComponents,
|
||
out double OutPrice)
|
||
{
|
||
OutComponents = ModelComponents
|
||
.Select(x => new AssemblyComponent
|
||
{
|
||
Component = Context.Components.First(y => y.Id == x.Key)
|
||
})
|
||
.ToList();
|
||
|
||
OutPrice = Components.Sum(x => x.Component.Cost);
|
||
}
|
||
}
|
||
}
|