PIbd-31_Rodionov.I.A._COP_28/COP/PortalAccountsView/FormMain.cs
2024-11-05 19:10:19 +04:00

218 lines
8.3 KiB
C#

using ComponentsLibraryNet60.Models;
using ControlsLibraryNet60.Models;
using PortalAccountsContracts.BindingModels;
using PortalAccountsContracts.BusinessLogicsContracts;
using PortalAccountsContracts.ViewModels;
using RodionovLibrary.NonVisualComponents.HelperModels;
using System.Text;
namespace PortalAccountsView
{
public partial class FormMain : Form
{
private readonly IAccountLogic _logic;
public FormMain(IAccountLogic logic)
{
InitializeComponent();
_logic = logic;
var nodeNames = new Queue<string>();
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<AccountViewModel>();
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<AccountViewModel>();
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<string> 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<string>();
var data = new Dictionary<string, List<(int Date, double Value)>>
{
{
"Àêêàóíòû",
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)
{
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
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<AccountViewModel>
{
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.OrderBy(x => x.Id).ToList()
});
MessageBox.Show("Ãîòîâî!");
}
catch (Exception ex)
{
MessageBox.Show("Ïðîèçîøëà îøèáêà: " + ex.Message, "Îøèáêà", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
}