PIbd-22_Chernyshev_G.J._30_.../GarmentFactoryFileImplement/Models/Order.cs

120 lines
3.8 KiB
C#
Raw Normal View History

2024-03-16 20:47:16 +04:00
using GarmentFactoryContracts.BindingModels;
using GarmentFactoryContracts.ViewModels;
using GarmentFactoryDataModels.Enums;
using GarmentFactoryDataModels.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
2024-03-16 20:47:16 +04:00
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace GarmentFactoryFileImplement.Models
{
[DataContract]
public class Order : IOrderModel
2024-03-16 20:47:16 +04:00
{
[DataMember]
public int Id { get; private set; }
[DataMember]
public int ClientId { get; private set; }
[DataMember]
2024-04-17 10:42:00 +04:00
public int? ImplementerId { get; set; }
[DataMember]
2024-04-17 10:42:00 +04:00
public int TextileId { get; private set; }
[DataMember]
public int Count { get; private set; }
[DataMember]
public double Sum { get; private set; }
[DataMember]
public OrderStatus Status { get; private set; } = OrderStatus.Неизвестен;
[DataMember]
public DateTime DateCreate { get; private set; } = DateTime.Now;
[DataMember]
public DateTime? DateComplete { get; private set; }
2024-03-16 20:47:16 +04:00
public static Order? Create(OrderBindingModel? model)
{
if (model == null)
{
return null;
}
return new Order()
{
Id = model.Id,
2024-04-11 13:43:22 +04:00
ClientId = model.ClientId,
2024-04-17 10:42:00 +04:00
ImplementerId = model.ImplementerId,
TextileId = model.TextileId,
2024-03-16 20:47:16 +04:00
Count = model.Count,
Sum = model.Sum,
Status = model.Status,
DateCreate = model.DateCreate,
DateComplete = model.DateComplete
};
}
public static Order? Create(XElement element)
{
if (element == null)
{
return null;
}
string dateComplete = element.Element("DateComplete")!.Value;
return new Order()
{
Id = Convert.ToInt32(element.Attribute("Id")!.Value),
2024-04-11 13:43:22 +04:00
ClientId = Convert.ToInt32(element.Element("ClientId")!.Value),
2024-03-16 20:47:16 +04:00
TextileId = Convert.ToInt32(element.Element("TextileId")!.Value),
2024-04-17 10:42:00 +04:00
ImplementerId = Convert.ToInt32(element.Element("ImplementerId")!.Value),
Count = Convert.ToInt32(element.Element("Count")!.Value),
2024-03-16 20:47:16 +04:00
Sum = Convert.ToDouble(element.Element("Sum")!.Value),
Status = (OrderStatus)(Enum.Parse(typeof(OrderStatus), element.Element("Status")!.Value)),
DateCreate = Convert.ToDateTime(element.Element("DateCreate")!.Value),
DateComplete = (dateComplete == "" || dateComplete is null) ? Convert.ToDateTime(null) : Convert.ToDateTime(dateComplete)
};
}
public void Update(OrderBindingModel? model)
{
if (model == null)
{
return;
}
Status = model.Status;
if (model.Status == OrderStatus.Выдан) {
DateComplete = model.DateComplete;
}
}
public OrderViewModel GetViewModel => new()
{
Id = Id,
2024-04-11 13:43:22 +04:00
ClientId = ClientId,
2024-03-16 20:47:16 +04:00
TextileId = TextileId,
2024-04-17 10:42:00 +04:00
ImplementerId = ImplementerId,
Count = Count,
2024-03-16 20:47:16 +04:00
Sum = Sum,
Status = Status,
DateCreate = DateCreate,
DateComplete = DateComplete
};
public XElement GetXElement => new("Order",
new XAttribute("Id", Id),
2024-04-11 13:43:22 +04:00
new XElement("ClientId", ClientId.ToString()),
2024-03-16 20:47:16 +04:00
new XElement("TextileId", TextileId.ToString()),
2024-04-17 10:42:00 +04:00
new XElement("ImplementerId", ImplementerId),
new XElement("Count", Count.ToString()),
2024-03-16 20:47:16 +04:00
new XElement("Sum", Sum.ToString()),
new XElement("Status", Status.ToString()),
new XElement("DateCreate", DateCreate.ToString()),
new XElement("DateComplete", DateComplete.ToString()));
}
}