111 lines
3.5 KiB
C#
111 lines
3.5 KiB
C#
|
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 STODataModels;
|
|||
|
using STOContracts.BindingModels;
|
|||
|
using STOContracts.ViewModels;
|
|||
|
using STODatabaseImplement;
|
|||
|
|
|||
|
namespace DatabaseImplement
|
|||
|
{
|
|||
|
public class Work: IWorkModel
|
|||
|
{
|
|||
|
public int Id { get; private set; }
|
|||
|
|
|||
|
[Required]
|
|||
|
public string Name { get; private set; } = string.Empty;
|
|||
|
|
|||
|
public int Time_duration { get; private set; } = 0;
|
|||
|
public Dictionary<int, (ICarPartModel, int)> _CarParts = null;
|
|||
|
|
|||
|
[NotMapped]
|
|||
|
public Dictionary<int, (ICarPartModel, int)> CarParts
|
|||
|
{
|
|||
|
get
|
|||
|
{
|
|||
|
if (_CarParts == null)
|
|||
|
{
|
|||
|
_CarParts = CarPartCar
|
|||
|
.ToDictionary(recPC => recPC.Id, recPC =>
|
|||
|
(recPC.CarPart as ICarPartModel, recPC.Count));
|
|||
|
}
|
|||
|
return _CarParts;
|
|||
|
}
|
|||
|
}
|
|||
|
[ForeignKey("WorkId")]
|
|||
|
public virtual List<CarPartWork> CarPartCar { get; set; } = new();
|
|||
|
|
|||
|
|
|||
|
public static Work? Create(STODatabase context, WorkBindingModel model)
|
|||
|
{
|
|||
|
if (model == null)
|
|||
|
{
|
|||
|
return null;
|
|||
|
}
|
|||
|
return new Work()
|
|||
|
{
|
|||
|
Id = model.Id,
|
|||
|
Name = model.Name,
|
|||
|
Time_duration = model.Time_duration,
|
|||
|
CarPartCar = model.CarParts.Select(x => new
|
|||
|
CarPartWork
|
|||
|
{
|
|||
|
CarPart = context.CarParts.First(y => y.Id == x.Key),
|
|||
|
Count = x.Value.Item2
|
|||
|
}).ToList()
|
|||
|
};
|
|||
|
}
|
|||
|
public void Update(WorkBindingModel model)
|
|||
|
{
|
|||
|
if (model == null)
|
|||
|
{
|
|||
|
return;
|
|||
|
}
|
|||
|
Id = model.Id;
|
|||
|
Name = model.Name;
|
|||
|
Time_duration = model.Time_duration;
|
|||
|
}
|
|||
|
public WorkViewModel GetViewModel => new()
|
|||
|
{
|
|||
|
Id = Id,
|
|||
|
Name = Name,
|
|||
|
Time_duration = Time_duration
|
|||
|
};
|
|||
|
|
|||
|
public void UpdateCarParts(STODatabase context, WorkBindingModel model)
|
|||
|
{
|
|||
|
var parts = context.CarPartWork.Where(rec =>
|
|||
|
rec.WorkId == model.Id).ToList();
|
|||
|
if (parts != null && CarParts.Count > 0)
|
|||
|
{
|
|||
|
context.CarPartWork.RemoveRange(parts.Where(rec
|
|||
|
=> !model.CarParts.ContainsKey(rec.CarPartId)));
|
|||
|
context.SaveChanges();
|
|||
|
foreach (var updateComponent in parts)
|
|||
|
{
|
|||
|
updateComponent.Count =
|
|||
|
model.CarParts[updateComponent.CarPartId].Item2;
|
|||
|
model.CarParts.Remove(updateComponent.CarPartId);
|
|||
|
}
|
|||
|
context.SaveChanges();
|
|||
|
}
|
|||
|
var manufacture = context.Works.First(x => x.Id == Id);
|
|||
|
foreach (var pc in model.CarParts)
|
|||
|
{
|
|||
|
context.CarPartWork.Add(new CarPartWork
|
|||
|
{
|
|||
|
Work = manufacture,
|
|||
|
CarPart = context.CarParts.First(x => x.Id == pc.Key),
|
|||
|
Count = pc.Value.Item2
|
|||
|
});
|
|||
|
context.SaveChanges();
|
|||
|
}
|
|||
|
_CarParts = null;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|