2024-04-25 01:10:50 +04:00
|
|
|
|
using ComputerShopContracts.BindingModels;
|
|
|
|
|
using ComputerShopContracts.ViewModels;
|
|
|
|
|
using ComputerShopDataModels.Models;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.ComponentModel.DataAnnotations;
|
|
|
|
|
using System.ComponentModel.DataAnnotations.Schema;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace ComputerShopDatabaseImplement.Models
|
|
|
|
|
{
|
|
|
|
|
public class Request : IRequestModel
|
|
|
|
|
{
|
2024-04-30 22:44:30 +04:00
|
|
|
|
public int Id { get; private set; }
|
2024-04-25 01:10:50 +04:00
|
|
|
|
[Required]
|
2024-04-30 22:44:30 +04:00
|
|
|
|
public int UserId { get; private set; }
|
2024-04-25 01:10:50 +04:00
|
|
|
|
|
|
|
|
|
public virtual User User { get; set; }
|
|
|
|
|
|
2024-04-25 17:44:09 +04:00
|
|
|
|
public int? AssemblyId { get; set; }
|
|
|
|
|
|
2024-04-27 15:55:58 +04:00
|
|
|
|
public virtual Assembly? Assembly { get; set; } = new();
|
|
|
|
|
|
2024-04-25 01:10:50 +04:00
|
|
|
|
[Required]
|
2024-04-25 23:01:53 +04:00
|
|
|
|
public DateTime DateRequest { get; set; }
|
2024-04-25 17:44:09 +04:00
|
|
|
|
|
2024-04-25 01:10:50 +04:00
|
|
|
|
[Required]
|
|
|
|
|
public string ClientFIO { get; set; } = string.Empty;
|
|
|
|
|
|
|
|
|
|
private Dictionary<int, IOrderModel>? _requestOrders = null;
|
|
|
|
|
|
|
|
|
|
[NotMapped]
|
|
|
|
|
public Dictionary<int, IOrderModel> RequestOrders
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (_requestOrders == null)
|
|
|
|
|
{
|
2024-04-30 00:02:09 +04:00
|
|
|
|
_requestOrders = Orders.ToDictionary(x => x.OrderId, x => x.Order as IOrderModel);
|
2024-04-25 01:10:50 +04:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
return _requestOrders;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-25 17:44:09 +04:00
|
|
|
|
[ForeignKey("RequestId")]
|
2024-04-25 01:10:50 +04:00
|
|
|
|
public virtual List<RequestOrder> Orders { get; set; } = new();
|
|
|
|
|
|
2024-04-27 15:55:58 +04:00
|
|
|
|
|
2024-04-25 01:10:50 +04:00
|
|
|
|
public static Request Create(ComputerShopDatabase context, RequestBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
return new Request() {
|
|
|
|
|
Id = model.Id,
|
|
|
|
|
UserId = model.UserId,
|
2024-04-27 15:55:58 +04:00
|
|
|
|
User = context.Users.First(x => x.Id == model.UserId),
|
2024-04-25 01:10:50 +04:00
|
|
|
|
AssemblyId = model.AssemblyId,
|
2024-04-27 15:55:58 +04:00
|
|
|
|
Assembly = model.AssemblyId.HasValue ? context.Assemblies.First(x => x.Id == model.AssemblyId) : null,
|
2024-04-25 23:01:53 +04:00
|
|
|
|
DateRequest = model.DateRequest,
|
2024-04-25 01:10:50 +04:00
|
|
|
|
ClientFIO = model.ClientFIO,
|
|
|
|
|
Orders = model.RequestOrders.Select(x => new RequestOrder
|
|
|
|
|
{
|
|
|
|
|
Order = context.Orders.First(y => y.Id == x.Key)
|
|
|
|
|
}).ToList()
|
|
|
|
|
};
|
|
|
|
|
}
|
2024-04-30 00:02:09 +04:00
|
|
|
|
|
2024-04-25 01:10:50 +04:00
|
|
|
|
public void Update(RequestBindingModel model)
|
|
|
|
|
{
|
2024-04-27 15:55:58 +04:00
|
|
|
|
if (model == null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-04-28 21:00:55 +04:00
|
|
|
|
if (!string.IsNullOrEmpty(model.ClientFIO))
|
|
|
|
|
{
|
|
|
|
|
ClientFIO = model.ClientFIO;
|
|
|
|
|
}
|
2024-04-30 00:02:09 +04:00
|
|
|
|
//DateMake не обновляю, потому что странно менять дату оформления заявки после её создания
|
2024-04-30 22:44:30 +04:00
|
|
|
|
//Обновление ссылки на сборку (assemblyId) отдельным методом
|
2024-04-25 01:10:50 +04:00
|
|
|
|
}
|
2024-04-30 00:02:09 +04:00
|
|
|
|
|
2024-04-25 01:10:50 +04:00
|
|
|
|
public RequestViewModel GetViewModel => new()
|
|
|
|
|
{
|
|
|
|
|
Id = Id,
|
|
|
|
|
UserId = UserId,
|
|
|
|
|
AssemblyId = AssemblyId,
|
2024-04-28 21:00:55 +04:00
|
|
|
|
Assembly = Assembly,
|
2024-04-25 23:01:53 +04:00
|
|
|
|
DateRequest = DateRequest,
|
2024-04-28 21:00:55 +04:00
|
|
|
|
ClientFIO = ClientFIO,
|
|
|
|
|
RequestOrders = RequestOrders
|
2024-04-25 01:10:50 +04:00
|
|
|
|
};
|
|
|
|
|
|
2024-04-30 22:44:30 +04:00
|
|
|
|
//обновление списка заказов у заявки (+ изменение суммы у соответствующих заказов)
|
2024-04-25 23:01:53 +04:00
|
|
|
|
public void UpdateOrders(ComputerShopDatabase context, RequestBindingModel model)
|
|
|
|
|
{
|
2024-04-30 22:44:30 +04:00
|
|
|
|
var currentRequest = context.Requests.First(x => x.Id == Id);
|
|
|
|
|
//стоимость сборки, связанной с заявкой (или 0, если заявка не связана со сборкой)
|
2024-05-01 13:56:07 +04:00
|
|
|
|
double cost_of_assembly = (currentRequest.Assembly.Price != null) ? currentRequest.Assembly.Price : 0;
|
2024-04-30 22:44:30 +04:00
|
|
|
|
|
2024-04-25 23:01:53 +04:00
|
|
|
|
var requestOrders = context.RequestOrders.Where(x => x.RequestId == model.Id).ToList();
|
2024-04-30 22:44:30 +04:00
|
|
|
|
|
|
|
|
|
//удаление тех заказов, которых нет в модели (+ изменение суммы у удаляемых заказов)
|
2024-04-25 23:01:53 +04:00
|
|
|
|
if (requestOrders != null && requestOrders.Count > 0)
|
|
|
|
|
{
|
2024-04-30 22:44:30 +04:00
|
|
|
|
var delOrders = requestOrders.Where(x => !model.RequestOrders.ContainsKey(x.OrderId));
|
|
|
|
|
foreach (var delOrder in delOrders)
|
|
|
|
|
{
|
|
|
|
|
context.RequestOrders.Remove(delOrder);
|
|
|
|
|
var order = context.Orders.First(x => x.Id == delOrder.OrderId);
|
|
|
|
|
//вычитание из стоимости соответствующего заказа стоимость соответствующей сборки
|
|
|
|
|
order.ChangeSum(-cost_of_assembly);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
}
|
2024-04-25 23:01:53 +04:00
|
|
|
|
}
|
2024-04-30 22:44:30 +04:00
|
|
|
|
|
|
|
|
|
//добавление новых заказов (+ изменение суммы у добавляемых заказов)
|
2024-04-25 23:01:53 +04:00
|
|
|
|
foreach (var request_order in model.RequestOrders)
|
|
|
|
|
{
|
2024-04-30 22:44:30 +04:00
|
|
|
|
var order = context.Orders.First(x => x.Id == request_order.Key);
|
2024-04-25 23:01:53 +04:00
|
|
|
|
context.RequestOrders.Add(new RequestOrder
|
|
|
|
|
{
|
|
|
|
|
Request = currentRequest,
|
2024-04-30 22:44:30 +04:00
|
|
|
|
Order = order
|
2024-04-25 23:01:53 +04:00
|
|
|
|
});
|
2024-04-30 22:44:30 +04:00
|
|
|
|
//увеличение стоимости заказа, с которым связываем заявку, на стоимость сборки
|
|
|
|
|
order.ChangeSum(cost_of_assembly);
|
2024-04-25 23:01:53 +04:00
|
|
|
|
context.SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
_requestOrders = null;
|
|
|
|
|
}
|
2024-04-28 21:00:55 +04:00
|
|
|
|
|
2024-04-30 22:44:30 +04:00
|
|
|
|
|
|
|
|
|
//Связывание заявки со сборкой (+ изменение суммы у соответствующих заказов)
|
|
|
|
|
public void ConnectAssembly(ComputerShopDatabase context, RequestBindingModel model)
|
2024-04-28 21:00:55 +04:00
|
|
|
|
{
|
2024-04-30 22:44:30 +04:00
|
|
|
|
//стоимость старой сборки (или 0, если её не было)
|
2024-05-01 13:56:07 +04:00
|
|
|
|
double cost_of_old_assembly = (Assembly.Price != null) ? Assembly.Price : 0;
|
2024-04-30 22:44:30 +04:00
|
|
|
|
|
|
|
|
|
AssemblyId = model.AssemblyId;
|
|
|
|
|
Assembly = context.Assemblies.First(x => x.Id == model.AssemblyId);
|
|
|
|
|
//изменение стоимости всех связанных заказов
|
|
|
|
|
foreach (var request_order in model.RequestOrders)
|
|
|
|
|
{
|
|
|
|
|
var connectedOrder = context.Orders.First(x => x.Id == request_order.Key);
|
|
|
|
|
//вычитание из стоимости заказа старой сборки
|
|
|
|
|
connectedOrder.ChangeSum(-cost_of_old_assembly);
|
|
|
|
|
//прибавление стоимости новой сборки
|
2024-05-01 13:56:07 +04:00
|
|
|
|
connectedOrder.ChangeSum(Assembly.Price);
|
2024-04-30 22:44:30 +04:00
|
|
|
|
context.SaveChanges();
|
|
|
|
|
}
|
2024-04-28 21:00:55 +04:00
|
|
|
|
}
|
2024-04-25 01:10:50 +04:00
|
|
|
|
}
|
|
|
|
|
}
|