слияние типа

This commit is contained in:
dasha 2023-04-04 00:22:03 +04:00
commit 1c67a1004f
26 changed files with 302 additions and 60 deletions

View File

@ -0,0 +1,146 @@

using HardwareShopContracts.BindingModels;
using HardwareShopContracts.SearchModels;
using HardwareShopContracts.StoragesContracts;
using HardwareShopContracts.ViewModels;
using HardwareShopDatabaseImplement.Implements.Storekeeper;
using HardwareShopDatabaseImplement.Implements.Worker;
using HardwareShopDatabaseImplement.Models.Storekeeper;
using HardwareShopDatabaseImplement.Models.Worker;
using System.Collections.Generic;
using System.ComponentModel;
namespace HardwareShopContracts.BusinessLogicsContracts
{
public class WorkerReportLogic : IWorkerReportLogic
{
private readonly IComponentStorage _componentStorage;
private readonly BuildStorage _buildStorage;
private readonly IPurchaseStorage _purchaseStorage;
private readonly IGoodStorage _goodStorage;
private readonly ICommentStorage _commentStorage;
public WorkerReportLogic(IComponentStorage componentStorage, BuildStorage buildStorage, IPurchaseStorage purchaseStorage, IGoodStorage goodStorage, ICommentStorage commentStorage)
{
_componentStorage = componentStorage;
_buildStorage = buildStorage;
_purchaseStorage = purchaseStorage;
_goodStorage = goodStorage;
_commentStorage = commentStorage;
}
/// <summary>
/// Получение списка компонент с указанием, в каких покупках используются
/// </summary>
/// <returns></returns>
public List<ReportPurchaseComponentViewModel> GetPurchaseComponent(List<PurchaseViewModel> purchaseList)
{
var list = new List<ReportPurchaseComponentViewModel>();
foreach (var purchase in purchaseList)
{
var record = new ReportPurchaseComponentViewModel
{
Id = purchase.Id,
Builds = new List<(string Build, int count, List<(string Component, int count)>)>(),
Goods = new List<(string Good, int count, List<(string Component, int count)>)>(),
TotalCount = 0,
TotalCost = 0
};
foreach (var good in purchase.PurchaseGoods)
{
List<(string Component, int count)> componentList = new List<(string Component, int count)>();
int goodTotalCount = 0;
foreach (var component in good.Value.Item1.GoodsComponents)
{
componentList.Add(new(component.Value.Item1.ComponentName, component.Value.Item2));
goodTotalCount += component.Value.Item2;
}
record.Goods.Add(new(good.Value.Item1.GoodName, good.Value.Item2, componentList));
record.TotalCount += goodTotalCount * good.Value.Item2;
}
foreach (var build in purchase.PurchaseBuilds)
{
List<(string Component, int count)> componentList = new List<(string Component, int count)>();
int buildTotalCount = 0;
foreach (var component in build.Value.Item1.BuildComponents)
{
componentList.Add(new(component.Value.Item1.ComponentName, component.Value.Item2));
buildTotalCount += component.Value.Item2;
}
record.Builds.Add(new(build.Value.Item1.BuildName, build.Value.Item2, componentList));
record.TotalCount += buildTotalCount * build.Value.Item2;
}
list.Add(record);
}
return list;
}
/// <summary>
/// Получение списка покупок за определенный период
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public List<ReportPurchaseViewModel> GetPurchase(ReportBindingModel model)
{
var list = new List<ReportPurchaseViewModel>();
var purchases = _purchaseStorage.GetFilteredList(new PurchaseSearchModel { DateFrom = model.DateFrom, DateTo = model.DateTo });
foreach (var purchase in purchases)
{
var record = new ReportPurchaseViewModel
{
Id = purchase.Id,
Builds = new List<(string Build, int count, List<string>, List<(string Component, int count)>)>(),
};
foreach (var build in purchase.PurchaseBuilds)
{
List<string> commentList = new List<string>();
foreach (var comment in build.Value.Item1.BuildComments)
{
commentList.Add(new(comment.Value.Text));
}
List<(string Component, int count)> componentList = new List<(string Component, int count)>();
foreach (var component in build.Value.Item1.BuildComponents)
{
componentList.Add(new(component.Value.Item1.ComponentName, component.Value.Item2));
}
record.Builds.Add(new (build.Value.Item1.BuildName, build.Value.Item2, commentList, componentList));
}
}
return list;
}
/// <summary>
/// Сохранение компонент с указаеним покупок в файл-Word
/// </summary>
/// <param name="model"></param>
public void SaveComponentsToWordFile(ReportBindingModel model)
{
throw new NotImplementedException();
}
/// <summary>
/// Сохранение компонент с указаеним покупок в файл-Excel
/// </summary>
/// <param name="model"></param>
public void SaveDishComponentToExcelFile(ReportBindingModel model)
{
throw new NotImplementedException();
}
/// <summary>
/// Сохранение отчёта по покупкам в файл-Pdf
/// </summary>
/// <param name="model"></param>
public void SaveOrdersToPdfFile(ReportBindingModel model)
{
throw new NotImplementedException();
}
}
}

