2024-02-26 11:05:12 +04:00
|
|
|
|
using FurnitureAssemblyContracts.BindingModels;
|
|
|
|
|
using FurnitureAssemblyContracts.SearchModels;
|
|
|
|
|
using FurnitureAssemblyContracts.StoragesContracts;
|
|
|
|
|
using FurnitureAssemblyContracts.ViewModels;
|
|
|
|
|
using FurnitureAssemblyFileImplement.Models;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace FurnitureAssemblyFileImplement.Implements
|
|
|
|
|
{
|
|
|
|
|
// Реализация интерфейса хранилища заказов
|
|
|
|
|
public class OrderStorage : IOrderStorage
|
|
|
|
|
{
|
|
|
|
|
private readonly DataFileSingleton source;
|
|
|
|
|
|
|
|
|
|
public OrderStorage()
|
|
|
|
|
{
|
|
|
|
|
source = DataFileSingleton.GetInstance();
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-10 22:00:13 +04:00
|
|
|
|
public OrderViewModel? GetElement(OrderSearchModel model)
|
2024-02-26 11:05:12 +04:00
|
|
|
|
{
|
2024-05-10 22:00:13 +04:00
|
|
|
|
if (!model.Id.HasValue)
|
2024-02-26 11:05:12 +04:00
|
|
|
|
{
|
2024-05-10 22:00:13 +04:00
|
|
|
|
return null;
|
2024-02-26 11:05:12 +04:00
|
|
|
|
}
|
|
|
|
|
|
2024-05-11 23:46:17 +04:00
|
|
|
|
if (model.ImplementerId.HasValue && model.Status.HasValue)
|
|
|
|
|
{
|
|
|
|
|
return source.Orders
|
|
|
|
|
.FirstOrDefault(x => x.ImplementerId == model.ImplementerId && x.Status == model.Status)
|
|
|
|
|
?.GetViewModel;
|
|
|
|
|
}
|
2024-05-11 02:30:02 +04:00
|
|
|
|
|
2024-05-11 23:46:17 +04:00
|
|
|
|
if (model.ImplementerId.HasValue)
|
|
|
|
|
{
|
|
|
|
|
return source.Orders
|
|
|
|
|
.FirstOrDefault(x => x.ImplementerId == model.ImplementerId)
|
|
|
|
|
?.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return source.Orders.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
|
2024-02-26 11:05:12 +04:00
|
|
|
|
}
|
|
|
|
|
|
2024-05-10 22:00:13 +04:00
|
|
|
|
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
|
2024-02-26 11:05:12 +04:00
|
|
|
|
{
|
2024-05-11 02:30:02 +04:00
|
|
|
|
if (!model.Id.HasValue && !model.DateFrom.HasValue && !model.DateTo.HasValue && !model.ClientId.HasValue && model.Status == null)
|
2024-02-26 11:05:12 +04:00
|
|
|
|
{
|
2024-05-10 22:00:13 +04:00
|
|
|
|
return new();
|
2024-02-26 11:05:12 +04:00
|
|
|
|
}
|
2024-05-11 02:30:02 +04:00
|
|
|
|
|
2024-05-10 22:00:13 +04:00
|
|
|
|
return source.Orders
|
2024-05-11 23:46:17 +04:00
|
|
|
|
.Where(x => x.Id == model.Id || model.DateFrom <= x.DateCreate && x.DateCreate <= model.DateTo
|
2024-05-11 02:30:02 +04:00
|
|
|
|
|| x.ClientId == model.ClientId || model.Status.Equals(x.Status))
|
2024-05-10 22:00:13 +04:00
|
|
|
|
.Select(x => GetViewModel(x)).ToList();
|
|
|
|
|
}
|
2024-02-26 11:05:12 +04:00
|
|
|
|
|
2024-05-10 22:00:13 +04:00
|
|
|
|
public List<OrderViewModel> GetFullList()
|
|
|
|
|
{
|
|
|
|
|
return source.Orders.Select(x => GetViewModel(x)).ToList();
|
2024-02-26 11:05:12 +04:00
|
|
|
|
}
|
|
|
|
|
|
2024-05-12 00:27:31 +04:00
|
|
|
|
// Для загрузки названий изделия и исполнителя в заказе
|
2024-02-26 11:05:12 +04:00
|
|
|
|
private OrderViewModel GetViewModel(Order order)
|
|
|
|
|
{
|
|
|
|
|
var viewModel = order.GetViewModel;
|
|
|
|
|
|
|
|
|
|
var furniture = source.Furnitures.FirstOrDefault(x => x.Id == order.FurnitureId);
|
|
|
|
|
|
2024-05-10 22:00:13 +04:00
|
|
|
|
var client = source.Clients.FirstOrDefault(x => x.Id == order.ClientId);
|
|
|
|
|
|
2024-05-11 23:46:17 +04:00
|
|
|
|
var implementer = source.Implementers.FirstOrDefault(x => x.Id == order.ImplementerId);
|
2024-05-11 02:30:02 +04:00
|
|
|
|
|
2024-05-11 23:46:17 +04:00
|
|
|
|
if (furniture != null)
|
2024-02-26 11:05:12 +04:00
|
|
|
|
{
|
|
|
|
|
viewModel.FurnitureName = furniture.FurnitureName;
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-10 22:00:13 +04:00
|
|
|
|
if (client != null)
|
|
|
|
|
{
|
|
|
|
|
viewModel.ClientFIO = client.ClientFIO;
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-11 23:46:17 +04:00
|
|
|
|
if (implementer != null)
|
|
|
|
|
{
|
|
|
|
|
viewModel.ImplementerFIO = implementer.ImplementerFIO;
|
|
|
|
|
}
|
2024-05-11 02:30:02 +04:00
|
|
|
|
|
2024-05-11 23:46:17 +04:00
|
|
|
|
return viewModel;
|
2024-02-26 11:05:12 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public OrderViewModel? Insert(OrderBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
model.Id = source.Orders.Count > 0 ? source.Orders.Max(x => x.Id) + 1 : 1;
|
|
|
|
|
|
|
|
|
|
var newOrder = Order.Create(model);
|
|
|
|
|
|
|
|
|
|
if (newOrder == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
source.Orders.Add(newOrder);
|
|
|
|
|
source.SaveOrders();
|
|
|
|
|
|
|
|
|
|
return GetViewModel(newOrder);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public OrderViewModel? Update(OrderBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
var order = source.Orders.FirstOrDefault(x => x.Id == model.Id);
|
|
|
|
|
|
2024-05-11 23:46:17 +04:00
|
|
|
|
if (order == null)
|
2024-02-26 11:05:12 +04:00
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
order.Update(model);
|
|
|
|
|
source.SaveOrders();
|
|
|
|
|
|
|
|
|
|
return GetViewModel(order);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public OrderViewModel? Delete(OrderBindingModel model)
|
|
|
|
|
{
|
2024-05-10 22:00:13 +04:00
|
|
|
|
var order = source.Orders.FirstOrDefault(x => x.Id == model.Id);
|
2024-02-26 11:05:12 +04:00
|
|
|
|
|
2024-05-11 23:46:17 +04:00
|
|
|
|
if (order != null)
|
2024-02-26 11:05:12 +04:00
|
|
|
|
{
|
2024-05-10 22:00:13 +04:00
|
|
|
|
source.Orders.Remove(order);
|
2024-02-26 11:05:12 +04:00
|
|
|
|
source.SaveOrders();
|
|
|
|
|
|
2024-05-10 22:00:13 +04:00
|
|
|
|
return GetViewModel(order);
|
2024-02-26 11:05:12 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|