COP-17/WinFormsProject/AppView/PluginsConvention.cs

247 lines
8.1 KiB
C#
Raw Normal View History

2023-12-14 01:22:20 +04:00
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)
{
2023-12-14 01:48:08 +04:00
try
{
ImageClass info = new ImageClass();
2023-12-14 01:22:20 +04:00
2023-12-14 01:48:08 +04:00
var images = _providerStorage.GetFullList().Select(x => x.Logo).ToList();
2023-12-14 01:22:20 +04:00
2023-12-14 01:48:08 +04:00
info.Title = "Images";
info.Path = saveDocument.FileName;
info.Files = images;
2023-12-14 01:22:20 +04:00
2023-12-14 01:48:08 +04:00
documentWithImage1.CreateDocument(info);
MessageBox.Show("Готово");
return true;
}
catch(Exception e)
{
MessageBox.Show(e.Message, "Ошибка");
return false;
}
2023-12-14 01:22:20 +04:00
}
public bool CreateTableDocument(PluginsConventionSaveDocument saveDocument)
{
2023-12-14 01:48:08 +04:00
try
{
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
2023-12-14 01:22:20 +04:00
2023-12-14 01:48:08 +04:00
List<ColumnDefinition> columnDefinitionsUp = new List<ColumnDefinition> {
2023-12-14 01:22:20 +04:00
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},
};
2023-12-14 01:48:08 +04:00
List<ColumnDefinition> columnDefinitionsDown = new List<ColumnDefinition> {
2023-12-14 01:22:20 +04:00
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},
};
2023-12-14 01:48:08 +04:00
var providers = _providerStorage.GetFullList();
2023-12-14 01:22:20 +04:00
2023-12-14 01:48:08 +04:00
List<int[]> mergedColums = new() { new int[] { 1, 2 } };
2023-12-14 01:22:20 +04:00
2023-12-14 01:48:08 +04:00
BigTable<ProviderViewModel> info = new(saveDocument.FileName, "Table", columnDefinitionsUp, columnDefinitionsDown, providers, mergedColums);
2023-12-14 01:22:20 +04:00
2023-12-14 01:48:08 +04:00
table2column1.CreateTable(info);
MessageBox.Show("Готово");
return true;
}
catch(Exception e)
{
MessageBox.Show(e.Message, "Ошибка");
return false;
}
2023-12-14 01:22:20 +04:00
}
public bool CreateChartDocument(PluginsConventionSaveDocument saveDocument)
{
2023-12-14 01:48:08 +04:00
try
{
var providers = _providerStorage.GetFullList();
2023-12-14 01:22:20 +04:00
2023-12-14 01:48:08 +04:00
var uniqueTypes = providers.Select(p => p.Type).Distinct();
2023-12-14 01:22:20 +04:00
2023-12-14 01:48:08 +04:00
List<ChartData> data = new List<ChartData>();
2023-12-14 01:22:20 +04:00
2023-12-14 01:48:08 +04:00
foreach (var uniqueType in uniqueTypes)
{
var typeProviders = providers.Where(p => p.Type == uniqueType).ToList();
2023-12-14 01:22:20 +04:00
2023-12-14 01:48:08 +04:00
var dataList = new List<double>();
2023-12-14 01:22:20 +04:00
2023-12-14 01:48:08 +04:00
// Используем Count() для подсчета общего количества поставщиков каждого типа
int totalCount = typeProviders.Count();
2023-12-14 01:22:20 +04:00
2023-12-14 01:48:08 +04:00
ChartData chData = new ChartData();
chData.SeriesName = uniqueType;
chData.Data = new double[] { totalCount };
2023-12-14 01:22:20 +04:00
2023-12-14 01:48:08 +04:00
data.Add(chData);
}
2023-12-14 01:22:20 +04:00
2023-12-14 01:48:08 +04:00
gistograma1.GenerateExcelChartDocument(saveDocument.FileName, "Сводка по типам изделия.", "Количество поставщиков для товаров каждого типа", WinFormsLibrary.not_visual.LegendPosition.Bottom, data);
MessageBox.Show("Готово");
return true;
}
catch(Exception e)
{
MessageBox.Show(e.Message, "Ошибка");
return false;
}
2023-12-14 01:22:20 +04:00
}
}
}