using ClientRecordBuisinessLogic.BuisinessLogic; using ClientRecordContracts.BindingModels; using ClientRecordContracts.BusinessLogicContracts; using ClientRecordContracts.ViewModels; using ClientRecordDatabaseImplement.Implements; using Components.Nonvisual; using ComponentsLibraryNet60.Models; using ControlsLibraryNet60.Data; using ControlsLibraryNet60.Models; using PluginsConventionLibrary; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using WinFormsLibrary.NonVisualComponents.Helpers; using static System.Windows.Forms.VisualStyles.VisualStyleElement.Window; namespace ClientRecordView { public class PluginsConvention : IPluginsConvention { private readonly IClientLogic _clientLogic; private readonly IStatusLogic _statusLogic; private readonly ControlDataTreeTable _controlDataTreeTable = new(); private readonly WinFormsLibrary.NonVisualComponents.WordText _wordText = new(); private readonly Components.Nonvisual.UserControlConfigurableTableDocument _componentDocumentWithTableMultiHeaderPdf = new(); private readonly ComponentsLibraryNet60.DocumentWithChart.ComponentDocumentWithChartPieExcel _componentDocumentWithChartPieExcel = new(); public PluginsConvention() { _clientLogic = new ClientLogic(new ClientStorage()); _statusLogic = new StatusLogic(new StatusStorage()); ReloadData(); } public string PluginName => "LabWork3 Plugin"; public UserControl GetControl => _controlDataTreeTable; public PluginsConventionElement GetElement { get { var selected = _controlDataTreeTable.GetSelectedObject() ?? throw new Exception("Не удалось получить выбранный элемент"); return new PluginsConventionClient() { Id = IntToGuid(selected.Id), ClientFIO= selected.ClientFIO, Amount= selected.AmountString, Reviews= selected.Reviews, Status= selected.Status, }; } } public Form GetForm(PluginsConventionElement element) { var formClient = new FormClient(_clientLogic, _statusLogic); if (element != null) { formClient.Id = element.Id.GetHashCode(); } return formClient; } public Form GetThesaurus() { return new FormStatuses(_statusLogic); } public bool DeleteElement(PluginsConventionElement element) { return _clientLogic.Delete( new ClientBindingModel { Id = element.Id.GetHashCode() } ); } public void ReloadData() { try { var clients = _clientLogic.ReadList(null) ?? throw new Exception("Не удалось получить список аккаунтов"); _controlDataTreeTable.Clear(); var nodeNames = new Queue(); nodeNames.Enqueue("Status"); nodeNames.Enqueue("AmountString"); nodeNames.Enqueue("Id"); nodeNames.Enqueue("ClientFIO"); _controlDataTreeTable.LoadConfig(new DataTreeNodeConfig { NodeNames = nodeNames }); foreach (var client in clients) { client.AmountString = client.Amount?.ToString() ?? "Нет"; } _controlDataTreeTable.AddTable(clients); } catch (Exception ex) { MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } } public bool CreateSimpleDocument(PluginsConventionSaveDocument saveDocument) { try { var clients = _clientLogic.ReadList(null) ?? throw new Exception("Не удалось получить список аккаунтов"); List paragraphs = new(); foreach (var client in clients) { if (client.Amount != null) { paragraphs.Add($"{client.ClientFIO}: {(string.IsNullOrWhiteSpace(client.Reviews) ? "Отзывы отсутствуют" : client.Reviews)}"); } } _wordText.CreateWordText(new LongWordInfo() { Path = saveDocument.FileName, Title = "Клиенты с покупками", Paragraphs = paragraphs.ToArray() }); return true; } catch (Exception) { return false; } } public bool CreateTableDocument(PluginsConventionSaveDocument saveDocument) { Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); try { var clients = _clientLogic.ReadList(null) ?? throw new Exception("Не удалось получить список клиентов"); foreach (var client in clients) { client.AmountString = client.Amount?.ToString() ?? "нет"; } _componentDocumentWithTableMultiHeaderPdf.SaveToDocument( saveDocument.FileName, "Учет клиентов", new List<(double, string Header, string PropertyName)> { (1, "Id", "Id"), (3, "ФИО", "ClientFIO"), (2, "Статус", "Status"), (3, "Сумма", "AmountString") }, 2, 2, clients.OrderBy(x => x.Id).ToList() ); return true; } catch (Exception) { return false; } } public bool CreateChartDocument(PluginsConventionSaveDocument saveDocument) { try { var clients = _clientLogic.ReadList(null) ?? throw new Exception("Не удалось получить список клиентов"); var roleMapping = new List(); var data = new Dictionary> { { "Клиенты", clients .Where(x => x.Amount != null) .GroupBy(x => x.Status) .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); } } }