View File

@ -12,6 +12,7 @@
<ItemGroup>
<ProjectReference Include="..\HardwareShopContracts\HardwareShopContracts.csproj" />
<ProjectReference Include="..\HardwareShopDatabaseImplement\HardwareShopDatabaseImplement.csproj" />
</ItemGroup>
</Project>

View File

@ -21,9 +21,6 @@
<th>
Название
</th>
<th>
Логин пользователя
</th>
</tr>
</thead>
<tbody>

View File

@ -23,9 +23,6 @@
<th>
Название сборки к которой относиться комментарий
</th>
<th>
Логин пользователя
</th>
</tr>
</thead>
<tbody>

View File

@ -36,9 +36,6 @@
<th>
Статус
</th>
<th>
Логин пользователя
</th>
</tr>
</thead>
<tbody>

View File

@ -21,9 +21,6 @@
<th>
Цена
</th>
<th>
Логин пользователя
</th>
<th>
Количество
</th>
@ -39,7 +36,7 @@
<button type="button" class="btn btn-primary btn-lg mb-5" data-bs-toggle="modal" data-bs-target="#exampleModal">Изменить</button>
<button type="button" class="btn btn-primary btn-lg mb-5">Удалить</button>
<button type="button" class="btn btn-primary btn-lg mb-5">Обновить</button>
<button type="button" class="btn btn-primary btn-lg mb-5">Сохрванить</button>
<button type="button" class="btn btn-primary btn-lg mb-5">Сохранить</button>
</div>
<div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog">

View File

@ -24,9 +24,6 @@
<th>
Статус
</th>
<th>
Логин пользователя
</th>
</tr>
</thead>
<tbody>

View File

@ -24,9 +24,6 @@
<th>
Статус
</th>
<th>
Логин пользователя
</th>
</tr>
</thead>
<tbody>

View File

@ -13,5 +13,7 @@ namespace HardwareShopContracts.BindingModels
public int UserId { get; set; }
public Dictionary<int, (IPurchaseModel, int)> BuildPurchases { get; set; } = new();
public Dictionary<int, (IComponentModel, int)> BuildComponents { get; set; } = new();
}
}

View File

@ -16,5 +16,7 @@ namespace HardwareShopContracts.BindingModels
public int UserId { get; set; }
public Dictionary<int, (IGoodModel, int)> PurchaseGoods { get; set; } = new();
public Dictionary<int, (IBuildModel, int)> PurchaseBuilds { get; set; } = new();
}
}

View File

@ -0,0 +1,40 @@

using HardwareShopContracts.BindingModels;
using HardwareShopContracts.ViewModels;
namespace HardwareShopContracts.BusinessLogicsContracts
{
public interface IWorkerReportLogic
{
/// <summary>
/// Получение списка компонент с указанием, в каких покупках используются
/// </summary>
/// <returns></returns>
List<ReportPurchaseComponentViewModel> GetPurchaseComponent(List<PurchaseViewModel> purchaseList);
/// <summary>
/// Получение списка покупок за определенный период
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
List<ReportPurchaseViewModel> GetPurchase(ReportBindingModel model);
/// <summary>
/// Сохранение компонент с указаеним покупок в файл-Word
/// </summary>
/// <param name="model"></param>
void SaveComponentsToWordFile(ReportBindingModel model);
/// <summary>
/// Сохранение компонент с указаеним покупок в файл-Excel
/// </summary>
/// <param name="model"></param>
void SaveDishComponentToExcelFile(ReportBindingModel model);
/// <summary>
/// Сохранение отчёта по покупкам в файл-Pdf
/// </summary>
/// <param name="model"></param>
void SaveOrdersToPdfFile(ReportBindingModel model);
}
}

View File

@ -1,4 +1,5 @@

