2024-04-25 01:10:50 +04:00
|
|
|
|
using ComputerShopContracts.BindingModels;
|
|
|
|
|
using ComputerShopContracts.ViewModels;
|
|
|
|
|
using ComputerShopDataModels.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 ComputerShopDatabaseImplement.Models
|
|
|
|
|
{
|
|
|
|
|
public class Shipment : IShipmentModel
|
|
|
|
|
{
|
2024-04-30 22:44:30 +04:00
|
|
|
|
public int Id { get; private set; }
|
2024-04-25 01:10:50 +04:00
|
|
|
|
|
|
|
|
|
[Required]
|
|
|
|
|
public int UserId { get; private set; }
|
|
|
|
|
|
|
|
|
|
[Required]
|
|
|
|
|
public string ProviderName { get; set; } = string.Empty;
|
|
|
|
|
|
|
|
|
|
[Required]
|
|
|
|
|
public DateTime DateShipment { get; set; } = DateTime.Now;
|
|
|
|
|
|
|
|
|
|
private Dictionary<int, IOrderModel>? _shipmentOrders = null;
|
|
|
|
|
|
|
|
|
|
[NotMapped]
|
2024-04-28 21:00:55 +04:00
|
|
|
|
public Dictionary<int, IOrderModel> ShipmentOrders
|
2024-04-25 01:10:50 +04:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (_shipmentOrders == null)
|
|
|
|
|
{
|
2024-04-25 17:44:09 +04:00
|
|
|
|
_shipmentOrders = Orders.ToDictionary(x => x.OrderId, x => (x.Order as IOrderModel));
|
2024-04-25 01:10:50 +04:00
|
|
|
|
}
|
|
|
|
|
return _shipmentOrders;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-04-30 22:44:30 +04:00
|
|
|
|
|
2024-04-25 17:44:09 +04:00
|
|
|
|
[ForeignKey("ShipmentId")]
|
2024-04-25 01:10:50 +04:00
|
|
|
|
public virtual List<ShipmentOrder> Orders { get; set; } = new();
|
|
|
|
|
|
2024-04-30 14:43:48 +04:00
|
|
|
|
[ForeignKey("ShipmentId")]
|
|
|
|
|
public virtual List<Product> Products { get; set; } = new();
|
|
|
|
|
|
2024-04-25 17:44:09 +04:00
|
|
|
|
|
2024-04-25 01:10:50 +04:00
|
|
|
|
public static Shipment Create(ComputerShopDatabase context, ShipmentBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
return new Shipment()
|
|
|
|
|
{
|
|
|
|
|
Id = model.Id,
|
|
|
|
|
UserId = model.UserId,
|
|
|
|
|
ProviderName = model.ProviderName,
|
|
|
|
|
DateShipment = model.DateShipment,
|
|
|
|
|
Orders = model.ShipmentOrders.Select(x => new ShipmentOrder
|
|
|
|
|
{
|
|
|
|
|
Order = context.Orders.First(y => y.Id == x.Key)
|
|
|
|
|
}).ToList()
|
|
|
|
|
};
|
|
|
|
|
}
|
2024-04-30 22:44:30 +04:00
|
|
|
|
|
2024-04-25 01:10:50 +04:00
|
|
|
|
public void Update(ShipmentBindingModel model)
|
|
|
|
|
{
|
2024-04-28 21:00:55 +04:00
|
|
|
|
if (model == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (!string.IsNullOrEmpty(model.ProviderName))
|
|
|
|
|
{
|
|
|
|
|
ProviderName = model.ProviderName;
|
|
|
|
|
}
|
2024-04-30 00:02:09 +04:00
|
|
|
|
DateShipment = model.DateShipment;
|
2024-04-25 01:10:50 +04:00
|
|
|
|
}
|
2024-04-30 22:44:30 +04:00
|
|
|
|
|
2024-04-25 01:10:50 +04:00
|
|
|
|
public ShipmentViewModel GetViewModel => new()
|
|
|
|
|
{
|
|
|
|
|
Id = Id,
|
|
|
|
|
UserId = UserId,
|
|
|
|
|
ProviderName = ProviderName,
|
|
|
|
|
DateShipment = DateShipment,
|
|
|
|
|
ShipmentOrders = ShipmentOrders
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
public void UpdateOrders(ComputerShopDatabase context, ShipmentBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
var shipmentOrders = context.ShipmentOrders.Where(x => x.ShipmentId == model.Id).ToList();
|
|
|
|
|
//удаление тех заказов, которых нет в модели
|
|
|
|
|
if (shipmentOrders != null && shipmentOrders.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
context.ShipmentOrders.RemoveRange(shipmentOrders.Where(x => !model.ShipmentOrders.ContainsKey(x.OrderId)));
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
//добавление новых заказов
|
|
|
|
|
var currentShipment = context.Shipments.First(x => x.Id == Id);
|
|
|
|
|
foreach (var shipment_order in model.ShipmentOrders)
|
|
|
|
|
{
|
|
|
|
|
context.ShipmentOrders.Add(new ShipmentOrder
|
|
|
|
|
{
|
|
|
|
|
Shipment = currentShipment,
|
|
|
|
|
Order = context.Orders.First(x => x.Id == shipment_order.Key)
|
|
|
|
|
});
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
_shipmentOrders = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|