improvements, fixes, etc...
This commit is contained in:
parent
c1bf3640b6
commit
56123bc787
|
|
@ -8,6 +8,7 @@ using FruitBank.Common.Server;
|
|||
using FruitBank.Common.SignalRs;
|
||||
using LinqToDB;
|
||||
using Mango.Nop.Core.Dtos;
|
||||
using Mango.Nop.Core.Loggers;
|
||||
using Mango.Nop.Core.Models;
|
||||
using Nop.Core;
|
||||
using Nop.Core.Domain.Customers;
|
||||
|
|
@ -137,7 +138,22 @@ namespace Nop.Plugin.Misc.FruitBankPlugin.Controllers
|
|||
|
||||
_logger.Detail($"UpdateShippingItem invoked; id: {shippingItem.Id}");
|
||||
|
||||
await ctx.ShippingItems.UpdateAsync(shippingItem);
|
||||
if (!await ctx.UpdateShippingItemAsync(shippingItem))
|
||||
return null; //await ctx.ShippingItems.GetByIdAsync(shippingItem.Id);
|
||||
|
||||
return shippingItem;
|
||||
}
|
||||
|
||||
[SignalR(SignalRTags.UpdateMeasuredShippingItem)]
|
||||
public async Task<ShippingItem> UpdateMeasuredShippingItem(ShippingItem shippingItem)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(shippingItem);
|
||||
|
||||
_logger.Detail($"UpdateMeasuredShippingItem invoked; id: {shippingItem.Id}");
|
||||
|
||||
if (!await ctx.UpdateMeasuredShippingItemAsync(shippingItem))
|
||||
return null; //await ctx.ShippingItems.GetByIdAsync(shippingItem.Id);
|
||||
|
||||
return shippingItem;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,4 +1,8 @@
|
|||
using FruitBank.Common.Models;
|
||||
using AyCode.Core.Loggers;
|
||||
using FruitBank.Common.Entities;
|
||||
using FruitBank.Common.Interfaces;
|
||||
using FruitBank.Common.Models;
|
||||
using Mango.Nop.Core.Loggers;
|
||||
using Mango.Nop.Core.Repositories;
|
||||
using Nop.Core.Caching;
|
||||
using Nop.Core.Domain.Catalog;
|
||||
|
|
@ -6,8 +10,8 @@ using Nop.Core.Domain.Customers;
|
|||
using Nop.Data;
|
||||
using Nop.Plugin.Misc.FruitBankPlugin.Domains.DataLayer.Interfaces;
|
||||
using Nop.Services.Catalog;
|
||||
using Nop.Services.Logging;
|
||||
using NUglify.Helpers;
|
||||
using System.Transactions;
|
||||
|
||||
namespace Nop.Plugin.Misc.FruitBankPlugin.Domains.DataLayer;
|
||||
|
||||
|
|
@ -33,7 +37,7 @@ public class FruitBankDbContext : MgDbContextBase, IPartnerDbSet<PartnerDbTable>
|
|||
IRepository<Customer> customerRepository,
|
||||
IRepository<CustomerCustomerRoleMapping> customerCustomerRoleMappingRepository,
|
||||
IRepository<CustomerRole> customerRoleRepository,
|
||||
ILogger logger) : base(dataProvider, logger)
|
||||
IEnumerable<IAcLogWriterBase> logWriters) : base(dataProvider, logWriters)
|
||||
{
|
||||
_productService = productService;
|
||||
_staticCacheManager = staticCacheManager;
|
||||
|
|
@ -76,4 +80,132 @@ public class FruitBankDbContext : MgDbContextBase, IPartnerDbSet<PartnerDbTable>
|
|||
|
||||
public IQueryable<Product> GetAllProducts()
|
||||
=> Products.Table.Where(p => !p.Deleted).OrderBy(o => o.Name);
|
||||
|
||||
public Task<bool> UpdateMeasuredShippingItemAsync(ShippingItem shippingItem)
|
||||
{
|
||||
if (!shippingItem.IsMeasurable || shippingItem.IsValidMeasuringValues()) return UpdateShippingItemAsync(shippingItem);
|
||||
|
||||
Logger.Error("shippingItem.IsMeasurable && !shippingItem.IsValidMeasuringValues()");
|
||||
return Task.FromResult(false);
|
||||
}
|
||||
|
||||
public async Task<bool> UpdateShippingItemAsync(ShippingItem shippingItem)
|
||||
{
|
||||
if (shippingItem == null)
|
||||
{
|
||||
Logger.Error("shippingItem == null");
|
||||
return await Task.FromResult(false);
|
||||
}
|
||||
|
||||
using (var transaction = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
|
||||
{
|
||||
try
|
||||
{
|
||||
//Mi van ha nem jött meg a termék? Nem fogják tudni menteni... - J.
|
||||
|
||||
if (shippingItem.MeasuredQuantity <= 0) shippingItem.MeasuredQuantity = null;
|
||||
|
||||
if (!shippingItem.IsMeasurable || shippingItem.MeasuredNetWeight <= 0) shippingItem.MeasuredNetWeight = null;
|
||||
if (!shippingItem.IsMeasurable || shippingItem.MeasuredGrossWeight <= 0) shippingItem.MeasuredGrossWeight = null;
|
||||
|
||||
//Update előtt kivesszük a korábbi ShippingItem-et a db-ből! - J.
|
||||
var dbShippingItem = await ShippingItems.GetByIdAsync(shippingItem.Id);
|
||||
if (dbShippingItem == null)
|
||||
{
|
||||
Logger.Error("dbShippingItem == null");
|
||||
return await Task.FromResult(false);
|
||||
}
|
||||
|
||||
Product product = null;
|
||||
if (shippingItem.ProductId > 0)
|
||||
{
|
||||
product = await Products.GetByIdAsync(shippingItem.ProductId);
|
||||
|
||||
if (product == null)
|
||||
{
|
||||
Logger.Error($"shippingItem.ProductId > 0 && product == null; shippingItem.ProductId: {shippingItem.ProductId}");
|
||||
return await Task.FromResult(false);
|
||||
}
|
||||
}
|
||||
|
||||
shippingItem.IsMeasured = product != null && shippingItem.IsValidMeasuringValues();
|
||||
|
||||
await ShippingItems.UpdateAsync(shippingItem);
|
||||
|
||||
//TODO: a measuredweight-eket is! - J.
|
||||
if (shippingItem.ProductId == dbShippingItem.ProductId &&
|
||||
shippingItem.IsMeasured == dbShippingItem.IsMeasured && shippingItem.IsMeasurable == dbShippingItem.IsMeasurable &&
|
||||
shippingItem.MeasuredQuantity == dbShippingItem.MeasuredQuantity &&
|
||||
shippingItem.MeasuredNetWeight == dbShippingItem.MeasuredNetWeight && shippingItem.MeasuredGrossWeight == dbShippingItem.MeasuredGrossWeight)
|
||||
{
|
||||
return await Task.FromResult(true);
|
||||
}
|
||||
|
||||
//TODO: productIdUnchanged-et lekezelni! - J.
|
||||
var productIdUnchanged = shippingItem.ProductId == dbShippingItem.ProductId;
|
||||
|
||||
if (shippingItem.IsMeasured)
|
||||
{
|
||||
product!.StockQuantity += shippingItem.MeasuredQuantity.GetValueOrDefault(0);
|
||||
|
||||
if (!await UpdateProductStockQuantityAsync(product))
|
||||
{
|
||||
Logger.Error($"UpdateProductStockQuantity() == false; shippingItem! id: {product.Id}");
|
||||
return await Task.FromResult(false);
|
||||
}
|
||||
}
|
||||
|
||||
if (dbShippingItem.IsMeasured)
|
||||
{
|
||||
product = await Products.GetByIdAsync(dbShippingItem.ProductId);
|
||||
|
||||
if (product != null)
|
||||
{
|
||||
product.StockQuantity -= dbShippingItem.MeasuredQuantity.GetValueOrDefault(0);
|
||||
|
||||
if (!await UpdateProductStockQuantityAsync(product))
|
||||
{
|
||||
Logger.Error($"UpdateProductStockQuantity() == false; dbShippingItem! id: {product.Id}");
|
||||
return await Task.FromResult(false);
|
||||
}
|
||||
}
|
||||
else Logger.Warning($"product == null; dbShippingItem.ProductId: {dbShippingItem.ProductId}");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.Error($"UpdateShippingItemAsync Transaction ERROR! Id: {shippingItem?.Id}", ex);
|
||||
return await Task.FromResult(false);
|
||||
}
|
||||
|
||||
transaction.Complete();
|
||||
}
|
||||
|
||||
return await Task.FromResult(true);
|
||||
}
|
||||
|
||||
private async Task<bool> UpdateProductStockQuantityAsync(int productId)
|
||||
{
|
||||
var product = await Products.GetByIdAsync(productId);
|
||||
if (product != null) return await UpdateProductStockQuantityAsync(product);
|
||||
|
||||
Logger.Error($"product == null; id: {productId}");
|
||||
return await Task.FromResult(false);
|
||||
}
|
||||
|
||||
private async Task<bool> UpdateProductStockQuantityAsync(Product product)
|
||||
{
|
||||
//Itt mi legyen? RollBack? - J.
|
||||
if (product.StockQuantity < 0)
|
||||
Logger.Error($"product.StockQuantity < 0; Id: {product.Id}; StockQuantity: {product.StockQuantity}");
|
||||
|
||||
await Products.UpdateAsync(product, true);
|
||||
return await Task.FromResult(true);
|
||||
|
||||
//var updatedRowsCount = await DataProvider.ExecuteNonQueryAsync($"update product set {nameof(Product.StockQuantity)} = {product.StockQuantity} where {nameof(Product.Id)} = {product.Id}");
|
||||
//if (updatedRowsCount == 1) return await Task.FromResult(true);
|
||||
|
||||
//Logger.Error($"Product updatedRowsCount != 1; id: {product.Id}");
|
||||
//return await Task.FromResult(false);
|
||||
}
|
||||
}
|
||||
|
|
@ -23,8 +23,9 @@ public class ShippingItemDbTable : MgDbTableBase<ShippingItem>
|
|||
{
|
||||
return loadRelations
|
||||
? GetAll()
|
||||
.LoadWith(sd => sd.ShippingDocument).ThenLoad(s => s.Shipping)
|
||||
.LoadWith(sd => sd.ShippingDocument).ThenLoad(p => p.Partner)
|
||||
.LoadWith(si => si.ShippingDocument).ThenLoad(s => s.Shipping)
|
||||
.LoadWith(si => si.ShippingDocument).ThenLoad(p => p.Partner)
|
||||
.LoadWith(si => si.Product)
|
||||
: GetAll();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,89 @@
|
|||
using AyCode.Core.Loggers;
|
||||
using FruitBank.Common.Entities;
|
||||
using FruitBank.Common.Interfaces;
|
||||
using FruitBank.Common.Loggers;
|
||||
using Mango.Nop.Core.Loggers;
|
||||
using Mango.Nop.Services;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Nop.Core.Domain.Catalog;
|
||||
using Nop.Core.Events;
|
||||
using Nop.Plugin.Misc.FruitBankPlugin.Controllers;
|
||||
using Nop.Plugin.Misc.FruitBankPlugin.Domains.DataLayer;
|
||||
using Nop.Services.Events;
|
||||
|
||||
namespace Nop.Plugin.Misc.FruitBankPlugin.Domains.EventConsumers;
|
||||
|
||||
public class FruitBankEventConsumer(IHttpContextAccessor httpContextAccessor, FruitBankDbContext ctx, IEnumerable<IAcLogWriterBase> logWriters) :
|
||||
MgEventConsumer(httpContextAccessor, logWriters),
|
||||
IConsumer<EntityUpdatedEvent<ShippingItem>>,
|
||||
IConsumer<EntityUpdatedEvent<ShippingDocument>>
|
||||
{
|
||||
public override Task HandleEventAsync(EntityUpdatedEvent<Product> eventMessage)
|
||||
{
|
||||
|
||||
return base.HandleEventAsync(eventMessage);
|
||||
}
|
||||
|
||||
public async Task HandleEventAsync(EntityInsertedEvent<ShippingItem> eventMessage)
|
||||
{
|
||||
Logger.Info($"HandleEventAsync EntityInsertedEvent<ShippingItem>; id: {eventMessage.Entity.Id}");
|
||||
|
||||
await UpdateShippingDocumentIsAllMeasuredAsync(eventMessage.Entity);
|
||||
}
|
||||
|
||||
public async Task HandleEventAsync(EntityUpdatedEvent<ShippingItem> eventMessage)
|
||||
{
|
||||
Logger.Info($"HandleEventAsync EntityUpdatedEvent<ShippingItem>; id: {eventMessage.Entity.Id}");
|
||||
|
||||
var shippingItem = eventMessage.Entity;
|
||||
var isMeasured = shippingItem.IsValidMeasuringValues();
|
||||
|
||||
if (shippingItem.IsMeasured != isMeasured)
|
||||
{
|
||||
shippingItem.IsMeasured = isMeasured;
|
||||
await ctx.ShippingItems.UpdateAsync(shippingItem, false);
|
||||
}
|
||||
|
||||
await UpdateShippingDocumentIsAllMeasuredAsync(shippingItem);
|
||||
}
|
||||
|
||||
private async Task UpdateShippingDocumentIsAllMeasuredAsync(ShippingItem shippingItem)
|
||||
{
|
||||
//TODO: where: && IsMeasureable!!!! - J.
|
||||
var isAllShippingItemMeasured = ctx.ShippingItems.GetAll(false).Where(si => si.ShippingDocumentId == shippingItem.ShippingDocumentId).All(si => si.IsMeasured);
|
||||
var shippingDocument = await ctx.ShippingDocuments.GetByIdAsync(shippingItem.ShippingDocumentId);
|
||||
|
||||
if (shippingDocument != null && shippingDocument.IsAllMeasured != isAllShippingItemMeasured)
|
||||
{
|
||||
shippingDocument.IsAllMeasured = isAllShippingItemMeasured;
|
||||
await ctx.ShippingDocuments.UpdateAsync(shippingDocument);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task HandleEventAsync(EntityInsertedEvent<ShippingDocument> eventMessage)
|
||||
{
|
||||
Logger.Info($"HandleEventAsync EntityInsertedEvent<ShippingDocument>; id: {eventMessage.Entity.Id}");
|
||||
|
||||
await UpdateShippingIsAllMeasuredAsync(eventMessage.Entity);
|
||||
}
|
||||
|
||||
public async Task HandleEventAsync(EntityUpdatedEvent<ShippingDocument> eventMessage)
|
||||
{
|
||||
Logger.Info($"HandleEventAsync EntityUpdatedEvent<ShippingDocument>; id: {eventMessage.Entity.Id}");
|
||||
|
||||
await UpdateShippingIsAllMeasuredAsync(eventMessage.Entity);
|
||||
}
|
||||
|
||||
private async Task UpdateShippingIsAllMeasuredAsync(ShippingDocument shippingDocument)
|
||||
{
|
||||
var isAllShippingDocumentMeasured = ctx.ShippingDocuments.GetAll(false).Where(si => si.ShippingId == shippingDocument.ShippingId).All(si => si.IsAllMeasured);
|
||||
var shipping = await ctx.Shippings.GetByIdAsync(shippingDocument.ShippingId);
|
||||
|
||||
if (shipping != null && shipping.IsAllMeasured != isAllShippingDocumentMeasured)
|
||||
{
|
||||
shipping.IsAllMeasured = isAllShippingDocumentMeasured;
|
||||
await ctx.Shippings.UpdateAsync(shipping);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -37,9 +37,9 @@ public class PluginNopStartup : INopStartup
|
|||
//register services and interfaces
|
||||
|
||||
|
||||
services.AddSingleton<IAcLogWriterBase, ConsoleLogWriter>();
|
||||
services.AddSingleton<IAcLogWriterBase, NopLogWriter>();
|
||||
services.AddSingleton<LoggerToLoggerApiController2>();
|
||||
services.AddScoped<IAcLogWriterBase, ConsoleLogWriter>();
|
||||
//services.AddScoped<IAcLogWriterBase, NopLogWriter>();
|
||||
services.AddScoped<LoggerToLoggerApiController2>();
|
||||
//services.AddSingleton<SessionService>();
|
||||
|
||||
services.AddScoped<PartnerDbTable>();
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
using AyCode.Core.Enums;
|
||||
using AyCode.Core.Loggers;
|
||||
using FruitBank.Common.Loggers;
|
||||
using Mango.Nop.Core.Loggers;
|
||||
|
||||
namespace Nop.Plugin.Misc.FruitBankPlugin.Services;
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue