286 lines
9.2 KiB
C#
286 lines
9.2 KiB
C#
using Components.NonVisual;
|
||
using Components.SaveToPdfHelpers;
|
||
using Contracts.BindingModels;
|
||
using Contracts.BusinessLogicContracts;
|
||
using Contracts.SearchModels;
|
||
using Contracts.ViewModels;
|
||
using ControlsLibraryNet60.Core;
|
||
using ControlsLibraryNet60.Models;
|
||
using PutincevLibrary;
|
||
using PutincevLibrary.Info;
|
||
using System.Text;
|
||
using ComponentsLibraryNet60.DocumentWithChart;
|
||
using ComponentsLibraryNet60.Models;
|
||
|
||
namespace WinForms
|
||
{
|
||
public partial class FormMain : Form
|
||
{
|
||
private IAccountLogic _logic;
|
||
|
||
public FormMain(IAccountLogic logic)
|
||
{
|
||
InitializeComponent();
|
||
_logic = logic;
|
||
|
||
controlDataTable.LoadColumns(new List<DataTableColumnConfig>
|
||
{
|
||
new DataTableColumnConfig { ColumnHeader = "Идентификатор", PropertyName = "Id", Visible = true, Width = 100 },
|
||
new DataTableColumnConfig { ColumnHeader = "Логин", PropertyName = "Login", Visible = true, Width = 200 },
|
||
new DataTableColumnConfig { ColumnHeader = "Город проживания", PropertyName = "ResidenceCityName", Visible = true, Width = 150 },
|
||
new DataTableColumnConfig { ColumnHeader = "Последние попытки авторизации", PropertyName = "AccountStatusHistory", Visible = true, Width = 250 },
|
||
new DataTableColumnConfig { ColumnHeader = "Дата создания аккаунта", PropertyName = "AccountCreationDate", Visible = true, Width = 125 },
|
||
});
|
||
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
|
||
}
|
||
|
||
private void LoadData()
|
||
{
|
||
controlDataTable.Clear();
|
||
var accounts = _logic.ReadList(null);
|
||
if (accounts != null)
|
||
{
|
||
var displayAccounts = accounts.Select(account => new
|
||
{
|
||
account.Id,
|
||
account.Login,
|
||
account.ResidenceCityName,
|
||
AccountStatusHistory = string.Join(", \n", account.AuthorizationAttemptsHistory),
|
||
account.AccountCreationDate
|
||
}).ToList();
|
||
controlDataTable.AddTable(displayAccounts);
|
||
}
|
||
}
|
||
private void FormMain_Load(object sender, EventArgs e)
|
||
{
|
||
LoadData();
|
||
}
|
||
private void создатьToolStripMenuItem_Click(object sender, EventArgs e)
|
||
{
|
||
var service = Program.ServiceProvider?.GetService(typeof(FormAccount));
|
||
if (service is FormAccount form)
|
||
{
|
||
form.ShowDialog();
|
||
LoadData();
|
||
}
|
||
}
|
||
|
||
private void редактироватьToolStripMenuItem_Click(object sender, EventArgs e)
|
||
{
|
||
var service = Program.ServiceProvider?.GetService(typeof(FormAccount));
|
||
if (service is FormAccount form)
|
||
{
|
||
form._id = controlDataTable.GetSelectedObject<AccountViewModel>().Id;
|
||
if (form.ShowDialog() == DialogResult.OK)
|
||
{
|
||
LoadData();
|
||
}
|
||
}
|
||
}
|
||
|
||
private void удалитьToolStripMenuItem_Click(object sender, EventArgs e)
|
||
{
|
||
var selectedAccount = controlDataTable.GetSelectedObject<AccountViewModel>();
|
||
if (selectedAccount == null)
|
||
{
|
||
MessageBox.Show("Не выбрана запись для удаления.");
|
||
return;
|
||
}
|
||
if (MessageBox.Show("Удалить запись?", "", MessageBoxButtons.YesNo) == DialogResult.Yes)
|
||
{
|
||
if (_logic.Delete(new AccountBindingModel { Id = selectedAccount.Id }))
|
||
{
|
||
LoadData();
|
||
MessageBox.Show("Запись успешно удалена.");
|
||
}
|
||
else
|
||
{
|
||
MessageBox.Show("Ошибка при удалении записи.");
|
||
}
|
||
}
|
||
}
|
||
|
||
private void GeneratePdfButton_Click(object sender, EventArgs e)
|
||
{
|
||
try
|
||
{
|
||
var accounts = _logic.ReadList(null);
|
||
|
||
var accountTables = new List<string[,]>();
|
||
|
||
foreach (var account in accounts)
|
||
{
|
||
|
||
int rowCount = account.AuthorizationAttemptsHistory.Count;
|
||
|
||
|
||
string[,] accountTable = new string[rowCount + 1, 4];
|
||
|
||
|
||
accountTable[0, 0] = "Попытки авторизаций";
|
||
accountTable[0, 1] = "Идентификатор аккаунта";
|
||
accountTable[0, 2] = "Город проживания";
|
||
accountTable[0, 3] = "Дата создания";
|
||
|
||
|
||
for (int i = 0; i < rowCount; i++)
|
||
{
|
||
accountTable[i + 1, 0] = account.AuthorizationAttemptsHistory[i].ToString("dd-MM-yyyy");
|
||
accountTable[i + 1, 1] = account.Id.ToString();
|
||
accountTable[i + 1, 2] = account.ResidenceCityName;
|
||
accountTable[i + 1, 3] = account.AccountCreationDate.ToString("dd-MM-yyyy");
|
||
}
|
||
|
||
|
||
accountTables.Add(accountTable);
|
||
}
|
||
|
||
using (System.Windows.Forms.SaveFileDialog saveFileDialog = new System.Windows.Forms.SaveFileDialog())
|
||
{
|
||
saveFileDialog.Filter = "PDF files (*.pdf)|*.pdf|All files (*.*)|*.*";
|
||
saveFileDialog.Title = "Сохранить PDF-документ";
|
||
saveFileDialog.FileName = "Отчет1.pdf";
|
||
|
||
if (saveFileDialog.ShowDialog() == DialogResult.OK)
|
||
{
|
||
|
||
var pdfData = new PdfDocumentData(
|
||
saveFileDialog.FileName,
|
||
"Отчет по попыткам авторизации аккаунтов",
|
||
accountTables
|
||
);
|
||
|
||
tablepdf1.GeneratePdf(pdfData);
|
||
MessageBox.Show("PDF-документ успешно создан!", "Успех", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||
}
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
MessageBox.Show($"Произошла ошибка: {ex.Message}", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
}
|
||
}
|
||
|
||
private void GenerateExelButton_Click(object sender, EventArgs e)
|
||
{
|
||
ComponentExcelWithTable table = new();
|
||
|
||
var accounts = _logic.ReadList(null);
|
||
if (accounts == null || accounts.Count == 0)
|
||
{
|
||
MessageBox.Show("Нет данных для отчета.", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
return;
|
||
}
|
||
|
||
Dictionary<string, (List<(string, string)>, List<int>)> headers = new()
|
||
{
|
||
{ "Идентификатор аккаунта", (new List<(string, string)> { ("Id", "Идентификатор") }, new List<int> { 30 }) },
|
||
{ "Логин", (new List<(string, string)> { ("Login", "Логин") }, new List<int> { 30 }) },
|
||
{ "Информация", (new List<(string, string)> { ("ResidenceCityName", "Город проживания"), ("AccountCreationDate", "Дата создания") }, new List<int> { 25, 25 }) }
|
||
};
|
||
|
||
string path = AppDomain.CurrentDomain.BaseDirectory + "OrderReport.xlsx";
|
||
|
||
using (System.Windows.Forms.SaveFileDialog saveFileDialog = new System.Windows.Forms.SaveFileDialog())
|
||
{
|
||
saveFileDialog.Title = "Сохранить Excel-документ";
|
||
saveFileDialog.FileName = "Отчет2.xlsx";
|
||
|
||
if (saveFileDialog.ShowDialog() == DialogResult.OK)
|
||
{
|
||
path = saveFileDialog.FileName;
|
||
}
|
||
}
|
||
|
||
ExcelTableInfo<Contracts.ViewModels.AccountViewModel> info = new(path, "Отчет по аккаунтам", accounts, headers);
|
||
try
|
||
{
|
||
table.GenerateDocument(info);
|
||
MessageBox.Show("Сохарнено успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
MessageBox.Show(ex.Message, "Ошибка",
|
||
MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
}
|
||
}
|
||
|
||
private void GenerateWordButton_Click(object sender, EventArgs e)
|
||
{
|
||
var accounts = _logic.ReadList(null).Cast<AccountViewModel>().ToList();
|
||
|
||
var chartData = new Dictionary<string, List<(DateTime Date, int Count)>>();
|
||
|
||
foreach (var order in accounts)
|
||
{
|
||
if (!chartData.ContainsKey(order.ResidenceCityName))
|
||
{
|
||
chartData[order.ResidenceCityName] = new List<(DateTime Date, int Count)>();
|
||
}
|
||
|
||
|
||
var existingData = chartData[order.ResidenceCityName]
|
||
.FirstOrDefault(d => d.Date.Date == order.AccountCreationDate.Date);
|
||
|
||
if (existingData.Date == default)
|
||
{
|
||
chartData[order.ResidenceCityName].Add((order.AccountCreationDate.Date, 1));
|
||
}
|
||
else
|
||
{
|
||
|
||
int index = chartData[order.ResidenceCityName].FindIndex(d => d.Date.Date == order.AccountCreationDate.Date);
|
||
var updatedValue = chartData[order.ResidenceCityName][index];
|
||
chartData[order.ResidenceCityName][index] = (updatedValue.Date, updatedValue.Count + 1);
|
||
}
|
||
}
|
||
|
||
string filePath = "Отчет3.docx";
|
||
|
||
using (System.Windows.Forms.SaveFileDialog saveFileDialog = new System.Windows.Forms.SaveFileDialog())
|
||
{
|
||
saveFileDialog.Title = "Сохранить Word-документ";
|
||
saveFileDialog.FileName = "Отчет3.docx";
|
||
|
||
if (saveFileDialog.ShowDialog() == DialogResult.OK)
|
||
{
|
||
filePath = saveFileDialog.FileName;
|
||
|
||
MessageBox.Show("Docx-документ успешно создан!", "Успех", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||
}
|
||
}
|
||
var config = new ComponentDocumentWithChartConfig
|
||
{
|
||
ChartTitle = "Отчет по аккаунтам",
|
||
LegendLocation = ComponentsLibraryNet60.Models.Location.Bottom,
|
||
Data = chartData.ToDictionary(
|
||
entry => entry.Key,
|
||
entry => entry.Value.Select(d => (DateTimeToInt(d.Date), (double)d.Count)).ToList()),
|
||
FilePath = filePath,
|
||
Header = "Заголовок аккаунта"
|
||
|
||
};
|
||
|
||
var documentComponent = new ComponentDocumentWithChartLineWord();
|
||
|
||
documentComponent.CreateDoc(config);
|
||
|
||
MessageBox.Show("Документ создан успешно!");
|
||
}
|
||
private int DateTimeToInt(DateTime date)
|
||
{
|
||
return date.Day;
|
||
}
|
||
|
||
|
||
private void выбранныеТоварыToolStripMenuItem_Click(object sender, EventArgs e)
|
||
{
|
||
var service = Program.ServiceProvider?.GetService(typeof(FormCities));
|
||
if (service is FormCities form)
|
||
{
|
||
form.ShowDialog();
|
||
}
|
||
}
|
||
}
|
||
}
|