using HardwareShopDataModels.Enums;
using System.ComponentModel;
namespace HardwareShopContracts.SearchModels
{
@ -8,7 +9,10 @@ namespace HardwareShopContracts.SearchModels
public int? UserId { get; set; }
public DateTime? DatePurchase { get; set; }
public PurchaseStatus? PurchaseStatus { get; set; }
public DateTime? DateFrom { get; set; }
public DateTime? DateTo { get; set; }
}
}

View File

@ -12,12 +12,10 @@ namespace HardwareShopContracts.ViewModels
[DisplayName("Название Сборки")]
public string BuildName { get; set; } = string.Empty;
[DisplayName("Логин пользователя")]
public string UserLogin { get; set; } = string.Empty;
public int UserId { get; set; }
public Dictionary<int, (IPurchaseModel, int)> BuildPurchases { get; set; } = new();
public Dictionary<int, (IComponentModel, int)> BuildComponents { get; set; } = new();
}
}

View File

@ -16,8 +16,5 @@ namespace HardwareShopContracts.ViewModels
public int BuildId { get; set; }
public int UserId { get; set; }
[DisplayName("Логин пользователя")]
public string UserLogin { get; set; } = string.Empty;
}
}

View File

@ -18,9 +18,8 @@ namespace HardwareShopContracts.ViewModels
public int UserId { get; set; }
[DisplayName("Логин пользователя")]
public string UserLogin { get; set; } = string.Empty;
public Dictionary<int, (IGoodModel, int)> PurchaseGoods { get; set; } = new();
public Dictionary<int, (IBuildModel, int)> PurchaseBuilds { get; set; } = new();
}
}

View File

@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HardwareShopContracts.ViewModels
{
public class ReportPurchaseComponentViewModel
{
public int Id { get; set; }
public List<(string Build, int count, List<(string Component, int count)>)> Builds { get; set; } = new();
public List<(string Good, int count, List<(string Component, int count)>)> Goods { get; set; } = new();
public int TotalCount { get; set; }
public int TotalCost { get; set; }
}
}

View File

@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HardwareShopContracts.ViewModels
{
public class ReportPurchaseViewModel
{
public int Id { get; set; }
public List<(string Build, int count, List<string>, List<(string Component, int count)>)> Builds { get; set; } = new();
}
}

View File

@ -16,5 +16,9 @@ namespace HardwareShopDataModels.Models
int UserId { get; }
Dictionary<int, (IPurchaseModel, int)> BuildPurchases { get; }
Dictionary<int, (IComponentModel, int)> BuildComponents { get; }
Dictionary<int, ICommentModel> BuildComments { get; }
}
}

View File

@ -12,6 +12,6 @@ namespace HardwareShopDataModels.Models
int BuildId { get; }
int UserId { get; }
int UserId { get; }
}
}

View File

@ -13,5 +13,7 @@ namespace HardwareShopDataModels.Models
int UserId { get; }
Dictionary<int, (IGoodModel, int)> PurchaseGoods { get; }
Dictionary<int, (IBuildModel, int)> PurchaseBuilds { get; }
}
}

View File

@ -15,7 +15,6 @@ namespace HardwareShopDatabaseImplement.Implements.Worker
return context.Builds
.Include(x => x.Purchases)
.ThenInclude(x => x.Purchase)
.Include(x => x.User)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
@ -57,7 +56,6 @@ namespace HardwareShopDatabaseImplement.Implements.Worker
return context.Builds
.Include(x => x.Purchases)
.ThenInclude(x => x.Purchase)
.Include(x => x.User)
.Where(x => x.Id == model.Id)
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.BuildName) && x.BuildName == model.BuildName) ||
(model.Id.HasValue && x.Id == model.Id))
@ -77,7 +75,6 @@ namespace HardwareShopDatabaseImplement.Implements.Worker
return context.Builds
.Include(x => x.Purchases)
.ThenInclude(x => x.Purchase)
.Include(x => x.User)
.Where(x => x.UserId == model.Id)
.FirstOrDefault(x => x.Id == newBuild.Id)
?.GetViewModel;
@ -92,7 +89,6 @@ namespace HardwareShopDatabaseImplement.Implements.Worker
var build = context.Builds
.Include(x => x.Purchases)
.ThenInclude(x => x.Purchase)
.Include(x => x.User)
.Where(x => x.UserId == model.UserId)
.FirstOrDefault(x => x.Id == model.Id);
if (build == null)
@ -118,7 +114,6 @@ namespace HardwareShopDatabaseImplement.Implements.Worker
var element = context.Builds
.Include(x => x.Purchases)
.ThenInclude(x => x.Purchase)
.Include(x => x.User)
.Where(x => x.UserId == model.Id)
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)

