using ComponentsLibraryNet60.Models; using ControlsLibraryNet60.Models; using PortalAccountsContracts.BindingModels; using PortalAccountsContracts.BusinessLogicsContracts; using PortalAccountsContracts.ViewModels; using RodionovLibrary.NonVisualComponents.HelperModels; namespace PortalAccountsView { public partial class FormMain : Form { private readonly IAccountLogic _logic; public FormMain(IAccountLogic logic) { InitializeComponent(); _logic = logic; var nodeNames = new Queue(); nodeNames.Enqueue("RoleName"); nodeNames.Enqueue("OutputRating"); nodeNames.Enqueue("Id"); nodeNames.Enqueue("Login"); controlDataTreeTable.LoadConfig(new DataTreeNodeConfig { NodeNames = nodeNames }); } private void РолиToolStripMenuItem_Click(object sender, EventArgs e) { var service = Program.ServiceProvider?.GetService(typeof(FormRoles)); if (service is FormRoles form) { form.ShowDialog(); } } private void FormMain_Load(object sender, EventArgs e) { LoadData(); } private void LoadData() { controlDataTreeTable.Clear(); var accounts = _logic.ReadList(null); if (accounts != null) { foreach (var account in accounts) { account.OutputRating = account.Rating?.ToString() ?? "Отсутствует"; } controlDataTreeTable.AddTable(accounts); } } private void СоздатьToolStripMenuItem_Click(object sender, EventArgs e) { var service = Program.ServiceProvider?.GetService(typeof(FormAccount)); if (service is FormAccount form) { if (form.ShowDialog() == DialogResult.OK) { LoadData(); } } } private void РедактироватьToolStripMenuItem_Click(object sender, EventArgs e) { var service = Program.ServiceProvider?.GetService(typeof(FormAccount)); if (service is FormAccount form) { var selected = controlDataTreeTable.GetSelectedObject(); if (selected == null) return; form.Id = selected.Id; if (form.ShowDialog() == DialogResult.OK) { LoadData(); } } } private void УдалитьToolStripMenuItem_Click(object sender, EventArgs e) { var selected = controlDataTreeTable.GetSelectedObject(); if (selected == null) return; if (MessageBox.Show("Удалить запись?", "", MessageBoxButtons.YesNo) == DialogResult.Yes) { if (_logic.Delete(new AccountBindingModel { Id = selected.Id })) { LoadData(); } } } private void ДокументСТекстомToolStripMenuItem_Click(object sender, EventArgs e) { using var dialog = new SaveFileDialog { Filter = "docx|*.docx" }; if (dialog.ShowDialog() == DialogResult.OK) { try { var accounts = _logic.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 = dialog.FileName, Title = "Аккаунты без рейтинга", Paragraphs = paragraphs.ToArray() }); MessageBox.Show("Готово!"); } catch (Exception ex) { MessageBox.Show("Произошла ошибка: " + ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } private void ДокументСДиаграммойToolStripMenuItem_Click(object sender, EventArgs e) { using var dialog = new SaveFileDialog { Filter = "Excel Files|*.xlsx" }; if (dialog.ShowDialog() == DialogResult.OK) { try { var accounts = _logic.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 = dialog.FileName, Header = $"Аккаунты с рейтингом по ролям ({string.Join(", ", roleMapping)})", ChartTitle = "Круговая диаграмма", LegendLocation = ComponentsLibraryNet60.Models.Location.Bottom, Data = data }); MessageBox.Show("Готово!"); } catch (Exception ex) { MessageBox.Show("Произошла ошибка: " + ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } private void ДокументСТаблицейToolStripMenuItem_Click(object sender, EventArgs e) { using var dialog = new SaveFileDialog { Filter = "PDF Files|*.pdf" }; if (dialog.ShowDialog() == DialogResult.OK) { try { var accounts = _logic.ReadList(null) ?? throw new Exception("Не удалось получить список аккаунтов"); foreach (var account in accounts) { account.OutputRating = account.Rating?.ToString() ?? "нет"; } componentDocumentWithTableMultiHeaderPdf.CreateDoc(new ComponentDocumentWithTableHeaderDataConfig { FilePath = dialog.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 }); MessageBox.Show("Готово!"); } catch (Exception ex) { MessageBox.Show("Произошла ошибка: " + ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } } }