2024-04-23 22:34:09 +04:00
|
|
|
|
using DataModels;
|
|
|
|
|
using DataModels.Models;
|
|
|
|
|
using Contracts.BindingModels;
|
|
|
|
|
using Contracts.ViewModels;
|
|
|
|
|
using Contracts.SearchModels;
|
|
|
|
|
using System.ComponentModel.DataAnnotations;
|
|
|
|
|
using System.ComponentModel.DataAnnotations.Schema;
|
2024-04-21 20:36:07 +04:00
|
|
|
|
|
|
|
|
|
namespace DatabaseImplement.Models
|
|
|
|
|
{
|
2024-04-23 22:34:09 +04:00
|
|
|
|
public class Production : IProductionModel
|
2024-04-21 20:36:07 +04:00
|
|
|
|
{
|
2024-04-23 22:34:09 +04:00
|
|
|
|
public int Id { get; set; }
|
|
|
|
|
[Required]
|
|
|
|
|
public string Name { get; set; } = string.Empty;
|
|
|
|
|
[Required]
|
|
|
|
|
public double Cost { get; set; }
|
|
|
|
|
[Required]
|
|
|
|
|
public int UserId { get; set; }
|
|
|
|
|
private Dictionary<int, (IDetailModel, int)>? _detailProductions = null;
|
|
|
|
|
[NotMapped]
|
|
|
|
|
public Dictionary<int, (IDetailModel, int)>? DetailProductions
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (_detailProductions == null)
|
|
|
|
|
{
|
|
|
|
|
_detailProductions = Details.ToDictionary(recDP => recDP.DetailId, recDp => (recDp.Detail as IDetailModel, recDp.Count));
|
|
|
|
|
}
|
|
|
|
|
return _detailProductions;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
[ForeignKey("ProductionId")]
|
2024-04-24 12:35:17 +04:00
|
|
|
|
public List<DetailProduction> Details { get; set; } = new();
|
2024-04-23 22:34:09 +04:00
|
|
|
|
public virtual Implementer User { get; set; }
|
2024-04-24 12:35:17 +04:00
|
|
|
|
|
2024-04-23 22:34:09 +04:00
|
|
|
|
public static Production Create(FactoryGoWorkDatabase context, ProductionBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
return new Production()
|
|
|
|
|
{
|
|
|
|
|
Id = model.Id,
|
|
|
|
|
Name = model.Name,
|
|
|
|
|
Cost = model.Cost,
|
|
|
|
|
UserId = model.UserId,
|
|
|
|
|
Details = model.ProductionDetails.Select(x => new DetailProduction
|
|
|
|
|
{
|
|
|
|
|
Detail = context.Details.First(y => y.Id == x.Key),
|
|
|
|
|
}).ToList(),
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
public void Update(ProductionBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
Name = model.Name;
|
|
|
|
|
Cost = model.Cost;
|
|
|
|
|
}
|
|
|
|
|
public ProductionViewModel GetViewModel => new()
|
|
|
|
|
{
|
|
|
|
|
Id = Id,
|
|
|
|
|
Name = Name,
|
|
|
|
|
Cost = Cost,
|
|
|
|
|
UserId = UserId,
|
|
|
|
|
DetailProductions = DetailProductions
|
|
|
|
|
};
|
|
|
|
|
public void UpdateDetails(FactoryGoWorkDatabase context, ProductionBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
var productionDetails = context.DetailProductions.Where(rec => rec.ProductionId == model.Id).ToList();
|
|
|
|
|
if (productionDetails != null && productionDetails.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
context.DetailProductions.RemoveRange(productionDetails.Where(rec => !model.ProductionDetails.ContainsKey(rec.DetailId)));
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
foreach(var upDetail in productionDetails)
|
|
|
|
|
{
|
|
|
|
|
upDetail.Count = model.ProductionDetails[upDetail.DetailId].Item2;
|
|
|
|
|
model.ProductionDetails.Remove(upDetail.DetailId);
|
|
|
|
|
}
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
var production = context.Productions.First(x => x.Id == model.Id);
|
|
|
|
|
foreach (var dp in model.ProductionDetails)
|
|
|
|
|
{
|
|
|
|
|
context.DetailProductions.Add(new DetailProduction
|
|
|
|
|
{
|
|
|
|
|
Production = production,
|
|
|
|
|
Detail = context.Details.First(x => x.Id == dp.Key),
|
|
|
|
|
Count = dp.Value.Item2
|
|
|
|
|
});
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
_detailProductions = null;
|
|
|
|
|
}
|
2024-04-21 20:36:07 +04:00
|
|
|
|
}
|
|
|
|
|
}
|