From 733040c71654652f2bf5bece3859798d8b1e2dae Mon Sep 17 00:00:00 2001 From: rakhaliullov Date: Tue, 26 Nov 2024 13:13:02 +0400 Subject: [PATCH] ExcelBuilder --- .../Entities/CurriculumSubject.cs | 14 +- .../ProjectShedule/Entities/Shedule.cs | 4 +- .../Entities/TempCurriculumSubject.cs | 13 + .../FormSchedulingBureau.Designer.cs | 14 +- .../ProjectShedule/FormSchedulingBureau.cs | 15 +- .../Forms/FormClassroom.Designer.cs | 5 +- .../Forms/FormCurriculumSubject.Designer.cs | 94 ++++-- .../Forms/FormCurriculumSubject.cs | 5 +- .../Forms/FormCurriculumSubject.resx | 12 + .../Forms/FormDirectoryReport.Designer.cs | 139 ++++---- .../Forms/FormDirectoryReport.cs | 84 ++--- .../ProjectShedule/Forms/FormShedule.cs | 9 +- .../Forms/FormSheduleReport.Designer.cs | 168 ++++++++++ .../ProjectShedule/Forms/FormSheduleReport.cs | 70 ++++ .../Forms/FormSheduleReport.resx | 120 +++++++ .../ProjectShedule/Reports/DocReport.cs | 5 - .../ProjectShedule/Reports/ExcelBuilder.cs | 311 ++++++++++++++++++ .../ProjectShedule/Reports/TableReport.cs | 71 ++++ .../ICurriculumSubjectRepository.cs | 2 +- .../Repositories/ISheduleRepository.cs | 2 +- .../CurriculumSubjectRepository.cs | 14 +- .../Implementations/SheduleRepository.cs | 6 +- 22 files changed, 1000 insertions(+), 177 deletions(-) create mode 100644 ProjectShedule/ProjectShedule/Entities/TempCurriculumSubject.cs create mode 100644 ProjectShedule/ProjectShedule/Forms/FormSheduleReport.Designer.cs create mode 100644 ProjectShedule/ProjectShedule/Forms/FormSheduleReport.cs create mode 100644 ProjectShedule/ProjectShedule/Forms/FormSheduleReport.resx create mode 100644 ProjectShedule/ProjectShedule/Reports/ExcelBuilder.cs create mode 100644 ProjectShedule/ProjectShedule/Reports/TableReport.cs diff --git a/ProjectShedule/ProjectShedule/Entities/CurriculumSubject.cs b/ProjectShedule/ProjectShedule/Entities/CurriculumSubject.cs index bda0107..f5099f4 100644 --- a/ProjectShedule/ProjectShedule/Entities/CurriculumSubject.cs +++ b/ProjectShedule/ProjectShedule/Entities/CurriculumSubject.cs @@ -5,18 +5,30 @@ namespace ProjectShedule.Entities; public class CurriculumSubject { public int Id { get; set; } + public DateTime Date { get; private set; } public int CurriculumId { get; private set; } public IEnumerable SubjectSubject { get; private set; } = []; public Course Course { get; private set; } - public static CurriculumSubject CreateCurriculumSubject(int id, int curriculumId, Course course, IEnumerable subjectSubjects) + public static CurriculumSubject CreateCurriculumSubject(int id, DateTime date, int curriculumId, Course course, IEnumerable subjectSubjects) { return new CurriculumSubject() { Id = id, + Date = date, CurriculumId = curriculumId, Course = course, SubjectSubject = subjectSubjects }; } + public static CurriculumSubject CreateCurriculumSubject(TempCurriculumSubject tempCurriculumSubject, IEnumerable subjectSubject) + { + return new CurriculumSubject + { + Id = tempCurriculumSubject.Id, + Date = tempCurriculumSubject.Date, + CurriculumId = tempCurriculumSubject.CurriculumId, + SubjectSubject = subjectSubject + }; + } } diff --git a/ProjectShedule/ProjectShedule/Entities/Shedule.cs b/ProjectShedule/ProjectShedule/Entities/Shedule.cs index 7b3d5d5..a2ca9f2 100644 --- a/ProjectShedule/ProjectShedule/Entities/Shedule.cs +++ b/ProjectShedule/ProjectShedule/Entities/Shedule.cs @@ -7,13 +7,14 @@ public class Shedule public int Id { get; private set; } public DateTime Date { get; private set; } public PairNumber PairNumber { get; private set; } + public int PairCount { get; private set; } public int StudentsGroupId { get; private set; } public int TeacherId { get; private set; } public int SubjectId { get; private set; } public int ClassroomId { get; private set; } - public static Shedule CreateOperation(int id, DateTime date, PairNumber pairNumber + public static Shedule CreateOperation(int id, DateTime date, PairNumber pairNumber, int pairCount , int studentsGroupId, int teacherId, int subjectId, int classroomId) { return new Shedule @@ -21,6 +22,7 @@ public class Shedule Id = id, Date = date, PairNumber = pairNumber, + PairCount = pairCount, StudentsGroupId = studentsGroupId, TeacherId = teacherId, SubjectId = subjectId, diff --git a/ProjectShedule/ProjectShedule/Entities/TempCurriculumSubject.cs b/ProjectShedule/ProjectShedule/Entities/TempCurriculumSubject.cs new file mode 100644 index 0000000..614eb69 --- /dev/null +++ b/ProjectShedule/ProjectShedule/Entities/TempCurriculumSubject.cs @@ -0,0 +1,13 @@ +using ProjectShedule.Entities.Enums; + +namespace ProjectShedule.Entities; + +public class TempCurriculumSubject +{ + public int Id { get; private set; } + public DateTime Date { get; private set; } + public int CurriculumId { get; private set; } + public int SubjectId { get; private set; } + public int LessonsCount { get; private set; } + public Course Course { get; private set; } +} diff --git a/ProjectShedule/ProjectShedule/FormSchedulingBureau.Designer.cs b/ProjectShedule/ProjectShedule/FormSchedulingBureau.Designer.cs index 1431998..7f38ef2 100644 --- a/ProjectShedule/ProjectShedule/FormSchedulingBureau.Designer.cs +++ b/ProjectShedule/ProjectShedule/FormSchedulingBureau.Designer.cs @@ -40,6 +40,7 @@ createCurriculumToolStripMenuItem = new ToolStripMenuItem(); отчетыToolStripMenuItem = new ToolStripMenuItem(); directoryReportToolStripMenuItem = new ToolStripMenuItem(); + sheduleReportToolStripMenuItem = new ToolStripMenuItem(); menuStrip.SuspendLayout(); SuspendLayout(); // @@ -118,7 +119,7 @@ // // отчетыToolStripMenuItem // - отчетыToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { directoryReportToolStripMenuItem }); + отчетыToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { directoryReportToolStripMenuItem, sheduleReportToolStripMenuItem }); отчетыToolStripMenuItem.Name = "отчетыToolStripMenuItem"; отчетыToolStripMenuItem.Size = new Size(73, 24); отчетыToolStripMenuItem.Text = "Отчеты"; @@ -127,10 +128,18 @@ // directoryReportToolStripMenuItem.Name = "directoryReportToolStripMenuItem"; directoryReportToolStripMenuItem.ShortcutKeys = Keys.Control | Keys.W; - directoryReportToolStripMenuItem.Size = new Size(354, 26); + directoryReportToolStripMenuItem.Size = new Size(382, 26); directoryReportToolStripMenuItem.Text = "Документ со справочниками "; directoryReportToolStripMenuItem.Click += directoryReportToolStripMenuItem_Click; // + // sheduleReportToolStripMenuItem + // + sheduleReportToolStripMenuItem.Name = "sheduleReportToolStripMenuItem"; + sheduleReportToolStripMenuItem.ShortcutKeys = Keys.Control | Keys.E; + sheduleReportToolStripMenuItem.Size = new Size(432, 26); + sheduleReportToolStripMenuItem.Text = "Cводка по прохождению учебного плана"; + sheduleReportToolStripMenuItem.Click += sheduleReportToolStripMenuItem_Click; + // // FormSchedulingBureau // AutoScaleDimensions = new SizeF(8F, 20F); @@ -163,5 +172,6 @@ private ToolStripMenuItem отчетыToolStripMenuItem; private ToolStripMenuItem createCurriculumToolStripMenuItem; private ToolStripMenuItem directoryReportToolStripMenuItem; + private ToolStripMenuItem sheduleReportToolStripMenuItem; } } diff --git a/ProjectShedule/ProjectShedule/FormSchedulingBureau.cs b/ProjectShedule/ProjectShedule/FormSchedulingBureau.cs index 197ec8b..bc7bdec 100644 --- a/ProjectShedule/ProjectShedule/FormSchedulingBureau.cs +++ b/ProjectShedule/ProjectShedule/FormSchedulingBureau.cs @@ -1,6 +1,4 @@ -using ProjectGasStation.Forms; using ProjectShedule.Forms; -using System.ComponentModel; using Unity; namespace ProjectShedule @@ -118,5 +116,18 @@ namespace ProjectShedule MessageBoxButtons.OK, MessageBoxIcon.Error); } } + + private void sheduleReportToolStripMenuItem_Click(object sender, EventArgs e) + { + try + { + _container.Resolve().ShowDialog(); + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, " ", + MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } } } diff --git a/ProjectShedule/ProjectShedule/Forms/FormClassroom.Designer.cs b/ProjectShedule/ProjectShedule/Forms/FormClassroom.Designer.cs index 78c6ef7..bc2fad4 100644 --- a/ProjectShedule/ProjectShedule/Forms/FormClassroom.Designer.cs +++ b/ProjectShedule/ProjectShedule/Forms/FormClassroom.Designer.cs @@ -70,12 +70,11 @@ textBoxClassroomNumber.Name = "textBoxClassroomNumber"; textBoxClassroomNumber.Size = new Size(150, 27); textBoxClassroomNumber.TabIndex = 3; - // // label3 // label3.AutoSize = true; - label3.Location = new Point(47, 149); + label3.Location = new Point(47, 154); label3.Name = "label3"; label3.Size = new Size(116, 20); label3.TabIndex = 4; @@ -85,7 +84,7 @@ // comboBoxTypeClassroom.DropDownStyle = ComboBoxStyle.DropDownList; comboBoxTypeClassroom.FormattingEnabled = true; - comboBoxTypeClassroom.Location = new Point(219, 141); + comboBoxTypeClassroom.Location = new Point(218, 146); comboBoxTypeClassroom.Name = "comboBoxTypeClassroom"; comboBoxTypeClassroom.Size = new Size(151, 28); comboBoxTypeClassroom.TabIndex = 5; diff --git a/ProjectShedule/ProjectShedule/Forms/FormCurriculumSubject.Designer.cs b/ProjectShedule/ProjectShedule/Forms/FormCurriculumSubject.Designer.cs index ef6be25..871354d 100644 --- a/ProjectShedule/ProjectShedule/Forms/FormCurriculumSubject.Designer.cs +++ b/ProjectShedule/ProjectShedule/Forms/FormCurriculumSubject.Designer.cs @@ -35,15 +35,17 @@ comboBoxDirection = new ComboBox(); comboBoxCourse = new ComboBox(); groupBoxCurriculum = new GroupBox(); - dataGridViewCur = new DataGridView(); - Column1 = new DataGridViewComboBoxColumn(); - Column2 = new DataGridViewTextBoxColumn(); dataGridView = new DataGridView(); ColumnSubject = new DataGridViewComboBoxColumn(); ColumnLessons = new DataGridViewTextBoxColumn(); + dataGridViewCur = new DataGridView(); + Column1 = new DataGridViewComboBoxColumn(); + Column2 = new DataGridViewTextBoxColumn(); + dateTimePicker1 = new DateTimePicker(); + label2 = new Label(); groupBoxCurriculum.SuspendLayout(); - ((System.ComponentModel.ISupportInitialize)dataGridViewCur).BeginInit(); ((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit(); + ((System.ComponentModel.ISupportInitialize)dataGridViewCur).BeginInit(); SuspendLayout(); // // buttonCancel @@ -69,7 +71,7 @@ // label4 // label4.AutoSize = true; - label4.Location = new Point(115, 96); + label4.Location = new Point(115, 113); label4.Name = "label4"; label4.Size = new Size(44, 20); label4.TabIndex = 16; @@ -78,7 +80,7 @@ // label1 // label1.AutoSize = true; - label1.Location = new Point(52, 44); + label1.Location = new Point(52, 73); label1.Name = "label1"; label1.Size = new Size(107, 20); label1.TabIndex = 11; @@ -88,7 +90,7 @@ // comboBoxDirection.DropDownStyle = ComboBoxStyle.DropDownList; comboBoxDirection.FormattingEnabled = true; - comboBoxDirection.Location = new Point(165, 36); + comboBoxDirection.Location = new Point(165, 65); comboBoxDirection.Name = "comboBoxDirection"; comboBoxDirection.Size = new Size(150, 28); comboBoxDirection.TabIndex = 10; @@ -97,7 +99,7 @@ // comboBoxCourse.DropDownStyle = ComboBoxStyle.DropDownList; comboBoxCourse.FormattingEnabled = true; - comboBoxCourse.Location = new Point(165, 93); + comboBoxCourse.Location = new Point(165, 110); comboBoxCourse.Name = "comboBoxCourse"; comboBoxCourse.Size = new Size(150, 28); comboBoxCourse.TabIndex = 20; @@ -114,33 +116,6 @@ groupBoxCurriculum.TabStop = false; groupBoxCurriculum.Text = "Учбеный план"; // - // dataGridViewCur - // - dataGridViewCur.AllowUserToResizeColumns = false; - dataGridViewCur.AllowUserToResizeRows = false; - dataGridViewCur.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize; - dataGridViewCur.Columns.AddRange(new DataGridViewColumn[] { Column1, Column2 }); - dataGridViewCur.Dock = DockStyle.Fill; - dataGridViewCur.Location = new Point(3, 23); - dataGridViewCur.Name = "dataGridViewCur"; - dataGridViewCur.RowHeadersWidth = 51; - dataGridViewCur.Size = new Size(354, 289); - dataGridViewCur.TabIndex = 0; - // - // Column1 - // - Column1.HeaderText = "Column1"; - Column1.MinimumWidth = 6; - Column1.Name = "Column1"; - Column1.Width = 125; - // - // Column2 - // - Column2.HeaderText = "Column2"; - Column2.MinimumWidth = 6; - Column2.Name = "Column2"; - Column2.Width = 125; - // // dataGridView // dataGridView.AllowUserToResizeColumns = false; @@ -171,11 +146,56 @@ ColumnLessons.MinimumWidth = 6; ColumnLessons.Name = "ColumnLessons"; // + // dataGridViewCur + // + dataGridViewCur.AllowUserToResizeColumns = false; + dataGridViewCur.AllowUserToResizeRows = false; + dataGridViewCur.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize; + dataGridViewCur.Columns.AddRange(new DataGridViewColumn[] { Column1, Column2 }); + dataGridViewCur.Dock = DockStyle.Fill; + dataGridViewCur.Location = new Point(3, 23); + dataGridViewCur.Name = "dataGridViewCur"; + dataGridViewCur.RowHeadersWidth = 51; + dataGridViewCur.Size = new Size(354, 289); + dataGridViewCur.TabIndex = 0; + // + // Column1 + // + Column1.HeaderText = "Column1"; + Column1.MinimumWidth = 6; + Column1.Name = "Column1"; + Column1.Width = 125; + // + // Column2 + // + Column2.HeaderText = "Column2"; + Column2.MinimumWidth = 6; + Column2.Name = "Column2"; + Column2.Width = 125; + // + // dateTimePicker1 + // + dateTimePicker1.Location = new Point(165, 23); + dateTimePicker1.Name = "dateTimePicker1"; + dateTimePicker1.Size = new Size(150, 27); + dateTimePicker1.TabIndex = 22; + // + // label2 + // + label2.AutoSize = true; + label2.Location = new Point(24, 30); + label2.Name = "label2"; + label2.Size = new Size(135, 20); + label2.TabIndex = 23; + label2.Text = "Дата составления:"; + // // FormCurriculumSubject // AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(384, 522); + Controls.Add(label2); + Controls.Add(dateTimePicker1); Controls.Add(groupBoxCurriculum); Controls.Add(comboBoxCourse); Controls.Add(buttonCancel); @@ -186,8 +206,8 @@ Name = "FormCurriculumSubject"; Text = "Составление учебного плана"; groupBoxCurriculum.ResumeLayout(false); - ((System.ComponentModel.ISupportInitialize)dataGridViewCur).EndInit(); ((System.ComponentModel.ISupportInitialize)dataGridView).EndInit(); + ((System.ComponentModel.ISupportInitialize)dataGridViewCur).EndInit(); ResumeLayout(false); PerformLayout(); } @@ -207,5 +227,7 @@ private DataGridView dataGridView; private DataGridViewComboBoxColumn ColumnSubject; private DataGridViewTextBoxColumn ColumnLessons; + private DateTimePicker dateTimePicker1; + private Label label2; } } \ No newline at end of file diff --git a/ProjectShedule/ProjectShedule/Forms/FormCurriculumSubject.cs b/ProjectShedule/ProjectShedule/Forms/FormCurriculumSubject.cs index ef75583..8dfffe9 100644 --- a/ProjectShedule/ProjectShedule/Forms/FormCurriculumSubject.cs +++ b/ProjectShedule/ProjectShedule/Forms/FormCurriculumSubject.cs @@ -1,6 +1,7 @@ using ProjectShedule.Entities; using ProjectShedule.Entities.Enums; using ProjectShedule.Repositories; +using System.Linq; namespace ProjectShedule.Forms @@ -39,6 +40,7 @@ namespace ProjectShedule.Forms throw new Exception("Есть незаполненные поля"); } _curriculumSubjectRepository.CreateCurriculumSubject(CurriculumSubject.CreateCurriculumSubject(0, + dateTimePicker1.Value, (int)comboBoxDirection.SelectedValue!, (Course)comboBoxCourse.SelectedItem!, CreateListSubjectSubjectFromDataGrid())); @@ -67,7 +69,8 @@ namespace ProjectShedule.Forms Convert.ToInt32(row.Cells["ColumnSubject"].Value), Convert.ToInt32(row.Cells["ColumnLessons"].Value))); } - return list; + return list.GroupBy(x => x.SubjectId, x => x.LessonsCount, (id, counts) => + SubjectSubject.CreateOperation(0, id, counts.Sum())).ToList(); } } } diff --git a/ProjectShedule/ProjectShedule/Forms/FormCurriculumSubject.resx b/ProjectShedule/ProjectShedule/Forms/FormCurriculumSubject.resx index 35196f1..0a317f4 100644 --- a/ProjectShedule/ProjectShedule/Forms/FormCurriculumSubject.resx +++ b/ProjectShedule/ProjectShedule/Forms/FormCurriculumSubject.resx @@ -123,6 +123,18 @@ True + + True + + + True + + + True + + + True + True diff --git a/ProjectShedule/ProjectShedule/Forms/FormDirectoryReport.Designer.cs b/ProjectShedule/ProjectShedule/Forms/FormDirectoryReport.Designer.cs index 08c907d..6e5df15 100644 --- a/ProjectShedule/ProjectShedule/Forms/FormDirectoryReport.Designer.cs +++ b/ProjectShedule/ProjectShedule/Forms/FormDirectoryReport.Designer.cs @@ -1,4 +1,4 @@ -namespace ProjectGasStation.Forms +namespace ProjectShedule.Forms { partial class FormDirectoryReport { @@ -28,92 +28,91 @@ /// private void InitializeComponent() { - checkBoxClassrooms = new CheckBox(); - checkBoxTeachers = new CheckBox(); - checkBoxSubjects = new CheckBox(); - checkBoxCurriculums = new CheckBox(); - buttonBuild = new Button(); checkBoxGroups = new CheckBox(); + buttonBuild = new Button(); + checkBoxCurriculums = new CheckBox(); + checkBoxSubjects = new CheckBox(); + checkBoxTeachers = new CheckBox(); + checkBoxClassrooms = new CheckBox(); SuspendLayout(); // - // checkBoxClassrooms - // - checkBoxClassrooms.AutoSize = true; - checkBoxClassrooms.Location = new Point(14, 16); - checkBoxClassrooms.Margin = new Padding(3, 4, 3, 4); - checkBoxClassrooms.Name = "checkBoxClassrooms"; - checkBoxClassrooms.Size = new Size(107, 24); - checkBoxClassrooms.TabIndex = 0; - checkBoxClassrooms.Text = "Аудитории"; - checkBoxClassrooms.UseVisualStyleBackColor = true; - // - // checkBoxTeachers - // - checkBoxTeachers.AutoSize = true; - checkBoxTeachers.Location = new Point(14, 61); - checkBoxTeachers.Margin = new Padding(3, 4, 3, 4); - checkBoxTeachers.Name = "checkBoxTeachers"; - checkBoxTeachers.Size = new Size(140, 24); - checkBoxTeachers.TabIndex = 1; - checkBoxTeachers.Text = "Преподаватели"; - checkBoxTeachers.UseVisualStyleBackColor = true; - // - // checkBoxSubjects - // - checkBoxSubjects.AutoSize = true; - checkBoxSubjects.Location = new Point(14, 109); - checkBoxSubjects.Margin = new Padding(3, 4, 3, 4); - checkBoxSubjects.Name = "checkBoxSubjects"; - checkBoxSubjects.Size = new Size(121, 24); - checkBoxSubjects.TabIndex = 2; - checkBoxSubjects.Text = "Дисциплины"; - checkBoxSubjects.UseVisualStyleBackColor = true; - // - // checkBoxCurriculums - // - checkBoxCurriculums.AutoSize = true; - checkBoxCurriculums.Location = new Point(14, 157); - checkBoxCurriculums.Margin = new Padding(3, 4, 3, 4); - checkBoxCurriculums.Name = "checkBoxCurriculums"; - checkBoxCurriculums.Size = new Size(127, 24); - checkBoxCurriculums.TabIndex = 3; - checkBoxCurriculums.Text = "Направлении"; - checkBoxCurriculums.UseVisualStyleBackColor = true; - // - // buttonBuild - // - buttonBuild.Location = new Point(12, 257); - buttonBuild.Margin = new Padding(3, 4, 3, 4); - buttonBuild.Name = "buttonBuild"; - buttonBuild.Size = new Size(159, 31); - buttonBuild.TabIndex = 4; - buttonBuild.Text = "Сформировать"; - buttonBuild.UseVisualStyleBackColor = true; - buttonBuild.Click += buttonBuild_Click; - // // checkBoxGroups // checkBoxGroups.AutoSize = true; - checkBoxGroups.Location = new Point(14, 206); + checkBoxGroups.Location = new Point(12, 203); checkBoxGroups.Margin = new Padding(3, 4, 3, 4); checkBoxGroups.Name = "checkBoxGroups"; checkBoxGroups.Size = new Size(83, 24); - checkBoxGroups.TabIndex = 5; + checkBoxGroups.TabIndex = 11; checkBoxGroups.Text = "Группы"; checkBoxGroups.UseVisualStyleBackColor = true; // + // buttonBuild + // + buttonBuild.Location = new Point(10, 254); + buttonBuild.Margin = new Padding(3, 4, 3, 4); + buttonBuild.Name = "buttonBuild"; + buttonBuild.Size = new Size(159, 31); + buttonBuild.TabIndex = 10; + buttonBuild.Text = "Сформировать"; + buttonBuild.UseVisualStyleBackColor = true; + buttonBuild.Click += buttonBuild_Click; + // + // checkBoxCurriculums + // + checkBoxCurriculums.AutoSize = true; + checkBoxCurriculums.Location = new Point(12, 154); + checkBoxCurriculums.Margin = new Padding(3, 4, 3, 4); + checkBoxCurriculums.Name = "checkBoxCurriculums"; + checkBoxCurriculums.Size = new Size(127, 24); + checkBoxCurriculums.TabIndex = 9; + checkBoxCurriculums.Text = "Направлении"; + checkBoxCurriculums.UseVisualStyleBackColor = true; + // + // checkBoxSubjects + // + checkBoxSubjects.AutoSize = true; + checkBoxSubjects.Location = new Point(12, 106); + checkBoxSubjects.Margin = new Padding(3, 4, 3, 4); + checkBoxSubjects.Name = "checkBoxSubjects"; + checkBoxSubjects.Size = new Size(121, 24); + checkBoxSubjects.TabIndex = 8; + checkBoxSubjects.Text = "Дисциплины"; + checkBoxSubjects.UseVisualStyleBackColor = true; + // + // checkBoxTeachers + // + checkBoxTeachers.AutoSize = true; + checkBoxTeachers.Location = new Point(12, 58); + checkBoxTeachers.Margin = new Padding(3, 4, 3, 4); + checkBoxTeachers.Name = "checkBoxTeachers"; + checkBoxTeachers.Size = new Size(140, 24); + checkBoxTeachers.TabIndex = 7; + checkBoxTeachers.Text = "Преподаватели"; + checkBoxTeachers.UseVisualStyleBackColor = true; + // + // checkBoxClassrooms + // + checkBoxClassrooms.AutoSize = true; + checkBoxClassrooms.Location = new Point(12, 13); + checkBoxClassrooms.Margin = new Padding(3, 4, 3, 4); + checkBoxClassrooms.Name = "checkBoxClassrooms"; + checkBoxClassrooms.Size = new Size(107, 24); + checkBoxClassrooms.TabIndex = 6; + checkBoxClassrooms.Text = "Аудитории"; + checkBoxClassrooms.UseVisualStyleBackColor = true; + // // FormDirectoryReport // AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(191, 317); + ClientSize = new Size(187, 300); Controls.Add(checkBoxGroups); Controls.Add(buttonBuild); Controls.Add(checkBoxCurriculums); Controls.Add(checkBoxSubjects); Controls.Add(checkBoxTeachers); Controls.Add(checkBoxClassrooms); - Margin = new Padding(3, 4, 3, 4); Name = "FormDirectoryReport"; Text = "FormDirectoryReport"; ResumeLayout(false); @@ -122,11 +121,11 @@ #endregion - private CheckBox checkBoxClassrooms; - private CheckBox checkBoxTeachers; - private CheckBox checkBoxSubjects; - private CheckBox checkBoxCurriculums; - private Button buttonBuild; private CheckBox checkBoxGroups; + private Button buttonBuild; + private CheckBox checkBoxCurriculums; + private CheckBox checkBoxSubjects; + private CheckBox checkBoxTeachers; + private CheckBox checkBoxClassrooms; } } \ No newline at end of file diff --git a/ProjectShedule/ProjectShedule/Forms/FormDirectoryReport.cs b/ProjectShedule/ProjectShedule/Forms/FormDirectoryReport.cs index 84c6d3b..d7a4bbc 100644 --- a/ProjectShedule/ProjectShedule/Forms/FormDirectoryReport.cs +++ b/ProjectShedule/ProjectShedule/Forms/FormDirectoryReport.cs @@ -1,52 +1,52 @@ using ProjectShedule.Reports; using Unity; -namespace ProjectGasStation.Forms; - -public partial class FormDirectoryReport : Form +namespace ProjectShedule.Forms { - private readonly IUnityContainer _container; - public FormDirectoryReport(IUnityContainer container) + public partial class FormDirectoryReport : Form { - InitializeComponent(); - _container = container ?? throw new ArgumentNullException(nameof(container)); - } - - private void buttonBuild_Click(object sender, EventArgs e) - { - try + private readonly IUnityContainer _container; + public FormDirectoryReport(IUnityContainer container) { - if (!checkBoxClassrooms.Checked && !checkBoxTeachers.Checked && !checkBoxSubjects.Checked && !checkBoxCurriculums.Checked && !checkBoxGroups.Checked) - { - throw new Exception("Не выбран ни один справочник для выгрузки"); - } - var sfd = new SaveFileDialog() - { - Filter = "Docx Files | *.docx" - }; - if (sfd.ShowDialog() != DialogResult.OK) - { - throw new Exception("Не выбран файла для отчета"); - } - if - (_container.Resolve().CreateDoc(sfd.FileName, checkBoxClassrooms.Checked, checkBoxTeachers.Checked, checkBoxSubjects.Checked, checkBoxCurriculums.Checked, checkBoxGroups.Checked)) - { - MessageBox.Show("Документ сформирован", - "Формирование документа", - MessageBoxButtons.OK, - MessageBoxIcon.Information); - } - else - { - MessageBox.Show("Возникли ошибки при формировании документа.Подробности в логах", - "Формирование документа", - MessageBoxButtons.OK, MessageBoxIcon.Information); - } - } - catch (Exception ex) - { - MessageBox.Show(ex.Message, "Ошибка при создании отчета", MessageBoxButtons.OK, MessageBoxIcon.Error); + InitializeComponent(); + _container = container ?? throw new ArgumentNullException(nameof(container)); } + private void buttonBuild_Click(object sender, EventArgs e) + { + try + { + if (!checkBoxClassrooms.Checked && !checkBoxTeachers.Checked && !checkBoxSubjects.Checked && !checkBoxCurriculums.Checked && !checkBoxGroups.Checked) + { + throw new Exception("Не выбран ни один справочник для выгрузки"); + } + var sfd = new SaveFileDialog() + { + Filter = "Docx Files | *.docx" + }; + if (sfd.ShowDialog() != DialogResult.OK) + { + throw new Exception("Не выбран файла для отчета"); + } + if + (_container.Resolve().CreateDoc(sfd.FileName, checkBoxClassrooms.Checked, checkBoxTeachers.Checked, checkBoxSubjects.Checked, checkBoxCurriculums.Checked, checkBoxGroups.Checked)) + { + MessageBox.Show("Документ сформирован", + "Формирование документа", + MessageBoxButtons.OK, + MessageBoxIcon.Information); + } + else + { + MessageBox.Show("Возникли ошибки при формировании документа.Подробности в логах", + "Формирование документа", + MessageBoxButtons.OK, MessageBoxIcon.Information); + } + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, "Ошибка при создании отчета", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } } } diff --git a/ProjectShedule/ProjectShedule/Forms/FormShedule.cs b/ProjectShedule/ProjectShedule/Forms/FormShedule.cs index d22007f..c8ec8eb 100644 --- a/ProjectShedule/ProjectShedule/Forms/FormShedule.cs +++ b/ProjectShedule/ProjectShedule/Forms/FormShedule.cs @@ -33,7 +33,7 @@ namespace ProjectShedule.Forms comboBoxClassroom.DataSource = classroomRepository.ReadClassroom(); comboBoxClassroom.DisplayMember = "Name"; comboBoxClassroom.ValueMember = "Id"; - } + } private void buttonSave_Click(object sender, EventArgs e) { @@ -43,15 +43,18 @@ namespace ProjectShedule.Forms comboBoxClassroom.SelectedIndex < 0 || checkedListBox.CheckedItems.Count == 0) { throw new Exception("Имеются незаполненные поля"); - } + } PairNumber pairNumber = PairNumber.None; + int pairCount = 0; foreach (var elem in checkedListBox.CheckedItems) { pairNumber |= (PairNumber)elem; - } + pairCount++; + } _sheduleRepository.CreateShedule(Shedule.CreateOperation(0, dateTimePicker.Value, pairNumber, + pairCount, (int)comboBoxGroup.SelectedValue!, (int)comboBoxTeacher.SelectedValue!, (int)comboBoxSubject.SelectedValue!, diff --git a/ProjectShedule/ProjectShedule/Forms/FormSheduleReport.Designer.cs b/ProjectShedule/ProjectShedule/Forms/FormSheduleReport.Designer.cs new file mode 100644 index 0000000..496e1be --- /dev/null +++ b/ProjectShedule/ProjectShedule/Forms/FormSheduleReport.Designer.cs @@ -0,0 +1,168 @@ +namespace ProjectShedule.Forms +{ + partial class FormSheduleReport + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + label4 = new Label(); + label3 = new Label(); + label2 = new Label(); + label1 = new Label(); + buttonBuild = new Button(); + buttonFile = new Button(); + textBoxFilePath = new TextBox(); + comboBoxSubject = new ComboBox(); + dateTimePickerStart = new DateTimePicker(); + dateTimePickerEnd = new DateTimePicker(); + SuspendLayout(); + // + // label4 + // + label4.AutoSize = true; + label4.Location = new Point(27, 183); + label4.Name = "label4"; + label4.Size = new Size(90, 20); + label4.TabIndex = 19; + label4.Text = "Дата конца:"; + // + // label3 + // + label3.AutoSize = true; + label3.Location = new Point(27, 132); + label3.Name = "label3"; + label3.Size = new Size(97, 20); + label3.TabIndex = 18; + label3.Text = "Дата начала:"; + // + // label2 + // + label2.AutoSize = true; + label2.Location = new Point(27, 75); + label2.Name = "label2"; + label2.Size = new Size(73, 20); + label2.TabIndex = 17; + label2.Text = "Предмет:"; + // + // label1 + // + label1.AutoSize = true; + label1.Location = new Point(27, 23); + label1.Name = "label1"; + label1.Size = new Size(112, 20); + label1.TabIndex = 16; + label1.Text = "Путь до файла:"; + // + // buttonBuild + // + buttonBuild.Location = new Point(27, 233); + buttonBuild.Margin = new Padding(3, 4, 3, 4); + buttonBuild.Name = "buttonBuild"; + buttonBuild.Size = new Size(338, 31); + buttonBuild.TabIndex = 15; + buttonBuild.Text = "Сформировать"; + buttonBuild.UseVisualStyleBackColor = true; + buttonBuild.Click += buttonBuild_Click; + // + // buttonFile + // + buttonFile.Location = new Point(333, 19); + buttonFile.Margin = new Padding(3, 4, 3, 4); + buttonFile.Name = "buttonFile"; + buttonFile.Size = new Size(30, 31); + buttonFile.TabIndex = 14; + buttonFile.Text = "..."; + buttonFile.UseVisualStyleBackColor = true; + buttonFile.Click += buttonFile_Click; + // + // textBoxFilePath + // + textBoxFilePath.Location = new Point(137, 19); + textBoxFilePath.Margin = new Padding(3, 4, 3, 4); + textBoxFilePath.Name = "textBoxFilePath"; + textBoxFilePath.Size = new Size(189, 27); + textBoxFilePath.TabIndex = 13; + // + // comboBoxSubject + // + comboBoxSubject.FormattingEnabled = true; + comboBoxSubject.Location = new Point(137, 71); + comboBoxSubject.Margin = new Padding(3, 4, 3, 4); + comboBoxSubject.Name = "comboBoxSubject"; + comboBoxSubject.Size = new Size(226, 28); + comboBoxSubject.TabIndex = 12; + // + // dateTimePickerStart + // + dateTimePickerStart.Location = new Point(137, 124); + dateTimePickerStart.Margin = new Padding(3, 4, 3, 4); + dateTimePickerStart.Name = "dateTimePickerStart"; + dateTimePickerStart.Size = new Size(228, 27); + dateTimePickerStart.TabIndex = 11; + // + // dateTimePickerEnd + // + dateTimePickerEnd.Location = new Point(137, 175); + dateTimePickerEnd.Margin = new Padding(3, 4, 3, 4); + dateTimePickerEnd.Name = "dateTimePickerEnd"; + dateTimePickerEnd.Size = new Size(228, 27); + dateTimePickerEnd.TabIndex = 10; + // + // FormSheduleReport + // + AutoScaleDimensions = new SizeF(8F, 20F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(402, 278); + Controls.Add(label4); + Controls.Add(label3); + Controls.Add(label2); + Controls.Add(label1); + Controls.Add(buttonBuild); + Controls.Add(buttonFile); + Controls.Add(textBoxFilePath); + Controls.Add(comboBoxSubject); + Controls.Add(dateTimePickerStart); + Controls.Add(dateTimePickerEnd); + Name = "FormSheduleReport"; + Text = "Отчет по прохождению учебного плана"; + ResumeLayout(false); + PerformLayout(); + } + + #endregion + + private Label label4; + private Label label3; + private Label label2; + private Label label1; + private Button buttonBuild; + private Button buttonFile; + private TextBox textBoxFilePath; + private ComboBox comboBoxSubject; + private DateTimePicker dateTimePickerStart; + private DateTimePicker dateTimePickerEnd; + } +} \ No newline at end of file diff --git a/ProjectShedule/ProjectShedule/Forms/FormSheduleReport.cs b/ProjectShedule/ProjectShedule/Forms/FormSheduleReport.cs new file mode 100644 index 0000000..efc1efe --- /dev/null +++ b/ProjectShedule/ProjectShedule/Forms/FormSheduleReport.cs @@ -0,0 +1,70 @@ +using ProjectShedule.Reports; +using ProjectShedule.Repositories; +using Unity; + +namespace ProjectShedule.Forms +{ + public partial class FormSheduleReport : Form + { + private readonly IUnityContainer _container; + public FormSheduleReport(IUnityContainer container, ISubjectRepository subjectRepository) + { + InitializeComponent(); + _container = container ?? throw new ArgumentNullException(nameof(container)); + + comboBoxSubject.DataSource = subjectRepository.ReadSubject(); + comboBoxSubject.DisplayMember = "Name"; + comboBoxSubject.ValueMember = "Id"; + } + + private void buttonFile_Click(object sender, EventArgs e) + { + var sfd = new SaveFileDialog() + { + Filter = "Excel Files | *.xlsx" + }; + if (sfd.ShowDialog() != DialogResult.OK) + { + return; + } + textBoxFilePath.Text = sfd.FileName; + } + + private void buttonBuild_Click(object sender, EventArgs e) + { + try + { + if (string.IsNullOrWhiteSpace(textBoxFilePath.Text)) + { + throw new Exception("Отсутствует имя файла для отчета"); + } + if (comboBoxSubject.SelectedIndex < 0) + { + throw new Exception("Не выбран корм"); + } + if (dateTimePickerEnd.Value <= dateTimePickerStart.Value) + { + throw new Exception("Дата начала должна быть раньше даты окончания"); + } + if (_container.Resolve().CreateTable(textBoxFilePath.Text, (int)comboBoxSubject.SelectedValue!, dateTimePickerStart.Value, dateTimePickerEnd.Value)) + { + MessageBox.Show("Документ сформирован", + "Формирование документа", + MessageBoxButtons.OK, + MessageBoxIcon.Information); + } + else + { + MessageBox.Show("Возникли ошибки при формировании документа.Подробности в логах", + "Формирование документа", + MessageBoxButtons.OK, MessageBoxIcon.Information); + } + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, "Ошибка при создании очета", + MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } +} diff --git a/ProjectShedule/ProjectShedule/Forms/FormSheduleReport.resx b/ProjectShedule/ProjectShedule/Forms/FormSheduleReport.resx new file mode 100644 index 0000000..af32865 --- /dev/null +++ b/ProjectShedule/ProjectShedule/Forms/FormSheduleReport.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/ProjectShedule/ProjectShedule/Reports/DocReport.cs b/ProjectShedule/ProjectShedule/Reports/DocReport.cs index 786e812..d9c8c6e 100644 --- a/ProjectShedule/ProjectShedule/Reports/DocReport.cs +++ b/ProjectShedule/ProjectShedule/Reports/DocReport.cs @@ -1,10 +1,5 @@ using Microsoft.Extensions.Logging; using ProjectShedule.Repositories; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace ProjectShedule.Reports; internal class DocReport diff --git a/ProjectShedule/ProjectShedule/Reports/ExcelBuilder.cs b/ProjectShedule/ProjectShedule/Reports/ExcelBuilder.cs new file mode 100644 index 0000000..aa24582 --- /dev/null +++ b/ProjectShedule/ProjectShedule/Reports/ExcelBuilder.cs @@ -0,0 +1,311 @@ +using DocumentFormat.OpenXml.Packaging; +using DocumentFormat.OpenXml.Spreadsheet; +using DocumentFormat.OpenXml; + +namespace ProjectShedule.Reports; + +internal class ExcelBuilder +{ + private readonly string _filePath; + private readonly SheetData _sheetData; + private readonly MergeCells _mergeCells; + private readonly Columns _columns; + private uint _rowIndex = 0; + + public ExcelBuilder(string filePath) + { + if (string.IsNullOrWhiteSpace(filePath)) + { + throw new ArgumentNullException(nameof(filePath)); + } + if (File.Exists(filePath)) + { + File.Delete(filePath); + } + _filePath = filePath; + _sheetData = new SheetData(); + _mergeCells = new MergeCells(); + _columns = new Columns(); + _rowIndex = 1; + } + + public ExcelBuilder AddHeader(string header, int startIndex, int count) + { + CreateCell(startIndex, _rowIndex, header, StyleIndex.BoldTextWithoutBorder); + for (int i = startIndex + 1; i < startIndex + count; ++i) + { + CreateCell(i, _rowIndex, "", StyleIndex.SimpleTextWithoutBorder); + } + _mergeCells.Append(new MergeCell() + { + Reference = new StringValue($"{GetExcelColumnName(startIndex)}{_rowIndex}:{GetExcelColumnName(startIndex + count - 1)}{_rowIndex}") + }); + _rowIndex++; + return this; + } + + public ExcelBuilder AddParagraph(string text, int columnIndex) + { + CreateCell(columnIndex, _rowIndex++, text, StyleIndex.SimpleTextWithoutBorder); + return this; + } + + public ExcelBuilder AddTable(int[] columnsWidths, List data) + { + if (columnsWidths == null || columnsWidths.Length == 0) + { + throw new ArgumentNullException(nameof(columnsWidths)); + } + if (data == null || data.Count == 0) + { + throw new ArgumentNullException(nameof(data)); + } + if (data.Any(x => x.Length != columnsWidths.Length)) + { + throw new InvalidOperationException("widths.Length != data.Length"); + } + + uint counter = 1; + int coef = 2; + _columns.Append(columnsWidths.Select(x => new Column + { + Min = counter, + Max = counter++, + Width = x * coef, + CustomWidth = true + })); + for (var j = 0; j < data.First().Length; ++j) + { + 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.SimpleTextWithBorder); + } + _rowIndex++; + } + for (var j = 0; j < data.Last().Length; ++j) + { + CreateCell(j, _rowIndex, data.Last()[j], StyleIndex.BoldTextWithBorder); + } + _rowIndex++; + return this; + } + + public void Build() + { + using var spreadsheetDocument = SpreadsheetDocument.Create(_filePath, SpreadsheetDocumentType.Workbook); + var workbookpart = spreadsheetDocument.AddWorkbookPart(); + GenerateStyle(workbookpart); + workbookpart.Workbook = new Workbook(); + var worksheetPart = workbookpart.AddNewPart(); + worksheetPart.Worksheet = new Worksheet(); + + if (_columns.HasChildren) + { + worksheetPart.Worksheet.Append(_columns); + } + + worksheetPart.Worksheet.Append(_sheetData); + var sheets = spreadsheetDocument.WorkbookPart!.Workbook.AppendChild(new Sheets()); + var sheet = new Sheet() + { + Id = spreadsheetDocument.WorkbookPart.GetIdOfPart(worksheetPart), + SheetId = 1, + Name = "Лист 1" + }; + sheets.Append(sheet); + if (_mergeCells.HasChildren) + { + worksheetPart.Worksheet.InsertAfter(_mergeCells, + worksheetPart.Worksheet.Elements().First()); + } + } + + private static void GenerateStyle(WorkbookPart workbookPart) + { + var workbookStylesPart = workbookPart.AddNewPart(); + workbookStylesPart.Stylesheet = new Stylesheet(); + + var fonts = new Fonts() + { + Count = 2, + KnownFonts = BooleanValue.FromBoolean(true) + }; + fonts.Append(new DocumentFormat.OpenXml.Spreadsheet.Font + { + FontSize = new FontSize() { Val = 11 }, + FontName = new FontName() { Val = "Calibri" }, + FontFamilyNumbering = new FontFamilyNumbering() { Val = 2 }, + FontScheme = new FontScheme() + { + Val = new EnumValue(FontSchemeValues.Minor) + } + }); + fonts.Append(new DocumentFormat.OpenXml.Spreadsheet.Font + { + FontSize = new FontSize() { Val = 11 }, + FontName = new FontName() { Val = "Calibri" }, + FontFamilyNumbering = new FontFamilyNumbering() { Val = 2 }, + FontScheme = new FontScheme() + { + Val = new EnumValue(FontSchemeValues.Minor) + }, + Bold = new Bold() { Val = true } + }); + workbookStylesPart.Stylesheet.Append(fonts); + + // Default Fill + var fills = new Fills() { Count = 1 }; + fills.Append(new Fill + { + PatternFill = new PatternFill() + { + PatternType = new EnumValue(PatternValues.None) + } + }); + workbookStylesPart.Stylesheet.Append(fills); + + // Default Border + var borders = new Borders() { Count = 2 }; + borders.Append(new Border + { + LeftBorder = new LeftBorder(), + RightBorder = new RightBorder(), + TopBorder = new TopBorder(), + BottomBorder = new BottomBorder(), + DiagonalBorder = new DiagonalBorder() + }); + borders.Append(new Border + { + LeftBorder = new LeftBorder() { Style = BorderStyleValues.Thin }, + RightBorder = new RightBorder() { Style = BorderStyleValues.Thin }, + TopBorder = new TopBorder() { Style = BorderStyleValues.Thin }, + BottomBorder = new BottomBorder() { Style = BorderStyleValues.Thin }, + DiagonalBorder = new DiagonalBorder() + }); + workbookStylesPart.Stylesheet.Append(borders); + + // Default cell format and a date cell format + var cellFormats = new CellFormats() { Count = 4 }; + cellFormats.Append(new CellFormat + { + NumberFormatId = 0, + FormatId = 0, + FontId = 0, + BorderId = 0, + FillId = 0, + Alignment = new Alignment() + { + Horizontal = HorizontalAlignmentValues.Left, + Vertical = VerticalAlignmentValues.Center, + WrapText = true + } + }); + cellFormats.Append(new CellFormat + { + NumberFormatId = 0, + FormatId = 0, + FontId = 0, + BorderId = 1, + FillId = 0, + Alignment = new Alignment() + { + Horizontal = HorizontalAlignmentValues.Right, + Vertical = VerticalAlignmentValues.Center, + WrapText = true + } + }); + cellFormats.Append(new CellFormat + { + NumberFormatId = 0, + FormatId = 0, + FontId = 1, + BorderId = 0, + FillId = 0, + Alignment = new Alignment() + { + Horizontal = HorizontalAlignmentValues.Center, + Vertical = VerticalAlignmentValues.Center, + WrapText = true + } + }); + cellFormats.Append(new CellFormat + { + NumberFormatId = 0, + FormatId = 0, + FontId = 1, + BorderId = 1, + FillId = 0, + Alignment = new Alignment() + { + Horizontal = HorizontalAlignmentValues.Center, + Vertical = VerticalAlignmentValues.Center, + WrapText = true + } + }); + workbookStylesPart.Stylesheet.Append(cellFormats); + } + + private enum StyleIndex + { + SimpleTextWithoutBorder = 0, + SimpleTextWithBorder = 1, + BoldTextWithoutBorder = 2, + BoldTextWithBorder = 3, + } + + private void CreateCell(int columnIndex, uint rowIndex, string text, StyleIndex styleIndex) + { + var columnName = GetExcelColumnName(columnIndex); + var cellReference = columnName + rowIndex; + var row = _sheetData.Elements().FirstOrDefault(r => r.RowIndex! == rowIndex); + if (row == null) + { + row = new Row() { RowIndex = rowIndex }; + _sheetData.Append(row); + } + var newCell = row.Elements().FirstOrDefault(c => c.CellReference != null && + c.CellReference.Value == columnName + rowIndex); + if (newCell == null) + { + Cell? refCell = null; + foreach (Cell cell in row.Elements()) + { + if (cell.CellReference?.Value != null && + cell.CellReference.Value.Length == cellReference.Length) + { + if (string.Compare(cell.CellReference.Value, cellReference, true) > 0) + { + refCell = cell; + break; + } + } + } + newCell = new Cell() { CellReference = cellReference }; + row.InsertBefore(newCell, refCell); + } + newCell.CellValue = new CellValue(text); + newCell.DataType = CellValues.String; + newCell.StyleIndex = (uint)styleIndex; + } + + private static string GetExcelColumnName(int columnNumber) + { + columnNumber += 1; + int dividend = columnNumber; + string columnName = string.Empty; + int modulo; + while (dividend > 0) + { + modulo = (dividend - 1) % 26; + columnName = Convert.ToChar(65 + modulo).ToString() + columnName; + dividend = (dividend - modulo) / 26; + } + return columnName; + } +} diff --git a/ProjectShedule/ProjectShedule/Reports/TableReport.cs b/ProjectShedule/ProjectShedule/Reports/TableReport.cs new file mode 100644 index 0000000..c295f66 --- /dev/null +++ b/ProjectShedule/ProjectShedule/Reports/TableReport.cs @@ -0,0 +1,71 @@ +using Microsoft.Extensions.Logging; +using ProjectShedule.Repositories; + +namespace ProjectShedule.Reports; + +internal class TableReport +{ + private readonly ICurriculumSubjectRepository _curriculumSubjectRepository; + private readonly ISheduleRepository _sheduleRepository; + private readonly ILogger _logger; + internal static readonly string[] item = ["Дата", "Количество установленных занятий", "Количество поставленных занятий"]; + + public TableReport(ICurriculumSubjectRepository curriculumSubjectRepository, ISheduleRepository sheduleRepository, ILogger logger) + { + _curriculumSubjectRepository = curriculumSubjectRepository ?? throw new ArgumentNullException(nameof(curriculumSubjectRepository)); + _sheduleRepository = sheduleRepository ?? throw new ArgumentNullException(nameof(sheduleRepository)); + _logger = logger ?? throw new ArgumentNullException(nameof(logger)); + } + + public bool CreateTable(string filePath, int subjectId, DateTime startDate, DateTime endDate) + { + try + { + new ExcelBuilder(filePath) + .AddHeader("Сводка по прохождению учебного плана", 0, 3) + .AddParagraph("за период", 0) + .AddTable([10, 15, 15], GetData(subjectId, startDate, endDate)) + .Build(); + return true; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при формировании документа"); + return false; + } + } + + private List GetData(int subjectId, DateTime startDate, DateTime endDate) + { + var data = _curriculumSubjectRepository + .ReadCurriculumSubject() + .Where(x => x.Date >= startDate && x.Date <= endDate && x.SubjectSubject.Any(y => y.SubjectId == subjectId)) + .Select(x => new {Date = x.Date, CountIn = x.SubjectSubject.FirstOrDefault(y => y.SubjectId == subjectId)?.LessonsCount, CountOut = (int?)null}) + .Union( + _sheduleRepository + .ReadShedule() + .Where(x => x.Date >= startDate && x.Date <= endDate && x.SubjectId == subjectId) + .Select(x => new { Date = x.Date, CountIn = (int?)null, CountOut = x?.PairCount}) + ) + .OrderBy(x => x.Date); + + var groupedData = data + .GroupBy(x => x.Date) + .Select(g => new + { + Date = g.Key, + TotalIn = g.Sum(x => x.CountIn), + TotalOut = g.Sum(x => x.CountOut) + }) + .OrderBy(x => x.Date); + + return + new List() { item } + .Union( groupedData + .Select(x => new string[] { x.Date.ToString("dd.MM.yyyy"), x.TotalIn.ToString()!, x.TotalOut.ToString()! })) + .Union( + new[] { new string[] { "Всего", groupedData.Sum(x => x.TotalIn).ToString()!, groupedData.Sum(x => x.TotalOut).ToString()! } } + ) + .ToList(); + } +} diff --git a/ProjectShedule/ProjectShedule/Repositories/ICurriculumSubjectRepository.cs b/ProjectShedule/ProjectShedule/Repositories/ICurriculumSubjectRepository.cs index 152ce12..d924c2d 100644 --- a/ProjectShedule/ProjectShedule/Repositories/ICurriculumSubjectRepository.cs +++ b/ProjectShedule/ProjectShedule/Repositories/ICurriculumSubjectRepository.cs @@ -4,7 +4,7 @@ namespace ProjectShedule.Repositories; public interface ICurriculumSubjectRepository { - IEnumerable ReadCurriculumSubject(); + IEnumerable ReadCurriculumSubject(DateTime? dateForm = null, DateTime? dateTo = null, int? subjectId = null); void DeleteCurriculumSubject(int id); void CreateCurriculumSubject(CurriculumSubject curriculumSubject); } diff --git a/ProjectShedule/ProjectShedule/Repositories/ISheduleRepository.cs b/ProjectShedule/ProjectShedule/Repositories/ISheduleRepository.cs index 22df657..90617b3 100644 --- a/ProjectShedule/ProjectShedule/Repositories/ISheduleRepository.cs +++ b/ProjectShedule/ProjectShedule/Repositories/ISheduleRepository.cs @@ -4,7 +4,7 @@ namespace ProjectShedule.Repositories; public interface ISheduleRepository { - IEnumerable ReadShedule(DateTime? dateTime = null, int? groupId = null); + IEnumerable ReadShedule(DateTime? dateForm = null, DateTime? dateTo = null, int? subjectId = null); void CreateShedule(Shedule shedule); void DeleteShedule(int id); } diff --git a/ProjectShedule/ProjectShedule/Repositories/Implementations/CurriculumSubjectRepository.cs b/ProjectShedule/ProjectShedule/Repositories/Implementations/CurriculumSubjectRepository.cs index f8c8d44..7929572 100644 --- a/ProjectShedule/ProjectShedule/Repositories/Implementations/CurriculumSubjectRepository.cs +++ b/ProjectShedule/ProjectShedule/Repositories/Implementations/CurriculumSubjectRepository.cs @@ -28,8 +28,8 @@ public class CurriculumSubjectRepository : ICurriculumSubjectRepository using var transaction = connection.BeginTransaction(); var queryInsert = @" - INSERT INTO CurriculumSubject (CurriculumId, Course) - VALUES (@CurriculumId, @Course); + INSERT INTO CurriculumSubject (Date, CurriculumId, Course) + VALUES (@Date, @CurriculumId, @Course); SELECT MAX(Id) FROM CurriculumSubject"; var curriculumSubjectId = connection.QueryFirst(queryInsert, curriculumSubject, transaction); @@ -72,17 +72,19 @@ public class CurriculumSubjectRepository : ICurriculumSubjectRepository throw; } } - public IEnumerable ReadCurriculumSubject() + public IEnumerable ReadCurriculumSubject(DateTime? dateForm = null, DateTime? dateTo = null, int? subjectId = null) { _logger.LogInformation("Получение всех объектов"); try { using var connection = new NpgsqlConnection(_connectionString.ConnectionString); - var querySelect = "SELECT * FROM CurriculumSubject"; - var curriculumSubjects = connection.Query(querySelect); + var querySelect = @"SELECT cs.*, ss.SubjectId, ss.LessonsCount FROM CurriculumSubject cs + INNER JOIN SubjectSubject ss on ss.CurriculumSubjectId = cs.Id"; + var curriculumSubjects = connection.Query(querySelect); _logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(curriculumSubjects)); - return curriculumSubjects; + return curriculumSubjects.GroupBy(x => x.Id, y => y, (key, value) => CurriculumSubject.CreateCurriculumSubject(value.First(), + value.Select(z => SubjectSubject.CreateOperation(0, z.SubjectId, z.LessonsCount)))).ToList(); } catch (Exception ex) { diff --git a/ProjectShedule/ProjectShedule/Repositories/Implementations/SheduleRepository.cs b/ProjectShedule/ProjectShedule/Repositories/Implementations/SheduleRepository.cs index a80aecd..58cc115 100644 --- a/ProjectShedule/ProjectShedule/Repositories/Implementations/SheduleRepository.cs +++ b/ProjectShedule/ProjectShedule/Repositories/Implementations/SheduleRepository.cs @@ -23,8 +23,8 @@ public class SheduleRepository : ISheduleRepository { using var connection = new NpgsqlConnection(_connectionString.ConnectionString); var queryInsert = @" - INSERT INTO Shedule (Date, PairNumber, StudentsGroupId, TeacherId, SubjectId, ClassroomId) - VALUES (@Date, @PairNumber, @StudentsGroupId, @TeacherId, @SubjectId, @ClassroomId)"; + INSERT INTO Shedule (Date, PairNumber, PairCount, StudentsGroupId, TeacherId, SubjectId, ClassroomId) + VALUES (@Date, @PairNumber, @PairCount, @StudentsGroupId, @TeacherId, @SubjectId, @ClassroomId)"; connection.Execute(queryInsert, shedule); } catch (Exception ex) @@ -53,7 +53,7 @@ public class SheduleRepository : ISheduleRepository } } - public IEnumerable ReadShedule(DateTime? dateTime = null, int? groupId = null) + public IEnumerable ReadShedule(DateTime? dateForm = null, DateTime? dateTo = null, int? subjectId = null) { _logger.LogInformation("Получение всех объектов"); try