279 lines
11 KiB
C#
279 lines
11 KiB
C#
using ConstructionFirmBusinessLogic.BusinessLogics;
|
||
using ConstructionFirmDataModels.Models;
|
||
using Subd_4.BindingModels;
|
||
using Subd_4.BusinessLogicContracts;
|
||
using Subd_4.SearchModels;
|
||
|
||
namespace viewmodel
|
||
{
|
||
public partial class FormCreateProject : Form
|
||
{
|
||
|
||
private readonly IClientLogic _logicP;
|
||
private readonly IEmployeeLogic _logicC;
|
||
private readonly IProjectLogic _logicT;
|
||
private int? _id;
|
||
private Dictionary<int, (IConstructionMaterialModel, int)> _projectmaterial;
|
||
private Dictionary<int, (ITeamModel, int)> _projecteam;
|
||
public int Id { set { _id = value; } }
|
||
|
||
public FormCreateProject(IProjectLogic tlogic, IClientLogic logicP, IEmployeeLogic logicC)
|
||
{
|
||
InitializeComponent();
|
||
_logicT = tlogic;
|
||
_projectmaterial = new Dictionary<int, (IConstructionMaterialModel, int)>();
|
||
_projecteam = new Dictionary<int, (ITeamModel, int)>();
|
||
_logicP = logicP;
|
||
_logicC = logicC;
|
||
}
|
||
|
||
private void FormCreateTask_Load(object sender, EventArgs e)
|
||
{
|
||
var _listP = _logicP.ReadList(null);
|
||
var _listC = _logicC.ReadList(null);
|
||
if (_listP != null)
|
||
{
|
||
comboBoxClient.DisplayMember = "Organization";
|
||
comboBoxClient.ValueMember = "Id";
|
||
comboBoxClient.DataSource = _listP;
|
||
comboBoxClient.SelectedItem = null;
|
||
}
|
||
if (_listC != null)
|
||
{
|
||
comboBoxEmployee.DisplayMember = "FullName";
|
||
comboBoxEmployee.ValueMember = "Id";
|
||
comboBoxEmployee.DataSource = _listC;
|
||
comboBoxEmployee.SelectedItem = null;
|
||
}
|
||
if (_id.HasValue)
|
||
{
|
||
try
|
||
{
|
||
var view = _logicT.ReadElement(new ProjectSearchModel { Id = _id.Value });
|
||
if (view != null)
|
||
{
|
||
textBoxPrice.Text = CalcPrice().ToString();
|
||
_projectmaterial = view.ConstructionMaterialProjects ?? new Dictionary<int, (IConstructionMaterialModel, int)>();
|
||
_projecteam = view.TeamProject ?? new Dictionary<int, (ITeamModel, int)>();
|
||
LoadData();
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
}
|
||
}
|
||
}
|
||
|
||
private void LoadData()
|
||
{
|
||
try
|
||
{
|
||
textBoxPrice.Text = CalcPrice().ToString();
|
||
if (_projectmaterial != null)
|
||
{
|
||
dataGridView.Rows.Clear();
|
||
foreach (var rc in _projectmaterial)
|
||
{
|
||
dataGridView.Rows.Add(new object[] { rc.Value.Item1.MaterialName, rc.Value.Item1.Cost * rc.Value.Item2, rc.Value.Item1.Quantity });
|
||
}
|
||
}
|
||
if (_projecteam != null)
|
||
{
|
||
dataGridViewTeams.Rows.Clear();
|
||
foreach (var rc in _projecteam)
|
||
{
|
||
dataGridViewTeams.Rows.Add(new object[] { rc.Value.Item1.TeamName, rc.Value.Item1.LeaderName, rc.Value.Item1.Experince, rc.Value.Item1.TeamType });
|
||
}
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
}
|
||
}
|
||
|
||
private void buttonAdd_Click(object sender, EventArgs e)
|
||
{
|
||
var service = Program.ServiceProvider?.GetService(typeof(FormProjectMaterial));
|
||
if (service is FormProjectMaterial form)
|
||
{
|
||
if (form.ShowDialog() == DialogResult.OK)
|
||
{
|
||
if (form.MenuModel == null)
|
||
{
|
||
return;
|
||
}
|
||
if (_projectmaterial.ContainsKey(form.Id))
|
||
{
|
||
_projectmaterial[form.Id] = (form.MenuModel, form.Count);
|
||
}
|
||
else
|
||
{
|
||
_projectmaterial.Add(form.Id, (form.MenuModel, form.Count));
|
||
}
|
||
LoadData();
|
||
}
|
||
}
|
||
}
|
||
|
||
private void buttonUpd_Click(object sender, EventArgs e)
|
||
{
|
||
if (dataGridView.SelectedRows.Count == 1)
|
||
{
|
||
var service = Program.ServiceProvider?.GetService(typeof(FormProjectMaterial));
|
||
if (service is FormProjectMaterial form)
|
||
{
|
||
int id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells[0].Value);
|
||
form.Id = id;
|
||
form.Count = _projectmaterial[id].Item2;
|
||
if (form.ShowDialog() == DialogResult.OK)
|
||
{
|
||
if (form.MenuModel == null)
|
||
{
|
||
return;
|
||
}
|
||
_projectmaterial[form.Id] = (form.MenuModel, form.Count);
|
||
LoadData();
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
private void buttonDel_Click(object sender, EventArgs e)
|
||
{
|
||
if (dataGridView.SelectedRows.Count == 1)
|
||
{
|
||
if (MessageBox.Show("Удалить запись?", "Вопрос", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
|
||
{
|
||
try
|
||
{
|
||
_projectmaterial?.Remove(Convert.ToInt32(dataGridView.SelectedRows[0].Cells[0].Value));
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
}
|
||
LoadData();
|
||
}
|
||
}
|
||
}
|
||
|
||
private void buttonSave_Click(object sender, EventArgs e)
|
||
{
|
||
if (_projectmaterial == null || _projectmaterial.Count == 0)
|
||
{
|
||
MessageBox.Show("Заполните блюда", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
return;
|
||
}
|
||
try
|
||
{
|
||
var model = new ProjectBindingModel
|
||
{
|
||
Id = _id ?? 0,
|
||
DeadLine = DateTime.Now,
|
||
Status = ConstructionFirmDataModels.Enum.TaskStatus.Принят,
|
||
CliendId = comboBoxClient.SelectedIndex + 1,
|
||
EmployeeId = comboBoxEmployee.SelectedIndex + 1,
|
||
ConstructionMaterialProjects = _projectmaterial,
|
||
TeamProject = _projecteam
|
||
};
|
||
var operationResult = _id.HasValue ? _logicT.Update(model) : _logicT.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 double CalcPrice()
|
||
{
|
||
double price = 0;
|
||
foreach (var elem in _projectmaterial)
|
||
{
|
||
price += ((elem.Value.Item1?.Cost ?? 0) * elem.Value.Item2);
|
||
}
|
||
return price;
|
||
}
|
||
|
||
private void buttonCancel_Click(object sender, EventArgs e)
|
||
{
|
||
DialogResult = DialogResult.Cancel;
|
||
Close();
|
||
}
|
||
|
||
private void buttonAddTeams_Click(object sender, EventArgs e)
|
||
{
|
||
var serviceTeam = Program.ServiceProvider?.GetService(typeof(FormTeamProject));
|
||
if (serviceTeam is FormTeamProject formTeam)
|
||
{
|
||
if (formTeam.ShowDialog() == DialogResult.OK)
|
||
{
|
||
if (formTeam.TeamModel == null)
|
||
{
|
||
return;
|
||
}
|
||
if (_projecteam.ContainsKey(formTeam.Id))
|
||
{
|
||
_projecteam[formTeam.Id] = (formTeam.TeamModel, formTeam.CountTeam);
|
||
}
|
||
else
|
||
{
|
||
_projecteam.Add(formTeam.Id, (formTeam.TeamModel, formTeam.CountTeam));
|
||
}
|
||
LoadData();
|
||
}
|
||
}
|
||
}
|
||
|
||
private void buttonUpsTeams_Click(object sender, EventArgs e)
|
||
{
|
||
if (dataGridViewTeams.SelectedRows.Count == 1)
|
||
{
|
||
var service = Program.ServiceProvider?.GetService(typeof(FormTeamProject));
|
||
if (service is FormTeamProject form)
|
||
{
|
||
int id = Convert.ToInt32(dataGridViewTeams.SelectedRows[0].Cells[0].Value);
|
||
form.Id = id;
|
||
form.CountTeam = _projecteam[id].Item2;
|
||
if (form.ShowDialog() == DialogResult.OK)
|
||
{
|
||
if (form.TeamModel == null)
|
||
{
|
||
return;
|
||
}
|
||
_projecteam[form.Id] = (form.TeamModel, form.CountTeam);
|
||
LoadData();
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
private void buttonDelTeams_Click(object sender, EventArgs e)
|
||
{
|
||
if (dataGridViewTeams.SelectedRows.Count == 1)
|
||
{
|
||
if (MessageBox.Show("Удалить запись?", "Вопрос", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
|
||
{
|
||
try
|
||
{
|
||
_projecteam?.Remove(Convert.ToInt32(dataGridViewTeams.SelectedRows[0].Cells[0].Value));
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
}
|
||
LoadData();
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|