2023-03-18 19:33:11 +04:00
|
|
|
|
using PrecastConcretePlantContracts.BindingModels;
|
|
|
|
|
using PrecastConcretePlantContracts.ViewModels;
|
|
|
|
|
using PrecastConcretePlantDataModels.Models;
|
|
|
|
|
using System;
|
2023-03-18 18:19:08 +04:00
|
|
|
|
using System.Collections.Generic;
|
2023-03-18 19:33:11 +04:00
|
|
|
|
using System.ComponentModel.DataAnnotations;
|
|
|
|
|
using System.ComponentModel.DataAnnotations.Schema;
|
2023-03-18 18:19:08 +04:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
2023-03-18 19:33:11 +04:00
|
|
|
|
namespace PrecastConcretePlantDatabaseImplement.Models
|
2023-03-18 18:19:08 +04:00
|
|
|
|
{
|
2023-03-18 19:33:11 +04:00
|
|
|
|
public class Reinforced : IReinforcedModel
|
2023-03-18 18:19:08 +04:00
|
|
|
|
{
|
2023-03-18 19:33:11 +04:00
|
|
|
|
public int Id { get; set; }
|
|
|
|
|
[Required]
|
|
|
|
|
public string ReinforcedName { get; set; } = string.Empty;
|
|
|
|
|
[Required]
|
|
|
|
|
public double Price { get; set; }
|
|
|
|
|
private Dictionary<int, (IComponentModel, int)>? _reinforcedComponents = null;
|
|
|
|
|
[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();
|
|
|
|
|
public static Reinforced Create(PrecastConcretePlantDatabase context,
|
|
|
|
|
ReinforcedBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
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
|
|
|
|
|
};
|
|
|
|
|
public void UpdateComponents(PrecastConcretePlantDatabase context, ReinforcedBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
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 rc in model.ReinforcedComponents)
|
|
|
|
|
{
|
|
|
|
|
context.ReinforcedComponents.Add(new ReinforcedComponent
|
|
|
|
|
{
|
|
|
|
|
Reinforced = Reinforced,
|
|
|
|
|
Component = context.Components.First(x => x.Id == rc.Key),
|
|
|
|
|
Count = rc.Value.Item2
|
|
|
|
|
});
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
_reinforcedComponents = null;
|
|
|
|
|
}
|
2023-03-18 18:19:08 +04:00
|
|
|
|
}
|
|
|
|
|
}
|