359 lines
12 KiB
C#

using System.Windows.Forms;
using UchetLabContracts.BindingModels;
using UchetLabContracts.BusinessLogicsContracts;
using UchetLabBusinessLogic.BusinessLogic;
using UchetLabContracts.BindingModels;
using UchetLabContracts.BusinessLogicsContracts;
using ControlsLibraryNet60.Models;
using ControlsLibraryNet60.Data;
using ComponentsLibraryNet60.DocumentWithChart;
using ComponentsLibraryNet60.Models;
using CustomComponents.Helpers;
using DocumentFormat.OpenXml.Spreadsheet;
using Microsoft.Data.SqlClient;
using Non_visual_components_Kouvshinoff.InfoModels;
using UchetLabContracts.ViewModels;
namespace WinFormUchetLab
{
public partial class FormMain : Form
{
private readonly ILabLogic _labLogic;
private readonly ICheckerLogic _checkerLogic;
public FormMain(ILabLogic dishLogic, ICheckerLogic orderLogic)
{
_labLogic = dishLogic;
_checkerLogic = orderLogic;
InitializeComponent();
List<DataTableColumnConfig> columnConfigs = new List<DataTableColumnConfig>
{
new DataTableColumnConfig
{
ColumnHeader = "Èäåíòèôèêàòîð",
PropertyName = "Id",
Width = 150,
Visible = false
},
new DataTableColumnConfig
{
ColumnHeader = "Çàäàíèå",
PropertyName = "LabTask",
Width = 250,
Visible = true
},
new DataTableColumnConfig
{
ColumnHeader = "Ïðîâåðÿþùèé",
PropertyName = "Checker",
Width = 200,
Visible = true
},
new DataTableColumnConfig
{
ColumnHeader = "Äàòà ñäà÷è",
PropertyName = "CheckDate",
Width = 200,
Visible = true
}
};
controlDataTableRow1.LoadColumns(columnConfigs);
controlDataTableRow1.ContextMenuStrip = contextMenuStrip1;
}
private void Form1_Load(object sender, EventArgs e)
{
LoadData();
}
private void LoadData()
{
controlDataTableRow1.Clear();
try
{
var labs = _labLogic.ReadList(null);
if (labs == null)
{
return;
}
foreach (var lab in labs)
{
controlDataTableRow1.AddRow(lab);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Îøèáêà", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void AddElement()
{
var service = Program.ServiceProvider?.GetService(typeof(FormLab));
if (!(service is FormLab form))
{
return;
}
if (form.ShowDialog() == DialogResult.OK)
{
LoadData();
}
}
private void UpdateElement()
{
var service = Program.ServiceProvider?.GetService(typeof(FormLab));
if (!(service is FormLab form))
{
return;
}
var selectedLab = controlDataTableRow1.GetSelectedObject<LabViewModel>();
if (selectedLab == null)
{
MessageBox.Show("Âûáåðèòå ëàáîðàòîðíóþ äëÿ ðåäàêòèðîâàíèÿ!", "Îøèáêà", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
form.Id = Convert.ToInt32(selectedLab.Id);
if (form.ShowDialog() == DialogResult.OK)
{
LoadData();
}
}
private void DeleteElement()
{
if (MessageBox.Show("Óäàëèòü çàïèñü?", "Âîïðîñ", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes)
{
return;
}
var selectedLab = controlDataTableRow1.GetSelectedObject<LabViewModel>();
int id = Convert.ToInt32(selectedLab.Id);
try
{
_labLogic.Delete(new LabBindingModel { Id = id });
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Îøèáêà", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
LoadData();
}
private void CreatePdf()
{
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;
}
List<string> images = new List<string>();
var list = _labLogic.ReadList(null);
try
{
if (list != null)
{
foreach (var item in list)
{
images.Add(item.TaskImage);
}
string[] imagesArray = images.ToArray();
pdfImage.CreatePdfDoc(new DataForImage(fileName, "Èçîáðàæåíèÿ ê ëàáîðàòîðíûì", imagesArray));
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Îøèáêà");
}
}
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);
}
else return;
}
string title = "Äîêóìåíò ñ òàáëèöåé";
var list = _labLogic.ReadList(null);
List<ColumnInfo> header = new List<ColumnInfo>();
ColumnInfo col1 = new ColumnInfo("Id", 5, "Id");
ColumnInfo col2 = new ColumnInfo("çàäàíèå", 20, "LabTask");
ColumnInfo col3 = new ColumnInfo("ïðîâåðÿþùèé", 6, "Checker");
ColumnInfo col4 = new ColumnInfo("äàòà ïðè¸ìà", 6.5, "CheckDate");
ColumnInfo colExam = new ColumnInfo("ïðè¸ì", new List<ColumnInfo> { col3, col4 });
header.Add(col1);
header.Add(col2);
header.Add(colExam);
try
{
customExcelTable.createExcel(fileName, title,
header, list);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Îøèáêà");
}
}
private void CreateWord()
{
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
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;
}
var data = new Dictionary<string, List<(int Date, double Value)>>();
var labs = _labLogic.ReadList(null);
var groupedLabs = labs.GroupBy(lab => lab.Checker)
.Select(group => new
{
CheckerName = group.Key,
LabCount = group.Count()
})
.ToList();
data["Ïðîâåðÿþùèå"] = new List<(int Date, double Value)>();
int counter = 1;
foreach (var group in groupedLabs)
{
data["Ïðîâåðÿþùèå"].Add((counter, group.LabCount));
counter++;
}
try
{
componentDocumentWithChartBarWord1.CreateDoc(new ComponentDocumentWithChartConfig
{
Header = "Ïðîâåðêè",
FilePath = fileName,
ChartTitle = "Êîëè÷åñòâî ëàáîðàòîðíûõ ó ïðîâåðÿþùèõ",
LegendLocation = ComponentsLibraryNet60.Models.Location.Bottom,
Data = data
});
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Îøèáêà");
}
}
private void ñîçäàòüToolStripMenuItem_Click(object sender, EventArgs e)
{
AddElement();
}
private void èçìåíèòüToolStripMenuItem_Click(object sender, EventArgs e)
{
UpdateElement();
}
private void óäàëèòüToolStripMenuItem_Click(object sender, EventArgs e)
{
DeleteElement();
}
private void wordToolStripMenuItem_Click(object sender, EventArgs e)
{
CreateWord();
}
private void excelToolStripMenuItem_Click(object sender, EventArgs e)
{
CreateExcel();
}
private void pdfToolStripMenuItem_Click(object sender, EventArgs e)
{
CreatePdf();
}
private void ShowFormChecker()
{
var service = Program.ServiceProvider?.GetService(typeof(FormChecker));
if (!(service is FormChecker form))
{
return;
}
form.ShowDialog();
}
private void ëàáàToolStripMenuItem_Click(object sender, EventArgs e)
{
ShowFormChecker();
}
private void MainForm_KeyDown(object sender, KeyEventArgs e)
{
if (e.Control)
{
switch (e.KeyCode)
{
case Keys.A:
AddElement(); // Ctrl+A - ñîçäàíèå íîâîé çàïèñè
e.SuppressKeyPress = true; // ïðåäîòâðàùàåì äàëüíåéøóþ îáðàáîòêó êëàâèø
break;
case Keys.U:
UpdateElement(); // Ctrl+U - ðåäàêòèðîâàíèå âûáðàííîé çàïèñè
e.SuppressKeyPress = true;
break;
case Keys.D:
DeleteElement(); // Ctrl+D - óäàëåíèå âûáðàííîé çàïèñè
e.SuppressKeyPress = true;
break;
case Keys.S:
CreatePdf(); // Ctrl+S - ñîçäàíèå ïðîñòîãî äîêóìåíòà
e.SuppressKeyPress = true;
break;
case Keys.T:
CreateExcel(); // Ctrl+T - ñîçäàíèå äîêóìåíòà ñ òàáëèöåé
e.SuppressKeyPress = true;
break;
case Keys.C:
CreateWord(); // Ctrl+C - ñîçäàíèå äîêóìåíòà ñ äèàãðàììîé
e.SuppressKeyPress = true;
break;
case Keys.M:
ShowFormChecker();
e.SuppressKeyPress = true;
break;
}
}
}
}
}