Лабораторная работа 3 (со всеми изменениями)

This commit is contained in:
Есения Андрианова 2024-12-15 10:34:08 +04:00
parent 68bb9245ac
commit 995ad19310
5 changed files with 25 additions and 30 deletions

View File

@ -4,8 +4,8 @@ using Microsoft.Extensions.Logging;
internal class ChartReport
{
private readonly ICheckRepository _checkRepository; // Репозиторий для получения данных о чеках
private readonly IProductRepository _productRepository; // Репозиторий для получения данных о продуктах
private readonly ICheckRepository _checkRepository;
private readonly IProductRepository _productRepository;
private readonly ILogger<ChartReport> _logger;
public ChartReport(ICheckRepository checkRepository, IProductRepository productRepository, ILoggerFactory loggerFactory)
@ -21,7 +21,7 @@ internal class ChartReport
{
new PdfBuilder(filePath)
.AddHeader("Отчет по продажам товаров")
.AddPieChart("Проданные товары", GetData(dateTime)) // Диаграмма с продуктами
.AddPieChart("Проданные товары", GetData(dateTime))
.Build();
return true;
@ -35,17 +35,17 @@ internal class ChartReport
private List<(string Caption, double Value)> GetData(DateTime dateTime)
{
// Получаем все чеки, которые были оформлены в указанную дату
var checkData = _checkRepository
.ReadAll()
.Where(x => x.PurchaseDate.Date == dateTime.Date) // Фильтрация по дате
.SelectMany(x => x.Products) // Получаем все продукты из всех чеков
.GroupBy(p => p.ProductID) // Группируем по ID продукта
.Where(x => x.PurchaseDate.Date == dateTime.Date)
.SelectMany(x => x.Products)
.GroupBy(p => p.ProductID)
.Select(g =>
{
// Получаем продукт по ProductID
var product = _productRepository.Read(g.Key);
return (Caption: product.Name, Value: (double)g.Sum(p => p.Count)); // Преобразуем Count в double
return (Caption: product.Name, Value: (double)g.Sum(p => p.Count));
})
.ToList();

View File

@ -84,10 +84,10 @@ namespace CompShop.DocumentsBuilder
CustomWidth = true
}));
// Добавляем строки таблицы
for (int i = 0; i < data.Count; ++i)
{
var isBoldRow = i == 0 || i == data.Count - 1; // Только заголовок и последняя строка жирные
var isBoldRow = i == 0 || i == data.Count - 1;
var styleIndex = isBoldRow ? StyleIndex.BoldTextWithBorders : StyleIndex.SimpleTextWithBorders;
for (int j = 0; j < data[i].Length; ++j)
@ -143,7 +143,6 @@ namespace CompShop.DocumentsBuilder
var workbookStylesPart = workbookPart.AddNewPart<WorkbookStylesPart>();
workbookStylesPart.Stylesheet = new Stylesheet();
// Шрифты
var fonts = new Fonts() { Count = 2 };
fonts.Append(new DocumentFormat.OpenXml.Spreadsheet.Font
{
@ -157,16 +156,16 @@ namespace CompShop.DocumentsBuilder
FontName = new FontName() { Val = "Calibri" }
});
// Заполнение
var fills = new Fills() { Count = 1 };
fills.Append(new Fill
{
PatternFill = new PatternFill { PatternType = PatternValues.None }
});
// Границы
var borders = new Borders() { Count = 2 };
borders.Append(new Border()); // Без границ
borders.Append(new Border());
borders.Append(new Border
{
LeftBorder = new LeftBorder { Style = BorderStyleValues.Thin },
@ -175,7 +174,7 @@ namespace CompShop.DocumentsBuilder
BottomBorder = new BottomBorder { Style = BorderStyleValues.Thin }
});
// Форматы ячеек
var cellFormats = new CellFormats() { Count = 4 };
cellFormats.Append(new CellFormat
{
@ -183,14 +182,14 @@ namespace CompShop.DocumentsBuilder
FillId = 0,
BorderId = 0,
ApplyFont = true
}); // Обычный текст без границ
});
cellFormats.Append(new CellFormat
{
FontId = 1,
FillId = 0,
BorderId = 0,
ApplyFont = true
}); // Жирный текст без границ
});
cellFormats.Append(new CellFormat
{
FontId = 0,
@ -198,7 +197,7 @@ namespace CompShop.DocumentsBuilder
BorderId = 1,
ApplyFont = true,
ApplyBorder = true
}); // Обычный текст с границами
});
cellFormats.Append(new CellFormat
{
FontId = 1,
@ -206,7 +205,7 @@ namespace CompShop.DocumentsBuilder
BorderId = 1,
ApplyFont = true,
ApplyBorder = true
}); // Жирный текст с границами
});
workbookStylesPart.Stylesheet.Append(fonts);
workbookStylesPart.Stylesheet.Append(fills);

View File

@ -1,10 +1,6 @@
using MigraDoc.DocumentObjectModel;
using MigraDoc.DocumentObjectModel.Shapes.Charts;
using MigraDoc.Rendering;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Chart = MigraDoc.DocumentObjectModel.Shapes.Charts.Chart;

View File

@ -38,7 +38,7 @@ internal class TableReport
private List<string[]> GetData(int productId, DateTime startDate, DateTime endDate)
{
// Получение данных о чеках, содержащих указанный товар
var checksWithProduct = _checkRepository
.ReadAll()
.Where(c => c.PurchaseDate >= startDate && c.PurchaseDate <= endDate
@ -53,7 +53,7 @@ internal class TableReport
})
.OrderBy(x => x.Date);
// Формирование итоговой таблицы
return new List<string[]> { Headers }
.Union(
checksWithProduct.Select(x => new string[]

View File

@ -35,12 +35,12 @@ namespace CompShop.DocumentsBuilder
var paragraph = _body.AppendChild(new Paragraph());
var run = paragraph.AppendChild(new Run());
// Добавляем свойства для жирного текста
var runProperties = new RunProperties();
runProperties.AppendChild(new Bold());
run.PrependChild(runProperties);
// Добавляем текст заголовка
run.AppendChild(new Text(header));
return this;
@ -109,7 +109,7 @@ namespace CompShop.DocumentsBuilder
)
));
// Заголовок
var tr = new TableRow();
for (var j = 0; j < widths.Length; ++j)
{
@ -124,7 +124,7 @@ namespace CompShop.DocumentsBuilder
}
table.Append(tr);
// Данные
table.Append(data.Skip(1).Select(x =>
new TableRow(x.Select(y => new TableCell(new Paragraph(new
Run(new Text(y))))))));