Add ImplementorSoft

This commit is contained in:
Sergey Kozyrev 2024-04-23 22:34:09 +04:00
parent 009e128f55
commit a4e790d380
8 changed files with 137 additions and 12 deletions

View File

@ -8,6 +8,6 @@ namespace Contracts.BindingModels
public int UserId { get; set; }
public string Name { get; set; } = string.Empty;
public double Cost { get; set; }
public Dictionary<int, (IDetailModel, int)> DetailProducts { get; set; } = new();
public Dictionary<int, (IDetailModel, int)> ProductDetails { get; set; } = new();
}
}

View File

@ -8,7 +8,7 @@ namespace Contracts.BindingModels
public int UserId { get; set; }
public string Name { get; set; } = string.Empty;
public double Cost { get; set; }
public Dictionary<int, IDetailModel> ProductionDetails { get; set; } = new();
public Dictionary<int, (IDetailModel, int)> ProductionDetails { get; set; } = new();
}
}

View File

@ -16,5 +16,6 @@ namespace Contracts.ViewModels
[DisplayName("Цена изделия")]
public double Cost { get; set; }
public int UserId { get; set; }
public Dictionary<int, (IDetailModel, int)> DetailProducts { get; set; } = new();
}
}

View File

@ -16,5 +16,6 @@ namespace Contracts.ViewModels
[DisplayName("Цена производства")]
public double Cost { get; set; }
public int UserId { get; set; }
public Dictionary<int, (IDetailModel, int)> DetailProductions { get; set; } = new();
}
}

View File

@ -17,6 +17,8 @@ namespace DatabaseImplement.Models
public int UserId { get; set; }
[ForeignKey("DetailId")]
public virtual List<DetailProduct> DetailProducts { get; set; } = new();
[ForeignKey("DetailId")]
public virtual List<DetailProduction> DetailProductions { get; set; } = new();
public static Detail? Create(DetailBindingModel model)
{
if (model == null)

View File

@ -1,12 +1,23 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DatabaseImplement.Models
{
internal class DetailProduction
public class DetailProduction
{
public int Id { get; set; }
[Required]
public int DetailId { get; set; }
[Required]
public int ProductionId { get; set; }
[Required]
public int Count { get; set; }
public virtual Detail Detail { get; set; } = new();
public virtual Production Production { get; set; } = new();
}
}

View File

@ -31,7 +31,7 @@ namespace DatabaseImplement.Models
[ForeignKey("ProductId")]
public virtual List<DetailProduct> Details { get; set; } = new();
public static Product? Create(ProductBindingModel model)
public static Product? Create(FactoryGoWorkDatabase context, ProductBindingModel model)
{
if (model == null)
{
@ -45,7 +45,8 @@ namespace DatabaseImplement.Models
UserId = model.UserId,
Details = model.DetailProducts.Select(x => new DetailProduct
{
//Detail =
Detail = context.Details.First(y => y.Id == x.Key),
Count = x.Value.Item2
}).ToList()
};
}
@ -71,7 +72,35 @@ namespace DatabaseImplement.Models
Id = Id,
Name = Name,
Cost = Cost,
UserId = UserId
UserId = UserId,
DetailProducts = DetailProducts
};
public void UpdateDetails(FactoryGoWorkDatabase context, ProductBindingModel model)
{
var productDetails = context.DetailProducts.Where(rec => rec.ProductId == model.Id).ToList();
if (productDetails != null && productDetails.Count > 0)
{
context.DetailProducts.RemoveRange(productDetails.Where(rec => !model.ProductDetails.ContainsKey(rec.DetailId)));
context.SaveChanges();
foreach (var upDetail in productDetails)
{
upDetail.Count = model.ProductDetails[upDetail.DetailId].Item2;
model.ProductDetails.Remove(upDetail.DetailId);
}
context.SaveChanges();
}
var product = context.Products.First(x => x.Id == model.Id);
foreach (var dp in model.ProductDetails)
{
context.DetailProducts.Add(new DetailProduct
{
Product = product,
Detail = context.Details.First(x => x.Id == dp.Key),
Count = dp.Value.Item2
});
context.SaveChanges();
}
_detailProducts = null;
}
}
}

View File

@ -1,12 +1,93 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DataModels;
using DataModels.Models;
using Contracts.BindingModels;
using Contracts.ViewModels;
using Contracts.SearchModels;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace DatabaseImplement.Models
{
internal class Production
public class Production : IProductionModel
{
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")]
public List<DetailProduction> Details { get; set; }
[ForeignKey("UserId")]
public virtual Implementer User { get; set; }
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;
}
}
}