2024-03-26 20:40:27 +04:00
|
|
|
|
using PlumbingRepairDataModels.Models;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.ComponentModel.DataAnnotations.Schema;
|
|
|
|
|
using System.ComponentModel.DataAnnotations;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using PlumbingRepairContracts.BindingModels;
|
|
|
|
|
using PlumbingRepairContracts.ViewModels;
|
2024-05-21 20:02:14 +04:00
|
|
|
|
using System.Runtime.Serialization;
|
2024-03-26 20:40:27 +04:00
|
|
|
|
|
|
|
|
|
namespace PlumbingRepairDataBaseImplement.Models
|
|
|
|
|
{
|
2024-05-21 20:02:14 +04:00
|
|
|
|
[DataContract]
|
2024-03-26 20:40:27 +04:00
|
|
|
|
public class Work : IWorkModel
|
|
|
|
|
{
|
2024-05-21 20:02:14 +04:00
|
|
|
|
[DataMember]
|
2024-03-26 20:40:27 +04:00
|
|
|
|
public int Id { get; set; }
|
|
|
|
|
[Required]
|
2024-05-21 20:02:14 +04:00
|
|
|
|
[DataMember]
|
2024-03-26 20:40:27 +04:00
|
|
|
|
public string WorkName { get; set; } = string.Empty;
|
|
|
|
|
[Required]
|
2024-05-21 20:02:14 +04:00
|
|
|
|
[DataMember]
|
2024-03-26 20:40:27 +04:00
|
|
|
|
public double Price { get; set; }
|
|
|
|
|
private Dictionary<int, (IComponentModel, int)>? _workComponents =
|
|
|
|
|
null;
|
|
|
|
|
[NotMapped]
|
2024-05-21 20:02:14 +04:00
|
|
|
|
[DataMember]
|
2024-03-26 20:40:27 +04:00
|
|
|
|
public Dictionary<int, (IComponentModel, int)> WorkComponents
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (_workComponents == null)
|
|
|
|
|
{
|
|
|
|
|
_workComponents = Components
|
|
|
|
|
.ToDictionary(recPC => recPC.ComponentId, recPC =>
|
|
|
|
|
(recPC.Component as IComponentModel, recPC.Count));
|
|
|
|
|
}
|
|
|
|
|
return _workComponents;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
[ForeignKey("WorkId")]
|
|
|
|
|
public virtual List<WorkComponent> Components { get; set; } = new();
|
|
|
|
|
[ForeignKey("WorkId")]
|
|
|
|
|
public virtual List<Order> Orders { get; set; } = new();
|
|
|
|
|
public static Work Create(PlumbingRepairDataBase context,
|
|
|
|
|
WorkBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
return new Work()
|
|
|
|
|
{
|
|
|
|
|
Id = model.Id,
|
|
|
|
|
WorkName = model.WorkName,
|
|
|
|
|
Price = model.Price,
|
|
|
|
|
Components = model.WorkComponents.Select(x => new
|
|
|
|
|
WorkComponent
|
|
|
|
|
{
|
|
|
|
|
Component = context.Components.First(y => y.Id == x.Key),
|
|
|
|
|
Count = x.Value.Item2
|
|
|
|
|
}).ToList()
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
public void Update(WorkBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
WorkName = model.WorkName;
|
|
|
|
|
Price = model.Price;
|
|
|
|
|
}
|
|
|
|
|
public WorkViewModel GetViewModel => new()
|
|
|
|
|
{
|
|
|
|
|
Id = Id,
|
|
|
|
|
WorkName = WorkName,
|
|
|
|
|
Price = Price,
|
|
|
|
|
WorkComponents = WorkComponents
|
|
|
|
|
};
|
|
|
|
|
public void UpdateComponents(PlumbingRepairDataBase context,
|
|
|
|
|
WorkBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
var workComponents = context.WorkComponents.Where(rec =>
|
|
|
|
|
rec.WorkId == model.Id).ToList();
|
|
|
|
|
if (workComponents != null && workComponents.Count > 0)
|
|
|
|
|
{ // удалили те, которых нет в модели
|
|
|
|
|
context.WorkComponents.RemoveRange(workComponents.Where(rec
|
|
|
|
|
=> !model.WorkComponents.ContainsKey(rec.ComponentId)));
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
// обновили количество у существующих записей
|
|
|
|
|
foreach (var updateComponent in workComponents)
|
|
|
|
|
{
|
|
|
|
|
updateComponent.Count = model.WorkComponents[updateComponent.ComponentId].Item2;
|
|
|
|
|
model.WorkComponents.Remove(updateComponent.ComponentId);
|
|
|
|
|
}
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
var work = context.Works.First(x => x.Id == Id);
|
|
|
|
|
foreach (var pc in model.WorkComponents)
|
|
|
|
|
{
|
|
|
|
|
context.WorkComponents.Add(new WorkComponent
|
|
|
|
|
{
|
|
|
|
|
Work = work,
|
|
|
|
|
Component = context.Components.First(x => x.Id == pc.Key),
|
|
|
|
|
Count = pc.Value.Item2
|
|
|
|
|
});
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
_workComponents = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|