сделал

This commit is contained in:
goldfest 2024-04-04 23:57:46 +04:00
parent 1a28ceccbf
commit f0850cfcbd
7 changed files with 111 additions and 129 deletions

View File

@ -71,7 +71,6 @@
textBoxCount.Name = "textBoxCount"; textBoxCount.Name = "textBoxCount";
textBoxCount.Size = new Size(246, 23); textBoxCount.Size = new Size(246, 23);
textBoxCount.TabIndex = 3; textBoxCount.TabIndex = 3;
textBoxCount.TextChanged += textBoxCount_TextChanged;
// //
// comboBoxTravel // comboBoxTravel
// //
@ -98,7 +97,6 @@
buttonSave.TabIndex = 6; buttonSave.TabIndex = 6;
buttonSave.Text = "Сохранить"; buttonSave.Text = "Сохранить";
buttonSave.UseVisualStyleBackColor = true; buttonSave.UseVisualStyleBackColor = true;
buttonSave.Click += buttonSave_Click;
// //
// buttonCancel // buttonCancel
// //
@ -108,7 +106,6 @@
buttonCancel.TabIndex = 7; buttonCancel.TabIndex = 7;
buttonCancel.Text = "Отмена"; buttonCancel.Text = "Отмена";
buttonCancel.UseVisualStyleBackColor = true; buttonCancel.UseVisualStyleBackColor = true;
buttonCancel.Click += buttonCancel_Click;
// //
// FormCreateOrder // FormCreateOrder
// //

View File

@ -1,16 +1,8 @@
using TravelCompanyContracts.BindingModels; using Microsoft.Extensions.Logging;
using TravelCompanyContracts.BindingModels;
using TravelCompanyContracts.BusinessLogicsContracts; using TravelCompanyContracts.BusinessLogicsContracts;
using TravelCompanyContracts.SearchModels; using TravelCompanyContracts.SearchModels;
using Microsoft.Extensions.Logging; using TravelCompanyContracts.ViewModels;
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 TravelCompany.Forms namespace TravelCompany.Forms
{ {
@ -19,6 +11,8 @@ namespace TravelCompany.Forms
private readonly ILogger _logger; private readonly ILogger _logger;
private readonly ITravelLogic _logicP; private readonly ITravelLogic _logicP;
private readonly IOrderLogic _logicO; private readonly IOrderLogic _logicO;
private List<TravelViewModel>? _list;
public FormCreateOrder(ILogger<FormCreateOrder> logger, ITravelLogic logicP, IOrderLogic logicO) public FormCreateOrder(ILogger<FormCreateOrder> logger, ITravelLogic logicP, IOrderLogic logicO)
{ {
InitializeComponent(); InitializeComponent();
@ -29,23 +23,17 @@ namespace TravelCompany.Forms
private void FormCreateOrder_Load(object sender, EventArgs e) private void FormCreateOrder_Load(object sender, EventArgs e)
{ {
try _list = _logicP.ReadList(null);
{ if (_list != null)
var list = _logicP.ReadList(null);
if (list != null)
{ {
comboBoxTravel.DisplayMember = "TravelName"; comboBoxTravel.DisplayMember = "TravelName";
comboBoxTravel.ValueMember = "Id"; comboBoxTravel.ValueMember = "Id";
comboBoxTravel.DataSource = list; comboBoxTravel.DataSource = _list;
comboBoxTravel.SelectedItem = null; comboBoxTravel.SelectedItem = null;
_logger.LogInformation("Загрузка изделий для заказа"); _logger.LogInformation("Загрузка пиццы для заказа");
}
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка загрузки списка изделий");
} }
} }
private void CalcSum() private void CalcSum()
{ {
if (comboBoxTravel.SelectedValue != null && !string.IsNullOrEmpty(textBoxCount.Text)) if (comboBoxTravel.SelectedValue != null && !string.IsNullOrEmpty(textBoxCount.Text))
@ -68,7 +56,8 @@ namespace TravelCompany.Forms
} }
} }
} }
private void textBoxCount_TextChanged(object sender, EventArgs e)
private void TextBoxCount_TextChanged(object sender, EventArgs e)
{ {
CalcSum(); CalcSum();
} }
@ -78,7 +67,7 @@ namespace TravelCompany.Forms
CalcSum(); CalcSum();
} }
private void buttonSave_Click(object sender, EventArgs e) private void ButtonSave_Click(object sender, EventArgs e)
{ {
if (string.IsNullOrEmpty(textBoxCount.Text)) if (string.IsNullOrEmpty(textBoxCount.Text))
{ {
@ -87,7 +76,7 @@ namespace TravelCompany.Forms
} }
if (comboBoxTravel.SelectedValue == null) if (comboBoxTravel.SelectedValue == null)
{ {
MessageBox.Show("Выберите изделие", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); MessageBox.Show("Выберите пиццу", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return; return;
} }
_logger.LogInformation("Создание заказа"); _logger.LogInformation("Создание заказа");
@ -109,15 +98,15 @@ namespace TravelCompany.Forms
} }
catch (Exception ex) catch (Exception ex)
{ {
_logger.LogError(ex, "Ошибка создания заказа"); MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); _logger.LogError(ex, "Ошибка создания заказа");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
} }
} }
private void buttonCancel_Click(object sender, EventArgs e) private void ButtonCancel_Click(object sender, EventArgs e)
{ {
DialogResult = DialogResult.Cancel; DialogResult = DialogResult.Cancel;
Close(); Close();
} }
} }
} }

