Написана (почти) бизнес логика для Поставщик. Нужна логика для отчетов

This commit is contained in:
Danil Markov 2023-04-07 17:34:20 +04:00
parent f5c1abd6ed
commit 13ed86ece8
21 changed files with 528 additions and 39 deletions

View File

@ -0,0 +1,101 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UniversityContracts.BindingModels;
using UniversityContracts.BusinessLogicContracts;
using UniversityContracts.SearchModels;
using UniversityContracts.StoragesContracts;
using UniversityContracts.ViewModels;
namespace UniversityBusinessLogic.BusinessLogics
{
public class DocumentLogic : IDocumentLogic
{
private readonly IDocumentStorage _documentStorage;
public DocumentLogic(IDocumentStorage documentStorage)
{
_documentStorage = documentStorage;
}
public bool Create(DocumentBindingModel model)
{
CheckModel(model);
if(_documentStorage.Insert(model) == null)
{
return false;
}
return true;
}
public bool Update(DocumentBindingModel model)
{
CheckModel(model, false);
if (_documentStorage.Update(model) == null)
{
return false;
}
return true;
}
public bool Delete(DocumentBindingModel model)
{
CheckModel(model, false);
if (_documentStorage.Delete(model) == null)
{
return false;
}
return true;
}
public DocumentViewModel? ReadElement(DocumentSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
var document = _documentStorage.GetElement(model);
if (document == null)
{
return null;
}
return document;
}
public List<DocumentViewModel>? ReadList(DocumentSearchModel? model)
{
var list = model == null ? _documentStorage.GetFullList() : _documentStorage.GetFilteredList(model);
if (list == null)
{
return null;
}
return list;
}
private void CheckModel(DocumentBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.Name))
{
throw new ArgumentNullException("Нет названия документа", nameof(model.Name));
}
if (model.Date > DateTime.Now)
{
throw new ArgumentNullException("Неверно указана дата", nameof(model.Name));
}
var document = _documentStorage.GetElement(new DocumentSearchModel
{
Name = model.Name,
});
if (document != null && document.Id != model.Id)
{
throw new InvalidOperationException("Приказ с таким названием уже есть");
}
}
}
}

View File

@ -0,0 +1,99 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UniversityContracts.BindingModels;
using UniversityContracts.BusinessLogicContracts;
using UniversityContracts.SearchModels;
using UniversityContracts.StoragesContracts;
using UniversityContracts.ViewModels;
namespace UniversityBusinessLogic.BusinessLogics
{
public class EducationStatusLogic : IEducationStatusLogic
{
private readonly IEducationStatusStorage _esStorage;
public EducationStatusLogic(IEducationStatusStorage cardStorage)
{
_esStorage = cardStorage;
}
public bool Create(EducationStatusBindingModel model)
{
CheckModel(model);
if (_esStorage.Insert(model) == null)
{
return false;
}
return true;
}
public bool Update(EducationStatusBindingModel model)
{
CheckModel(model);
if (_esStorage.Update(model) == null)
{
return false;
}
return true;
}
public bool Delete(EducationStatusBindingModel model)
{
CheckModel(model, false);
if (_esStorage.Delete(model) == null)
{
return false;
}
return true;
}
public EducationStatusViewModel? ReadElement(EducationStatusSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
var es = _esStorage.GetElement(model);
if (es == null)
{
return null;
}
return es;
}
public List<EducationStatusViewModel>? ReadList(EducationStatusSearchModel? model)
{
var list = model == null ? _esStorage.GetFullList() : _esStorage.GetFilteredList(model);
if (list == null)
{
return null;
}
return list;
}
private void CheckModel(EducationStatusBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.Name))
{
throw new ArgumentNullException("Нет названия статуса обучения", nameof(model.Name));
}
var es = _esStorage.GetElement(new EducationStatusSearchModel
{
Name = model.Name,
});
if (es != null && es.Id != model.Id)
{
throw new InvalidOperationException("Статус с таким названием уже есть");
}
}
}
}

View File

@ -0,0 +1,42 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UniversityContracts.BindingModels;
using UniversityContracts.BusinessLogicContracts;
using UniversityContracts.ViewModels;
using UniversityContracts.SearchModels;
using UniversityContracts.StoragesContracts;
namespace UniversityBusinessLogic.BusinessLogics
{
public class ReportProviderLogic : IReportProviderLogic
{
private readonly IDocumentStorage _documentStorage;
public List<ReportStudentsDisciplineViewModel> GetStudentsDiscipline()
{
throw new NotImplementedException();
}
public List<ReportStreamStudentEdStatPeriodViewModel> StreamStudentEdStatPeriod(ReportBindingModel model)
{
}
public void SaveBlanksToWordFile(ReportBindingModel model)
{
throw new NotImplementedException();
}
public void SaveDocumentBlankToExcelFile(ReportBindingModel model)
{
throw new NotImplementedException();
}
public void SaveOrdersToPdfFile(ReportBindingModel model)
{
throw new NotImplementedException();
}
}
}

