LabSubd/Subd/Forms/FormVoyage.cs
2023-05-05 20:47:09 +03:00

173 lines
6.6 KiB
C#

using Contracts.BindingModels;
using Contracts.BusinessLogic;
using Contracts.SearchModel;
using DataBase;
using Microsoft.Extensions.Logging;
using NLog.LayoutRenderers;
using System.Collections.Generic;
using System.Diagnostics;
namespace Forms
{
public partial class FormVoyage : Form
{
private readonly ILogger _logger;
private readonly IVoyageLogic _logic;
private readonly IRouteLogic _routeLogic;
private readonly ICarLogic _carLogic;
private readonly IHumanLogic _humanLogic;
private readonly ICompanyLogic _companyLogic;
private int? _id;
public int Id { set { _id = value; } }
Random rndModel = new Random();
public FormVoyage(ILogger<FormVoyage> logger, IVoyageLogic logic, IRouteLogic routeLogic, ICarLogic carLogic, IHumanLogic humanLogic, ICompanyLogic companyLogic)
{
InitializeComponent();
_logger = logger;
_logic = logic;
_routeLogic = routeLogic;
_carLogic = carLogic;
_humanLogic = humanLogic;
_companyLogic = companyLogic;
}
private void FormComponent_Load(object sender, EventArgs e)
{
try
{
var routes = _routeLogic.ReadList(null);
if (routes != null)
{
RoutecomboBox.DisplayMember = "Title";
RoutecomboBox.ValueMember = "Id";
RoutecomboBox.DataSource = routes;
RoutecomboBox.SelectedItem = null;
}
var cars = _carLogic.ReadList(new CarSM { StatusId = 1});
if (cars != null)
{
CarcomboBox.DisplayMember = "Model";
CarcomboBox.ValueMember = "Id";
CarcomboBox.DataSource = cars;
CarcomboBox.SelectedItem = null;
}
var companys = _companyLogic.ReadList(new CompanySM { StatusId =1});
if (companys != null)
{
CompanycomboBox.DisplayMember = "Title";
CompanycomboBox.ValueMember = "Id";
CompanycomboBox.DataSource = companys;
CompanycomboBox.SelectedItem = null;
}
var humans = _humanLogic.ReadList(new HumanSM { StatusId=1});
if (humans != null)
{
HumancomboBox.DisplayMember = "Name";
HumancomboBox.ValueMember = "Id";
HumancomboBox.DataSource = humans;
HumancomboBox.SelectedItem = null;
}
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения voyage");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
private void SaveButton_Click(object sender, EventArgs e)
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
_logger.LogInformation("Сохранение компонента");
try
{
var model = new VoyageBM
{
Id = _id ?? 0,
RouteId = Convert.ToInt32(RoutecomboBox.SelectedValue),
CarId = Convert.ToInt32(CarcomboBox.SelectedValue),
HumanId = Convert.ToInt32(HumancomboBox.SelectedValue),
CompanyId = Convert.ToInt32(CompanycomboBox.SelectedValue),
DateStart = DateOnly.FromDateTime(DateTime.SpecifyKind(dateTimePickerFrom.Value, DateTimeKind.Utc)),
DateEnd = DateOnly.FromDateTime(DateTime.SpecifyKind(dateTimePickerTo.Value, DateTimeKind.Utc)),
};
var operationResult = _logic.Create(model);
if (!operationResult)
{
throw new Exception("Ошибка при сохранении. Дополнительная информация в логах.");
}
stopwatch.Stop();
var time = stopwatch.ElapsedMilliseconds;
Timelabel.Text = time.ToString();
//MessageBox.Show("Сохранение прошло успешно", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information);
//DialogResult = DialogResult.OK;
//Close();
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка сохранения компонента");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void ButtonCancel_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel;
Close();
}
private void PlacecomboBox_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void AddTenbutton_Click(object sender, EventArgs e)
{
Stopwatch stopwatch = new Stopwatch();
try
{
stopwatch.Start();
for (int i = 0; i < 10; i++)
{
var dateNew = new DateOnly(rndModel.Next(2000, 2023), rndModel.Next(1, 13), rndModel.Next(1, 30));
var dateEnd = dateNew.AddMonths(rndModel.Next(1, 30));
var model = new VoyageBM
{
Id = _id ?? 0,
CarId = rndModel.Next(1, _carLogic.ReadList(null).Count),
CompanyId = rndModel.Next(1, _companyLogic.ReadList(null).Count),
HumanId = rndModel.Next(1, _humanLogic.ReadList(null).Count),
RouteId = rndModel.Next(1, _routeLogic.ReadList(null).Count),
DateStart = dateNew,
DateEnd = dateEnd
};
var operationResult = _logic.Create(model);
}
stopwatch.Stop();
var time = stopwatch.ElapsedMilliseconds;
Timelabel.Text = time.ToString();
}
catch(Exception ex)
{
stopwatch.Stop();
var time = stopwatch.ElapsedMilliseconds;
Timelabel.Text = time.ToString();
MessageBox.Show(ex.Message);
}
}
}
}