using ComponentsLibraryNet60.Models; using ControlsLibraryNet60.Data; using ControlsLibraryNet60.Models; using PluginsConventionLibrary; using PortalAccountsBusinessLogic.BusinessLogics; using PortalAccountsContracts.BindingModels; using PortalAccountsContracts.BusinessLogicsContracts; using PortalAccountsContracts.ViewModels; using PortalAccountsDatabaseImplement.Implements; using RodionovLibrary.NonVisualComponents.HelperModels; using System.Text; namespace PortalAccountsView { public class PluginsConvention : IPluginsConvention { private readonly IAccountLogic _accountLogic; private readonly IRoleLogic _roleLogic; private readonly ControlDataTreeTable _controlDataTreeTable = new(); private readonly RodionovLibrary.NonVisualComponents.WordLongTextComponent _wordLongTextComponent = new(); private readonly ComponentsLibraryNet60.DocumentWithTable.ComponentDocumentWithTableMultiHeaderPdf _componentDocumentWithTableMultiHeaderPdf = new(); private readonly ComponentsLibraryNet60.DocumentWithChart.ComponentDocumentWithChartPieExcel _componentDocumentWithChartPieExcel = new(); public PluginsConvention() { _accountLogic = new AccountLogic(new AccountStorage()); _roleLogic = new RoleLogic(new RoleStorage()); ReloadData(); } public string PluginName => "LabWork3 Plugin"; public UserControl GetControl => _controlDataTreeTable; public PluginsConventionElement GetElement { get { var selected = _controlDataTreeTable.GetSelectedObject() ?? throw new Exception("Не удалось получить выбранный элемент"); return new PluginsConventionAccount() { Id = IntToGuid(selected.Id), Login = selected.Login, Warnings = selected.Warnings, RoleName = selected.RoleName, Rating = selected.OutputRating }; } } public Form GetForm(PluginsConventionElement element) { var formAccount = new FormAccount(_accountLogic, _roleLogic); if (element != null) { formAccount.Id = element.Id.GetHashCode(); } return formAccount; } public Form GetThesaurus() { return new FormRoles(_roleLogic); } public bool DeleteElement(PluginsConventionElement element) { return _accountLogic.Delete( new AccountBindingModel { Id = element.Id.GetHashCode() } ); } public void ReloadData() { try { var accounts = _accountLogic.ReadList(null) ?? throw new Exception("Не удалось получить список аккаунтов"); _controlDataTreeTable.Clear(); var nodeNames = new Queue(); nodeNames.Enqueue("RoleName"); nodeNames.Enqueue("OutputRating"); nodeNames.Enqueue("Id"); nodeNames.Enqueue("Login"); _controlDataTreeTable.LoadConfig(new DataTreeNodeConfig { NodeNames = nodeNames }); foreach (var account in accounts) { account.OutputRating = account.Rating?.ToString() ?? "Отсутствует"; } _controlDataTreeTable.AddTable(accounts); } catch (Exception ex) { MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } } public bool CreateSimpleDocument(PluginsConventionSaveDocument saveDocument) { try { var accounts = _accountLogic.ReadList(null) ?? throw new Exception("Не удалось получить список аккаунтов"); List paragraphs = new(); foreach (var account in accounts) { if (account.Rating == null) { paragraphs.Add($"{account.Login}: {(string.IsNullOrWhiteSpace(account.Warnings) ? "Предупреждения отсутствуют" : account.Warnings)}"); } } _wordLongTextComponent.CreateWordText(new WordLongTextInfo() { FileName = saveDocument.FileName, Title = "Аккаунты без рейтинга", Paragraphs = paragraphs.ToArray() }); return true; } catch (Exception) { return false; } } public bool CreateTableDocument(PluginsConventionSaveDocument saveDocument) { Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); try { var accounts = _accountLogic.ReadList(null) ?? throw new Exception("Не удалось получить список аккаунтов"); foreach (var account in accounts) { account.OutputRating = account.Rating?.ToString() ?? "нет"; } _componentDocumentWithTableMultiHeaderPdf.CreateDoc(new ComponentDocumentWithTableHeaderDataConfig { FilePath = saveDocument.FileName, Header = "Аккаунты портала", ColumnsRowsWidth = new List<(int, int)> { (10, 10), (10, 10), (10, 10), (10, 10) }, // ширина столбцов и высота строк Headers = new List<(int ColumnIndex, int RowIndex, string Header, string PropertyName)> { (0, 0, "Id", "Id"), (1, 0, "Логин", "Login"), (2, 0, "Роль", "RoleName"), (3, 0, "Рейтинг", "OutputRating") }, Data = accounts.OrderBy(x => x.Id).ToList() }); return true; } catch (Exception) { return false; } } public bool CreateChartDocument(PluginsConventionSaveDocument saveDocument) { try { var accounts = _accountLogic.ReadList(null) ?? throw new Exception("Не удалось получить список аккаунтов"); var roleMapping = new List(); var data = new Dictionary> { { "Аккаунты", accounts .Where(x => x.Rating != null) .GroupBy(x => x.RoleName) .Select((group, index) => { roleMapping.Add($"{group.Key} - {index + 1}"); return (Date: index + 1, Value: (double)group.Count()); }) .ToList() } }; _componentDocumentWithChartPieExcel.CreateDoc(new ComponentDocumentWithChartConfig { FilePath = saveDocument.FileName, Header = $"Аккаунты с рейтингом по ролям ({string.Join(", ", roleMapping)})", ChartTitle = "Круговая диаграмма", LegendLocation = Location.Bottom, Data = data }); return true; } catch (Exception) { return false; } } private Guid IntToGuid(int value) { byte[] bytes = new byte[16]; BitConverter.GetBytes(value).CopyTo(bytes, 0); return new Guid(bytes); } } }