SUBD_PIbd-21_Balberova_D.N./BeautySaloon/BeautySaloonDatabaseImplement/Order.cs

140 lines
4.3 KiB
C#
Raw Permalink Normal View History

2023-03-28 21:24:52 +04:00
using BeautySaloonContracts.BindingModels;
using BeautySaloonContracts.ViewModels;
using BeautySaloonDataModels;
2023-03-29 22:56:00 +04:00
using System.ComponentModel.DataAnnotations.Schema;
2023-03-28 21:24:52 +04:00
namespace BeautySaloonDatabaseImplement;
/// <summary>
/// Сущность заказы
/// </summary>
public partial class Order : IOrderModel
{
/// <summary>
/// Уникальный идентификатор
/// </summary>
public int Id { get; set; }
/// <summary>
/// Дата
/// </summary>
public DateOnly Date { get; set; }
/// <summary>
/// Сумма
/// </summary>
public decimal Sum { get; set; }
/// <summary>
/// Идентификатор клиента
/// </summary>
public int ClientId { get; set; }
/// <summary>
/// Идентификатор сотрудника
/// </summary>
public int EmployeeId { get; set; }
public virtual Client Client { get; set; } = null!;
public virtual Employee Employee { get; set; } = null!;
2023-03-29 22:56:00 +04:00
public virtual List<ServiceOrder> ServiceOrders { get; set; } = new List<ServiceOrder>();
2023-03-28 21:24:52 +04:00
2023-03-29 20:00:53 +04:00
public Dictionary<int, (TimeOnly, IServiceModel, int)> _orderServices = null;
2023-03-29 22:56:00 +04:00
2023-03-29 20:00:53 +04:00
public Dictionary<int, (TimeOnly, IServiceModel, int)> OrderServices
2023-03-28 21:24:52 +04:00
{
get
{
2023-03-29 20:00:53 +04:00
if (_orderServices == null)
2023-03-28 21:24:52 +04:00
{
2023-03-29 22:56:00 +04:00
_orderServices = ServiceOrders
.ToDictionary(recPC => recPC.ServiceId,
recPC => (recPC.Date, recPC.Service as IServiceModel, recPC.EmployeeId));
2023-03-28 21:24:52 +04:00
}
return _orderServices;
}
}
2023-03-29 20:00:53 +04:00
public static Order? Create(NewdbContext context, OrderBindingModel? model)
2023-03-28 21:24:52 +04:00
{
if (model == null)
{
return null;
}
return new Order()
{
Id = model.Id,
Date = model.Date,
Sum = model.Sum,
ClientId = model.ClientId,
2023-03-29 20:00:53 +04:00
EmployeeId = model.EmployeeId,
2023-03-29 22:56:00 +04:00
ServiceOrders = model.OrderServices.Select(x => new ServiceOrder
2023-03-29 20:00:53 +04:00
{
Service = context.Services.First(y => y.Id == x.Key),
Date = x.Value.Item1,
EmployeeId = x.Value.Item3,
}).ToList()
2023-03-28 21:24:52 +04:00
};
}
2023-03-29 20:00:53 +04:00
public void Update(OrderBindingModel model)
{
Date = model.Date;
Sum = model.Sum;
EmployeeId = model.EmployeeId;
ClientId = model.ClientId;
}
2023-03-28 21:24:52 +04:00
public OrderViewModel GetViewModel => new()
{
Id = Id,
Date = Date,
Sum = Sum,
ClientId = ClientId,
EmployeeId = EmployeeId,
2023-03-29 20:00:53 +04:00
ClientName = Client.Name + " " + Client.Surname + " " + Client.Patronymic,
EmployeeName = Employee.Name + " " + Employee.Surname + " " + Employee.Patronymic,
2023-03-28 21:24:52 +04:00
ClientPhone = Client.Phone,
2023-03-30 00:23:51 +04:00
EmployeePhone = Employee.Phone,
OrderServices = OrderServices
2023-03-28 21:24:52 +04:00
};
2023-03-29 20:00:53 +04:00
public void UpdateServices(NewdbContext context, OrderBindingModel model)
{
var orderServices = context.ServiceOrders
.Where(rec => rec.OrderId == model.Id).ToList();
if (orderServices != null && orderServices.Count > 0)
{ // удалили те, которых нет в модели
context.ServiceOrders
.RemoveRange(orderServices
.Where(rec => !model.OrderServices.ContainsKey(rec.ServiceId)));
context.SaveChanges();
// обновили данные у существующих записей
foreach (var updateService in orderServices)
{
updateService.Date = model.OrderServices[updateService.ServiceId].Item1;
updateService.EmployeeId =
model.OrderServices[updateService.ServiceId].Item3;
model.OrderServices.Remove(updateService.ServiceId);
}
context.SaveChanges();
}
var order = context.Orders.First(x => x.Id == Id);
foreach (var os in model.OrderServices)
{
context.ServiceOrders.Add(new ServiceOrder
{
Order = order,
Service = context.Services.First(x => x.Id == os.Key),
Date = os.Value.Item1,
EmployeeId = os.Value.Item3
});
context.SaveChanges();
}
_orderServices = null;
}
2023-03-28 21:24:52 +04:00
}