2024-04-21 20:36:07 +04:00
|
|
|
|
using Contracts.BindingModels;
|
|
|
|
|
using Contracts.ViewModels;
|
|
|
|
|
using DataModels.Models;
|
|
|
|
|
using System.ComponentModel.DataAnnotations;
|
|
|
|
|
using System.ComponentModel.DataAnnotations.Schema;
|
|
|
|
|
|
|
|
|
|
namespace DatabaseImplement.Models
|
|
|
|
|
{
|
|
|
|
|
public class Product : IProductModel
|
|
|
|
|
{
|
|
|
|
|
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; }
|
2024-04-24 12:35:17 +04:00
|
|
|
|
public virtual Implementer User { get; set; }
|
2024-04-27 21:46:39 +04:00
|
|
|
|
public int? MachineId { get; set; }
|
|
|
|
|
public virtual Machine? Machine { get; set; }
|
2024-04-21 20:36:07 +04:00
|
|
|
|
private Dictionary<int, (IDetailModel, int)>? _detailProducts = null;
|
|
|
|
|
[NotMapped]
|
|
|
|
|
public Dictionary<int, (IDetailModel, int)> DetailProducts
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (_detailProducts == null)
|
|
|
|
|
{
|
|
|
|
|
_detailProducts = Details.ToDictionary(recDP => recDP.DetailId, recDP => (recDP.Detail as IDetailModel, recDP.Count));
|
|
|
|
|
}
|
|
|
|
|
return _detailProducts;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[ForeignKey("ProductId")]
|
|
|
|
|
public virtual List<DetailProduct> Details { get; set; } = new();
|
2024-04-27 21:07:45 +04:00
|
|
|
|
public static Product? Create(FactoryGoWorkDatabase context, ProductBindingModel? model)
|
2024-04-21 20:36:07 +04:00
|
|
|
|
{
|
|
|
|
|
if (model == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return new Product
|
|
|
|
|
{
|
|
|
|
|
Id = model.Id,
|
|
|
|
|
Name = model.Name,
|
|
|
|
|
Cost = model.Cost,
|
|
|
|
|
UserId = model.UserId,
|
2024-04-27 21:46:39 +04:00
|
|
|
|
MachineId = model.MachineId,
|
|
|
|
|
Machine = model.MachineId.HasValue ? context.Machines.FirstOrDefault(x => x.Id == model.MachineId) : null,
|
2024-04-24 12:35:17 +04:00
|
|
|
|
Details = model.ProductDetails.Select(x => new DetailProduct
|
2024-04-21 20:36:07 +04:00
|
|
|
|
{
|
2024-04-23 22:34:09 +04:00
|
|
|
|
Detail = context.Details.First(y => y.Id == x.Key),
|
|
|
|
|
Count = x.Value.Item2
|
2024-04-21 20:36:07 +04:00
|
|
|
|
}).ToList()
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
public static Product Create(ProductViewModel model)
|
|
|
|
|
{
|
|
|
|
|
return new Product
|
|
|
|
|
{
|
|
|
|
|
Id = model.Id,
|
|
|
|
|
Name = model.Name,
|
|
|
|
|
Cost = model.Cost,
|
2024-04-27 21:46:39 +04:00
|
|
|
|
UserId = model.UserId,
|
|
|
|
|
MachineId = model.MachineId
|
2024-04-21 20:36:07 +04:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
public void Update(ProductBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
if (model == null)
|
|
|
|
|
return;
|
|
|
|
|
Name = model.Name;
|
|
|
|
|
Cost = model.Cost;
|
2024-05-26 23:23:24 +04:00
|
|
|
|
MachineId = model.MachineId == null ? MachineId : model.MachineId;
|
2024-04-21 20:36:07 +04:00
|
|
|
|
}
|
|
|
|
|
public ProductViewModel GetViewModel => new()
|
|
|
|
|
{
|
|
|
|
|
Id = Id,
|
|
|
|
|
Name = Name,
|
|
|
|
|
Cost = Cost,
|
2024-04-23 22:34:09 +04:00
|
|
|
|
UserId = UserId,
|
2024-04-27 21:46:39 +04:00
|
|
|
|
DetailProducts = DetailProducts,
|
2024-05-26 23:23:24 +04:00
|
|
|
|
MachineId= MachineId,
|
|
|
|
|
MachineName = Machine == null ? null : Machine.Title
|
2024-04-21 20:36:07 +04:00
|
|
|
|
};
|
2024-04-23 22:34:09 +04:00
|
|
|
|
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)
|
|
|
|
|
{
|
2024-04-30 17:03:49 +04:00
|
|
|
|
if (model.ProductDetails.ContainsKey(upDetail.DetailId))
|
|
|
|
|
{
|
|
|
|
|
upDetail.Count = model.ProductDetails[upDetail.DetailId].Item2;
|
|
|
|
|
model.ProductDetails.Remove(upDetail.DetailId);
|
|
|
|
|
}
|
2024-04-23 22:34:09 +04:00
|
|
|
|
}
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
}
|
2024-04-30 17:03:49 +04:00
|
|
|
|
var product = context.Products.FirstOrDefault(x => x.Id == model.Id);
|
2024-04-23 22:34:09 +04:00
|
|
|
|
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;
|
|
|
|
|
}
|
2024-04-21 20:36:07 +04:00
|
|
|
|
}
|
|
|
|
|
}
|