Compare commits

...

4 Commits

11 changed files with 203 additions and 36 deletions

View File

@ -0,0 +1,52 @@
using ComputerShopContracts.BindingModels;
using ComputerShopContracts.BusinessLogicContracts;
using ComputerShopContracts.SearchModels;
using ComputerShopContracts.StorageContracts;
using ComputerShopContracts.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ComputerShopBusinessLogic.BusinessLogics
{
public class ReportImplementerLogic : IReportImplementerLogic
{
private readonly IAssemblyStorage _assemblyStorage;
private readonly IRequestStorage _requestStorage;
private readonly IOrderStorage _orderStorage;
public ReportImplementerLogic(IAssemblyStorage assemblyStorage, IRequestStorage requestStorage, IOrderStorage orderStorage)
{
_assemblyStorage = assemblyStorage;
_requestStorage = requestStorage;
_orderStorage = orderStorage;
}
/// <summary>
/// Отчёт для doc/xls
/// </summary>
/// <returns></returns>
public List<ReportOrderAssemblyViewModel> GetReportOrdersAssemblies(List<OrderSearchModel> selectedOrders)
{
return _orderStorage.GetOrdersAssemblies(selectedOrders);
}
/// <summary>
/// Отчёт для почты/страницы
/// </summary>
/// <returns></returns>
public List<ReportOrdersViewModel> GetReportOrdersByDates(UserSearchModel currentUser, ReportBindingModel report)
{
return _orderStorage.GetOrdersInfoByDates(currentUser, report);
}
public void SaveReportOrderAssembliesToWordFile(ReportBindingModel model)
{
throw new NotImplementedException();
}
public void SaveReportOrderAssembliesToExcelFile(ReportBindingModel model)
{
throw new NotImplementedException();
}
}
}

View File

@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ComputerShopContracts.BindingModels
{
public class ReportBindingModel
{
public string FileName { get; set; } = string.Empty;
public DateTime? DateFrom { get; set; }
public DateTime? DateTo { get; set; }
}
}

View File

@ -0,0 +1,29 @@
using ComputerShopContracts.BindingModels;
using ComputerShopContracts.SearchModels;
using ComputerShopContracts.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ComputerShopContracts.BusinessLogicContracts
{
public interface IReportImplementerLogic
{
/// <summary>
/// Получение отчёта для word/excel
/// </summary>
/// <returns></returns>
List<ReportOrderAssemblyViewModel> GetReportOrdersAssemblies(List<OrderSearchModel> selectedOrders);
/// <summary>
/// Получение отчёта для почты
/// </summary>
/// <returns></returns>
List<ReportOrdersViewModel> GetReportOrdersByDates(UserSearchModel currentUser, ReportBindingModel report);
void SaveReportOrderAssembliesToWordFile(ReportBindingModel model);
void SaveReportOrderAssembliesToExcelFile(ReportBindingModel model);
}
}

View File

@ -17,5 +17,8 @@ namespace ComputerShopContracts.StorageContracts
OrderViewModel? Insert(OrderBindingModel model);
OrderViewModel? Update(OrderBindingModel model);
OrderViewModel? Delete(OrderBindingModel model);
//получение данных о заказах для отчётов
List<ReportOrderAssemblyViewModel> GetOrdersAssemblies(List<OrderSearchModel> model);
List<ReportOrdersViewModel> GetOrdersInfoByDates(UserSearchModel currentUser, ReportBindingModel report);
}
}

View File

@ -0,0 +1,22 @@
using ComputerShopDataModels.Enums;
using ComputerShopDataModels.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ComputerShopContracts.ViewModels
{
public class ReportOrderAssemblyViewModel
{
public int OrderId { get; set; }
public DateTime DateCreateOrder { get; set; }
public double OrderSum { get; set; }
public OrderStatus OrderStatus { get; set; }
//данные о сборках
public List<(string AssemblyName, string AssemblyCategory, double AssemblyPrice)> Assemblies { get; set; }
}
}

View File

@ -0,0 +1,23 @@
using ComputerShopDataModels.Enums;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ComputerShopContracts.ViewModels
{
public class ReportOrdersViewModel
{
public int OrderId { get; set; }
public DateTime DateCreateOrder { get; set; }
public double OrderSum { get; set; }
public OrderStatus OrderStatus { get; set; }
//данные заявок
public List<(int RequestId, string ClientFIO, DateTime DateRequest)> Requests { get; set; }
//данные сборок
public List<(string AssemblyName, string AssemblyCategory, double AssemblyPrice)> Assemblies { get; set; }
}
}