View File

@ -44,7 +44,6 @@
buttonRef.TabIndex = 9; buttonRef.TabIndex = 9;
buttonRef.Text = "Обновить"; buttonRef.Text = "Обновить";
buttonRef.UseVisualStyleBackColor = true; buttonRef.UseVisualStyleBackColor = true;
buttonRef.Click += buttonRef_Click;
// //
// buttonDel // buttonDel
// //
@ -54,7 +53,6 @@
buttonDel.TabIndex = 8; buttonDel.TabIndex = 8;
buttonDel.Text = "Удалить"; buttonDel.Text = "Удалить";
buttonDel.UseVisualStyleBackColor = true; buttonDel.UseVisualStyleBackColor = true;
buttonDel.Click += buttonDel_Click;
// //
// buttonUpd // buttonUpd
// //
@ -64,7 +62,6 @@
buttonUpd.TabIndex = 7; buttonUpd.TabIndex = 7;
buttonUpd.Text = "Изменить"; buttonUpd.Text = "Изменить";
buttonUpd.UseVisualStyleBackColor = true; buttonUpd.UseVisualStyleBackColor = true;
buttonUpd.Click += buttonUpd_Click;
// //
// buttonAdd // buttonAdd
// //
@ -74,7 +71,6 @@
buttonAdd.TabIndex = 6; buttonAdd.TabIndex = 6;
buttonAdd.Text = "Добавить"; buttonAdd.Text = "Добавить";
buttonAdd.UseVisualStyleBackColor = true; buttonAdd.UseVisualStyleBackColor = true;
buttonAdd.Click += buttonAdd_Click;
// //
// dataGridView // dataGridView
// //
@ -100,7 +96,6 @@
Margin = new Padding(3, 2, 3, 2); Margin = new Padding(3, 2, 3, 2);
Name = "FormTravels"; Name = "FormTravels";
Text = "Список туристических путёвок"; Text = "Список туристических путёвок";
Load += FormTravels_Load;
((System.ComponentModel.ISupportInitialize)dataGridView).EndInit(); ((System.ComponentModel.ISupportInitialize)dataGridView).EndInit();
ResumeLayout(false); ResumeLayout(false);
} }

View File

