diff --git a/VeterinaryView/VeterinaryBusinessLogic/BusinessLogic/ReportLogicDoctor.cs b/VeterinaryView/VeterinaryBusinessLogic/BusinessLogic/ReportLogicDoctor.cs new file mode 100644 index 0000000..b860eb7 --- /dev/null +++ b/VeterinaryView/VeterinaryBusinessLogic/BusinessLogic/ReportLogicDoctor.cs @@ -0,0 +1,86 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using VeterinaryBusinessLogic.OfficePackage.Implements; +using VeterinaryContracts.BusinessLogicContracts; +using VeterinaryContracts.BindingModels; +using VeterinaryContracts.StorageContracts; +using VeterinaryContracts.ViewModels; +using VeterinaryContracts.SearchModels; + +namespace VeterinaryBusinessLogic.BusinessLogic +{ + public class ReportLogicDoctor : IReportLogicDoctor + { + private readonly IDrugStorage _drugStorage; + private readonly IMedicationStorage _medicationStorage; + private readonly AbstractSaveToExcelDoctor _saveToExcel; + private readonly AbstractSaveToWordDoctor _saveToWord; + public ReportLogicDoctor(IPurchaseStorage purchaseStorage, IDrugStorage drugStorage, IMedicationStorage medicationStorage, + AbstractSaveToExcelDoctor saveToExcel, AbstractSaveToWordDoctor saveToWord) + { + _drugStorage = drugStorage; + _medicationStorage = medicationStorage; + _saveToExcel = saveToExcel; + _saveToWord = saveToWord; + } + public void SavePurchasesToExcelFile(ReportPurchaseMedicationBindingModel model) + { + _saveToExcel.CreateReport(new ExcelInfoDoctor + { + FileName = model.FileName, + Title = "Список покупок по медикаментам", + PurchaseMedications = GetPurchaseMedications(model.Medications) + }); + } + + public void SavePurchasesToWordFile(ReportPurchaseMedicationBindingModel model) + { + _saveToWord.CreateDoc(new WordInfoDoctor + { + FileName = model.FileName, + Title = "Список покупок по медикаментам", + PurchaseMedications = GetPurchaseMedications(model.Medications) + }); + } + + public List GetPurchaseMedications(List medications) + { + List servs = new(); + List>>>> response = + _medicationStorage.GetReportInfo(new ListPurchasesSearchModel { medicationsIds = medications }); + + foreach (var medication in response) + { + Dictionary counter = new(); + foreach (var drug in medication.Item2) + { + foreach (var purchase in drug.Item2) + { + if (!counter.ContainsKey(purchase.Id)) + counter.Add(purchase.Id, (purchase, 1)); + else + { + counter[purchase.Id] = (counter[purchase.Id].Item1, counter[purchase.Id].Item2 + 1); + } + } + } + List res = new(); + foreach (var cont in counter) + { + if (cont.Value.Item2 != medication.Item2.Count) + continue; + res.Add(cont.Value.Item1); + } + servs.Add(new ReportPurchaseMedicationViewModel + { + MedicationName = medication.Item1.MedicationName, + Purchases = res + }); + } + return servs; + } + } +} diff --git a/VeterinaryView/VeterinaryContracts/BindingModels/ReportPurchaseMedicationBindingModel.cs b/VeterinaryView/VeterinaryContracts/BindingModels/ReportPurchaseMedicationBindingModel.cs new file mode 100644 index 0000000..c3d61b1 --- /dev/null +++ b/VeterinaryView/VeterinaryContracts/BindingModels/ReportPurchaseMedicationBindingModel.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace VeterinaryContracts.BindingModels +{ + public class ReportPurchaseMedicationBindingModel + { + public string FileName { get; set; } = string.Empty; + public List Medications { get; set; } = new(); + } +} diff --git a/VeterinaryView/VeterinaryContracts/BusinessLogicContracts/IReportLogicDoctor.cs b/VeterinaryView/VeterinaryContracts/BusinessLogicContracts/IReportLogicDoctor.cs new file mode 100644 index 0000000..e7892d4 --- /dev/null +++ b/VeterinaryView/VeterinaryContracts/BusinessLogicContracts/IReportLogicDoctor.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using VeterinaryContracts.BindingModels; +using VeterinaryContracts.ViewModels; + + +namespace VeterinaryContracts.BusinessLogicContracts +{ + public interface IReportLogicDoctor + {// will be done later + List GetPurchaseMedications(List medications); + void SavePurchasesToWordFile(ReportPurchaseMedicationBindingModel model); + void SavePurchasesToExcelFile(ReportPurchaseMedicationBindingModel model); + } +} diff --git a/VeterinaryView/VeterinaryContracts/SearchModels/DrugSearchModel.cs b/VeterinaryView/VeterinaryContracts/SearchModels/DrugSearchModel.cs index 0f3dfa3..160bd36 100644 --- a/VeterinaryView/VeterinaryContracts/SearchModels/DrugSearchModel.cs +++ b/VeterinaryView/VeterinaryContracts/SearchModels/DrugSearchModel.cs @@ -10,6 +10,5 @@ namespace VeterinaryContracts.SearchModels { public int? Id { get; set; } public string? DrugName { get; set; } - } } diff --git a/VeterinaryView/VeterinaryContracts/SearchModels/ListPurchasesSearchModel.cs b/VeterinaryView/VeterinaryContracts/SearchModels/ListPurchasesSearchModel.cs new file mode 100644 index 0000000..60705a6 --- /dev/null +++ b/VeterinaryView/VeterinaryContracts/SearchModels/ListPurchasesSearchModel.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace VeterinaryContracts.SearchModels +{ + public class ListPurchasesSearchModel + { + public List? medicationsIds { get; set; } + } +} diff --git a/VeterinaryView/VeterinaryContracts/SearchModels/PurchaseSearchModel.cs b/VeterinaryView/VeterinaryContracts/SearchModels/PurchaseSearchModel.cs index f1d8336..80f84e8 100644 --- a/VeterinaryView/VeterinaryContracts/SearchModels/PurchaseSearchModel.cs +++ b/VeterinaryView/VeterinaryContracts/SearchModels/PurchaseSearchModel.cs @@ -7,5 +7,6 @@ public int? DrugId { get; set; } public DateTime? DateCreate { get; set; } public DateTime? DateImplement { get; set;} + } } diff --git a/VeterinaryView/VeterinaryContracts/StorageContracts/IMedicationStorage.cs b/VeterinaryView/VeterinaryContracts/StorageContracts/IMedicationStorage.cs index 9e213ed..65bae8e 100644 --- a/VeterinaryView/VeterinaryContracts/StorageContracts/IMedicationStorage.cs +++ b/VeterinaryView/VeterinaryContracts/StorageContracts/IMedicationStorage.cs @@ -13,6 +13,7 @@ namespace VeterinaryContracts.StorageContracts { List GetFullList(); List GetFilteredList(MedicationSearchModel model); + List>>>> GetReportInfo(ListPurchasesSearchModel model); MedicationViewModel? GetElement(MedicationSearchModel model); MedicationViewModel? Insert(MedicationBindingModel model); MedicationViewModel? Update(MedicationBindingModel model); diff --git a/VeterinaryView/VeterinaryContracts/ViewModels/ReportPurchaseMedicationModel.cs b/VeterinaryView/VeterinaryContracts/ViewModels/ReportPurchaseMedicationModel.cs deleted file mode 100644 index aee087a..0000000 --- a/VeterinaryView/VeterinaryContracts/ViewModels/ReportPurchaseMedicationModel.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace VeterinaryContracts.ViewModels -{ - public class ReportPurchaseMedicationModel - { - public string FlowerName { get; set; } = string.Empty; - public int TotalCount { get; set; } - public List> Components { get; set; } = new(); - } -} diff --git a/VeterinaryView/VeterinaryContracts/ViewModels/ReportPurchaseMedicationViewModel.cs b/VeterinaryView/VeterinaryContracts/ViewModels/ReportPurchaseMedicationViewModel.cs new file mode 100644 index 0000000..c871ecb --- /dev/null +++ b/VeterinaryView/VeterinaryContracts/ViewModels/ReportPurchaseMedicationViewModel.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace VeterinaryContracts.ViewModels +{ + public class ReportPurchaseMedicationViewModel + { + public string MedicationName { get; set; } = string.Empty; + public List Purchases { get; set; } = new(); + } +} diff --git a/VeterinaryView/VeterinaryDatabaseImplement/Implements/MedicationStorage.cs b/VeterinaryView/VeterinaryDatabaseImplement/Implements/MedicationStorage.cs index 89b1c78..ecb62af 100644 --- a/VeterinaryView/VeterinaryDatabaseImplement/Implements/MedicationStorage.cs +++ b/VeterinaryView/VeterinaryDatabaseImplement/Implements/MedicationStorage.cs @@ -10,7 +10,7 @@ using VeterinaryContracts.SearchModels; using VeterinaryContracts.ViewModels; using VeterinaryDatabaseImplement.Models; using VeterinaryDatabaseImplement; - +using Microsoft.EntityFrameworkCore; namespace VeterinaryDatabaseImplement.Implements { @@ -82,5 +82,23 @@ namespace VeterinaryDatabaseImplement.Implements } return null; } + + public List>>>> GetReportInfo(ListPurchasesSearchModel model) + { + if (model.medicationsIds == null) + { + return new(); + } + using var context = new VeterinaryDatabase(); + return context.Medications + .Where(pet => model.medicationsIds.Contains(pet.Id)) + .Select(pet => new Tuple>>>(pet.GetViewModel, + context.VisitPets.Include(visit => visit.Visit) + .Include(visit => visit.Pet).Where(visit => pet.Id == visit.PetId). + Select(visit => new Tuple>(visit.Visit.GetViewModel, + context.Services.Include(x => x.Visits).Where(x => x.MedicineId == visit.Medicine.Id). + Select(x => x.Animal.GetViewModel).ToList())).ToList())).ToList(); + + } } } diff --git a/VeterinaryView/VeterinaryRestApi/Controllers/PurchaseController.cs b/VeterinaryView/VeterinaryRestApi/Controllers/PurchaseController.cs index 33aafb9..0946f11 100644 --- a/VeterinaryView/VeterinaryRestApi/Controllers/PurchaseController.cs +++ b/VeterinaryView/VeterinaryRestApi/Controllers/PurchaseController.cs @@ -59,5 +59,6 @@ namespace VeterinaryRestApi.Controllers throw; } } + } }