using DepartmentContract.BindingModels; using DepartmentContract.Logics.IGenericEntityLogic; using DepartmentContract.Services.IGenericEntityService; using DepartmentContract.ViewModels; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Threading.Tasks; using ToolsModule.ManagmentDependency; using ToolsModule.ManagmentEntity; using ToolsModule.ManagmentExtension; using ToolsModule.ManagmentSecurity; using ToolsOffice.Interfaces; using ToolsOffice.Interfaces.Word; using ToolsOffice.Interfaces.Word.Models; using ToolsOffice.Interfaces.Word.Models.Cases; namespace DepartmentBusinessLogic.BusinessLogics.GenericBusinessLogic { /// /// Логика работы с учебными группами /// public class StudentGroupBusinessLogic : GenericBusinessLogic, IStudentGroupLogic { public StudentGroupBusinessLogic(IStudentGroupService service) : base(service, "Учебные Группы", AccessOperation.УчебныеГруппы) { } public async Task SaveToWord(StudentGroupSaveToWordBindingModel model) { if (model == null) { throw new ArgumentNullException(nameof(model)); } if (model.FileName.IsEmpty()) { throw new ArgumentNullException("Имя файла", nameof(model.FileName)); } var builderWordDocument = DependencyManager.Instance.Resolve(); if (builderWordDocument == null) { throw new ArgumentNullException("Сервис генерации документов", nameof(builderWordDocument)); } var studentLogic = DependencyManager.Instance.Resolve(); if (studentLogic == null) { throw new ArgumentNullException("Сервис студентов", nameof(studentLogic)); } var studetns = studentLogic.GetList(new StudentGetBindingModel { StudentGroupId = model.StudentGroupId }); if (studetns == null) { throw new InvalidOperationException(string.Join(Environment.NewLine, studentLogic.Errors.Select(x => x.Message))); } var studentGroup = GetElement(new StudentGroupGetBindingModel { Id = model.StudentGroupId }); if (studentGroup == null) { throw new InvalidOperationException(string.Join(Environment.NewLine, Errors.Select(x => x.Message))); } var table = new ModelWordTable() { RowsHeight = new Dictionary(), BorderType = TypeWordTableBorder.Single, BorderWidth = 4, Headers = new List<(int ColumnIndex, int RowIndex, string Header)>(), ColumnWidths = new Dictionary(), Data = new List(), LookFirstRow = true, LookFirstColumn = true }; int columnIndex = 0; table.Headers.Add((columnIndex, 0, "№")); table.ColumnWidths.Add(columnIndex, 1); columnIndex++; if (model.ShowNumberOfBook) { table.Headers.Add((columnIndex, 0, "Номер зачетной книжки")); table.ColumnWidths.Add(columnIndex, 3); columnIndex++; } if (model.UnionFIO) { table.Headers.Add((columnIndex, 0, "ФИО")); table.ColumnWidths.Add(columnIndex, 5); columnIndex++; } else { table.Headers.Add((columnIndex, 0, "Фамилия")); table.ColumnWidths.Add(columnIndex, 5); columnIndex++; table.Headers.Add((columnIndex, 0, "Имя")); table.ColumnWidths.Add(columnIndex, 5); columnIndex++; table.Headers.Add((columnIndex, 0, "Отчество")); table.ColumnWidths.Add(columnIndex, 5); columnIndex++; } if (model.ShowStatus) { table.Headers.Add((columnIndex, 0, "Статус")); table.ColumnWidths.Add(columnIndex, 3); columnIndex++; } if (model.ShowInfo) { table.Headers.Add((columnIndex, 0, "Информация")); table.ColumnWidths.Add(columnIndex, 15); columnIndex++; } table.RowsHeight.Add(0, 1.5); int rowIndex = 0; foreach(var student in studetns.List) { var listOfData = new List() { (rowIndex + 1).ToString() }; if (model.ShowNumberOfBook) { listOfData.Add(student.NumberOfBook); } if (model.UnionFIO) { if (model.UseInitials) { listOfData.Add($"{student.LastName}{(student.FirstName.Length > 0 ? $" {student.FirstName[0]}." : "")}{(student.Patronymic.Length > 0 ? $" {student.Patronymic[0]}." : "")}"); } else { listOfData.Add($"{student.LastName} {student.FirstName} {student.Patronymic}"); } } else { listOfData.Add(student.LastName); listOfData.Add(student.FirstName); listOfData.Add(student.Patronymic); } if (model.ShowStatus) { listOfData.Add(student.StudentStateTitle); } if (model.ShowInfo) { listOfData.Add(student.Description); } table.Data.Add(listOfData.ToArray()); table.RowsHeight.Add(rowIndex + 1, 1); rowIndex++; } var pageSize = new WordPageSizes("А4"); var docModel = new ModelWordDocumentWithHeaderAndTable { Document = new ModelWordDocument { WordPageOrientation = TypeWordPageOrientation.Landscape, PageSizeHeight = pageSize.PageSizeWidth, PageSizeWidth = pageSize.PageSizeHeight, PageMarginBottom = pageSize.PageMarginBottom, PageMarginTop = pageSize.PageMarginTop, PageMarginLeft = pageSize.PageMarginLeft, PageMarginRight = pageSize.PageMarginRight, PageMarginGutter = pageSize.PageMarginGutter, PageHeader = pageSize.PageHeader, PageFooter = pageSize.PageFooter }, Header = new ModelWordParagraph { FontName = FontNames.TimesNewRoman, FontSize = 16, IsBold = true, JustificationType = TypeWordJustification.Center, Text = studentGroup.ToString(), SpacingBetweenLinesAfter = 10 }, Table = table }; var stream = builderWordDocument.CreateDocumentWithTable(docModel); if (stream == null) { throw new InvalidOperationException("Не удалось получить поток с документом"); } stream.Position = 0; using (var saveStream = new FileStream(model.FileName, FileMode.OpenOrCreate)) { await stream.CopyToAsync(saveStream); } stream.Close(); return true; } } }