@ -1,16 +1,6 @@
using System; using Microsoft.Extensions.Logging;
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;
using TravelCompany;
using TravelCompanyContracts.BindingModels; using TravelCompanyContracts.BindingModels;
using TravelCompanyContracts.BusinessLogicsContracts; using TravelCompanyContracts.BusinessLogicsContracts;
using Microsoft.Extensions.Logging;
namespace TravelCompany.Forms namespace TravelCompany.Forms
{ {
@ -18,6 +8,7 @@ namespace TravelCompany.Forms
{ {
private readonly ILogger _logger; private readonly ILogger _logger;
private readonly ITravelLogic _logic; private readonly ITravelLogic _logic;
public FormTravels(ILogger<FormTravels> logger, ITravelLogic logic) public FormTravels(ILogger<FormTravels> logger, ITravelLogic logic)
{ {
InitializeComponent(); InitializeComponent();
@ -25,7 +16,7 @@ namespace TravelCompany.Forms
_logic = logic; _logic = logic;
} }
private void FormTravels_Load(object sender, EventArgs e) private void FormTravel_Load(object sender, EventArgs e)
{ {
LoadData(); LoadData();
} }
@ -40,17 +31,19 @@ namespace TravelCompany.Forms
dataGridView.DataSource = list; dataGridView.DataSource = list;
dataGridView.Columns["Id"].Visible = false; dataGridView.Columns["Id"].Visible = false;
dataGridView.Columns["TravelComponents"].Visible = false; dataGridView.Columns["TravelComponents"].Visible = false;
dataGridView.Columns["TravelName"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; dataGridView.Columns["TravelName"].AutoSizeMode =
DataGridViewAutoSizeColumnMode.Fill;
} }
_logger.LogInformation("Загрузка путёвок"); _logger.LogInformation("Загрузка пиццы");
} }
catch (Exception ex) catch (Exception ex)
{ {
_logger.LogError(ex, "Ошибка загрузки компонентов"); _logger.LogError(ex, "Ошибка загрузки пиццы");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
} }
} }
private void buttonAdd_Click(object sender, EventArgs e) private void ButtonAdd_Click(object sender, EventArgs e)
{ {
var service = Program.ServiceProvider?.GetService(typeof(FormTravel)); var service = Program.ServiceProvider?.GetService(typeof(FormTravel));
if (service is FormTravel form) if (service is FormTravel form)
@ -62,7 +55,7 @@ namespace TravelCompany.Forms
} }
} }
private void buttonUpd_Click(object sender, EventArgs e) private void ButtonUpd_Click(object sender, EventArgs e)
{ {
if (dataGridView.SelectedRows.Count == 1) if (dataGridView.SelectedRows.Count == 1)
{ {
@ -78,15 +71,14 @@ namespace TravelCompany.Forms
} }
} }
private void buttonDel_Click(object sender, EventArgs e) private void ButtonDel_Click(object sender, EventArgs e)
{ {
if (dataGridView.SelectedRows.Count == 1) if (dataGridView.SelectedRows.Count == 1)
{ {
if (MessageBox.Show("Удалить запись?", "Вопрос", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) if (MessageBox.Show("Удалить запись?", "Вопрос", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{ {
int id = int id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value);
Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); _logger.LogInformation("Удаление пиццы");
_logger.LogInformation("Удаление путёвок");
try try
{ {
if (!_logic.Delete(new TravelBindingModel if (!_logic.Delete(new TravelBindingModel
@ -100,14 +92,14 @@ namespace TravelCompany.Forms
} }
catch (Exception ex) catch (Exception ex)
{ {
_logger.LogError(ex, "Ошибка удаления путевки"); _logger.LogError(ex, "Ошибка удаления пиццы");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
} }
} }
} }
} }
private void buttonRef_Click(object sender, EventArgs e) private void ButtonRef_Click(object sender, EventArgs e)
{ {
LoadData(); LoadData();
} }

View File

@ -1,33 +1,25 @@
using TravelCompanyContracts.BindingModels; using Microsoft.Extensions.Logging;
using TravelCompanyContracts.BindingModels;
using TravelCompanyContracts.BusinessLogicsContracts; using TravelCompanyContracts.BusinessLogicsContracts;
using TravelCompanyContracts.SearchModels; using TravelCompanyContracts.SearchModels;
using TravelCompanyContracts.StoragesContracts; using TravelCompanyContracts.StoragesContracts;
using TravelCompanyContracts.ViewModels; using TravelCompanyContracts.ViewModels;
using TravelCompanyDataModels.Enums; using TravelCompanyDataModels.Enums;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TravelCompanyBusinessLogic.BusinessLogic namespace TravelCompanyBusinessLogic.BusinessLogics
{ {
public class OrderLogic : IOrderLogic public class OrderLogic : IOrderLogic
{ {
//Класс с логикой для заказов будет отвечать за получение списка заказов,
//создания заказа и смены его статусов. Следует учитывать, что у заказа можно
//менять статус на новый, если его текущий статус предшествует новому
private readonly ILogger _logger; private readonly ILogger _logger;
private readonly IOrderStorage _orderStorage; private readonly IOrderStorage _orderStorage;
private readonly IShopStorage _shopStorage;
public OrderLogic(ILogger<OrderLogic> logger, IOrderStorage orderStorage) public OrderLogic(ILogger<OrderLogic> logger, IOrderStorage orderStorage, IShopStorage shopStorage)
{ {
_logger = logger; _logger = logger;
_orderStorage = orderStorage; _orderStorage = orderStorage;
_shopStorage = shopStorage;
} }
public List<OrderViewModel>? ReadList(OrderSearchModel? model) public List<OrderViewModel>? ReadList(OrderSearchModel? model)
{ {
_logger.LogInformation("ReadList. OrderId:{Id}", model?.Id); _logger.LogInformation("ReadList. OrderId:{Id}", model?.Id);
@ -40,18 +32,14 @@ namespace TravelCompanyBusinessLogic.BusinessLogic
_logger.LogInformation("ReadList. Count:{Count}", list.Count); _logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list; return list;
} }
public bool CreateOrder(OrderBindingModel model) public bool CreateOrder(OrderBindingModel model)
{ {
CheckModel(model); CheckModel(model);
if (model.Status != OrderStatus.Неизвестен) if (model.Status != OrderStatus.Неизвестен)
return false; return false;
model.Status = OrderStatus.Принят; model.Status = OrderStatus.Принят;
if (_orderStorage.Insert(model) == null) if (_orderStorage.Insert(model) == null)
{ {
model.Status = OrderStatus.Неизвестен;
_logger.LogWarning("Insert operation failed"); _logger.LogWarning("Insert operation failed");
return false; return false;
} }
@ -60,57 +48,34 @@ namespace TravelCompanyBusinessLogic.BusinessLogic
public bool TakeOrderInWork(OrderBindingModel model) public bool TakeOrderInWork(OrderBindingModel model)
{ {
return ToNextStatus(model, OrderStatus.Выполняется); return ChangeStatus(model, OrderStatus.Выполняется);
} }
public bool FinishOrder(OrderBindingModel model) public bool FinishOrder(OrderBindingModel model)
{ {
return ToNextStatus(model, OrderStatus.Готов); return ChangeStatus(model, OrderStatus.Готов);
} }
public bool DeliveryOrder(OrderBindingModel model) public bool DeliveryOrder(OrderBindingModel model)
{ {
return ToNextStatus(model, OrderStatus.Выдан); var order = _orderStorage.GetElement(new OrderSearchModel
}
public bool ToNextStatus(OrderBindingModel model, OrderStatus orderStatus)
{ {
CheckModel(model, false); Id = model.Id,
var element = _orderStorage.GetElement(new OrderSearchModel()
{
Id = model.Id
}); });
if (element == null) if (order == null)
{ {
throw new ArgumentNullException(nameof(element)); throw new ArgumentNullException(nameof(order));
}
if (!_shopStorage.RestockingShops(new SupplyBindingModel
{
TravelId = order.TravelId,
Count = order.Count
}))
{
throw new ArgumentException("Недостаточно места");
} }
model.TravelId = element.TravelId; return ChangeStatus(model, OrderStatus.Выдан);
model.DateCreate = element.DateCreate;
model.DateImplement = element.DateImplement;
model.Status = element.Status;
model.Count = element.Count;
model.Sum = element.Sum;
if (model.Status != orderStatus - 1)
{
_logger.LogWarning("Status update to " + orderStatus + " operation failed");
return false;
}
model.Status = orderStatus;
if (model.Status == OrderStatus.Выдан)
{
model.DateImplement = DateTime.Now;
}
if (_orderStorage.Update(model) == null)
{
model.Status--;
_logger.LogWarning("Changing status operation faled");
return false;
}
return true;
} }
private void CheckModel(OrderBindingModel model, bool withParams = true) private void CheckModel(OrderBindingModel model, bool withParams = true)
@ -125,17 +90,51 @@ namespace TravelCompanyBusinessLogic.BusinessLogic
} }
if (model.Count <= 0) if (model.Count <= 0)
{ {
throw new ArgumentNullException("Количество изделий должно быть больше 0", nameof(model.Count)); throw new ArgumentException("Колличество пиццы в заказе не может быть меньше 1", nameof(model.Count));
} }
if (model.Sum <= 0) if (model.Sum <= 0)
{ {
throw new ArgumentNullException("Цена заказа должна быть больше 0", nameof(model.Sum)); throw new ArgumentException("Стоимость заказа на может быть меньше 1", nameof(model.Sum));
} }
if (model.DateImplement.HasValue && model.DateImplement < model.DateCreate) if (model.DateImplement.HasValue && model.DateImplement < model.DateCreate)
{ {
throw new ArithmeticException($"Дата выдачи заказа {model.DateImplement} должна быть позже даты его создания {model.DateCreate}"); throw new ArithmeticException($"Дата выдачи заказа {model.DateImplement} не может быть раньше даты его создания {model.DateCreate}");
} }
_logger.LogInformation("Travel. TravelId:{TravelId}.Count:{Count}.Sum:{Sum}Id:{Id}", model.TravelId, model.Count, model.Sum, model.Id); _logger.LogInformation("Travel. TravelId:{TravelId}.Count:{Count}.Sum:{Sum}Id:{Id}",
model.TravelId, model.Count, model.Sum, model.Id);
}
private bool ChangeStatus(OrderBindingModel model, OrderStatus requiredStatus)
{
CheckModel(model, false);
var element = _orderStorage.GetElement(new OrderSearchModel()
{
Id = model.Id
});
if (element == null)
{
throw new ArgumentNullException(nameof(element));
}
model.DateCreate = element.DateCreate;
model.TravelId = element.TravelId;
model.DateImplement = element.DateImplement;
model.Status = element.Status;
model.Count = element.Count;
model.Sum = element.Sum;
if (requiredStatus - model.Status == 1)
{
model.Status = requiredStatus;
if (model.Status == OrderStatus.Выдан)
model.DateImplement = DateTime.Now;
if (_orderStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
_logger.LogWarning("Changing status operation faled: Current-{Status}:required-{requiredStatus}.", model.Status, requiredStatus);
throw new ArgumentException($"Невозможно присвоить статус {requiredStatus} заказу с текущим статусом {model.Status}");
} }
} }
} }

View File

@ -1,14 +1,14 @@
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using TravelCompanyContracts.BindingModels;
using TravelCompanyContracts.BusinessLogicsContracts;
using TravelCompanyContracts.SearchModels;
using TravelCompanyContracts.StoragesContracts;
using TravelCompanyContracts.ViewModels;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using TravelCompanyContracts.BindingModels;
using TravelCompanyContracts.BusinessLogicsContracts;
using TravelCompanyContracts.SearchModels;
using TravelCompanyContracts.StoragesContracts;
using TravelCompanyContracts.ViewModels;
namespace TravelCompanyBusinessLogic.BusinessLogics namespace TravelCompanyBusinessLogic.BusinessLogics
{ {
@ -125,6 +125,17 @@ namespace TravelCompanyBusinessLogic.BusinessLogics
} }
shop.ShopTravels.Add(model.TravelId, (Travel, model.Count)); shop.ShopTravels.Add(model.TravelId, (Travel, model.Count));
} }
_shopStorage.Update(new ShopBindingModel()
{
Id = shop.Id,
ShopName = shop.ShopName,
Adress = shop.Adress,
OpeningDate = shop.OpeningDate,
ShopTravels = shop.ShopTravels,
TravelMaxCount = shop.TravelMaxCount,
});
return true; return true;
} }

View File

@ -16,8 +16,7 @@ namespace TravelCompanyBusinessLogic.BusinessLogic
{ {
private readonly ILogger _logger; private readonly ILogger _logger;
private readonly ITravelStorage _TravelStorage; private readonly ITravelStorage _TravelStorage;
public TravelLogic(ILogger<TravelLogic> logger, ITravelStorage public TravelLogic(ILogger<TravelLogic> logger, ITravelStorage TravelStorage)
TravelStorage)
{ {
_logger = logger; _logger = logger;
_TravelStorage = TravelStorage; _TravelStorage = TravelStorage;