2023-10-27 20:29:38 +04:00

228 lines
6.4 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using ClientBusinessLogic.BusinessLogics;
using ClientsContracts.BusinessLogicContracts;
using ClientsContracts.ViewModels;
using ClientsContracts.BindingModels;
using ControlsLibraryNet60.Data;
using ComponentsLibraryNet60.Models;
using UnvisableComponents;
using Unity;
using DocumentFormat.OpenXml.Spreadsheet;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Window;
namespace WinForms
{
public partial class FormMain : Form
{
private readonly IClientLogic _clientLogic;
private readonly IStatusLogic _statusLogic;
public FormMain(IClientLogic clientLogic, IStatusLogic statusLogic)
{
_clientLogic = clientLogic;
_statusLogic = statusLogic;
InitializeComponent();
List<string> stringToHierachy = new List<string>() { "Status", "Amount", "Id", "Name" };
myTreeView1.addToHierarchy(stringToHierachy);
myTreeView1.ContextMenuStrip = contextMenuStrip1;
}
private void FormMain_Load(object sender, EventArgs e)
{
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 = 0; }
}
myTreeView1.LoadTree(list);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void AddNewElement()
{
var form = Program.Container.Resolve<FormClient>();
if (form.ShowDialog() == DialogResult.OK)
{
LoadData();
}
}
private void UpdateElement()
{
var form = Program.Container.Resolve<FormClient>();
var selectedClient = myTreeView1.GetNode(typeof(ClientBindingModel));
if (selectedClient != null)
{
form.Id = Convert.ToInt32((selectedClient as ClientBindingModel).Id);
if (form.ShowDialog() == DialogResult.OK)
{
LoadData();
}
}
else
{
MessageBox.Show("Выберите клиента для редактирования");
}
}
private void DeleteElement()
{
if (MessageBox.Show("Удалить запись", "Вопрос", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
var selectedClient = myTreeView1.GetNode(typeof(ClientBindingModel));
int id = Convert.ToInt32((selectedClient as ClientBindingModel).Id);
try
{
_clientLogic.Delete(new ClientBindingModel { Id = id });
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
LoadData();
}
}
private void CreateWord()
{
string fileName = "";
using (var dialog = new SaveFileDialog { Filter = "docx|*.docx" })
{
if (dialog.ShowDialog() == DialogResult.OK)
{
fileName = dialog.FileName.ToString();
MessageBox.Show("Выполнено", "Успех", MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
}
List<string> textList = new List<string>();
var list = _clientLogic.Read(null);
if (list != null)
{
foreach (var item in list)
{
if (item.Amount != null)
{
string clients = string.Concat("ФИО: ", item.Name, " Отзывы: ", item.Reviews);
textList.Add(clients);
}
}
string[] textArray = textList.ToArray();
wordText1.CreateWordText(new(fileName, "Документ по клиентам, совершавшим покупки (ФИО клиента и его отзывы)", textArray));
}
}
private void createExcel()
{
string fileName = "";
using (var dialog = new SaveFileDialog { Filter = "xlsx|*.xlsx" })
{
if (dialog.ShowDialog() == DialogResult.OK)
{
fileName = dialog.FileName.ToString();
MessageBox.Show("Выполнено", "Успех", MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
}
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 = fileName,
Title = "Сколько клиентов какого статуса совершали покупки",
DiagrammTitle = "Круговая диаграмма",
Dates = dates,
DirLegend = DirectionLegend.Right
});
}
private void CreatePdf()
{
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
string fileName = "";
using (var dialog = new SaveFileDialog { Filter = "pdf|*.pdf" })
{
if (dialog.ShowDialog() == DialogResult.OK)
{
fileName = dialog.FileName.ToString();
MessageBox.Show("Выполнено", "Успех", MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
}
var clients = _clientLogic.Read(null);
for (int i = 0; i < clients.Count; i++)
{
if (clients[i].Amount == null) { clients[i].Amount = 0; }
}
componentDocumentWithTableMultiHeaderPdf1.CreateDoc(new ComponentDocumentWithTableHeaderDataConfig<ClientViewModel>
{
FilePath = 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
});
}
private void статусыToolStripMenuItem_Click(object sender, EventArgs e)
{
var form = Program.Container.Resolve<FormStatus>();
form.ShowDialog();
}
private void сохранитьВПдфToolStripMenuItem_Click(object sender, EventArgs e)
{
CreatePdf();
}
private void сохранитьВЭксельToolStripMenuItem_Click(object sender, EventArgs e)
{
createExcel();
}
private void сохранитьВВордToolStripMenuItem_Click(object sender, EventArgs e)
{
CreateWord();
}
private void удалитьToolStripMenuItem_Click(object sender, EventArgs e)
{
DeleteElement();
}
private void редактироватьToolStripMenuItem_Click(object sender, EventArgs e)
{
UpdateElement();
}
private void добавитьToolStripMenuItem_Click(object sender, EventArgs e)
{
AddNewElement();
}
}
}