COP-17/WinFormsProject/AppView/PluginsConvention.cs

247 lines
8.1 KiB
C#

using Contracts.StorageContracts;
using Contracts.ViewModels;
using ControlsLibraryNet60.Data;
using ControlsLibraryNet60.Models;
using DateBaseImplement.Implements;
using PdfFormsLibrary;
using PluginsConventionLibrary;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WinFormsLibrary;
using WinFormsLibrary.not_visual;
using WinFormsLibrary.SupportClasses;
namespace AppView
{
public class PluginsConvention : IPluginsConvention
{
private readonly IProviderStorage _providerStorage;
private readonly ITypeStorage _typeStorage;
private readonly DocumentWithImage documentWithImage1;
private readonly Table2column table2column1;
private readonly Gistograma gistograma1;
private readonly ControlDataTableTable DataTable = new ControlDataTableTable();
public string PluginName { get; set; } = "LabWork_03_plugin";
public UserControl GetControl
{
get
{
ReloadData();
return DataTable;
}
}
public PluginsConventionElement GetElement
{
get
{
var provider = DataTable.GetSelectedObject<ProviderViewModel>();
int id = -1;
if (provider != null)
{
id = Convert.ToInt32(provider.Id);
}
byte[] bytes = new byte[16];
BitConverter.GetBytes(id).CopyTo(bytes, 0);
return new()
{
Id = new Guid(bytes)
};
}
}
public PluginsConvention()
{
_providerStorage = new ProviderStorage();
_typeStorage = new TypeStorage();
documentWithImage1 = new();
table2column1 = new();
gistograma1 = new();
List<DataTableColumnConfig> columns = new List<DataTableColumnConfig>
{
new DataTableColumnConfig
{
ColumnHeader = "Id",
PropertyName = "Id",
Visible = false
},
new DataTableColumnConfig
{
ColumnHeader = "Название",
PropertyName = "Name",
Visible = true
},
new DataTableColumnConfig
{
ColumnHeader = "Тип изделий",
PropertyName = "Type",
Visible = true
},
new DataTableColumnConfig
{
ColumnHeader = "Телефон",
PropertyName = "Number",
Visible = true
}
};
DataTable.LoadColumns(columns);
}
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 list = _providerStorage.GetFullList();
DataTable.Clear();
DataTable.AddTable(list);
}
catch (Exception ex)
{
MessageBox.Show(
ex.Message,
"Ошибка",
MessageBoxButtons.OK,
MessageBoxIcon.Error
);
}
}
public bool CreateWordDocument(PluginsConventionSaveDocument saveDocument)
{
try
{
ImageClass info = new ImageClass();
var images = _providerStorage.GetFullList().Select(x => x.Logo).ToList();
info.Title = "Images";
info.Path = saveDocument.FileName;
info.Files = images;
documentWithImage1.CreateDocument(info);
MessageBox.Show("Готово");
return true;
}
catch(Exception e)
{
MessageBox.Show(e.Message, "Ошибка");
return false;
}
}
public bool CreateTableDocument(PluginsConventionSaveDocument saveDocument)
{
try
{
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
List<ColumnDefinition> columnDefinitionsUp = new List<ColumnDefinition> {
new ColumnDefinition{Header = "#", PropertyName = "Id", Weight = 30},
new ColumnDefinition{Header = "Информация", PropertyName = "NumberType", Weight = 30},
new ColumnDefinition{Header = "", PropertyName = "NumberType", Weight = 30},
new ColumnDefinition{Header = "Номер телефона", PropertyName = "Number", Weight = 30},
};
List<ColumnDefinition> columnDefinitionsDown = new List<ColumnDefinition> {
new ColumnDefinition{Header = "#", PropertyName = "Id", Weight = 30},
new ColumnDefinition{Header = "Название", PropertyName = "Name", Weight = 30},
new ColumnDefinition{Header = "Тип изделия", PropertyName = "Type", Weight = 30},
new ColumnDefinition{Header = "-", PropertyName = "Number", Weight = 30},
};
var providers = _providerStorage.GetFullList();
List<int[]> mergedColums = new() { new int[] { 1, 2 } };
BigTable<ProviderViewModel> info = new(saveDocument.FileName, "Table", columnDefinitionsUp, columnDefinitionsDown, providers, mergedColums);
table2column1.CreateTable(info);
MessageBox.Show("Готово");
return true;
}
catch(Exception e)
{
MessageBox.Show(e.Message, "Ошибка");
return false;
}
}
public bool CreateChartDocument(PluginsConventionSaveDocument saveDocument)
{
try
{
var providers = _providerStorage.GetFullList();
var uniqueTypes = providers.Select(p => p.Type).Distinct();
List<ChartData> data = new List<ChartData>();
foreach (var uniqueType in uniqueTypes)
{
var typeProviders = providers.Where(p => p.Type == uniqueType).ToList();
var dataList = new List<double>();
// Используем Count() для подсчета общего количества поставщиков каждого типа
int totalCount = typeProviders.Count();
ChartData chData = new ChartData();
chData.SeriesName = uniqueType;
chData.Data = new double[] { totalCount };
data.Add(chData);
}
gistograma1.GenerateExcelChartDocument(saveDocument.FileName, "Сводка по типам изделия.", "Количество поставщиков для товаров каждого типа", WinFormsLibrary.not_visual.LegendPosition.Bottom, data);
MessageBox.Show("Готово");
return true;
}
catch(Exception e)
{
MessageBox.Show(e.Message, "Ошибка");
return false;
}
}
}
}