View File

@ -0,0 +1,103 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UniversityContracts.BindingModels;
using UniversityContracts.BusinessLogicContracts;
using UniversityContracts.SearchModels;
using UniversityContracts.StoragesContracts;
using UniversityContracts.ViewModels;
namespace UniversityBusinessLogic.BusinessLogics
{
public class StudentLogic : IStudentLogic
{
private readonly IStudentStorage _studentStorage;
public StudentLogic(IStudentStorage studentStorage)
{
_studentStorage = studentStorage;
}
public bool Create(StudentBindingModel model)
{
CheckModel(model);
if (_studentStorage.Insert(model) == null)
{
return false;
}
return true;
}
public bool Update(StudentBindingModel model)
{
CheckModel(model, false);
if (_studentStorage.Update(model) == null)
{
return false;
}
return true;
}
public bool Delete(StudentBindingModel model)
{
CheckModel(model, false);
if (_studentStorage.Delete(model) == null)
{
return false;
}
return true;
}
public StudentViewModel? ReadElement(StudentSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
var student = _studentStorage.GetElement(model);
if (student == null)
{
return null;
}
return student;
}
public List<StudentViewModel>? ReadList(StudentSearchModel? model)
{
var list = model == null ? _studentStorage.GetFullList() : _studentStorage.GetFilteredList(model);
if (list == null)
{
return null;
}
return list;
}
private void CheckModel(StudentBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.Name))
{
throw new ArgumentNullException("Нет названия документа", nameof(model.Name));
}
if (model.StudentCard <= 0)
{
throw new ArgumentNullException("Неверно указан номер студенческого билета", nameof(model.Name));
}
var student = _studentStorage.GetElement(new StudentSearchModel
{
StudentCard = model.StudentCard,
});
if (student != null && student.Id != model.Id)
{
throw new InvalidOperationException("Приказ с таким названием уже есть");
}
}
}
}

View File

@ -1,5 +0,0 @@
namespace UniversityBusinessLogic;
public class Class1
{
}

View File

@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace UniversityBusinessLogic.OfficePackage
{
public abstract class AbstractSaveToExcelProvider
{
}
}

View File

@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace UniversityBusinessLogic.OfficePackage
{
public abstract class AbstractSaveToPdfProvider
{
}
}

View File

@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace UniversityBusinessLogic.OfficePackage
{
public abstract class AbstractSaveToWordProvider
{
}
}

View File

@ -6,4 +6,14 @@
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\UniversityContracts\UniversityContracts.csproj" />
</ItemGroup>
<ItemGroup>
<Folder Include="OfficePackage\HelperEnums\" />
<Folder Include="OfficePackage\HelperModels\" />
<Folder Include="OfficePackage\Implements\" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace UniversityContracts.BindingModels
{
public class ReportBindingModel
{
public string FileName { get; set; } = string.Empty;
public DateTime? DateFrom { get; set; }
public DateTime? DateTo { get; set; }
}
}

View File

