PIbd-32_Bondarenko_M.S._COP_16/WinForm/AppView/PluginsConvention.cs
2023-11-15 01:14:11 +04:00

250 lines
9.0 KiB
C#

using Contracts.StoragesContracts;
using Contracts.ViewModels;
using ControlsLibraryNet60.Data;
using ControlsLibraryNet60.Models;
using DataBaseImplement.Implements;
using PdfFormsLibrary;
using PluginsConventionLibrary;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WinFormsLibrary;
using WinFormsLibrary.not_visual;
using WinFormsLibrary.SupportClasses.Enums;
using WinFormsLibrary.SupportClasses;
namespace AppView
{
public class PluginsConvention : IPluginsConvention
{
private readonly IProviderStorage _providerStorage;
private readonly ITypeStorage _typeStorage;
private readonly ControlDataTreeCell _controlDataTreeCell;
private readonly PdfGeneratorControl _pdfGeneratorControl;
private readonly ComponentWithSettings _componentWithSettings;
private readonly CircleDiagram _circleDiagram;
public string PluginName { get; set; } = "LabWork_03_plugin";
public UserControl GetControl
{
get { return _controlDataTreeCell; }
}
public PluginsConventionElement GetElement
{
get
{
int Id = _controlDataTreeCell.GetSelectedObject<ProviderViewModel>()!.Id;
byte[] bytes = new byte[16];
BitConverter.GetBytes(Id).CopyTo(bytes, 0);
Guid guid = new Guid(bytes);
return new PluginsConventionElement() { Id = guid };
}
}
public PluginsConvention()
{
_providerStorage = new ProviderStorage();
_typeStorage = new TypeStorage();
_pdfGeneratorControl = new();
_componentWithSettings = new();
_circleDiagram = new();
_controlDataTreeCell = new();
}
public Form GetForm(PluginsConventionElement element)
{
if (element == null)
{
return new FormProvider(_providerStorage, _typeStorage);
}
else
{
FormProvider form = new FormProvider(_providerStorage, _typeStorage);
form.Id = element.Id.GetHashCode();
return form;
}
}
public Form GetThesaurus()
{
return new FormType(_typeStorage);
}
public bool DeleteElement(PluginsConventionElement element)
{
_providerStorage.Delete(
new(element.Id.GetHashCode())
);
return true;
}
public void ReloadData()
{
try
{
var providers = _providerStorage.GetFullList();
if (providers != null)
{
_controlDataTreeCell.Clear();
DataTreeNodeConfig treeConfig = new();
treeConfig.NodeNames = new();
treeConfig.NodeNames.Enqueue("Type");
treeConfig.NodeNames.Enqueue("SupplyDateTime");
treeConfig.NodeNames.Enqueue("Id");
treeConfig.NodeNames.Enqueue("Name");
_controlDataTreeCell.LoadConfig(treeConfig);
if (providers.Count > 0)
{
int numOfProperties = typeof(ProviderViewModel).GetProperties().Length;
for (int i = 0; i < providers.Count; ++i)
{
providers[i].SupplyDateTime = providers[i].SupplyDate.ToString();
for (int j = 0; j < numOfProperties; ++j)
{
_controlDataTreeCell.AddCell(j, providers[i]);
}
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(
ex.Message,
"Ошибка",
MessageBoxButtons.OK,
MessageBoxIcon.Error
);
}
}
public bool CreateSimpleDocument(PluginsConventionSaveDocument saveDocument)
{
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
string filePath = saveDocument.FileName;
string documentTitle = "Поставщики";
List<string> textData = new();
foreach (var provider in _providerStorage.GetFullList())
{
textData.Add(provider.Name + ": " + provider.Furniture);
}
try
{
_pdfGeneratorControl.GeneratePdf(filePath, documentTitle, textData);
MessageBox.Show("PDF-документ успешно сохранен.", "Успех", MessageBoxButtons.OK, MessageBoxIcon.Information);
return true;
}
catch (Exception ex)
{
MessageBox.Show(
$"Ошибка при создании PDF-документа: {ex.Message}",
"Ошибка",
MessageBoxButtons.OK,
MessageBoxIcon.Error
);
return false;
}
}
public bool CreateTableDocument(PluginsConventionSaveDocument saveDocument)
{
List<ProviderViewModel> providersList = _providerStorage.GetFullList();
foreach (var provider in providersList)
{
if (provider.SupplyDate == null)
{
provider.SupplyDateTime = "Поставок не было";
provider.SupplyDate = DateTime.MinValue;
continue;
}
provider.SupplyDateTime = provider.SupplyDate.ToString();
}
var columnConfigs = new List<ColumnConfig>
{
new ColumnConfig { Width = 50f, PropertyName = "Id" },
new ColumnConfig { Width = 50f, PropertyName = "Name" },
new ColumnConfig { Width = 50f, PropertyName = "Type" },
new ColumnConfig { Width = 70f, PropertyName = "SupplyDateTime" },
};
try
{
_componentWithSettings.GenerateExcelDocument(saveDocument.FileName, "Отчет по всем поставщикам", columnConfigs, 25f, 35f, providersList);
MessageBox.Show(
"Excel-документ успешно сохранен.",
"Успех",
MessageBoxButtons.OK,
MessageBoxIcon.Information
);
return true;
}
catch (Exception ex)
{
MessageBox.Show(
$"Ошибка при создании Excel-документа: {ex.Message}",
"Ошибка",
MessageBoxButtons.OK,
MessageBoxIcon.Error
);
return false;
}
}
public bool CreateChartDocument(PluginsConventionSaveDocument saveDocument)
{
var providersList = _providerStorage
.GetFullList()
.Where(item => item.SupplyDate?.Year == DateTime.Now.Year)
.GroupBy(item => item.Type)
.Select(group => new
{
Type = group.Key,
Date = group.Select(item => item.SupplyDate),
Count = (double)group.Count(),
});
List<double> results = new();
List<string> names = new();
foreach (var provider in providersList)
{
results.Add(provider.Count);
names.Add(provider.Type);
}
SimpleCircleDiagram simpleCircleDiagram = new(saveDocument.FileName, "Круговая диаграмма", "Количество поставщиков в разрезе типа организации",
EnumAreaLegend.Right, new() { new DataCircleDiagram("Типы организации", results.ToArray()) });
simpleCircleDiagram.NameData = names.ToArray();
try
{
_circleDiagram.AddCircleDiagram(simpleCircleDiagram);
MessageBox.Show(
"Word-документ успешно сохранен.",
"Успех",
MessageBoxButtons.OK,
MessageBoxIcon.Information
);
return true;
}
catch (Exception ex)
{
MessageBox.Show(
$"Ошибка при создании Word-документа: {ex.Message}",
"Ошибка",
MessageBoxButtons.OK,
MessageBoxIcon.Error
);
return false;
}
}
}
}