From c9df0ceb822e3d012f11944e3b22c32aa40b9997 Mon Sep 17 00:00:00 2001 From: Danil Markov Date: Sat, 8 Apr 2023 20:16:42 +0400 Subject: [PATCH] =?UTF-8?q?ReportLogic=20-=20=D0=9F=D0=BE=D1=81=D1=82?= =?UTF-8?q?=D0=B0=D0=B2=D1=89=D0=B8=D0=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BusinessLogics/ReportProviderLogic.cs | 74 ++++++++++++++++++- .../BindingModels/DocumentBindingModel.cs | 1 + .../BindingModels/ReportBindingModel.cs | 1 + .../BindingModels/StreamBindingModel.cs | 2 + .../IReportProviderLogic.cs | 2 +- .../ViewModels/DocumentViewModel.cs | 2 + ...eportStreamStudentEdStatPeriodViewModel.cs | 1 + .../ViewModels/StreamViewModel.cs | 2 + UniversityDataBaseImplemet/Database.cs | 12 +-- .../Implements/DocumentStorage.cs | 38 ++++------ UniversityDataBaseImplemet/Models/Document.cs | 48 +++++++++++- UniversityDataBaseImplemet/Models/Stream.cs | 45 ++++++++++- UniversityModels/Models/IDocumentModel.cs | 1 + UniversityModels/Models/IStreamModel.cs | 2 + 14 files changed, 194 insertions(+), 37 deletions(-) diff --git a/UniversityBusinessLogic/BusinessLogics/ReportProviderLogic.cs b/UniversityBusinessLogic/BusinessLogics/ReportProviderLogic.cs index d0f02c6..0bbc2b9 100644 --- a/UniversityBusinessLogic/BusinessLogics/ReportProviderLogic.cs +++ b/UniversityBusinessLogic/BusinessLogics/ReportProviderLogic.cs @@ -8,35 +8,103 @@ using UniversityContracts.BusinessLogicContracts; using UniversityContracts.ViewModels; using UniversityContracts.SearchModels; using UniversityContracts.StoragesContracts; +using UniversityBusinessLogic.OfficePackage; namespace UniversityBusinessLogic.BusinessLogics { public class ReportProviderLogic : IReportProviderLogic { private readonly IDocumentStorage _documentStorage; + private readonly IStudentStorage _studentStorage; + private readonly IEducationStatusStorage _educationStatusStorage; + private readonly IEducationGroupStorage _educationGroupStorage; + private readonly IDisciplineStorage _disciplineStorage; + private readonly IStreamStorage _streamStorage; + private readonly AbstractSaveToExcelProvider _saveToExcel; + private readonly AbstractSaveToWordProvider _saveToWord; + private readonly AbstractSaveToPdfProvider _saveToPdf; + + public ReportProviderLogic(IDocumentStorage documentStorage, + IStudentStorage studentStorage, + IEducationStatusStorage educationStatusStorage, + IEducationGroupStorage educationGroupStorage, + IDisciplineStorage disciplineStorage, + IStreamStorage streamStorage, + AbstractSaveToExcelProvider saveToExcel, + AbstractSaveToWordProvider saveToWord, + AbstractSaveToPdfProvider saveToPdf) + { + _documentStorage = documentStorage; + _studentStorage = studentStorage; + _educationStatusStorage = educationStatusStorage; + _educationGroupStorage = educationGroupStorage; + _disciplineStorage = disciplineStorage; + _streamStorage = streamStorage; + _saveToExcel = saveToExcel; + _saveToWord = saveToWord; + _saveToPdf = saveToPdf; + } public List GetStudentsDiscipline(ReportBindingModel model) { - throw new NotImplementedException(); + var result = _studentStorage + .GetFilteredList(new StudentSearchModel { UserId = model.UserId }) + .Select(student => new ReportStudentsDisciplineViewModel + { + StudentFIO = $"{student.Name} {student.Surname}", + Disciplines = _streamStorage.GetFilteredList(new StreamSearchModel { UserId = model.UserId }) + .Where(stream => stream.StudentStream.ContainsKey(student.Id)) + .Join(_disciplineStorage.GetFilteredList(new DisciplineSearchModel { UserId = model.UserId }), + stream => stream.Id, + discipline => discipline.StreamId, + (stream, discipline) => discipline.Name) + .ToList() + }) + .ToList(); + + return result; } - public List StreamStudentEdStatPeriod(ReportBindingModel model) + public List GetStreamStudentEdStatPeriod(ReportBindingModel model) { - throw new NotImplementedException(); + var result = _streamStorage + .GetFilteredList(new StreamSearchModel { UserId = model.UserId }) + .Select(stream => new ReportStreamStudentEdStatPeriodViewModel + { + StreamName = stream.Name, + StudentEdStatus = stream.StudentStream + .Where(student => _documentStorage.GetFilteredList(new DocumentSearchModel + { + UserId = model.UserId, + DateFrom = model.DateFrom, + DateTo = model.DateTo, + }) + .Any(document => document.StudentDocument.ContainsKey(student.Value.Id)))//Выбираем студентов, которые есть в приказах за выбранный промежуток времени + .Select(student => ( + StudentFIO: $"{student.Value.Name} {student.Value.Surname}", + EdStatus: _educationStatusStorage.GetFilteredList(new EducationStatusSearchModel { UserId = model.UserId }) + .First(x => x.Id == student.Value.EducationStatusId).Name)) + .ToList() + }) + .ToList(); + return result; } public void SaveBlanksToWordFile(ReportBindingModel model) { throw new NotImplementedException(); + //TODO } public void SaveDocumentBlankToExcelFile(ReportBindingModel model) { throw new NotImplementedException(); + //TODO } public void SaveOrdersToPdfFile(ReportBindingModel model) { throw new NotImplementedException(); + //TODO } } } diff --git a/UniversityContracts/BindingModels/DocumentBindingModel.cs b/UniversityContracts/BindingModels/DocumentBindingModel.cs index 700dc38..edcd72c 100644 --- a/UniversityContracts/BindingModels/DocumentBindingModel.cs +++ b/UniversityContracts/BindingModels/DocumentBindingModel.cs @@ -13,5 +13,6 @@ namespace UniversityContracts.BindingModels public string Name { get; set; } = string.Empty; public DateTime Date { get; set; } = DateTime.Now; public int UserId { get; set; } + public Dictionary StudentDocument { get; set; } = new(); } } diff --git a/UniversityContracts/BindingModels/ReportBindingModel.cs b/UniversityContracts/BindingModels/ReportBindingModel.cs index e1bcee0..f626497 100644 --- a/UniversityContracts/BindingModels/ReportBindingModel.cs +++ b/UniversityContracts/BindingModels/ReportBindingModel.cs @@ -11,5 +11,6 @@ namespace UniversityContracts.BindingModels public string FileName { get; set; } = string.Empty; public DateTime? DateFrom { get; set; } public DateTime? DateTo { get; set; } + public int? UserId { get; set; } } } diff --git a/UniversityContracts/BindingModels/StreamBindingModel.cs b/UniversityContracts/BindingModels/StreamBindingModel.cs index 5bb6aff..b60cbdb 100644 --- a/UniversityContracts/BindingModels/StreamBindingModel.cs +++ b/UniversityContracts/BindingModels/StreamBindingModel.cs @@ -16,5 +16,7 @@ namespace UniversityContracts.BindingModels public int UserId { get; set; } public int Id { get; set; } + public Dictionary StudentStream { get; set; } = new(); + } } diff --git a/UniversityContracts/BusinessLogicContracts/IReportProviderLogic.cs b/UniversityContracts/BusinessLogicContracts/IReportProviderLogic.cs index 96131e2..25a5126 100644 --- a/UniversityContracts/BusinessLogicContracts/IReportProviderLogic.cs +++ b/UniversityContracts/BusinessLogicContracts/IReportProviderLogic.cs @@ -12,7 +12,7 @@ namespace UniversityContracts.BusinessLogicContracts { List GetStudentsDiscipline(ReportBindingModel model); - List StreamStudentEdStatPeriod(ReportBindingModel model); + List GetStreamStudentEdStatPeriod(ReportBindingModel model); void SaveBlanksToWordFile(ReportBindingModel model); diff --git a/UniversityContracts/ViewModels/DocumentViewModel.cs b/UniversityContracts/ViewModels/DocumentViewModel.cs index c74461c..b65f686 100644 --- a/UniversityContracts/ViewModels/DocumentViewModel.cs +++ b/UniversityContracts/ViewModels/DocumentViewModel.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.ComponentModel; using System.Linq; +using System.Reflection; using System.Text; using System.Threading.Tasks; using UniversityModels.Models; @@ -16,5 +17,6 @@ namespace UniversityContracts.ViewModels public string Name { get; set; } = string.Empty; [DisplayName("Дата создания документа")] public DateTime Date { get; set; } = DateTime.Now; + public Dictionary StudentDocument { get; set; } = new(); } } diff --git a/UniversityContracts/ViewModels/ReportStreamStudentEdStatPeriodViewModel.cs b/UniversityContracts/ViewModels/ReportStreamStudentEdStatPeriodViewModel.cs index a743e16..3871b27 100644 --- a/UniversityContracts/ViewModels/ReportStreamStudentEdStatPeriodViewModel.cs +++ b/UniversityContracts/ViewModels/ReportStreamStudentEdStatPeriodViewModel.cs @@ -8,6 +8,7 @@ namespace UniversityContracts.ViewModels { public class ReportStreamStudentEdStatPeriodViewModel { + public int Id { get; set; } public string StreamName { get; set; } = string.Empty; public List<(string StudentFIO, string EdStatus)> StudentEdStatus { get; set; } = new(); } diff --git a/UniversityContracts/ViewModels/StreamViewModel.cs b/UniversityContracts/ViewModels/StreamViewModel.cs index 412e6e5..44053bf 100644 --- a/UniversityContracts/ViewModels/StreamViewModel.cs +++ b/UniversityContracts/ViewModels/StreamViewModel.cs @@ -4,6 +4,7 @@ using System.ComponentModel; using System.Linq; using System.Text; using System.Threading.Tasks; +using UniversityModels.Models; namespace UniversityContracts.ViewModels { @@ -15,5 +16,6 @@ namespace UniversityContracts.ViewModels public string Name { get; set; } = string.Empty; [DisplayName("Номер курса")] public int Course { get; set; } + public Dictionary StudentStream { get; set; } = new(); } } diff --git a/UniversityDataBaseImplemet/Database.cs b/UniversityDataBaseImplemet/Database.cs index af816dd..a9a5f1c 100644 --- a/UniversityDataBaseImplemet/Database.cs +++ b/UniversityDataBaseImplemet/Database.cs @@ -1,10 +1,6 @@ using Microsoft.EntityFrameworkCore; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; using UniversityDataBaseImplemet.Models; +using Stream = UniversityDataBaseImplemet.Models.Stream; namespace UniversityDataBaseImplemet { @@ -20,9 +16,15 @@ namespace UniversityDataBaseImplemet } public virtual DbSet User { get; set; } public virtual DbSet Documents { get; set; } + public virtual DbSet Discipline { get; set; } public virtual DbSet EducationStatuses { get; set; } + public virtual DbSet EducationGroups { get; set; } + public virtual DbSet EducationGroupsDocuments { get; set; } + public virtual DbSet EducationGroupsStreams { get; set; } + public virtual DbSet Streams { get; set; } public virtual DbSet Students { get; set; } public virtual DbSet StudentDocuments { get; set; } + public virtual DbSet StudentStreams { get; set; } public virtual DbSet Roles { get; set; } } } diff --git a/UniversityDataBaseImplemet/Implements/DocumentStorage.cs b/UniversityDataBaseImplemet/Implements/DocumentStorage.cs index 07ddccd..d09df82 100644 --- a/UniversityDataBaseImplemet/Implements/DocumentStorage.cs +++ b/UniversityDataBaseImplemet/Implements/DocumentStorage.cs @@ -1,8 +1,10 @@ using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; using System; using System.Collections.Generic; using System.Linq; using System.Net.Http.Headers; +using System.Security.Cryptography.X509Certificates; using System.Text; using System.Threading.Tasks; using UniversityContracts.BindingModels; @@ -39,33 +41,19 @@ namespace UniversityDataBaseImplemet.Implements .Select(record => record.GetViewModel) .ToList(); } - else if (model.DateFrom != null && model.DateTo != null && model.UserId.HasValue) // фильтрация для отчета#2 Поставщик + else if (model.DateFrom != null && model.DateTo != null && model.UserId.HasValue) { return context.Documents - .Where(d => d.Date >= model.DateFrom && d.Date <= model.DateTo) - .Join(context.StudentDocuments, - doc => doc.Id, - studDoc => studDoc.DocumentId, - (doc, studDoc) => new { Document = doc, StudentDocument = studDoc }) - .Join(context.Students, - studDoc => studDoc.StudentDocument.StudentId, - stud => stud.Id, - (studDoc, stud) => new { studDoc.Document, Student = stud }) - .Include(record => record.Student.EducationStatus) - .Join(context.StudentStreams, - studEdu => studEdu.Student.Id, - studStream => studStream.StudentId, - (studEdu, studStream) => new { studEdu.Document, studEdu.Student, studEdu.EducationStatus, StudentStream = studStream }) - .Join(context.Streams, - studStream => studStream.StudentStream.StreamId, - stream => stream.Id, - (studStream, stream) => new { Stream = stream.Name, studStream.Student, studStream.EducationStatus.Name }) - .Select(result => new { - result.Stream, - result.Student, - result.EducationStatus - }) - .ToList(); + .Where(d => d.Date >= model.DateFrom && d.Date <= model.DateTo && d.UserId == model.UserId) + .Include(d => d.Students) + .ThenInclude(sd => sd.Student) + .ThenInclude(s => s.StudentStream) + .ThenInclude(ss => ss.Stream) + .Include(d => d.Students) + .ThenInclude(sd => sd.Student) + .ThenInclude(s => s.EducationStatus) + .Select(result => result.GetViewModel) + .ToList(); } else if (model.UserId.HasValue) { diff --git a/UniversityDataBaseImplemet/Models/Document.cs b/UniversityDataBaseImplemet/Models/Document.cs index 700e956..bfeedcd 100644 --- a/UniversityDataBaseImplemet/Models/Document.cs +++ b/UniversityDataBaseImplemet/Models/Document.cs @@ -25,7 +25,19 @@ namespace UniversityDataBaseImplemet.Models [ForeignKey("DocumentId")] public virtual List EducationGroupDocument { get; set; } = new(); public virtual User User { get; set; } - + private Dictionary? _studentDocument = null; + [NotMapped] + public Dictionary StudentDocument + { + get + { + if (_studentDocument == null) + { + _studentDocument = Students.ToDictionary(rec => rec.StudentId, rec => rec.Student as IStudentModel); + } + return _studentDocument; + } + } public static Document? Create(DocumentBindingModel model) { return new Document() @@ -46,12 +58,44 @@ namespace UniversityDataBaseImplemet.Models Date = model.Date; UserId = model.UserId; } + public void UpdateStudents(Database context, DocumentBindingModel model) + { + var studentDocument = context.StudentDocuments + .Where(rec => rec.DocumentId == model.Id) + .ToList(); + if (studentDocument != null) + { + context.StudentDocuments + .RemoveRange(studentDocument + .Where(rec => !model.StudentDocument + .ContainsKey(rec.StudentId)) + ); + context.SaveChanges(); + var document = context.Documents + .First(x => x.Id == Id); + foreach (var sd in studentDocument) + { + model.StudentDocument.Remove(sd.StudentId); + } + foreach (var sd in model.StudentDocument) + { + context.StudentDocuments.Add(new StudentDocument + { + Document = document, + Student = context.Students.First(x => x.Id == sd.Key), + }); + context.SaveChanges(); + } + _studentDocument = null; + } + } public DocumentViewModel GetViewModel => new() { Id = Id, Name = Name, Date = Date, - UserId = UserId, + UserId = UserId, + StudentDocument = StudentDocument }; } } diff --git a/UniversityDataBaseImplemet/Models/Stream.cs b/UniversityDataBaseImplemet/Models/Stream.cs index 0781469..863667d 100644 --- a/UniversityDataBaseImplemet/Models/Stream.cs +++ b/UniversityDataBaseImplemet/Models/Stream.cs @@ -23,7 +23,19 @@ namespace UniversityDataBaseImplemet.Models [ForeignKey("StreamId")] public virtual List StreamStudents { get; set; } = new(); public virtual User User { get; set; } - + private Dictionary? _studentStream = null; + [NotMapped] + public Dictionary StudentStream + { + get + { + if (_studentStream == null) + { + _studentStream = StreamStudents.ToDictionary(rec => rec.StudentId, rec => rec.Student as IStudentModel); + } + return _studentStream; + } + } public static Stream Create(StreamBindingModel model) { return new Stream() @@ -40,6 +52,37 @@ namespace UniversityDataBaseImplemet.Models Course = model.Course; UserId = model.UserId; } + public void UpdateStreamStudents(Database context, StreamBindingModel model) + { + var studentStream = context.StudentStreams + .Where(rec => rec.StreamId == model.Id) + .ToList(); + if (studentStream != null) + { + context.StudentStreams + .RemoveRange(studentStream + .Where(rec => !model.StudentStream + .ContainsKey(rec.StudentId)) + ); + context.SaveChanges(); + var stream = context.Streams + .First(x => x.Id == Id); + foreach (var sd in studentStream) + { + model.StudentStream.Remove(sd.StudentId); + } + foreach (var sd in model.StudentStream) + { + context.StudentStreams.Add(new StudentStream + { + Stream = stream, + Student = context.Students.First(x => x.Id == sd.Key), + }); + context.SaveChanges(); + } + _studentStream = null; + } + } public StreamViewModel GetViewModel => new() { Id = Id, diff --git a/UniversityModels/Models/IDocumentModel.cs b/UniversityModels/Models/IDocumentModel.cs index 408b787..ad58bb5 100644 --- a/UniversityModels/Models/IDocumentModel.cs +++ b/UniversityModels/Models/IDocumentModel.cs @@ -11,5 +11,6 @@ namespace UniversityModels.Models string Name { get; } DateTime Date { get; } int UserId { get; } + Dictionary StudentDocument { get; } } } diff --git a/UniversityModels/Models/IStreamModel.cs b/UniversityModels/Models/IStreamModel.cs index 4a69809..4243e5b 100644 --- a/UniversityModels/Models/IStreamModel.cs +++ b/UniversityModels/Models/IStreamModel.cs @@ -11,5 +11,7 @@ namespace UniversityModels.Models string Name { get; } int Course { get; } int UserId { get; } + + Dictionary StudentStream { get; } } }