2024-04-27 19:12:43 +04:00
|
|
|
|
using ServiceStationContracts.BindingModels;
|
|
|
|
|
using ServiceStationContracts.ViewModels;
|
|
|
|
|
using ServiceStationDataModels.Enums;
|
|
|
|
|
using ServiceStationDataModels.Models;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.ComponentModel.DataAnnotations;
|
|
|
|
|
using System.ComponentModel.DataAnnotations.Schema;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace ServiceStationDatabaseImplement.Models
|
|
|
|
|
{
|
|
|
|
|
public class Work : IWorkModel
|
|
|
|
|
{
|
|
|
|
|
public int Id { get; set; }
|
|
|
|
|
|
|
|
|
|
[Required]
|
|
|
|
|
public string WorkName { get; set; } = string.Empty;
|
|
|
|
|
|
|
|
|
|
[Required]
|
|
|
|
|
public WorkStatus Status { get; set; }
|
|
|
|
|
|
|
|
|
|
[Required]
|
|
|
|
|
public double WorkPrice { get; set; }
|
|
|
|
|
|
|
|
|
|
[Required]
|
|
|
|
|
public int GuarantorId { get; set; }
|
|
|
|
|
|
2024-04-29 00:50:40 +04:00
|
|
|
|
public int? TechnicalWorkId { get; set; }
|
|
|
|
|
|
2024-04-27 19:12:43 +04:00
|
|
|
|
public virtual Guarantor Guarantor { get; set; }
|
2024-04-29 00:50:40 +04:00
|
|
|
|
public virtual TechnicalWork? TechnicalWork { get; set; }
|
2024-04-27 19:12:43 +04:00
|
|
|
|
|
|
|
|
|
private Dictionary<int, ISparePartModel>? _workSpareParts = null;
|
|
|
|
|
|
2024-04-28 21:27:37 +04:00
|
|
|
|
[NotMapped]
|
2024-04-27 19:12:43 +04:00
|
|
|
|
public Dictionary<int, ISparePartModel> WorkSpareParts
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (_workSpareParts == null)
|
|
|
|
|
{
|
|
|
|
|
_workSpareParts = SpareParts.ToDictionary(rec => rec.SparePartId, rec => rec.SparePart as ISparePartModel);
|
|
|
|
|
}
|
|
|
|
|
return _workSpareParts;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[ForeignKey("WorkId")]
|
|
|
|
|
public virtual List<SparePartWork> SpareParts { get; set; } = new();
|
|
|
|
|
|
|
|
|
|
public static Work? Create(ServiceStationDatabase context, WorkBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
if (model == null) return null;
|
|
|
|
|
return new Work()
|
|
|
|
|
{
|
|
|
|
|
Id = model.Id,
|
|
|
|
|
WorkName = model.WorkName,
|
|
|
|
|
Status = model.Status,
|
|
|
|
|
WorkPrice = model.WorkPrice,
|
|
|
|
|
GuarantorId = model.GuarantorId,
|
|
|
|
|
SpareParts = model.WorkSpareParts.Select(x => new SparePartWork
|
|
|
|
|
{
|
|
|
|
|
SparePart = context.SpareParts.First(y => y.Id == x.Key)
|
|
|
|
|
}).ToList()
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Update(WorkBindingModel model)
|
|
|
|
|
{
|
2024-04-29 00:50:40 +04:00
|
|
|
|
if(model == null) return;
|
|
|
|
|
if (model.TechnicalWorkId.HasValue)
|
|
|
|
|
{
|
|
|
|
|
TechnicalWorkId = model.TechnicalWorkId;
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-04-27 19:12:43 +04:00
|
|
|
|
WorkName = model.WorkName;
|
|
|
|
|
Status = model.Status;
|
|
|
|
|
WorkPrice = model.WorkPrice;
|
|
|
|
|
GuarantorId = model.GuarantorId;
|
|
|
|
|
}
|
|
|
|
|
public WorkViewModel GetViewModel => new()
|
|
|
|
|
{
|
|
|
|
|
Id = Id,
|
|
|
|
|
WorkName = WorkName,
|
|
|
|
|
Status = Status,
|
|
|
|
|
WorkPrice = WorkPrice,
|
|
|
|
|
GuarantorId = GuarantorId,
|
2024-04-29 00:50:40 +04:00
|
|
|
|
TechnicalWorkId = TechnicalWorkId,
|
|
|
|
|
WorkSpareParts = WorkSpareParts,
|
2024-04-27 19:12:43 +04:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
public void UpdateSpareParts(ServiceStationDatabase context, WorkBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
var sparepartWorks = context.SparePartWorks.Where(rec => rec.WorkId == model.Id).ToList();
|
|
|
|
|
if (sparepartWorks != null && sparepartWorks.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
context.SparePartWorks.RemoveRange(sparepartWorks.Where(rec => !model.WorkSpareParts.ContainsKey(rec.SparePartId)));
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
|
|
|
|
|
foreach (var updateCar in sparepartWorks)
|
|
|
|
|
{
|
|
|
|
|
model.WorkSpareParts.Remove(updateCar.SparePartId);
|
|
|
|
|
}
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var work = context.Works.First(x => x.Id == Id);
|
|
|
|
|
foreach (var cd in model.WorkSpareParts)
|
|
|
|
|
{
|
|
|
|
|
context.SparePartWorks.Add(new SparePartWork
|
|
|
|
|
{
|
|
|
|
|
Work = work,
|
|
|
|
|
SparePart = context.SpareParts.First(x => x.Id == cd.Key)
|
|
|
|
|
});
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
_workSpareParts = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|