PIbd-32_Turner_I.A._COP_10/COP/WinForms/PluginsConvention.cs

216 lines
5.9 KiB
C#
Raw Normal View History

2023-10-31 22:44:35 +04:00
using ClientBusinessLogic.BusinessLogics;
using ClientsContracts.BindingModels;
using Plugins;
using VisableComponents;
using UnvisableComponents;
using VisualCompLib.Components;
using ComponentsLibraryNet60.DocumentWithTable;
using ClientsContracts.ViewModels;
using ComponentsLibraryNet60.Models;
using ClientsDatabaseImplement.Implements;
namespace WinForms
{
public class PluginsConvention : IPluginsConvention
{
private readonly ClientLogic _clientLogic = new ClientLogic(new ClientStorage());
private readonly StatusLogic _statusLogic = new StatusLogic(new StatusStorage());
private MyTreeView myTreeView1 = new MyTreeView();
public string PluginName { get; set; } = "Дерево большое";
public UserControl GetControl
{
get
{
Load();
return myTreeView1;
}
}
public PluginsConventionElement GetElement
{
get
{
var selectedClient = myTreeView1.GetNode(typeof(ClientViewModel));
int? id = null;
if (selectedClient != null)
{
id = Convert.ToInt32((selectedClient as ClientViewModel).Id);
}
byte[] bytes = new byte[16];
if (!id.HasValue)
{
id = -1;
}
BitConverter.GetBytes(id.Value).CopyTo(bytes, 0);
return new()
{
Id = new Guid(bytes)
};
}
}
public bool CreateChartDocument(PluginsConventionSaveDocument saveDocument)
{
try
{
ExcelChart excelChart1 = new ExcelChart();
var statuses = _statusLogic.Read(null);
var clients = _clientLogic.Read(null);
List<(string, int)> dates = new List<(string, int)>();
for (int i = 0; i < statuses.Count; i++)
{
int counter = 0;
for (int j = 0; j < clients.Count; j++)
{
if (clients[j].Status == statuses[i].Name && clients[j].Amount != null) counter++;
}
dates.Add((statuses[i].Name, counter));
}
excelChart1.Load(new ChartInfo
{
Path = saveDocument.FileName,
Title = "Сколько клиентов какого статуса совершали покупки",
DiagrammTitle = "Круговая диаграмма",
Dates = dates,
DirLegend = DirectionLegend.Right
});
return true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
}
public bool CreateSimpleDocument(PluginsConventionSaveDocument saveDocument)
{
try
{
WordText wordText1 = new WordText();
List<string> textList = new List<string>();
var list = _clientLogic.Read(null);
if (list != null)
{
foreach (var item in list)
{
if (item.Amount != null && item.Amount != "")
{
string clients = string.Concat("ФИО: ", item.Name, " Отзывы: ", item.Reviews);
textList.Add(clients);
}
}
string[] textArray = textList.ToArray();
wordText1.CreateWordText(new(saveDocument.FileName, "Документ по клиентам, совершавшим покупки (ФИО клиента и его отзывы)", textArray));
}
return true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
}
public bool CreateTableDocument(PluginsConventionSaveDocument saveDocument)
{
try
{
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
ComponentDocumentWithTableMultiHeaderPdf componentDocumentWithTableMultiHeaderPdf1 = new ComponentDocumentWithTableMultiHeaderPdf();
var clients = _clientLogic.Read(null);
for (int i = 0; i < clients.Count; i++)
{
if (clients[i].Amount == null || clients[i].Amount == "") { clients[i].Amount = "нет"; }
}
componentDocumentWithTableMultiHeaderPdf1.CreateDoc(new ComponentDocumentWithTableHeaderDataConfig<ClientViewModel>
{
FilePath = saveDocument.FileName,
Header = "Отчет по клиентам",
ColumnsRowsWidth = new List<(int, int)> { (5, 5), (10, 5), (15, 0), (15, 0) },
Headers = new List<(int ColumnIndex, int RowIndex, string Header, string PropertyName)>
{
(0, 0, "Id", "Id"),
(1, 0, "ФИО", "Name"),
(2, 0, "Статус", "Status"),
(3, 0, "Сумма покупок", "Amount")
},
Data = clients
});
return true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
}
private void Load()
{
List<string> stringToHierachy = new List<string>() { "Status", "Amount", "Id", "Name" };
myTreeView1.addToHierarchy(stringToHierachy);
LoadData();
}
private void LoadData()
{
try
{
var list = _clientLogic.Read(null);
for (int i = 0; i < list.Count; i++)
{
if (list[i].Amount == null || list[i].Amount == "") { list[i].Amount = "нет"; }
}
myTreeView1.LoadTree(list);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
public bool DeleteElement(PluginsConventionElement element)
{
var selectedClient = myTreeView1.GetNode(typeof(ClientViewModel));
int id = Convert.ToInt32((selectedClient as ClientViewModel).Id);
try
{
_clientLogic.Delete(new ClientBindingModel { Id = id });
LoadData();
return true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
}
public Form GetForm(PluginsConventionElement element)
{
if (element == null)
{
return new FormClient(_clientLogic, _statusLogic);
}
if (element.Id.GetHashCode() >= 0)
{
FormClient form = new FormClient(_clientLogic, _statusLogic);
form.Id = element.Id.GetHashCode();
return form;
}
return null;
}
public Form GetThesaurus()
{
return new FormStatus(_statusLogic);
}
public void ReloadData()
{
LoadData();
}
}
}