PIbd-31_Putincev.D.M._COP_29/COP/PortalAccountsView/FormMain.cs

249 lines
8.6 KiB
C#
Raw Normal View History

2024-11-19 23:18:49 +04:00
using Components.SaveToPdfHelpers;
using DocumentFormat.OpenXml.Spreadsheet;
using PdfSharp.Pdf.Advanced;
using PortalAccountsContracts.BindingModels;
using PortalAccountsContracts.BusinessLogicsContracts;
using PortalAccountsContracts.ViewModels;
using RodionovLibrary.NonVisualComponents;
using RodionovLibrary.NonVisualComponents.HelperModels;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Window;
namespace PortalAccountsView
{
public partial class FormMain : Form
{
public readonly IAccountLogic _logic;
public readonly IInterestLogic _interestlogic;
public FormMain(IAccountLogic logic, IInterestLogic interestlogic)
{
InitializeComponent();
_logic = logic;
var accounts = new List<(string, string, float)> {
("Идентификатор", "Id", 1),
("Логин", "Login", 1),
("Интерес", "InterestName", 2),
("Почта", "Email", 2)
};
customDataGridViewTable.ConfigureColumns(accounts);
_interestlogic = interestlogic;
}
private void LoadData()
{
var accounts = _logic.ReadList(null);
customDataGridViewTable.FillData(accounts);
}
private void создатьToolStripMenuItem_Click(object sender, EventArgs e)
{
var service = Program.ServiceProvider?.GetService(typeof(FormAccount));
if (service is FormAccount form)
{
if (form.ShowDialog() == DialogResult.OK)
{
LoadData();
}
}
}
private void FormMain_Load(object sender, EventArgs e)
{
LoadData();
}
private void интересыToolStripMenuItem_Click(object sender, EventArgs e)
{
var service = Program.ServiceProvider?.GetService(typeof(FormInterests));
if (service is FormInterests form)
{
form.ShowDialog();
}
}
private void документСАватарамиToolStripMenuItem_Click(object sender, EventArgs e)
{
string fileName = "";
using (var dialog = new SaveFileDialog { Filter = "Excel Files|*.xlsx" })
{
if (dialog.ShowDialog() == DialogResult.OK)
{
fileName = dialog.FileName.ToString();
MessageBox.Show("Файл выбран", "Успех", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else return;
}
List<string> images = new List<string>();
var list = _logic.ReadList(null);
try
{
if (list != null)
{
foreach (var item in list)
{
images.Add(item.AvatarPath);
}
string[] imagesArray = images.ToArray();
componentExcelWithImage.CreateExcelWithImages(fileName, "Сканы чеков", imagesArray);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка");
}
}
private void редактироватьToolStripMenuItem_Click(object sender, EventArgs e)
{
var service = Program.ServiceProvider?.GetService(typeof(FormAccount));
if (service is FormAccount form)
{
var selected = customDataGridViewTable.GetSelectedObject<AccountViewModel>();
if (selected == null)
return;
form.Id = selected.Id;
if (form.ShowDialog() == DialogResult.OK)
{
LoadData();
}
}
}
private void удалитьToolStripMenuItem_Click(object sender, EventArgs e)
{
var selected = customDataGridViewTable.GetSelectedObject<AccountViewModel>();
if (selected == null)
return;
if (MessageBox.Show("Удалить запись?", "", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
if (_logic.Delete(new AccountBindingModel { Id = selected.Id }))
{
LoadData();
}
}
}
private void документСТаблицейToolStripMenuItem_Click(object sender, EventArgs e)
{
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);
}
else return;
}
string title = "Документ с таблицей";
var mergedColumns = new List<(int, int)>
{
(1, 2),
};
var columns = new List<ColumnParameters>
{
new() { FirstRowHeader = "Идент", PropertyName = "Id", Width = 1.3 },
new() { FirstRowHeader = "Личные данные", SecondRowHeader = "Логин", PropertyName = "Login", Width = 1.7 },
new() { FirstRowHeader = "Личные данные", SecondRowHeader = "Почта", PropertyName = "Email", Width = 1.7 },
new() { FirstRowHeader = "Интерес", PropertyName = "InterestName", Width = 1.7 },
};
var list = _logic.ReadList(null);
var tableInfo = new WordTableInfo<AccountViewModel>
{
FileName = fileName,
Title = title,
ColumnParameters = columns,
Items = list,
MergedColumns = mergedColumns
};
try
{
wordTableComponent.CreateTable(tableInfo);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка");
}
}
private void документСГистограммойToolStripMenuItem_Click(object sender, EventArgs e)
{
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);
}
else return;
}
var accounts = _logic.ReadList(null);
var list = new ChartData();
var groupedAccounts = accounts.GroupBy(account => account.InterestName)
.Select(group => new
{
InterestName = group.Key,
AccountCount = group.Count()
})
.ToList();
var data = new Dictionary<string, double>();
foreach (var group in groupedAccounts)
{
data.Add(group.InterestName, (double)group.AccountCount);
}
list.Data= data;
list.SeriesName = "Пользователи";
var acc = new List<ChartData>();
acc.Add(list);
var histogrammInfo = new HistogramData
{
FilePath = fileName,
DocumentTitle = "Гистограмма PDF",
ChartTitle = "Количетсво интересов у пользователей",
LegendPosition = LegendPositions.Bottom,
ChartData = acc,
};
try
{
histogrampdf.CreateHistogramPdf(histogrammInfo);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка");
}
}
}
}