@ -4,6 +4,8 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UniversityContracts.BindingModels;
using UniversityContracts.SearchModels;
using UniversityContracts.ViewModels;
namespace UniversityContracts.BusinessLogicContracts
{

View File

@ -4,6 +4,8 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UniversityContracts.BindingModels;
using UniversityContracts.SearchModels;
using UniversityContracts.ViewModels;
namespace UniversityContracts.BusinessLogicContracts
{

View File

@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UniversityContracts.BindingModels;
using UniversityContracts.ViewModels;
namespace UniversityContracts.BusinessLogicContracts
{
public interface IReportProviderLogic
{
List<ReportStudentsDisciplineViewModel> GetStudentsDiscipline();
List<ReportStreamStudentEdStatPeriodViewModel> StreamStudentEdStatPeriod(ReportBindingModel model);
void SaveBlanksToWordFile(ReportBindingModel model);
void SaveDocumentBlankToExcelFile(ReportBindingModel model);
void SaveOrdersToPdfFile(ReportBindingModel model);
}
}

View File

@ -4,6 +4,8 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UniversityContracts.BindingModels;
using UniversityContracts.SearchModels;
using UniversityContracts.ViewModels;
namespace UniversityContracts.BusinessLogicContracts
{

View File

@ -9,6 +9,7 @@ namespace UniversityContracts.SearchModels
public class DocumentSearchModel
{
public int? Id { get; set; }
public string? Name { get; set; }
public int? UserId { get; set; }
}
}

View File

@ -9,6 +9,9 @@ namespace UniversityContracts.SearchModels
public class StudentSearchModel
{
public int? Id { get; set; }
public int? StudentCard { get; set; }
public int? UserId { get; set; }
public DateTime? DateFrom { get; set; }
public DateTime? DateTo { get; set; }
}
}

View File

@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace UniversityContracts.ViewModels
{
public class ReportStreamStudentEdStatPeriodViewModel
{
public string StreamName { get; set; } = string.Empty;
public List<(string StudentFIO, string EdStatus)> StudentEdStatus { get; set; } = new();
}
}

View File

@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace UniversityContracts.ViewModels
{
public class ReportStudentsDisciplineViewModel
{
public string StudentFIO { get; set; } = string.Empty;
public List<string> Disciplines { get; set; } = new();
}
}

View File

@ -24,8 +24,9 @@ namespace UniversityDataBaseImplemet.Implements
using var context = new Database();
return context.Documents
.Include(record => record.User)
.FirstOrDefault(record => record.Id == model.Id)?
.GetViewModel;
.FirstOrDefault(record => record.Id == model.Id
|| record.Name.Equals(model.Name))
?.GetViewModel;
}
public List<DocumentViewModel> GetFilteredList(DocumentSearchModel model)
{
@ -33,7 +34,7 @@ namespace UniversityDataBaseImplemet.Implements
if (model.Id.HasValue)
{
return context.Documents
.Include(x => x.User)
.Include(record => record.User)
.Where(record => record.Id.Equals(model.Id))
.Select(record => record.GetViewModel)
.ToList();
@ -41,9 +42,9 @@ namespace UniversityDataBaseImplemet.Implements
else if (model.UserId.HasValue)
{
return context.Documents
.Include(x => x.User)
.Where(x => x.UserId == model.UserId)
.Select(x => x.GetViewModel)
.Include(record => record.User)
.Where(record => record.UserId == model.UserId)
.Select(record => record.GetViewModel)
.ToList();
}
else
@ -78,7 +79,7 @@ namespace UniversityDataBaseImplemet.Implements
try
{
var document = context.Documents
.Include(x => x.User)
.Include(record => record.User)
.FirstOrDefault(record => record.Id.Equals(model.Id));
if (document == null)
{
@ -99,7 +100,7 @@ namespace UniversityDataBaseImplemet.Implements
{
using var context = new Database();
var document = context.Documents
.Include(x => x.User)
.Include(record => record.User)
.FirstOrDefault(record => record.Id.Equals(model.Id));
if (document == null)
{

View File

@ -22,9 +22,10 @@ namespace UniversityDataBaseImplemet.Implements
}
using var context = new Database();
return context.EducationStatuses
.Include(x => x.User)
.FirstOrDefault(record => record.Id == model.Id)?
.GetViewModel;
.Include(record => record.User)
.FirstOrDefault(record => record.Id == model.Id
|| record.Name.Equals(model.Name))
?.GetViewModel;
}
public List<EducationStatusViewModel> GetFilteredList(EducationStatusSearchModel model)
{
@ -32,7 +33,7 @@ namespace UniversityDataBaseImplemet.Implements
if (model.Id.HasValue)
{
return context.EducationStatuses
.Include(x => x.User)
.Include(record => record.User)
.Where(record => record.Id.Equals(model.Id))
.Select(record => record.GetViewModel)
.ToList();
@ -40,9 +41,9 @@ namespace UniversityDataBaseImplemet.Implements
else if (model.UserId.HasValue)
{
return context.EducationStatuses
.Include(x => x.User)
.Where(x => x.UserId == model.UserId)
.Select(x => x.GetViewModel)
.Include(record => record.User)
.Where(record => record.UserId == model.UserId)
.Select(record => record.GetViewModel)
.ToList();
}
else
@ -54,7 +55,7 @@ namespace UniversityDataBaseImplemet.Implements
{
using var context = new Database();
return context.EducationStatuses
.Include(x => x.User)
.Include(record => record.User)
.Select(record => record.GetViewModel)
.ToList();
}
@ -69,8 +70,8 @@ namespace UniversityDataBaseImplemet.Implements
context.EducationStatuses.Add(newEducationStatus);
context.SaveChanges();
return context.EducationStatuses
.Include(x => x.User)
.FirstOrDefault(x => x.Id.Equals(newEducationStatus.Id))
.Include(record => record.User)
.FirstOrDefault(record => record.Id.Equals(newEducationStatus.Id))
?.GetViewModel;
}
public EducationStatusViewModel? Update(EducationStatusBindingModel model)
@ -80,7 +81,7 @@ namespace UniversityDataBaseImplemet.Implements
try
{
var educationStatus = context.EducationStatuses
.Include(x => x.User)
.Include(record => record.User)
.FirstOrDefault(record => record.Id.Equals(model.Id));
if (educationStatus == null)
{
@ -101,7 +102,7 @@ namespace UniversityDataBaseImplemet.Implements
{
using var context = new Database();
var educationStatus = context.EducationStatuses
.Include(x => x.User)
.Include(record => record.User)
.FirstOrDefault(record => record.Id.Equals(model.Id));
if (educationStatus == null)
{

View File

@ -1,6 +1,7 @@
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
@ -17,7 +18,7 @@ namespace UniversityDataBaseImplemet.Implements
{
public StudentViewModel? GetElement(StudentSearchModel model)
{
if (!model.Id.HasValue)
if (!model.Id.HasValue && model.StudentCard <= 0)
{
return null;
}
@ -25,7 +26,8 @@ namespace UniversityDataBaseImplemet.Implements
return context.Students
.Include(record => record.User)
.Include(record => record.EducationStatus)
.FirstOrDefault(record => record.Id.Equals(model.Id))
.FirstOrDefault(record => record.Id.Equals(model.Id)
|| record.StudentCard.Equals(model.StudentCard))
?.GetViewModel;
}
public List<StudentViewModel> GetFilteredList(StudentSearchModel model)
@ -35,7 +37,7 @@ namespace UniversityDataBaseImplemet.Implements
{
return context.Students
.Include(record => record.EducationStatus)
.Include(x => x.User)
.Include(record => record.User)
.Where(record => record.Id.Equals(model.Id))
.Select(record => record.GetViewModel)
.ToList();
@ -43,12 +45,35 @@ namespace UniversityDataBaseImplemet.Implements
else if (model.UserId.HasValue)
{
return context.Students
.Include(x => x.EducationStatus)
.Include(x => x.User)
.Where(x => x.UserId == model.UserId)
.Select(x => x.GetViewModel)
.Include(record => record.EducationStatus)
.Include(record => record.User)
.Where(record => record.UserId == model.UserId)
.Select(record => record.GetViewModel)
.ToList();
}
/*else if (model.DateFrom != null && model.DateTo != null) // для отчета#2 Поставщик
{
return context.Students
.Include(record => record.User)
.Include(record => record.EducationStatus)
.Join(context.StudentStreams, student => student.Id, studentStream => studentStream.StudentId, (student, studentStream) => new { Student = student, StreamId = studentStream.StreamId })
.Join(context.Streams, studentStream => studentStream.StreamId, stream => stream.Id, (studentStream, stream) => new { Student = studentStream.Student, Stream = stream })
.GroupBy(record => record.Stream)
.Select(result => new
{
Stream = result.Key,
Students = result.Select(record => (record.Student, record.EducationStatus)).ToList()
})
.ToList();
var result = context.Students
.Include(student => student.User)
.Include(student => student.StudentStreams)
.ThenInclude(studentStream => studentStream.Stream)
.ThenInclude(stream => stream.Disciplines)
.SelectMany(student => student.StudentStreams, (student, studentStream) => new { Student = student, Stream = studentStream.Stream })
.Select(record => new { StudentName = record.Student.User.Name + " " + record.Student.User.Surname, Discipline = record.Stream.Disciplines })
.ToList();
}*/
else
{
return new();
@ -58,8 +83,8 @@ namespace UniversityDataBaseImplemet.Implements
{
using var context = new Database();
return context.Students
.Include(x => x.User)
.Include(x => x.EducationStatus)
.Include(record => record.User)
.Include(record => record.EducationStatus)
.Select(record => record.GetViewModel)
.ToList();
}
@ -82,8 +107,8 @@ namespace UniversityDataBaseImplemet.Implements
try
{
var student = context.Students
.Include(x => x.User)
.Include(x => x.EducationStatus)
.Include(record => record.User)
.Include(record => record.EducationStatus)
.FirstOrDefault(record => record.Id.Equals(model.Id));
if (student == null)
{
@ -104,8 +129,8 @@ namespace UniversityDataBaseImplemet.Implements
{
using var context = new Database();
var student = context.Students
.Include(x => x.User)
.Include(x => x.EducationStatus)
.Include(record => record.User)
.Include(record => record.EducationStatus)
.FirstOrDefault(record => record.Id.Equals(model.Id));
if (student == null)
{