using Microsoft.EntityFrameworkCore.Metadata.Internal; using PersonnelDepartmentContracts.BindingModels; using PersonnelDepartmentContracts.BusinessLogicContracts; using PersonnelDepartmentContracts.SearchModels; 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; namespace PersonnelDepartmentView { public partial class FormDeal : Form { private readonly IDealLogic _dealLogic; private readonly IDepartmentLogic _departmentLogic; private readonly IEmployeeLogic _employeeLogic; private readonly IPositionLogic _positionLogic; private readonly ITypeLogic _typeLogic; private int? _id; public int Id { set { _id = value; } } public FormDeal(IDealLogic dealLogic, IDepartmentLogic departmentLogic, IEmployeeLogic employeeLogic, IPositionLogic positionLogic, ITypeLogic typeLogic) { InitializeComponent(); _dealLogic = dealLogic; _departmentLogic = departmentLogic; _employeeLogic = employeeLogic; _positionLogic = positionLogic; _typeLogic = typeLogic; } private void FormDeal_Load(object sender, EventArgs e) { comboBoxDepartment.DisplayMember = "Name"; comboBoxDepartment.ValueMember = "Id"; comboBoxDepartment.DataSource = _departmentLogic.ReadList(null); comboBoxEmployee.DisplayMember = "LastName"; comboBoxEmployee.ValueMember = "Id"; comboBoxEmployee.DataSource = _employeeLogic.ReadList(null); comboBoxPosition.DisplayMember = "Name"; comboBoxPosition.ValueMember = "Id"; comboBoxPosition.DataSource = _positionLogic.ReadList(null); comboBoxType.DisplayMember = "Name"; comboBoxType.ValueMember = "Id"; comboBoxType.DataSource = _typeLogic.ReadList(null); if (_id.HasValue) { try { var view = _dealLogic.ReadElement(new DealSearchModel { Id = _id.Value }); if (view != null) { dateTimePickerFrom.Value = view.DateFrom; dateTimePickerTo.Value = view.DateTo; comboBoxDepartment.SelectedItem = view.DepartmentId; comboBoxEmployee.SelectedItem = view.EmployeeId; comboBoxPosition.SelectedItem = view.PositionId; comboBoxType.SelectedItem = view.TypeId; } } catch (Exception ex) { MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } } else { comboBoxDepartment.SelectedItem = null; comboBoxEmployee.SelectedItem = null; comboBoxPosition.SelectedItem = null; comboBoxType.SelectedItem = null; } } private void ButtonSave_Click(object sender, EventArgs e) { if (comboBoxDepartment.SelectedItem == null) { MessageBox.Show("Выберите отдел", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (comboBoxEmployee.SelectedItem == null) { MessageBox.Show("Выберите сотрудника", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (comboBoxPosition.SelectedItem == null) { MessageBox.Show("Выберите должность", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (comboBoxType.SelectedItem == null) { MessageBox.Show("Выберите тип договора", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } try { var model = new DealBindingModel { Id = _id ?? 0, DateFrom = DateTime.SpecifyKind(dateTimePickerFrom.Value, DateTimeKind.Utc), DateTo = DateTime.SpecifyKind(dateTimePickerTo.Value, DateTimeKind.Utc), DepartmentId = Convert.ToInt32(comboBoxDepartment.SelectedValue), EmployeeId = Convert.ToInt32(comboBoxEmployee.SelectedValue), PositionId = Convert.ToInt32(comboBoxPosition.SelectedValue), TypeId = Convert.ToInt32(comboBoxType.SelectedValue) }; var operationResult = _id.HasValue ? _dealLogic.Update(model) : _dealLogic.Create(model); if (!operationResult) { throw new Exception("Ошибка при сохранении. Дополнительная информация в логах."); } MessageBox.Show("Сохранение прошло успешно", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information); DialogResult = DialogResult.OK; Close(); } catch (Exception ex) { MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void ButtonCancel_Click(object sender, EventArgs e) { DialogResult = DialogResult.Cancel; Close(); } } }