using PersonnelDepartmentContracts.BindingModels; using PersonnelDepartmentContracts.BusinessLogicContracts; namespace PersonnelDepartmentView { public partial class FormDeals : Form { private readonly IDealLogic _dealLogic; private readonly IBackUpLogic _backUpLogic; public FormDeals(IDealLogic dealLogic, IBackUpLogic backUpLogic) { InitializeComponent(); _dealLogic = dealLogic; _backUpLogic = backUpLogic; } private void LoadData() { try { var list = _dealLogic.ReadList(null); if (list != null) { dataGridView.DataSource = list; dataGridView.Columns["Id"].Visible = false; dataGridView.Columns["EmployeeId"].Visible = false; dataGridView.Columns["PositionId"].Visible = false; dataGridView.Columns["DepartmentId"].Visible = false; dataGridView.Columns["TypeId"].Visible = false; dataGridView.Columns["DateFrom"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; dataGridView.Columns["DateTo"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; dataGridView.Columns["TypeName"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; dataGridView.Columns["EmployeeName"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; dataGridView.Columns["DepartmentName"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; dataGridView.Columns["PositionName"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; } } catch (Exception ex) { MessageBox.Show(ex.Message, "Ошибка загрузки", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void FormDeals_Load(object sender, EventArgs e) { LoadData(); } private void ButtonDepartment_Click(object sender, EventArgs e) { var service = Program.ServiceProvider?.GetService(typeof(FormDepartments)); if (service is FormDepartments form) { form.ShowDialog(); } } private void ButtonEmployee_Click(object sender, EventArgs e) { var service = Program.ServiceProvider?.GetService(typeof(FormEmployees)); if (service is FormEmployees form) { form.ShowDialog(); } } private void ButtonPosition_Click(object sender, EventArgs e) { var service = Program.ServiceProvider?.GetService(typeof(FormPositions)); if (service is FormPositions form) { form.ShowDialog(); } } private void ButtonType_Click(object sender, EventArgs e) { var service = Program.ServiceProvider?.GetService(typeof(FormTypes)); if (service is FormTypes form) { form.ShowDialog(); } } private void ButtonAdd_Click(object sender, EventArgs e) { var service = Program.ServiceProvider?.GetService(typeof(FormDeal)); if (service is FormDeal form) { if (form.ShowDialog() == DialogResult.OK) { LoadData(); } } } private void ButtonChange_Click(object sender, EventArgs e) { if (dataGridView.SelectedRows.Count == 1) { var service = Program.ServiceProvider?.GetService(typeof(FormDeal)); if (service is FormDeal form) { form.Id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); if (form.ShowDialog() == DialogResult.OK) { LoadData(); } } } } private void ButtonDelete_Click(object sender, EventArgs e) { if (dataGridView.SelectedRows.Count == 1) { if (MessageBox.Show("Удалить запись?", "Вопрос", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { int id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); try { if (!_dealLogic.Delete(new DealBindingModel { Id = id })) { throw new Exception("Ошибка при удалении."); } LoadData(); } catch (Exception ex) { MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } } private void ButtonUpdate_Click(object sender, EventArgs e) { LoadData(); } private void ButtonGenerate_Click(object sender, EventArgs e) { var service = Program.ServiceProvider?.GetService(typeof(FormGeneration)); if (service is FormGeneration form) { form.ShowDialog(); } } private void CreateBackup_Click(object sender, EventArgs e) { try { if (_backUpLogic != null) { var fbd = new FolderBrowserDialog(); if (fbd.ShowDialog() == DialogResult.OK) { _backUpLogic.CreateBackUp(new BackUpBinidngModel { FolderName = fbd.SelectedPath }); MessageBox.Show("Бекап создан", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information); } } } catch (Exception ex) { MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } }