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

This commit is contained in:
Мария Котова 2024-12-11 09:09:46 +04:00
parent c1dbd5c181
commit 101eaa9bc8
2 changed files with 85 additions and 54 deletions

View File

@ -36,12 +36,10 @@ internal class ExcelBuilder
public ExcelBuilder AddHeader(string header, int startIndex, int count)
{
CreateCell(startIndex, _rowIndex, header,
StyleIndex.SimpleTextWithoutBorder);
CreateCell(startIndex, _rowIndex, header, StyleIndex.BoldTextWithoutBorder);
for (int i = startIndex + 1; i < startIndex + count; ++i)
{
CreateCell(i, _rowIndex, "",
StyleIndex.SimpleTextWithoutBorder);
CreateCell(i, _rowIndex, "",StyleIndex.SimpleTextWithoutBorder);
}
_mergeCells.Append(new MergeCell()
@ -57,11 +55,11 @@ internal class ExcelBuilder
public ExcelBuilder AddParagraph(string text, int columnIndex)
{
CreateCell(columnIndex, _rowIndex++, text,
StyleIndex.SimpleTextWithoutBorder);
CreateCell(columnIndex, _rowIndex++, text, StyleIndex.SimpleTextWithoutBorder);
return this;
}
public ExcelBuilder AddTable(int[] columnsWidths, List<string[]> data)
{
if (columnsWidths == null || columnsWidths.Length == 0)
@ -76,7 +74,7 @@ internal class ExcelBuilder
if (data.Any(x => x.Length != columnsWidths.Length))
{
throw new InvalidOperationException("widths.Length != data.Length");
throw new InvalidOperationException("widths.Length != data.Length");
}
uint counter = 1;
@ -89,31 +87,30 @@ internal class ExcelBuilder
CustomWidth = true
}));
// Добавление заголовка
for (var j = 0; j < data.First().Length; ++j)
{
CreateCell(j, _rowIndex, data.First()[j],
StyleIndex.BoldTextWithoutBorder);
CreateCell(j, _rowIndex, data.First()[j], StyleIndex.BoldTextWithBorder);
}
_rowIndex++;
// Добавление основных данных
for (var i = 1; i < data.Count - 1; ++i)
{
for (var j = 0; j < data[i].Length; ++j)
{
CreateCell(j, _rowIndex, data[i][j],
StyleIndex.SimpleTextWithoutBorder);
CreateCell(j, _rowIndex, data[i][j], StyleIndex.SimpleTextWithBorder);
}
_rowIndex++;
}
// Добавление итоговой строки
for (var j = 0; j < data.Last().Length; ++j)
{
CreateCell(j, _rowIndex, data.Last()[j],
StyleIndex.BoldTextWithoutBorder);
CreateCell(j, _rowIndex, data.Last()[j], StyleIndex.BoldTextWithBorder);
}
_rowIndex++;
return this;
}

View File

@ -1,5 +1,6 @@
using DocumentFormat.OpenXml.Wordprocessing;
using Microsoft.Extensions.Logging;
using ProjectTimeTable.Entites;
using ProjectTimeTable.Repositories;
using System;
using System.Collections.Generic;
@ -16,7 +17,6 @@ internal class TableReport
private readonly IDisciplineRepositories _disciplineRepositories;
private readonly IGroupRepositories _groupRepositories;
private readonly ILogger<TableReport> _logger;
internal static readonly string[] item = ["Название", "Дисциплина", "Количество часов", "Курс", "Временно"];
public TableReport(IPlanRepositories planRepository, ITimeTableRepositories timeTableRepository, IDisciplineRepositories disciplineRepositories,
@ -34,10 +34,10 @@ internal class TableReport
try
{
new ExcelBuilder(filePath)
.AddHeader("Сводка по количеству дисциплин на курсе", 0, 3)
.AddParagraph("по плану", 0)
.AddTable([10, 10, 15, 10, 10], GetData(week, discilineId, groupId))
.Build();
.AddHeader("Сводка по количеству дисциплин на курсе", 0, 3)
.AddParagraph("по плану", 0)
.AddTable([10, 10, 15, 10], GetData(week, discilineId, groupId))
.Build();
return true;
}
@ -48,43 +48,77 @@ internal class TableReport
}
}
private List<string[]> GetData(int week, int discilineId, int groupId)
private List<string[]> GetData(int week, int disciplineId, int groupId)
{
var data = _timeTableRepository
var timeTableData = _timeTableRepository
.ReadTimeTable()
.Where(x => x.Week == week)
.Select(x => new { Week = (int?)x.Week, Day = (int?)x.Day, Name = (string?)null, Group = (string?)null, CountLesson = (int?)null })
.Union(
_disciplineRepositories
.ReadDiscipline()
.Where(x => x.ID == discilineId)
.Select(x => new { Week = (int?)null, Day = (int?)null, Name = (string?)x.Name, Group = (string?)null, CountLesson = (int?)null }))
.Union(
_groupRepositories
.ReadGroup()
.Where(x => x.Id == groupId)
.Select(x => new
{
Week = (int?)null,
Day = (int?)null,
Name = (string?)null,
Group = (string?)x.Name,
CountLesson = (int?)null
})
.Select(x => new
{
Week = (int?)x.Week,
GroupName = (string?)null,
Discipline = (string?)null,
CountLesson = (int?)x.NumberLesson
});
).OrderBy(x => x.Day);
var disciplineData = _disciplineRepositories
.ReadDiscipline()
.Where(x => x.ID == disciplineId)
.Select(x => new
{
Week = (int?)null,
GroupName = (string?)null,
Discipline = (string)x.Name,
CountLesson = (int?)null
});
return new List<string[]>() { item }
.Union(
data
.Select(x => new string[] { x.Week.ToString() ?? string.Empty, x.Day.ToString() ?? string.Empty, x.Name?.ToString() ?? string.Empty, x.Group?.ToString() ?? string.Empty, x.CountLesson.ToString() ?? string.Empty }))
.Union(
[["Всего",
"",
"",
"",
data.Sum(x => x.CountLesson ?? 0).ToString()]])
.ToList();
var groupData = _groupRepositories
.ReadGroup()
.Where(x => x.Id == groupId)
.Select(x => new
{
Week = (int?)null,
GroupName = (string)x.Name,
Discipline = (string?)null,
CountLesson = (int?)null
});
// Объединение данных с использованием CrossJoin
var result = from t in timeTableData
from d in disciplineData
from g in groupData
select new
{
Week = t.Week,
GroupName = g?.GroupName ?? string.Empty,
Discipline = d?.Discipline ?? string.Empty,
CountLesson = t.CountLesson ?? 0
};
// Преобразование данных в строки
var reportData = result
.Select(x => new string[]
{
x.Week.ToString() ?? string.Empty,
x.Discipline,
x.GroupName,
x.CountLesson.ToString()
})
.ToList();
reportData.Insert(0, new string[] { "Неделя", "Дисциплина", "Группа", "Количество пар" });
// Добавление итоговой строки
reportData.Add(new string[]
{
"Всего",
"",
"",
result.Sum(x => x.CountLesson).ToString()
});
return reportData;
}
}
}