2024-04-04 21:21:21 +04:00
|
|
|
|
using PrecastConcretePlantDataModels.Models;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.ComponentModel.DataAnnotations.Schema;
|
|
|
|
|
using System.ComponentModel.DataAnnotations;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using PrecastConcretePlantContracts.BindingModels;
|
|
|
|
|
using PrecastConcretePlantContracts.ViewModels;
|
2024-05-16 20:23:36 +04:00
|
|
|
|
using System.Runtime.Serialization;
|
2024-04-04 21:21:21 +04:00
|
|
|
|
|
|
|
|
|
namespace PrecastConcretePlantDatabaseImplement.Models
|
|
|
|
|
{
|
2024-05-16 20:23:36 +04:00
|
|
|
|
[DataContract]
|
2024-04-04 21:21:21 +04:00
|
|
|
|
public class Reinforced : IReinforcedModel
|
|
|
|
|
{
|
2024-05-16 20:23:36 +04:00
|
|
|
|
[DataMember]
|
2024-04-04 21:21:21 +04:00
|
|
|
|
public int Id { get; set; }
|
|
|
|
|
|
2024-05-16 20:23:36 +04:00
|
|
|
|
[DataMember]
|
2024-04-04 21:21:21 +04:00
|
|
|
|
[Required]
|
|
|
|
|
public string ReinforcedName { get; set; } = string.Empty;
|
|
|
|
|
|
2024-05-16 20:23:36 +04:00
|
|
|
|
[DataMember]
|
2024-04-04 21:21:21 +04:00
|
|
|
|
[Required]
|
|
|
|
|
public double Price { get; set; }
|
|
|
|
|
|
|
|
|
|
private Dictionary<int, (IComponentModel, int)>? _ReinforcedComponents = null;
|
|
|
|
|
|
2024-05-16 20:23:36 +04:00
|
|
|
|
[DataMember]
|
2024-04-04 21:21:21 +04:00
|
|
|
|
[NotMapped]//заполняется при обращеии к гетвьюмодел
|
|
|
|
|
public Dictionary<int, (IComponentModel, int)> ReinforcedComponents
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (_ReinforcedComponents == null)
|
|
|
|
|
{
|
|
|
|
|
_ReinforcedComponents = Components
|
|
|
|
|
.ToDictionary(recPC => recPC.ComponentId, recPC =>
|
|
|
|
|
(recPC.Component as IComponentModel, recPC.Count));
|
|
|
|
|
}
|
|
|
|
|
return _ReinforcedComponents;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[ForeignKey("ReinforcedId")] //список записей класса-связи
|
|
|
|
|
public virtual List<ReinforcedComponent> Components { get; set; } = new();
|
|
|
|
|
|
|
|
|
|
[ForeignKey("ReinforcedId")]
|
|
|
|
|
public virtual List<Order> Orders { get; set; } = new();
|
|
|
|
|
|
2024-05-02 20:47:10 +04:00
|
|
|
|
public static Reinforced Create(PrecastConcretePlantDatabase context, ReinforcedBindingModel model)
|
2024-04-04 21:21:21 +04:00
|
|
|
|
{
|
|
|
|
|
return new Reinforced()
|
|
|
|
|
{
|
|
|
|
|
Id = model.Id,
|
|
|
|
|
ReinforcedName = model.ReinforcedName,
|
|
|
|
|
Price = model.Price,
|
|
|
|
|
Components = model.ReinforcedComponents.Select(x =>
|
|
|
|
|
new ReinforcedComponent
|
|
|
|
|
{
|
|
|
|
|
Component = context.Components.First(y => y.Id == x.Key),
|
|
|
|
|
Count = x.Value.Item2
|
|
|
|
|
}).ToList()
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
public void Update(ReinforcedBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
ReinforcedName = model.ReinforcedName;
|
|
|
|
|
Price = model.Price;
|
|
|
|
|
}
|
|
|
|
|
public ReinforcedViewModel GetViewModel => new()
|
|
|
|
|
{
|
|
|
|
|
Id = Id,
|
|
|
|
|
ReinforcedName = ReinforcedName,
|
|
|
|
|
Price = Price,
|
|
|
|
|
ReinforcedComponents = ReinforcedComponents
|
|
|
|
|
};
|
|
|
|
|
//метод обновления списка связей(вместо контейнски)
|
2024-05-02 20:47:10 +04:00
|
|
|
|
public void UpdateComponents(PrecastConcretePlantDatabase context, ReinforcedBindingModel model)
|
2024-04-04 21:21:21 +04:00
|
|
|
|
{
|
|
|
|
|
var ReinforcedComponents = context.ReinforcedComponents
|
|
|
|
|
.Where(rec => rec.ReinforcedId == model.Id).ToList();
|
|
|
|
|
|
|
|
|
|
if (ReinforcedComponents != null && ReinforcedComponents.Count > 0)
|
|
|
|
|
{ //удаляем те, которых нет в модели
|
|
|
|
|
context.ReinforcedComponents.RemoveRange(ReinforcedComponents
|
|
|
|
|
.Where(rec => !model.ReinforcedComponents.ContainsKey(rec.ComponentId)));
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
//обновляем кол-во у сущ-х записей
|
|
|
|
|
foreach (var updateComponent in ReinforcedComponents)
|
|
|
|
|
{
|
|
|
|
|
updateComponent.Count =
|
|
|
|
|
model.ReinforcedComponents[updateComponent.ComponentId].Item2;
|
|
|
|
|
model.ReinforcedComponents.Remove(updateComponent.ComponentId);
|
|
|
|
|
}
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
var Reinforced = context.Reinforceds.First(x => x.Id == Id);
|
|
|
|
|
foreach (var pc in model.ReinforcedComponents)
|
|
|
|
|
{
|
|
|
|
|
context.ReinforcedComponents.Add(new ReinforcedComponent
|
|
|
|
|
{
|
|
|
|
|
Reinforced = Reinforced,
|
|
|
|
|
Component = context.Components.First(x => x.Id == pc.Key),
|
|
|
|
|
Count = pc.Value.Item2
|
|
|
|
|
});
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
_ReinforcedComponents = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|