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

116 lines
4.1 KiB
C#
Raw Permalink 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;
2024-06-19 23:52:53 +04:00
using System.Runtime.Serialization;
2024-04-22 22:48:19 +04:00
namespace LawFirmFileImplement.Models
{
2024-06-19 23:52:53 +04:00
[DataContract]
public class Order : IOrderModel
2024-04-22 22:48:19 +04:00
{
2024-06-19 23:52:53 +04:00
[DataMember]
public int Id { get; private set; }
[DataMember]
public int DocumentId { get; private set; }
[DataMember]
public int ClientId { get; set; }
2024-06-19 23:52:53 +04:00
[DataMember]
public int? ImplementerId { get; set; }
[DataMember]
public int Count { get; private set; }
[DataMember]
public double Sum { get; private set; }
[DataMember]
public OrderStatus Status { get; private set; } = OrderStatus.Неизвестен;
[DataMember]
2024-06-19 16:06:36 +04:00
public DateTime DateCreate { get; private set; } = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc);
2024-06-19 23:52:53 +04:00
[DataMember]
2024-06-19 16:06:36 +04:00
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
}
}