View File

@ -14,7 +14,6 @@ namespace HardwareShopDatabaseImplement.Implements.Worker
using var context = new HardwareShopDatabase();
return context.Comments
.Include(x => x.Build)
.Include(x => x.User)
.Select(x => x.GetViewModel)
.ToList();
}
@ -30,14 +29,12 @@ namespace HardwareShopDatabaseImplement.Implements.Worker
{
return context.Comments
.Include(x => x.Build)
.Include(x => x.User)
.Where(x => x.UserId == model.UserId)
.Select(x => x.GetViewModel)
.ToList();
}
return context.Comments
.Include(x => x.Build)
.Include(x => x.User)
.Where(x => x.BuildId == model.BuildId)
.Select(x => x.GetViewModel)
.ToList();
@ -52,7 +49,6 @@ namespace HardwareShopDatabaseImplement.Implements.Worker
using var context = new HardwareShopDatabase();
return context.Comments
.Include(x => x.Build)
.Include(x => x.User)
.FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)
?.GetViewModel;
}
@ -75,7 +71,6 @@ namespace HardwareShopDatabaseImplement.Implements.Worker
using var context = new HardwareShopDatabase();
var comment = context.Comments
.Include(x => x.Build)
.Include(x => x.User)
.FirstOrDefault(x => x.Id == model.Id);
if (comment == null)
{
@ -91,7 +86,6 @@ namespace HardwareShopDatabaseImplement.Implements.Worker
using var context = new HardwareShopDatabase();
var element = context.Comments
.Include(x => x.Build)
.Include(x => x.User)
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{

View File

@ -17,7 +17,6 @@ namespace HardwareShopDatabaseImplement.Implements.Worker
return context.Purchases
.Include(x => x.Goods)
.ThenInclude(x => x.Good)
.Include(x => x.User)
.Select(x => x.GetViewModel)
.ToList();
}
@ -25,16 +24,20 @@ namespace HardwareShopDatabaseImplement.Implements.Worker
public List<PurchaseViewModel> GetFilteredList(PurchaseSearchModel model)
{
using var context = new HardwareShopDatabase();
if (!model.UserId.HasValue && !model.DatePurchase.HasValue)
if (!model.UserId.HasValue && !model.PurchaseStatus.HasValue && model.DateFrom.HasValue && model.DateTo.HasValue)
{
return new();
return context.Purchases
.Include(x => x.Goods)
.ThenInclude(x => x.Good)
.Where(x => x.DatePurchase >= model.DateFrom && x.DatePurchase <= model.DateTo)
.Select(x => x.GetViewModel)
.ToList();
}
if (model.UserId.HasValue)
{
return context.Purchases
.Include(x => x.Goods)
.ThenInclude(x => x.Good)
.Include(x => x.User)
.Where(x => x.UserId == model.UserId)
.Select(x => x.GetViewModel)
.ToList();
@ -42,8 +45,7 @@ namespace HardwareShopDatabaseImplement.Implements.Worker
return context.Purchases
.Include(x => x.Goods)
.ThenInclude(x => x.Good)
.Include(x => x.User)
.Where(x => x.DatePurchase == model.DatePurchase)
.Where(x => x.PurchaseStatus == model.PurchaseStatus)
.Select(x => x.GetViewModel)
.ToList();
}
@ -58,7 +60,6 @@ namespace HardwareShopDatabaseImplement.Implements.Worker
return context.Purchases
.Include(x => x.Goods)
.ThenInclude(x => x.Good)
.Include(x => x.User)
.FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)
?.GetViewModel;
}
@ -76,7 +77,6 @@ namespace HardwareShopDatabaseImplement.Implements.Worker
return context.Purchases
.Include(x => x.Goods)
.ThenInclude(x => x.Good)
.Include(x => x.User)
.FirstOrDefault(x => x.Id == newPurchase.Id)
?.GetViewModel;
}
@ -90,7 +90,6 @@ namespace HardwareShopDatabaseImplement.Implements.Worker
var purchase = context.Purchases
.Include(x => x.Goods)
.ThenInclude(x => x.Good)
.Include(x => x.User)
.FirstOrDefault(x => x.Id == model.Id);
if (purchase == null)
{
@ -115,7 +114,6 @@ namespace HardwareShopDatabaseImplement.Implements.Worker
var element = context.Purchases
.Include(x => x.Goods)
.ThenInclude(x => x.Good)
.Include(x => x.User)
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{

View File

@ -33,7 +33,37 @@ namespace HardwareShopDatabaseImplement.Models.Worker
[ForeignKey("BuildId")]
public virtual List<PurchaseBuild> Purchases { get; set; } = new();
public Dictionary<int, (IPurchaseModel, int)>? _buildPurchases = null;
private Dictionary<int, ICommentModel> _buildComments = null;
[NotMapped]
public Dictionary<int, ICommentModel> BuildComments
{
get
{
if (_buildComments == null)
{
_buildComments = Comments.ToDictionary(recBC=> recBC.Id, recBC => recBC as ICommentModel);
}
return _buildComments;
}
}
private Dictionary<int, (IComponentModel, int)>? _buildComponents = null;
[NotMapped]
public Dictionary<int, (IComponentModel, int)> BuildComponents
{
get
{
if (_buildComponents == null)
{
_buildComponents = Components.ToDictionary(recBC => recBC.ComponentId, recBC => (recBC.Component as IComponentModel, recBC.Count));
}
return _buildComponents;
}
}
private Dictionary<int, (IPurchaseModel, int)>? _buildPurchases = null;
[NotMapped]
public Dictionary<int, (IPurchaseModel, int)> BuildPurchases
@ -70,7 +100,6 @@ namespace HardwareShopDatabaseImplement.Models.Worker
Id = Id,
BuildName = BuildName,
Price = Price,
UserLogin = User.Login,
UserId = UserId,
BuildPurchases = BuildPurchases,
};
@ -91,7 +120,7 @@ namespace HardwareShopDatabaseImplement.Models.Worker
context.SaveChanges();
}
var build = context.Builds.First(x => x.Id == Id);
//добавляем в бд элементы которые есть в модели, но ещё нет в бд
//добавляем в бд сборки которые есть в модели, но ещё нет в бд
foreach (var bp in model.BuildPurchases)
{
context.PurchasesBuilds.Add(new PurchaseBuild
@ -117,7 +146,5 @@ namespace HardwareShopDatabaseImplement.Models.Worker
var buildPurchases = context.PurchasesBuilds.Where(rec => rec.BuildId == model.Id).ToList();
}
}
}

