using AccountingWarehouseProductsContracts.BindingModels; using AccountingWarehouseProductsContracts.ViewModels; using AccountingWarehouseProductsDataModels.Enums; using AccountingWarehouseProductsDataModels.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 AccountingWarehouseProductsDatabaseImplement.Models { public class Order : IOrderModel { public int Id { get; set; } public int ProductId { get; set; } public int SupplierId { get; set; } public string ProductName { get; set; } public int Count { get; set; } public double Sum { get; set; } public virtual Product Product { get; set; } public DateTime? DateofOrder { get; set; } = DateTime.Now; public DateTime? DateImplement { get; private set; } public AccountingWarehouseProductsDataModels.Enums.OrderStatus Status { get; set; } private Dictionary? _orderProduct = null; [NotMapped] public Dictionary OrderProduct { get { if (_orderProduct == null) { _orderProduct = Products.ToDictionary(recOP => recOP.ProductId, recOP => (recOP.Product as IProductModel, recOP.Count)); } return _orderProduct; } } [ForeignKey("OrderId")] public virtual List Products { get; set; } = new(); public static Order Create(AccountingWarehouseProductsDatabase context, OrderBindingModel model) { return new Order() { Id = model.Id, ProductId = model.ProductId, SupplierId = model.SupplierId, ProductName = model.ProductName, Count = model.Count, Sum = model.Sum, Status = model.Status, DateofOrder = model.DateofOrder, DateImplement = model.DateImplement, Products = model.OrderProduct.Select(x => new OrderProduct { Product = context.Products.First(y => y.Id == x.Key), Count = x.Value.Item2 }).ToList(), }; } public void Update(OrderBindingModel model) { if (model == null) { return; } Status = model.Status; if (model.DateofOrder != null) { DateofOrder = model.DateofOrder; } DateImplement = model.DateImplement; } public OrderViewModel GetViewModel => new() { Id = Id, ProductId = ProductId, SupplierId = SupplierId, ProductName = ProductName, Count = Count, Sum = Sum, Status = Status, DateofOrder = DateofOrder, DateImplement = DateImplement, OrderProduct = OrderProduct }; public void UpdateProducts(AccountingWarehouseProductsDatabase context, OrderBindingModel model) { var orderProduct = context.OrderProduct.Where(rec => rec.OrderId == model.Id).ToList(); if (orderProduct != null && orderProduct.Count > 0) { context.OrderProduct.RemoveRange(orderProduct.Where(rec => !model.OrderProduct.ContainsKey(rec.ProductId))); context.SaveChanges(); foreach (var updateProduct in orderProduct) { updateProduct.Count = model.OrderProduct[updateProduct.ProductId].Item2; model.OrderProduct.Remove(updateProduct.ProductId); } context.SaveChanges(); } var order = context.Orders.First(x => x.Id == Id); foreach (var rc in model.OrderProduct) { context.OrderProduct.Add(new OrderProduct { Order = order, Product = context.Products.First(x => x.Id == rc.Key), Count = rc.Value.Item2 }); context.SaveChanges(); } _orderProduct = null; } } }