2023-05-06 16:30:44 +04:00

135 lines
4.3 KiB
C#

using BeautySaloonDataModels;
using MongoDB.Bson.Serialization.Attributes;
using MongoDB.Bson;
using BeautySaloonContracts.BindingModels;
using BeautySaloonContracts.ViewModels;
using Microsoft.Extensions.Logging;
using Amazon.Auth.AccessControlPolicy;
using MongoDB.Driver;
namespace BeautySaloonNoSQLDatabaseImplement
{
public class Order : IOrderModel
{
[BsonId]
[BsonElement("_id"), BsonRepresentation(BsonType.ObjectId)]
private string IdMongo { get; set; } = string.Empty;
[BsonElement("id")]
public int Id { get; set; }
[BsonElement("date")]
private DateTime DateMongo { get; set; }
[BsonIgnore]
public DateOnly Date {
get
{
return DateOnly.FromDateTime(DateMongo);
}
private set {
DateMongo = value.ToDateTime(new TimeOnly());
}
}
[BsonElement("sum"), BsonRepresentation(BsonType.Double)]
public decimal Sum { get; private set; }
[BsonElement("client")]
public int ClientId { get; private set; }
[BsonElement("employee")]
public int EmployeeId { get; private set; }
private Client? _client;
private Employee? _employee;
[BsonIgnore]
public Client Client
{
get
{
if (_client == null)
{
_client = NewdbContext.GetInstance().Clients
.Find(new BsonDocument("id", ClientId)).FirstOrDefault();
}
return _client;
}
}
[BsonIgnore]
public Employee Employee
{
get
{
if (_employee == null)
{
_employee = NewdbContext.GetInstance().Employees
.Find(new BsonDocument("id", EmployeeId)).FirstOrDefault();
}
return _employee;
}
}
[BsonElement("services")]
public virtual List<ServiceOrder> ServiceOrders { get; set; } = new List<ServiceOrder>();
[BsonIgnore]
public Dictionary<int, (TimeOnly, IServiceModel, int)> _orderServices = null!;
[BsonIgnore]
public Dictionary<int, (TimeOnly, IServiceModel, int)> OrderServices
{
get
{
if (_orderServices == null)
{
_orderServices = ServiceOrders
.ToDictionary(recPC => recPC.Service.Id,
recPC => (recPC.Date, recPC.Service as IServiceModel, recPC.Employee.Id));
}
return _orderServices;
}
}
public static Order? Create(OrderBindingModel? model)
{
if (model == null)
{
return null;
}
return new Order()
{
Id = model.Id,
Date = model.Date,
Sum = model.Sum,
ClientId = model.ClientId,
EmployeeId = model.EmployeeId,
ServiceOrders = model.OrderServices.Select(x => new ServiceOrder
{
Service = NewdbContext.GetInstance().Services.Find(new BsonDocument("id", x.Key)).FirstOrDefault(),
Date = x.Value.Item1,
Employee = NewdbContext.GetInstance().Employees.Find(new BsonDocument("id", x.Value.Item3)).FirstOrDefault(),
}).ToList()
};
}
public void Update(OrderBindingModel model)
{
Date = model.Date;
Sum = model.Sum;
EmployeeId = model.EmployeeId;
ClientId = model.ClientId;
}
public OrderViewModel GetViewModel => new()
{
Id = Id,
Date = Date,
Sum = Sum,
ClientId = ClientId,
EmployeeId = EmployeeId,
ClientName = Client.Name + " " + Client.Surname + " " + Client.Patronymic,
EmployeeName = Employee.Name + " " + Employee.Surname + " " + Employee.Patronymic,
ClientPhone = Client.Phone,
EmployeePhone = Employee.Phone,
OrderServices = OrderServices
};
}
}