PIbd-31_Putincev.D.M._COP_29/COP/PortalAccountsView/PluginsConvention.cs

225 lines
7.6 KiB
C#
Raw Permalink Normal View History

2024-11-21 01:48:17 +04:00
using Components;
using Components.NonVisual;
using Components.SaveToPdfHelpers;
using NPOI.HPSF;
using PluginsConventionLibrary;
using PortalAccountsBusinessLogic.BusinessLogics;
using PortalAccountsContracts.BindingModels;
using PortalAccountsContracts.BusinessLogicsContracts;
using PortalAccountsContracts.ViewModels;
using PortalAccountsDatabaseImplement.Implements;
using PutincevLibrary;
using RodionovLibrary.NonVisualComponents;
using RodionovLibrary.NonVisualComponents.HelperModels;
using System.Text;
namespace PortalAccountsView
{
public class PluginsConvention : IPluginsConvention
{
private readonly IAccountLogic _accountLogic;
private readonly IInterestLogic _interestLogic;
private readonly CustomDataGridView _customDataGridViewTable = new();
private readonly ComponentExcelWithImage _componentExcelWithImage = new();
private readonly WordTableComponent _wordTableComponent = new();
private readonly HistogramPDF _histogrampdf = new();
public PluginsConvention()
{
_accountLogic = new AccountLogic(new AccountStorage());
_interestLogic = new InterestLogic(new InterestStorage());
ReloadData();
}
public string PluginName => "LabWork3 Plugin";
public UserControl GetControl => _customDataGridViewTable;
public PluginsConventionElement GetElement
{
get
{
var selected = _customDataGridViewTable.GetSelectedObject<AccountViewModel>()
?? throw new Exception("Не удалось получить выбранный элемент");
return new PluginsConventionAccount()
{
Id = IntToGuid(selected.Id),
Login = selected.Login,
AvatarPath = selected.AvatarPath,
InterestName = selected.InterestName,
Email = selected.Email
};
}
}
public Form GetForm(PluginsConventionElement element)
{
var formAccount = new FormAccount(_accountLogic, _interestLogic);
if (element != null)
{
formAccount.Id = element.Id.GetHashCode();
}
return formAccount;
}
public Form GetThesaurus()
{
return new FormInterests(_interestLogic);
}
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("Не удалось получить список аккаунтов");
_customDataGridViewTable.ClearRows();
var tables = new List<(string, string, float)> {
("Идентификатор", "Id", 1),
("Логин", "Login", 1),
("Интерес", "InterestName", 2),
("Почта", "Email", 2)
};
_customDataGridViewTable.ConfigureColumns(tables);
_customDataGridViewTable.FillData(accounts);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
public bool CreateSimpleDocument(PluginsConventionSaveDocument saveDocument)
{
try
{
List<string> images = new List<string>();
var list = _accountLogic.ReadList(null);
if (list != null)
{
foreach (var item in list)
{
images.Add(item.AvatarPath);
}
string[] imagesArray = images.ToArray();
_componentExcelWithImage.CreateExcelWithImages(saveDocument.FileName, "Сканы чеков", imagesArray);
}
return true;
}
catch (Exception)
{
return false;
}
}
public bool CreateTableDocument(PluginsConventionSaveDocument saveDocument)
{
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
try
{
string title = "Документ с таблицей";
var mergedColumns = new List<(int, int)>
{
(1, 2),
};
var columns = new List<ColumnParameters>
{
new() { FirstRowHeader = "Идент", PropertyName = "Id", Width = 1.3 },
new() { FirstRowHeader = "Личные данные", SecondRowHeader = "Логин", PropertyName = "Login", Width = 1.7 },
new() { FirstRowHeader = "Личные данные", SecondRowHeader = "Почта", PropertyName = "Email", Width = 1.7 },
new() { FirstRowHeader = "Интерес", PropertyName = "InterestName", Width = 1.7 },
};
var list = _accountLogic.ReadList(null);
var tableInfo = new WordTableInfo<AccountViewModel>
{
FileName = saveDocument.FileName,
Title = title,
ColumnParameters = columns,
Items = list,
MergedColumns = mergedColumns
};
_wordTableComponent.CreateTable(tableInfo);
return true;
}
catch (Exception)
{
return false;
}
}
public bool CreateChartDocument(PluginsConventionSaveDocument saveDocument)
{
try
{
var accounts = _accountLogic.ReadList(null);
var list = new ChartData();
var groupedAccounts = accounts.GroupBy(account => account.InterestName)
.Select(group => new
{
InterestName = group.Key,
AccountCount = group.Count()
})
.ToList();
var data = new Dictionary<string, double>();
foreach (var group in groupedAccounts)
{
data.Add(group.InterestName, (double)group.AccountCount);
}
list.Data = data;
list.SeriesName = "Пользователи";
var acc = new List<ChartData>();
acc.Add(list);
var histogrammInfo = new HistogramData
{
FilePath = saveDocument.FileName,
DocumentTitle = "Гистограмма PDF",
ChartTitle = "Количетсво интересов у пользователей",
LegendPosition = LegendPositions.Bottom,
ChartData = acc,
};
_histogrampdf.CreateHistogramPdf(histogrammInfo);
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);
}
}
}