From 2cc83f06cb846553385ee573b380f24e529090a9 Mon Sep 17 00:00:00 2001 From: Yuee Shiness Date: Sat, 20 May 2023 02:22:42 +0400 Subject: [PATCH] A bunch of fixes + saving files logic was changed. --- .../BusinessLogic/EmployeeReportLogic.cs | 10 ++--- .../OfficePackage/AbstractSaveToExcel.cs | 39 +++++++++++++------ .../OfficePackage/AbstractSaveToWord.cs | 9 +++-- .../OfficePackage/Implements/SaveToExcel.cs | 11 +++--- .../OfficePackage/Implements/SaveToWord.cs | 6 ++- .../IEmployeeReportLogic.cs | 4 +- .../Implements/PCStorage.cs | 10 ++--- .../Controllers/MainController.cs | 29 ++++++++++---- 8 files changed, 75 insertions(+), 43 deletions(-) diff --git a/ComputerStoreBusinessLogic/BusinessLogic/EmployeeReportLogic.cs b/ComputerStoreBusinessLogic/BusinessLogic/EmployeeReportLogic.cs index b0368a0..bf45181 100644 --- a/ComputerStoreBusinessLogic/BusinessLogic/EmployeeReportLogic.cs +++ b/ComputerStoreBusinessLogic/BusinessLogic/EmployeeReportLogic.cs @@ -109,11 +109,11 @@ namespace ComputerStoreBusinessLogic.BusinessLogic } } - foreach(var consignment in specifiedConsignments.Values) + foreach(var consignment in consignments) { - if(!consignments.Contains(consignment)) + if (!specifiedConsignments.ContainsKey(consignment.ID)) { - specifiedConsignments.Remove(consignment.ID); + specifiedConsignments.Add(consignment.ID,consignment); } } @@ -146,7 +146,7 @@ namespace ComputerStoreBusinessLogic.BusinessLogic }); } - public byte[] SaveConsignmentsToExcelFile(ReportComponentsBindingModel model) + public MemoryStream SaveConsignmentsToExcelFile(ReportComponentsBindingModel model) { return _saveToExcel.CreateReport(new ExcelInfo { @@ -156,7 +156,7 @@ namespace ComputerStoreBusinessLogic.BusinessLogic }); } - public byte[] SaveConsignmentsToWordFile(ReportComponentsBindingModel model) + public MemoryStream SaveConsignmentsToWordFile(ReportComponentsBindingModel model) { return _saveToWord.CreateDoc(new WordInfo { diff --git a/ComputerStoreBusinessLogic/OfficePackage/AbstractSaveToExcel.cs b/ComputerStoreBusinessLogic/OfficePackage/AbstractSaveToExcel.cs index cb7acd1..034fdf9 100644 --- a/ComputerStoreBusinessLogic/OfficePackage/AbstractSaveToExcel.cs +++ b/ComputerStoreBusinessLogic/OfficePackage/AbstractSaveToExcel.cs @@ -10,7 +10,7 @@ namespace ComputerStoreBusinessLogic.OfficePackage { public abstract class AbstractSaveToExcel { - public byte[] CreateReport(ExcelInfo info) + public MemoryStream CreateReport(ExcelInfo info) { CreateExcel(info); @@ -27,49 +27,64 @@ namespace ComputerStoreBusinessLogic.OfficePackage CellToName = "C1" }); uint rowIndex = 2; - foreach (var product in info.ConsignmentProducts) + foreach (var consignment in info.ConsignmentProducts) { InsertCellInWorksheet(new ExcelCellParameters { ColumnName = "A", RowIndex = rowIndex, - Text = product.ID.ToString(), + Text = $"ConsignmentID: {consignment.ID}", StyleInfo = ExcelStyleInfoType.Text - }); + }); rowIndex++; - foreach (var (Product, Count) in product.Products) + foreach (var (Product, Count) in consignment.Products) { InsertCellInWorksheet(new ExcelCellParameters { ColumnName = "B", RowIndex = rowIndex, - Text = Product, + Text = "ProductID:", StyleInfo = ExcelStyleInfoType.TextWithBroder }); InsertCellInWorksheet(new ExcelCellParameters { ColumnName = "C", RowIndex = rowIndex, + Text = Product, + StyleInfo = ExcelStyleInfoType.TextWithBroder + }); + InsertCellInWorksheet(new ExcelCellParameters + { + ColumnName = "E", + RowIndex = rowIndex, + Text = "Count:", + StyleInfo = ExcelStyleInfoType.TextWithBroder + }); + InsertCellInWorksheet(new ExcelCellParameters + { + ColumnName = "F", + RowIndex = rowIndex, Text = Count.ToString(), StyleInfo = ExcelStyleInfoType.TextWithBroder }); - rowIndex++; + rowIndex+=2; } + rowIndex++; InsertCellInWorksheet(new ExcelCellParameters { - ColumnName = "A", + ColumnName = "B", RowIndex = rowIndex, - Text = "Total", + Text = "Total products:", StyleInfo = ExcelStyleInfoType.Text }); InsertCellInWorksheet(new ExcelCellParameters { ColumnName = "C", RowIndex = rowIndex, - Text = product.ID.ToString(), + Text = consignment.ID.ToString(), StyleInfo = ExcelStyleInfoType.Text }); - rowIndex++; + rowIndex+=3; } return SaveExcel(info); @@ -82,6 +97,6 @@ namespace ComputerStoreBusinessLogic.OfficePackage protected abstract void MergeCells(ExcelMergeParameters excelParams); - protected abstract byte[] SaveExcel(ExcelInfo info); + protected abstract MemoryStream SaveExcel(ExcelInfo info); } } diff --git a/ComputerStoreBusinessLogic/OfficePackage/AbstractSaveToWord.cs b/ComputerStoreBusinessLogic/OfficePackage/AbstractSaveToWord.cs index 9686ef1..bbe1e56 100644 --- a/ComputerStoreBusinessLogic/OfficePackage/AbstractSaveToWord.cs +++ b/ComputerStoreBusinessLogic/OfficePackage/AbstractSaveToWord.cs @@ -10,7 +10,7 @@ namespace ComputerStoreBusinessLogic.OfficePackage { public abstract class AbstractSaveToWord { - public byte[] CreateDoc(WordInfo info) + public MemoryStream CreateDoc(WordInfo info) { CreateWord(info); CreateParagraph(new WordParagraph @@ -26,7 +26,7 @@ namespace ComputerStoreBusinessLogic.OfficePackage { CreateParagraph(new WordParagraph { - Texts = new List<(string, WordTextProperties)> { (consigment.ID.ToString(), new WordTextProperties { Bold = true, Size = "24", }) }, + Texts = new List<(string, WordTextProperties)> { ($"ConsignmentID: {consigment.ID}", new WordTextProperties { Bold = true, Size = "24", }) }, TextProperties = new WordTextProperties { Size = "24", @@ -38,7 +38,8 @@ namespace ComputerStoreBusinessLogic.OfficePackage { CreateParagraph(new WordParagraph { - Texts = new List<(string, WordTextProperties)> { (products.product, new WordTextProperties { Bold = true, Size = "24", }), ($" {products.count}", new WordTextProperties { Bold = true, Size = "24", }) }, + Texts = new List<(string, WordTextProperties)> { + ($"ProductID: {products.product} ", new WordTextProperties { Bold = false, Size = "24", }), ($"ProductCount: {products.count}", new WordTextProperties { Bold = false, Size = "24", }) }, TextProperties = new WordTextProperties { Size = "24", @@ -53,6 +54,6 @@ namespace ComputerStoreBusinessLogic.OfficePackage protected abstract void CreateWord(WordInfo info); protected abstract void CreateParagraph(WordParagraph paragraph); - protected abstract byte[] SaveWord(WordInfo info); + protected abstract MemoryStream SaveWord(WordInfo info); } } diff --git a/ComputerStoreBusinessLogic/OfficePackage/Implements/SaveToExcel.cs b/ComputerStoreBusinessLogic/OfficePackage/Implements/SaveToExcel.cs index 0ee4acb..6afc38c 100644 --- a/ComputerStoreBusinessLogic/OfficePackage/Implements/SaveToExcel.cs +++ b/ComputerStoreBusinessLogic/OfficePackage/Implements/SaveToExcel.cs @@ -18,7 +18,7 @@ namespace ComputerStoreBusinessLogic.OfficePackage.Implements private SpreadsheetDocument? _spreadsheetDocument; private SharedStringTablePart? _shareStringPart; private Worksheet? _worksheet; - private MemoryStream? mem; + private MemoryStream mem; private static void CreateStyles(WorkbookPart workbookpart) { var sp = workbookpart.AddNewPart(); @@ -244,7 +244,7 @@ namespace ComputerStoreBusinessLogic.OfficePackage.Implements Id = _spreadsheetDocument.WorkbookPart.GetIdOfPart(worksheetPart), SheetId = 1, - Name = "Лист" + Name = "List" }; sheets.Append(sheet); _worksheet = worksheetPart.Worksheet; @@ -341,7 +341,7 @@ namespace ComputerStoreBusinessLogic.OfficePackage.Implements mergeCells.Append(mergeCell); } - protected override byte[] SaveExcel(ExcelInfo info) + protected override MemoryStream SaveExcel(ExcelInfo info) { if (_spreadsheetDocument == null) { @@ -349,8 +349,9 @@ namespace ComputerStoreBusinessLogic.OfficePackage.Implements } _spreadsheetDocument.WorkbookPart!.Workbook.Save(); _spreadsheetDocument.Dispose(); - - return mem.ToArray(); + + mem.Seek(0,SeekOrigin.Begin); + return mem; } } } diff --git a/ComputerStoreBusinessLogic/OfficePackage/Implements/SaveToWord.cs b/ComputerStoreBusinessLogic/OfficePackage/Implements/SaveToWord.cs index e65a4f8..319a5ef 100644 --- a/ComputerStoreBusinessLogic/OfficePackage/Implements/SaveToWord.cs +++ b/ComputerStoreBusinessLogic/OfficePackage/Implements/SaveToWord.cs @@ -103,7 +103,7 @@ namespace ComputerStoreBusinessLogic.OfficePackage.Implements _docBody = mainPart.Document.AppendChild(new Body()); } - protected override byte[] SaveWord(WordInfo info) + protected override MemoryStream SaveWord(WordInfo info) { if (_docBody == null || _wordDocument == null) { @@ -112,7 +112,9 @@ namespace ComputerStoreBusinessLogic.OfficePackage.Implements _docBody.AppendChild(CreateSectionProperties()); _wordDocument.MainDocumentPart!.Document.Save(); _wordDocument.Dispose(); - return mem.ToArray(); + + mem.Seek(0, SeekOrigin.Begin); + return mem; } } } diff --git a/ComputerStoreContracts/BusinessLogicContracts/IEmployeeReportLogic.cs b/ComputerStoreContracts/BusinessLogicContracts/IEmployeeReportLogic.cs index 850b0c5..913a0dd 100644 --- a/ComputerStoreContracts/BusinessLogicContracts/IEmployeeReportLogic.cs +++ b/ComputerStoreContracts/BusinessLogicContracts/IEmployeeReportLogic.cs @@ -15,7 +15,7 @@ namespace ComputerStoreContracts.BusinessLogicContracts List GetConsignmentsByComponents(ReportComponentsBindingModel model); void SavePCsAndProductsToPdfFile(ReportDateBindingModel model); - byte[] SaveConsignmentsToExcelFile(ReportComponentsBindingModel model); - byte[] SaveConsignmentsToWordFile(ReportComponentsBindingModel model); + MemoryStream SaveConsignmentsToExcelFile(ReportComponentsBindingModel model); + MemoryStream SaveConsignmentsToWordFile(ReportComponentsBindingModel model); } } diff --git a/ComputerStoreDatabaseImplement/Implements/PCStorage.cs b/ComputerStoreDatabaseImplement/Implements/PCStorage.cs index 3626223..d344b31 100644 --- a/ComputerStoreDatabaseImplement/Implements/PCStorage.cs +++ b/ComputerStoreDatabaseImplement/Implements/PCStorage.cs @@ -21,7 +21,7 @@ namespace ComputerStoreDatabaseImplement.Implements if (string.IsNullOrEmpty(model.Name) && !model.ID.HasValue) { return null; } using var context = new ComputerStoreDatabase(); - return context.PCS.Include(x => x.Employee).Include(x => x.Request).FirstOrDefault(x => (!string.IsNullOrEmpty(model.Name) && model.Name.Equals(x.Name)) || (model.ID.HasValue && model.ID == x.ID))?.GetViewModel; + return context.PCS.Include(x => x.Employee).Include(x => x.Request).ThenInclude(x => x.PCs).ThenInclude(x => x.Component).FirstOrDefault(x => (!string.IsNullOrEmpty(model.Name) && model.Name.Equals(x.Name)) || (model.ID.HasValue && model.ID == x.ID))?.GetViewModel; } @@ -35,9 +35,9 @@ namespace ComputerStoreDatabaseImplement.Implements if(model.EmployeeID.HasValue) { - return context.PCS.Include(x => x.Employee).Where(x => x.EmployeeID == model.EmployeeID).Select(x => x.GetViewModel).ToList(); + return context.PCS.Include(x => x.Employee).Include(x => x.Request).ThenInclude(x => x.PCs).ThenInclude(x => x.Component).Where(x => x.EmployeeID == model.EmployeeID).Select(x => x.GetViewModel).ToList(); } - return context.PCS.Include(x => x.Employee).Include(x => x.Request).Where(p => context.Requests.Where(r => context.Orders.Where(o => o.DateCreate >= model.DateFrom && o.DateCreate <= model.DateTo).Select(o => o.ID).Contains(r.OrderID)).Select(r => r.ID).Contains(p.RequestID)).ToList().Select(x => x.GetViewModel).ToList(); + return context.PCS.Include(x => x.Employee).Include(x => x.Request).ThenInclude(x => x.PCs).ThenInclude(x => x.Component).Where(p => context.Requests.Where(r => context.Orders.Where(o => o.DateCreate >= model.DateFrom && o.DateCreate <= model.DateTo).Select(o => o.ID).Contains(r.OrderID)).Select(r => r.ID).Contains(p.RequestID)).ToList().Select(x => x.GetViewModel).ToList(); } public List GetFullList() @@ -72,7 +72,7 @@ namespace ComputerStoreDatabaseImplement.Implements using var transaction = context.Database.BeginTransaction(); try { - var specPC = context.PCS.FirstOrDefault(x => x.ID == model.ID); + var specPC = context.PCS.Include(x => x.Request).Include(x => x.Employee).FirstOrDefault(x => x.ID == model.ID); if(specPC == null) { return null; } specPC.Update(model); @@ -91,7 +91,7 @@ namespace ComputerStoreDatabaseImplement.Implements public PCViewModel? Delete(PCBindingModel model) { using var context = new ComputerStoreDatabase(); - var specPC = context.PCS.Include(x => x.Request).FirstOrDefault(x => x.ID == model.ID); + var specPC = context.PCS.Include(x => x.Request).Include(x => x.Employee).FirstOrDefault(x => x.ID == model.ID); if(specPC == null) { return null;} context.PCS.Remove(specPC); context.SaveChanges(); diff --git a/ComputerStoreRestAPI/Controllers/MainController.cs b/ComputerStoreRestAPI/Controllers/MainController.cs index bb4d542..3a5ab20 100644 --- a/ComputerStoreRestAPI/Controllers/MainController.cs +++ b/ComputerStoreRestAPI/Controllers/MainController.cs @@ -505,29 +505,42 @@ namespace ComputerStoreRestAPI.Controllers #endregion [HttpGet] - public List GetReportConsignments(ReportComponentsBindingModel model) + public List GetReportConsignments(string str) { + var model = new ReportComponentsBindingModel {}; + + var components = str.Split(";", StringSplitOptions.RemoveEmptyEntries); + foreach (var component in components) + { + var fields = component.Split("|"); + model.Components.Add(new ComponentSearchModel { ID = Convert.ToInt32(fields[0]), Name = fields[1] }); + } + return _employeeReportLogic.GetConsignmentsByComponents(model); } - [HttpGet] - public byte[] SaveReportConsignmentsToExcel(ReportComponentsBindingModel model) + [HttpPost] + public ActionResult SaveReportConsignmentsToExcel(ReportComponentsBindingModel model) { - return _employeeReportLogic.SaveConsignmentsToExcelFile(new ReportComponentsBindingModel + + var stream = _employeeReportLogic.SaveConsignmentsToExcelFile(new ReportComponentsBindingModel { - FileName = "ReportConsignments", + FileName = model.FileName, Components = model.Components }); + return File(stream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", model.FileName); } - [HttpGet] - public byte[] SaveReportConsignmentsToWord(ReportComponentsBindingModel model) + [HttpPost] + public ActionResult SaveReportConsignmentsToWord(ReportComponentsBindingModel model) { - return _employeeReportLogic.SaveConsignmentsToWordFile(new ReportComponentsBindingModel + var stream = _employeeReportLogic.SaveConsignmentsToWordFile(new ReportComponentsBindingModel { FileName = "ReportConsignments", Components = model.Components }); + + return File(stream, "application/vnd.openxmlformats-officedocument.spreadsheetml.document", model.FileName); } } }