PIbd-33_Abazov_A.A._KOP_29/AbazovApp/AccountsApp/PluginsConvention.cs

164 lines
6.0 KiB
C#
Raw Normal View History

2023-11-30 21:05:37 +04:00
using AbazovViewComponents.LogicalComponents;
using AccountsContracts.BusinessLogicContracts;
using AccountsContracts.StorageContracts;
using AccountsContracts.ViewModels;
using ComponentsLibraryNet60.DocumentWithChart;
using ComponentsLibraryNet60.Models;
using ControlsLibraryNet60.Core;
using ControlsLibraryNet60.Data;
using ControlsLibraryNet60.Models;
using NevaevaLibrary.LogicalComponents;
using PluginsConventionLibrary;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AccountsApp
{
public class PluginsConvention : IPluginsConvention
{
private readonly IAccountLogic _accountLogic;
private readonly IInterestLogic _interestLogic;
private readonly ControlDataTableTable _controlDataTable;
private readonly ExcelImagesComponent _excelImagesComponent;
private readonly WordTableComponent _wordTableComponent;
private readonly ComponentDocumentWithChartBarPdf _chartBar;
public string PluginName { get; set; } = "LabWork_03_plugin";
public UserControl GetControl
{
get { return _controlDataTable; }
}
public PluginsConventionElement GetElement
{
get
{
int Id = _controlDataTable.GetSelectedObject<AccountViewModel>()!.Id;
byte[] bytes = new byte[16];
BitConverter.GetBytes(Id).CopyTo(bytes, 0);
Guid guid = new Guid(bytes);
return new PluginsConventionElement() { Id = guid };
}
}
public Form GetForm(PluginsConventionElement element)
{
if (element == null)
{
return new FormAccount(_accountLogic, _interestLogic);
}
else
{
FormAccount form = new FormAccount(_accountLogic, _interestLogic);
form.Id = element.Id.GetHashCode();
return form;
}
}
public Form GetThesaurus()
{
return new FormInterests(_interestLogic);
}
public bool DeleteElement(PluginsConventionElement element)
{
_accountLogic.Delete(
new(element.Id.GetHashCode())
);
return true;
}
public void ReloadData()
{
try
{
var account = _accountLogic.ReadList(null);
if (account != null)
{
_controlDataTable.Clear();
_controlDataTable.LoadColumns(new List<DataTableColumnConfig> {
new DataTableColumnConfig { ColumnHeader = "", PropertyName = "Id", Visible = false, Width = 10},
new DataTableColumnConfig { ColumnHeader = "Логин", PropertyName = "Login", Visible = true, Width = 200},
new DataTableColumnConfig { ColumnHeader = "Выбранный интерес", PropertyName = "InterestName", Visible = true, Width = 200},
new DataTableColumnConfig { ColumnHeader = "Email", PropertyName = "Email", Visible = true, Width = 200},
});
_controlDataTable.AddTable(account);
}
}
catch (Exception ex)
{
MessageBox.Show(
ex.Message,
"Ошибка",
MessageBoxButtons.OK,
MessageBoxIcon.Error
);
}
}
public bool CreateSimpleDocument(PluginsConventionSaveDocument saveDocument)
{
List<string> avatars = new List<string>();
foreach (var account in _accountLogic.ReadList(null))
{
avatars.Add(account.Avatar);
}
string path = AppDomain.CurrentDomain.BaseDirectory + "Аватары.xlsx";
if (_excelImagesComponent.createWithImages(new ExcelImageInfo(path, "Аватары", avatars.ToArray())))
{
MessageBox.Show("Документ создан");
return true;
}
return false;
}
public bool CreateTableDocument(PluginsConventionSaveDocument saveDocument)
{
string path = AppDomain.CurrentDomain.BaseDirectory + "Аккаунты.docx";
List<(int, int)> merges = new List<(int, int)> { (1, 2) };
List<int> widths = new List<int> { 100, 100, 100, 100 };
List<(string, string)> headers = new List<(string, string)> {
("Id", "Идентификатор"),
("", "Личные данные"),
("Login", "Логин"),
("Email", "Эл. почта"),
("InterestName", "Выбранный интерес")
};
_wordTableComponent.createWithTable(path, "Список аккаунтов", merges, widths, headers, _accountLogic.ReadList(null));
MessageBox.Show("Документ создан");
return true;
}
public bool CreateChartDocument(PluginsConventionSaveDocument saveDocument)
{
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
string path = AppDomain.CurrentDomain.BaseDirectory + "Интересы.pdf";
Dictionary<string, List<(int, double)>> data = new Dictionary<string, List<(int, double)>>();
data = _accountLogic
.ReadList(null)
.GroupBy(x => x.InterestName)
.ToDictionary(x => x.Key, x => new List<(int, double)> { (0, x.Count()) });
_chartBar.CreateDoc(new ComponentDocumentWithChartConfig
{
Header = "Интересы",
FilePath = path,
ChartTitle = "Интересы",
LegendLocation = ComponentsLibraryNet60.Models.Location.Bottom,
Data = data,
});
MessageBox.Show("Успех");
return false;
}
}
}