View File

@ -3,6 +3,7 @@ using ComputerShopContracts.SearchModels;
using ComputerShopContracts.StorageContracts;
using ComputerShopContracts.ViewModels;
using ComputerShopDatabaseImplement.Models;
using ComputerShopDataModels.Models;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using System;
@ -10,22 +11,17 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory;
namespace ComputerShopDatabaseImplement.Implements
{
public class OrderStorage : IOrderStorage
{
//id пользователя учитывается в GetFilteredList
public List<OrderViewModel> GetFullList()
{
using var context = new ComputerShopDatabase();
return context.Orders
.Include(x => x.Shipments)
.ThenInclude(x => x.Shipment)
.Include(x => x.Requests)
.ThenInclude(x => x.Request)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
return context.Orders.Select(x => x.GetViewModel).ToList();
}
//Учитывается id пользователя (везде получение списка только тех записей, что создал сам пользователь)
@ -37,11 +33,6 @@ namespace ComputerShopDatabaseImplement.Implements
{
return context.Orders
.Where(x => (x.UserId == model.UserId) && (x.DateCreate >= model.DateFrom && x.DateCreate <= model.DateTo))
.Include(x => x.Shipments)
.ThenInclude(x => x.Shipment)
.Include(x => x.Requests)
.ThenInclude(x => x.Request)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
@ -50,26 +41,61 @@ namespace ComputerShopDatabaseImplement.Implements
{
return context.Orders
.Where(x => (x.UserId == model.UserId) && (x.Status == model.Status))
.Include(x => x.Shipments)
.ThenInclude(x => x.Shipment)
.Include(x => x.Requests)
.ThenInclude(x => x.Request)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
//возвращение просто всех заказов пользователя (замена GetFullList)
return context.Orders
.Where(x => x.UserId == model.UserId)
.Include(x => x.Shipments)
.ThenInclude(x => x.Shipment)
.Include(x => x.Requests)
.ThenInclude(x => x.Request)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
//получение данных сборок по выбранным заказам для отчёта (doc/xls)
public List<ReportOrderAssemblyViewModel> GetOrdersAssemblies(List<OrderSearchModel> selectedModels)
{
using var context = new ComputerShopDatabase();
//id заказов, которые выбрал пользователь
List<int?> id_of_selected_models = selectedModels.Select(x => x.Id).ToList();
//те заказы из бд, которые выбрал пользователь и имеют сборку
return context.Orders.Include(x => x.Requests)
.ThenInclude(x => x.Request)
.ThenInclude(x => x.Assembly)
.Where(x => id_of_selected_models.Contains(x.Id) && x.Requests.Any(r => r.Request.Assembly != null))
.ToList()
.Select(x => new ReportOrderAssemblyViewModel
{
OrderId = x.Id,
DateCreateOrder = x.DateCreate,
OrderSum = x.Sum,
OrderStatus = x.Status,
Assemblies = x.Requests.Select(r => (r.Request.Assembly.AssemblyName, r.Request.Assembly.Category, r.Request.Assembly.Price)).ToList(),
})
.ToList();
}
//получение заказов (все, что создал сам пользователь) за период с расшифровкой по заявкам и сборкам для отчёта (почта/страница)
public List<ReportOrdersViewModel> GetOrdersInfoByDates(UserSearchModel currentUser, ReportBindingModel report)
{
using var context = new ComputerShopDatabase();
return context.Orders.Include(x => x.Requests)
.ThenInclude(x => x.Request)
.ThenInclude(x => x.Assembly)
.Where(x => x.UserId == currentUser.Id && x.DateCreate >= report.DateFrom && x.DateCreate <= report.DateTo)
.ToList()
.Select(x => new ReportOrdersViewModel
{
OrderId = x.Id,
DateCreateOrder = x.DateCreate,
OrderSum = x.Sum,
OrderStatus = x.Status,
Requests = x.Requests.Select(r => (r.Request.Id, r.Request.ClientFIO, r.Request.DateRequest)).ToList(),
Assemblies = x.Requests.Select(r => (r.Request.Assembly.AssemblyName, r.Request.Assembly.Category, r.Request.Assembly.Price)).ToList(),
})
.ToList();
}
//Поиск только по id, потому что другие поля не уникальные - нет смысла искать 1 элемент
public OrderViewModel? GetElement(OrderSearchModel model)
{
@ -78,12 +104,7 @@ namespace ComputerShopDatabaseImplement.Implements
return null;
}
using var context = new ComputerShopDatabase();
return context.Orders
.Include(x => x.Shipments)
.ThenInclude(x => x.Shipment)
.Include(x => x.Requests)
.ThenInclude(x => x.Request)
.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel;
return context.Orders.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel;
}
public OrderViewModel? Insert(OrderBindingModel model)

View File

@ -14,6 +14,7 @@ namespace ComputerShopDatabaseImplement.Implements
{
public class RequestStorage : IRequestStorage
{
//id пользователя учитывается в GetFilteredList
public List<RequestViewModel> GetFullList()
{
using var context = new ComputerShopDatabase();

View File

@ -14,6 +14,7 @@ namespace ComputerShopDatabaseImplement.Implements
{
public class ShipmentStorage : IShipmentStorage
{
//id пользователя учитывается в GetFilteredList
public List<ShipmentViewModel> GetFullList()
{
using var context = new ComputerShopDatabase();

View File

@ -64,9 +64,9 @@ namespace ComputerShopDatabaseImplement.Models
}
//отдельный метод изменения стоимости заказа (+ или -) на стоимость сборки внутри заявки (после действий с заявками, кот. привязаны к заказу)
public void ChangeSum(double cost_of_assembly, bool justUpdate = false)
public void ChangeSum(double price_of_assembly, bool justUpdate = false)
{
Sum += cost_of_assembly;
Sum += price_of_assembly;
}
public OrderViewModel GetViewModel => new()

View File

@ -95,7 +95,7 @@ namespace ComputerShopDatabaseImplement.Models
{
var currentRequest = context.Requests.First(x => x.Id == Id);
//стоимость сборки, связанной с заявкой (или 0, если заявка не связана со сборкой)
double cost_of_assembly = (currentRequest.Assembly.Price != null) ? currentRequest.Assembly.Price : 0;
double price_of_assembly = (currentRequest.Assembly.Price != null) ? currentRequest.Assembly.Price : 0;
var requestOrders = context.RequestOrders.Where(x => x.RequestId == model.Id).ToList();
@ -108,7 +108,7 @@ namespace ComputerShopDatabaseImplement.Models
context.RequestOrders.Remove(delOrder);
var order = context.Orders.First(x => x.Id == delOrder.OrderId);
//вычитание из стоимости соответствующего заказа стоимость соответствующей сборки
order.ChangeSum(-cost_of_assembly);
order.ChangeSum(-price_of_assembly);
context.SaveChanges();
}
}
@ -123,7 +123,7 @@ namespace ComputerShopDatabaseImplement.Models
Order = order
});
//увеличение стоимости заказа, с которым связываем заявку, на стоимость сборки
order.ChangeSum(cost_of_assembly);
order.ChangeSum(price_of_assembly);
context.SaveChanges();
}
_requestOrders = null;
@ -134,7 +134,7 @@ namespace ComputerShopDatabaseImplement.Models
public void ConnectAssembly(ComputerShopDatabase context, RequestBindingModel model)
{
//стоимость старой сборки (или 0, если её не было)
double cost_of_old_assembly = (Assembly.Price != null) ? Assembly.Price : 0;
double price_of_old_assembly = (Assembly.Price != null) ? Assembly.Price : 0;
AssemblyId = model.AssemblyId;
Assembly = context.Assemblies.First(x => x.Id == model.AssemblyId);
@ -143,7 +143,7 @@ namespace ComputerShopDatabaseImplement.Models
{
var connectedOrder = context.Orders.First(x => x.Id == request_order.Key);
//вычитание из стоимости заказа старой сборки
connectedOrder.ChangeSum(-cost_of_old_assembly);
connectedOrder.ChangeSum(-price_of_old_assembly);
//прибавление стоимости новой сборки
connectedOrder.ChangeSum(Assembly.Price);
context.SaveChanges();