Статус поставки

This commit is contained in:
the 2024-06-23 22:18:51 +04:00
parent 8a1732026c
commit c87d686110
8 changed files with 89 additions and 51 deletions

View File

@ -31,7 +31,8 @@ namespace BusinessLogic.BusinessLogic
{
throw new Exception("Insert operation failed.");
}
return sell;
//return sell;
return new();
}
public SellViewModel GetElement(SellSearchModel model)
{
@ -41,17 +42,19 @@ namespace BusinessLogic.BusinessLogic
{
throw new Exception("Get element operation failed.");
}
return sell;
return new();
//return sell;
}
public IEnumerable<SellViewModel> GetElements(SellSearchModel? model)
{
var sell_list = _sellStorage.GetList(model);
if (sell_list is null || sell_list.Count() == 0)
{
_logger.LogWarning("ReadList return null list");
return [];
}
return sell_list;
//var sell_list = _sellStorage.GetList(model);
//if (sell_list is null || sell_list.Count() == 0)
//{
// _logger.LogWarning("ReadList return null list");
// return [];
//}
return [];
//return sell_list;
}
public SellViewModel Update(SellSearchModel model)
{
@ -61,7 +64,8 @@ namespace BusinessLogic.BusinessLogic
{
throw new Exception("Update operation failed.");
}
return sell;
return new();
//return sell;
}
public SellViewModel Delete(SellSearchModel model)
{
@ -71,7 +75,8 @@ namespace BusinessLogic.BusinessLogic
{
throw new Exception("Update operation failed.");
}
return sell;
return new();
//return sell;
}
}
}

View File

@ -3,6 +3,7 @@ using Contracts.BusinessLogicContracts;
using Contracts.SearchModels;
using Contracts.StorageContracts;
using Contracts.ViewModels;
using DataModels.Enums;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
@ -101,5 +102,22 @@ namespace BusinessLogic.BusinessLogic
throw new ArgumentNullException("Нет названия поставки", nameof(model.Name));
}
}
public bool StatusUpdate(SupplyBindingModel model, SupplyStatus newStatus)
{
if (model.Status + 1 != newStatus)
{
_logger.LogWarning("Status update to " + newStatus.ToString() + " operation failed. Supply status incorrect.");
return false;
}
model.Status = newStatus;
if (_supplyStorage.Update(model) == null)
{
model.Status--;
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
}
}

View File

@ -1,6 +1,7 @@
using Contracts.BindingModels;
using Contracts.SearchModels;
using Contracts.ViewModels;
using DataModels.Enums;
using System;
using System.Collections.Generic;
using System.Linq;
@ -16,5 +17,6 @@ namespace Contracts.BusinessLogicContracts
bool Create(SupplyBindingModel model);
bool Update(SupplyBindingModel model);
bool Delete(SupplyBindingModel model);
bool StatusUpdate(SupplyBindingModel model, SupplyStatus status);
}
}

View File

@ -15,7 +15,7 @@ namespace Contracts.StorageContracts
List<SupplyViewModel> GetFilteredList(SupplySearchModel model);
SupplyViewModel? GetElement(SupplySearchModel model);
SupplyViewModel? Insert(SupplyBindingModel model);
SupplyViewModel? Update(SupplyBindingModel model);
bool? Update(SupplyBindingModel model);
SupplyViewModel? Delete(SupplyBindingModel model);
}
}

View File