View File

@ -52,7 +52,6 @@ namespace HardwareShopDatabaseImplement.Models.Worker
BuildId = BuildId,
BuildName = Build.BuildName,
UserId = UserId,
UserLogin = User.Login,
};
}
}

View File

@ -32,7 +32,7 @@ namespace HardwareShopDatabaseImplement.Models.Worker
[ForeignKey("PurchaseId")]
public virtual List<PurchaseGood> Goods { get; set; } = new();
public Dictionary<int, (IGoodModel, int)>? _purchaseGoods = null;
private Dictionary<int, (IGoodModel, int)>? _purchaseGoods = null;
[NotMapped]
public Dictionary<int, (IGoodModel, int)> PurchaseGoods
@ -47,6 +47,21 @@ namespace HardwareShopDatabaseImplement.Models.Worker
}
}
private Dictionary<int, (IBuildModel, int)>? _purchaseBuilds = null;
[NotMapped]
public Dictionary<int, (IBuildModel, int)> PurchaseBuilds
{
get
{
if (_purchaseBuilds == null)
{
_purchaseBuilds = Builds.ToDictionary(recPG => recPG.BuildId, recPG => (recPG.Build as IBuildModel, recPG.Count));
}
return _purchaseBuilds;
}
}
public static Purchase Create(HardwareShopDatabase context, PurchaseBindingModel model)
{
return new Purchase()
@ -78,7 +93,6 @@ namespace HardwareShopDatabaseImplement.Models.Worker
PurchaseStatus = PurchaseStatus,
DatePurchase = DatePurchase,
UserId = UserId,
UserLogin = User.Login,
PurchaseGoods = PurchaseGoods
};
@ -98,7 +112,7 @@ namespace HardwareShopDatabaseImplement.Models.Worker
context.SaveChanges();
}
var purchase = context.Purchases.First(x => x.Id == Id);
//добавляем в бд блюда которые есть в моделе, но ещё нет в бд
//добавляем в бд товары которые есть в моделе, но ещё нет в бд
foreach (var dc in model.PurchaseGoods)
{
context.PurchasesGoods.Add(new PurchaseGood