2024-04-30 19:12:40 +04:00
|
|
|
|
|
|
|
|
|
using ComputerHardwareStoreContracts.BindingModels;
|
|
|
|
|
using ComputerHardwareStoreContracts.ViewModels;
|
|
|
|
|
using ComputerHardwareStoreDataModels.Models;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using System.ComponentModel.DataAnnotations;
|
|
|
|
|
using System.ComponentModel.DataAnnotations.Schema;
|
|
|
|
|
|
|
|
|
|
namespace ComputerHardwareStoreDatabaseImplement.Models
|
|
|
|
|
{
|
|
|
|
|
public class Purchase : IPurchaseModel
|
|
|
|
|
{
|
|
|
|
|
public int Id { get; private set; }
|
|
|
|
|
[Required]
|
|
|
|
|
public int VendorId { get; set; }
|
|
|
|
|
[Required]
|
|
|
|
|
public double Cost { get; set; }
|
|
|
|
|
[Required]
|
|
|
|
|
public DateTime DateCreate { get; set; } = DateTime.Now;
|
2024-05-01 11:55:41 +04:00
|
|
|
|
public DateTime? DateImplement { get; set; } = null;
|
2024-04-30 19:12:40 +04:00
|
|
|
|
private Dictionary<int, (IBuildModel, int)>? _purchaseBuilds = null;
|
|
|
|
|
[NotMapped]
|
|
|
|
|
public Dictionary<int, (IBuildModel, int)> PurchaseBuilds
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (_purchaseBuilds == null)
|
|
|
|
|
{
|
|
|
|
|
_purchaseBuilds = Builds
|
|
|
|
|
.ToDictionary(op => op.BuildId, op => (op.Build as IBuildModel, op.Count));
|
|
|
|
|
}
|
|
|
|
|
return _purchaseBuilds;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[ForeignKey("PurchaseId")]
|
|
|
|
|
public virtual List<PurchaseBuild> Builds { get; set; } = new();
|
|
|
|
|
private Dictionary<int, (IProductModel, int)>? _purchaseProducts = null;
|
|
|
|
|
[NotMapped]
|
|
|
|
|
public Dictionary<int, (IProductModel, int)> PurchaseProducts
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (_purchaseProducts == null)
|
|
|
|
|
{
|
|
|
|
|
_purchaseProducts = Products
|
|
|
|
|
.ToDictionary(op => op.ProductId, op => (op.Product as IProductModel, op.Count));
|
|
|
|
|
}
|
|
|
|
|
return _purchaseProducts;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
[ForeignKey("PurchaseId")]
|
|
|
|
|
public virtual List<PurchaseProduct> Products { get; set; } = new();
|
|
|
|
|
|
|
|
|
|
public static Purchase Create(ComputerHardwareStoreDBContext context, PurchaseBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
return new Purchase()
|
|
|
|
|
{
|
|
|
|
|
Id = model.Id,
|
|
|
|
|
Cost = model.Cost,
|
|
|
|
|
DateCreate = model.DateCreate,
|
2024-04-30 20:06:35 +04:00
|
|
|
|
Builds = context.Builds
|
|
|
|
|
.Where(b => model.PurchaseBuilds.ContainsKey(b.Id))
|
|
|
|
|
.Select(b => new PurchaseBuild()
|
|
|
|
|
{
|
|
|
|
|
PurchaseId = model.Id,
|
|
|
|
|
BuildId = b.Id,
|
|
|
|
|
Build = b,
|
|
|
|
|
Count = model.PurchaseProducts[b.Id].Item2
|
|
|
|
|
}).ToList(),
|
|
|
|
|
Products = context.Products
|
|
|
|
|
.Where(p => model.PurchaseProducts.ContainsKey(p.Id))
|
|
|
|
|
.Select (p => new PurchaseProduct()
|
|
|
|
|
{
|
|
|
|
|
PurchaseId = model.Id,
|
|
|
|
|
ProductId = p.Id,
|
|
|
|
|
Product = p,
|
|
|
|
|
Count = model.PurchaseProducts[p.Id].Item2
|
|
|
|
|
})
|
|
|
|
|
.ToList()
|
2024-04-30 19:12:40 +04:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
public void Update(PurchaseBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
Cost = model.Cost;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
public PurchaseViewModel GetViewModel => new()
|
|
|
|
|
{
|
|
|
|
|
Id = Id,
|
|
|
|
|
Cost = Cost,
|
|
|
|
|
DateCreate = DateCreate,
|
|
|
|
|
PurchaseBuilds = PurchaseBuilds,
|
|
|
|
|
PurchaseProducts = PurchaseProducts
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
public static void UpdateBuilds(ComputerHardwareStoreDBContext context, PurchaseBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
var productBuilds = context.PurchaseBuilds
|
|
|
|
|
.Where(pc => pc.PurchaseId == model.Id)
|
|
|
|
|
.ToList();
|
|
|
|
|
if (productBuilds.Count != 0 && productBuilds.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
// удалили те, которых нет в модели
|
|
|
|
|
context.PurchaseBuilds
|
|
|
|
|
.Where(pc => !model.PurchaseBuilds.ContainsKey(pc.BuildId))
|
|
|
|
|
.ExecuteDelete();
|
|
|
|
|
// обновили количество у существующих записей
|
|
|
|
|
productBuilds
|
|
|
|
|
.ForEach(updateBuild =>
|
|
|
|
|
{
|
|
|
|
|
updateBuild.Count = model.PurchaseBuilds[updateBuild.BuildId].Item2;
|
|
|
|
|
model.PurchaseBuilds.Remove(updateBuild.BuildId);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
// добавили новые
|
|
|
|
|
context.PurchaseBuilds
|
|
|
|
|
.AddRange(model.PurchaseBuilds.Values
|
|
|
|
|
.Select(val => new PurchaseBuild()
|
|
|
|
|
{
|
|
|
|
|
PurchaseId = model.Id,
|
|
|
|
|
BuildId = val.Item1.Id,
|
|
|
|
|
Count = val.Item2
|
|
|
|
|
}));
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void UpdateProducts(ComputerHardwareStoreDBContext context, PurchaseBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
var productProducts = context.PurchaseProducts
|
|
|
|
|
.Where(pc => pc.PurchaseId == model.Id)
|
|
|
|
|
.ToList();
|
|
|
|
|
if (productProducts.Count != 0 && productProducts.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
// удалили те, которых нет в модели
|
|
|
|
|
context.PurchaseProducts
|
|
|
|
|
.Where(pc => !model.PurchaseProducts.ContainsKey(pc.ProductId))
|
|
|
|
|
.ExecuteDelete();
|
|
|
|
|
// обновили количество у существующих записей
|
|
|
|
|
productProducts
|
|
|
|
|
.ForEach(updateProduct =>
|
|
|
|
|
{
|
|
|
|
|
updateProduct.Count = model.PurchaseProducts[updateProduct.ProductId].Item2;
|
|
|
|
|
model.PurchaseProducts.Remove(updateProduct.ProductId);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
// добавили новые
|
|
|
|
|
context.PurchaseProducts
|
|
|
|
|
.AddRange(model.PurchaseProducts.Values
|
|
|
|
|
.Select(val => new PurchaseProduct()
|
|
|
|
|
{
|
|
|
|
|
PurchaseId = model.Id,
|
|
|
|
|
ProductId = val.Item1.Id,
|
|
|
|
|
Count = val.Item2
|
|
|
|
|
}));
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|