@ -127,7 +127,7 @@ namespace DatabaseImplement.Implements
return newProduct.GetViewModel;
}
public SupplyViewModel? Update(SupplyBindingModel model)
public bool? Update(SupplyBindingModel model)
{
using var context = new Database();
using var transaction = context.Database.BeginTransaction();
@ -137,13 +137,12 @@ namespace DatabaseImplement.Implements
rec.Id == model.Id);
if (product == null)
{
return null;
return false;
}
product.Update(model);
context.SaveChanges();
product.UpdateProducts(context, model);
transaction.Commit();
return product.GetViewModel;
return true;
}
catch
{

View File

@ -65,8 +65,7 @@ namespace DatabaseImplement.Models
}
public void Update(SupplyBindingModel model)
{
Name = model.Name;
Price = model.Price;
Status = model.Status;
}
public SupplyViewModel GetViewModel
{
@ -79,41 +78,12 @@ namespace DatabaseImplement.Models
Name = Name,
Price = Price,
Products = SupplyProducts,
SupplierId = SupplierId,
Date = Date,
Status = Status,
SupplierName = context.Suppliers.FirstOrDefault(x => x.Id == Id)?.Name ?? string.Empty,
SupplierName = Supplier.Name,
};
}
}
public void UpdateProducts(Database context, SupplyBindingModel model)
{
var supplyProducts = context.SupplyProducts.Where(rec =>
rec.Id == model.Id).ToList();
if (supplyProducts != null && supplyProducts.Count > 0)
{ // удалили те, которых нет в модели
context.SupplyProducts.RemoveRange(supplyProducts.Where(rec
=> !model.SupplyProducts.ContainsKey(rec.ProductId)));
context.SaveChanges();
// обновили количество у существующих записей
foreach (var updateProduct in supplyProducts)
{
updateProduct.Count = model.SupplyProducts[updateProduct.ProductId].Item2;
model.SupplyProducts.Remove(updateProduct.ProductId);
}
context.SaveChanges();
}
var supply = context.Supplies.First(x => x.Id == Id);
foreach (var pc in model.SupplyProducts)
{
context.SupplyProducts.Add(new SupplyProduct
{
Supply = supply,
Product = context.Products.First(x => x.Id == pc.Key),
Count = pc.Value.Item2
});
context.SaveChanges();
}
_supplyProducts = null;
}
}
}

View File

@ -33,6 +33,7 @@
menuStrip1 = new MenuStrip();
товарыToolStripMenuItem = new ToolStripMenuItem();
поставщикиToolStripMenuItem = new ToolStripMenuItem();
buttonSupplyStatusArriving = new Button();
((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit();
menuStrip1.SuspendLayout();
SuspendLayout();
@ -48,7 +49,7 @@
//
// buttonCreateSupply
//
buttonCreateSupply.Location = new Point(706, 24);
buttonCreateSupply.Location = new Point(707, 27);
buttonCreateSupply.Name = "buttonCreateSupply";
buttonCreateSupply.Size = new Size(154, 23);
buttonCreateSupply.TabIndex = 1;
@ -79,11 +80,22 @@
поставщикиToolStripMenuItem.Text = "Поставщики";
поставщикиToolStripMenuItem.Click += поставщикиToolStripMenuItem_Click_1;
//
// buttonSupplyStatusArriving
//
buttonSupplyStatusArriving.Location = new Point(707, 76);
buttonSupplyStatusArriving.Name = "buttonSupplyStatusArriving";
buttonSupplyStatusArriving.Size = new Size(154, 23);
buttonSupplyStatusArriving.TabIndex = 3;
buttonSupplyStatusArriving.Text = "Поставка в пути";
buttonSupplyStatusArriving.UseVisualStyleBackColor = true;
buttonSupplyStatusArriving.Click += buttonSupplyStatusArriving_Click;
//
// FormMain
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(901, 384);
Controls.Add(buttonSupplyStatusArriving);
Controls.Add(buttonCreateSupply);
Controls.Add(dataGridView);
Controls.Add(menuStrip1);
@ -105,5 +117,6 @@
private MenuStrip menuStrip1;
private ToolStripMenuItem товарыToolStripMenuItem;
private ToolStripMenuItem поставщикиToolStripMenuItem;
private Button buttonSupplyStatusArriving;
}
}

View File

@ -1,4 +1,7 @@
using Contracts.BusinessLogicContracts;
using Contracts.BindingModels;
using Contracts.BusinessLogicContracts;
using DataModels.Enums;
using DataModels.Models;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.Extensions.Logging;
using System;
@ -77,5 +80,33 @@ namespace WinFormsApp
form.ShowDialog();
}
}
private void buttonSupplyStatusArriving_Click(object sender, EventArgs e)
{
if (dataGridView.SelectedRows.Count == 1)
{
Guid id = (Guid)dataGridView.SelectedRows[0].Cells["Id"].Value;
_logger.LogInformation("Поставка No{id}. Меняется статус", id);
try
{
var operationResult = _supplyLogic.StatusUpdate(new SupplyBindingModel
{
Id = id,
Status = (SupplyStatus)dataGridView.SelectedRows[0].Cells["Status"].Value
}, SupplyStatus.Arriving);
if (!operationResult)
{
//throw new Exception("Ошибка при сохранении. Дополнительная информация в логах.");
}
LoadData();
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка передачи заказа в работу");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
}
}
}