PIbd-32_Chubykina_P.P._COP_11/COP/WinFormsTest/PluginsConvention.cs

176 lines
6.4 KiB
C#

using ClientsBusinessLogics.BusinessLogics;
using ClientsContracts.BusinessLogicContracts;
using ClientsContracts.StorageContracts;
using ClientsContracts.ViewModels;
using ClientDataBaseImplement.Implements;
using BulatovaComponents.NonVisualComponents;
using ChubykinaComponents.LogicalComponents;
using ComponentsLibraryNet60.DocumentWithChart;
using ComponentsLibraryNet60.Models;
using ControlsLibraryNet60.Core;
using ControlsLibraryNet60.Data;
using ControlsLibraryNet60.Models;
using PluginsConventionLibrary;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WinFormsTest
{
public class PluginsConvention : IPluginsConvention
{
private readonly IClientLogic _clientLogic;
private readonly ICategoryLogic _categoryLogic;
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 PluginsConvention()
{
_clientLogic = new ClientLogic(new ClientStorage());
_categoryLogic = new CategoryLogic(new CategoryStorage());
_excelImagesComponent = new();
_wordTableComponent = new();
_chartBar = new();
_controlDataTable = new();
}
public PluginsConventionElement GetElement
{
get
{
int Id = _controlDataTable.GetSelectedObject<ClientViewModel>()!.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 FormClient(_clientLogic, _categoryLogic);
}
else
{
FormClient form = new FormClient(_clientLogic, _categoryLogic);
form.Id = element.Id.GetHashCode();
return form;
}
}
public Form GetThesaurus()
{
return new FormCategories(_categoryLogic);
}
public bool DeleteElement(PluginsConventionElement element)
{
_clientLogic.Delete(
new ClientsContracts.BindingModels.ClientBindingModel { Id = element.Id.GetHashCode() }
);
return true;
}
public void ReloadData()
{
try
{
var client = _clientLogic.ReadList(null);
if (client != null)
{
_controlDataTable.Clear();
_controlDataTable.LoadColumns(new List<DataTableColumnConfig> {
new DataTableColumnConfig { ColumnHeader = "", PropertyName = "Id", Visible = false, Width = 10},
new DataTableColumnConfig { ColumnHeader = "ФИО", PropertyName = "Fio", Visible = true, Width = 200},
new DataTableColumnConfig { ColumnHeader = "Выбранная категория", PropertyName = "CategoryName", Visible = true, Width = 200},
new DataTableColumnConfig { ColumnHeader = "Email", PropertyName = "Email", Visible = true, Width = 200},
});
_controlDataTable.AddTable(client);
}
}
catch (Exception ex)
{
MessageBox.Show(
ex.Message,
"Ошибка",
MessageBoxButtons.OK,
MessageBoxIcon.Error
);
}
}
public bool CreateSimpleDocument(PluginsConventionSaveDocument saveDocument)
{
List<string> photos = new List<string>();
foreach (var client in _clientLogic.ReadList(null))
{
photos.Add(client.Photo);
}
string path = saveDocument.FileName;
if (_excelImagesComponent.createWithImages(new ExcelImageInfo(path, "Фотографии", photos.ToArray())))
{
MessageBox.Show("Документ создан");
return true;
}
return false;
}
public bool CreateTableDocument(PluginsConventionSaveDocument saveDocument)
{
string path = saveDocument.FileName;
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", "Идентификатор"),
("", "Личные данные"),
("Fio", "ФИО"),
("Email", "Эл. почта"),
("CategoryName", "Выбранная категория")
};
_wordTableComponent.createWithTable(path, "Список аккаунтов", merges, widths, headers, _clientLogic.ReadList(null));
MessageBox.Show("Документ создан");
return true;
}
public bool CreateChartDocument(PluginsConventionSaveDocument saveDocument)
{
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
string path = saveDocument.FileName;
Dictionary<string, List<(int, double)>> data = new Dictionary<string, List<(int, double)>>();
data = _clientLogic
.ReadList(null)
.GroupBy(x => x.CategoryName)
.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;
}
}
}