PIbd-31_Rodionov.I.A._COP_28/COP/PortalAccountsView/PluginsConvention.cs

217 lines
8.8 KiB
C#
Raw Normal View History

2024-11-04 23:43:51 +04:00
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;
2024-11-04 23:43:51 +04:00
private readonly IRoleLogic _roleLogic;
2024-11-04 23:43:51 +04:00
private readonly ControlDataTreeTable _controlDataTreeTable = new();
2024-11-04 23:43:51 +04:00
private readonly RodionovLibrary.NonVisualComponents.WordLongTextComponent _wordLongTextComponent = new();
2024-11-04 23:43:51 +04:00
private readonly ComponentsLibraryNet60.DocumentWithTable.ComponentDocumentWithTableMultiHeaderPdf _componentDocumentWithTableMultiHeaderPdf = new();
2024-11-04 23:43:51 +04:00
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<AccountViewModel>()
?? 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<string>();
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<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 = saveDocument.FileName,
Title = "Аккаунты без рейтинга",
Paragraphs = paragraphs.ToArray()
});
MessageBox.Show("Готово!");
return true;
}
catch (Exception ex)
{
MessageBox.Show("Произошла ошибка: " + ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
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<AccountViewModel>
{
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()
});
MessageBox.Show("Готово!");
return true;
}
catch (Exception ex)
{
MessageBox.Show("Произошла ошибка: " + ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
}
public bool CreateChartDocument(PluginsConventionSaveDocument saveDocument)
{
try
{
var accounts = _accountLogic.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 = saveDocument.FileName,
Header = $"Аккаунты с рейтингом по ролям ({string.Join(", ", roleMapping)})",
ChartTitle = "Круговая диаграмма",
LegendLocation = Location.Bottom,
Data = data
});
MessageBox.Show("Готово!");
return true;
}
catch (Exception ex)
{
MessageBox.Show("Произошла ошибка: " + ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
}
private Guid IntToGuid(int value)
{
byte[] bytes = new byte[16];
BitConverter.GetBytes(value).CopyTo(bytes, 0);
return new Guid(bytes);
}
}
}