PIbd-23_Leonteva_V._A._LawFirm/LawFirm/LawFirmFileImplement/Models/Order.cs

105 lines
4.0 KiB
C#
Raw Normal View History

2024-04-22 22:48:19 +04:00
using LawFirmContracts.BindingModels;
using LawFirmContracts.ViewModels;
using LawFirmDataModels.Enums;
using LawFirmDataModels.Models;
using System.Xml.Linq;
namespace LawFirmFileImplement.Models
{
public class Order : IOrderModel
{
public int Id { get; private set; }
public int DocumentId { get; private set; }
public int ClientId { get; set; }
2024-05-24 18:50:45 +04:00
public int? ImplementerId { get; set; }
public int Count { get; private set; }
2024-04-22 22:48:19 +04:00
public double Sum { get; private set; }
public OrderStatus Status { get; private set; } = OrderStatus.Неизвестен;
2024-06-19 16:06:36 +04:00
public DateTime DateCreate { get; private set; } = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc);
public DateTime? DateImplement { get; private set; }
public static Order? Create(OrderBindingModel? model)
2024-04-22 22:48:19 +04:00
{
if (model == null)
{
return null;
}
return new Order()
{
Id = model.Id,
DocumentId = model.DocumentId,
ClientId = model.ClientId,
Count = model.Count,
2024-04-22 22:48:19 +04:00
Sum = model.Sum,
Status = model.Status,
DateCreate = model.DateCreate,
DateImplement = model.DateImplement,
2024-05-24 18:50:45 +04:00
ImplementerId = model.ImplementerId
2024-04-22 22:48:19 +04:00
};
}
public static Order? Create(XElement element)
{
if (element == null)
{
return null;
}
2024-05-24 18:50:45 +04:00
var dateImplement = element.Element("DateImplement")!.Value;
2024-04-22 22:48:19 +04:00
var order = new Order()
{
Id = Convert.ToInt32(element.Attribute("Id")!.Value),
DocumentId = Convert.ToInt32(element.Element("DocumentId")!.Value),
Count = Convert.ToInt32(element.Element("Count")!.Value),
Sum = Convert.ToDouble(element.Element("Sum")!.Value),
2024-05-24 18:50:45 +04:00
Status = (OrderStatus)Convert.ToInt32(element.Element("Status")!.Value),
ClientId = Convert.ToInt32(element.Element("ClientId")!.Value),
DateCreate = Convert.ToDateTime(element.Element("DateCreate")!.Value),
DateImplement = string.IsNullOrEmpty(dateImplement) ? null : Convert.ToDateTime(dateImplement),
ImplementerId = Convert.ToInt32(element.Element("ImplementerId")!.Value),
2024-04-22 22:48:19 +04:00
};
DateTime.TryParse(element.Element("DateImplement")!.Value, out DateTime dateImpl);
order.DateImplement = dateImpl;
if (!Enum.TryParse(element.Element("Status")!.Value, out OrderStatus status))
{
return null;
}
order.Status = status;
return order;
}
public void Update(OrderBindingModel? model)
{
if (model == null)
{
return;
}
2024-05-24 18:50:45 +04:00
DocumentId = model.DocumentId;
Count = model.Count;
Sum = model.Sum;
2024-04-22 22:48:19 +04:00
Status = model.Status;
2024-05-24 18:50:45 +04:00
DateCreate = model.DateCreate;
ImplementerId = model.ImplementerId;
2024-04-22 22:48:19 +04:00
DateImplement = model.DateImplement;
2024-05-24 18:50:45 +04:00
Id = model.Id;
2024-04-22 22:48:19 +04:00
}
public OrderViewModel GetViewModel => new()
{
DocumentId = DocumentId,
ClientId = ClientId,
Count = Count,
2024-04-22 22:48:19 +04:00
Sum = Sum,
DateCreate = DateCreate,
DateImplement = DateImplement,
Id = Id,
Status = Status,
2024-05-24 18:50:45 +04:00
ImplementerId = ImplementerId
2024-04-22 22:48:19 +04:00
};
public XElement GetXElement => new("Order",
new XAttribute("Id", Id),
new XElement("DocumentId", DocumentId),
new XElement("Count", Count.ToString()),
new XElement("Sum", Sum.ToString()),
new XElement("Status", Status.ToString()),
new XElement("DateCreate", DateCreate.ToString()),
2024-05-24 18:50:45 +04:00
new XElement("DateImplement", DateImplement.ToString()),
new XElement("ImplementerId", ImplementerId));
2024-04-22 22:48:19 +04:00
}
}