Mango.Nop.Plugins/Nop.Plugin.Misc.AIPlugin/Areas/Admin/Controllers/StockSignalREndpointServer.cs

140 lines
5.4 KiB
C#

using AyCode.Core.Loggers;
using AyCode.Services.SignalRs;
using DevExpress.Data.Helpers;
using FruitBank.Common.Entities;
using FruitBank.Common.Enums;
using FruitBank.Common.Server.Interfaces;
using FruitBank.Common.Server.Services.SignalRs;
using FruitBank.Common.SignalRs;
using Mango.Nop.Core.Loggers;
using Nop.Core;
using Nop.Core.Domain.Orders;
using Nop.Core.Events;
using Nop.Plugin.Misc.FruitBankPlugin.Domains.DataLayer;
namespace Nop.Plugin.Misc.FruitBankPlugin.Areas.Admin.Controllers;
public class StockSignalREndpointServer(StockTakingDbContext ctx, SignalRSendToClientService sendToClient, IEventPublisher eventPublisher, IWorkContext workContext, IEnumerable<IAcLogWriterBase> logWriters)
: IStockSignalREndpointServer
{
private const int LastStockTakingDays = 15;
private readonly ILogger _logger = new Logger<StockSignalREndpointServer>(logWriters.ToArray());
[SignalR(SignalRTags.GetStockTakings)]
public async Task<List<StockTaking>> GetStockTakings(bool loadRelations)
{
return await ctx.StockTakings.GetAll(loadRelations).ToListAsync();
}
public async Task<List<StockTaking>> GetStockTakingsByProductId(int productId)
{
throw new NotImplementedException();
}
[SignalR(SignalRTags.AddStockTaking)]
public async Task<StockTaking> AddStockTaking(StockTaking stockTaking)
{
var result = await ctx.TransactionSafeAsync(async _ =>
{
await ctx.StockTakings.InsertAsync(stockTaking);
var productDtos = await ctx.ProductDtos.GetAll(true).ToListAsync();
var orderItemDtos = (await ctx.OrderItemDtos.GetAllByProductIds(productDtos.Select(p => p.Id))
.Where(oi => oi.OrderDto.OrderStatusId != (int)OrderStatus.Complete).ToArrayAsync())
.Where(x => x.MeasuringStatus == MeasuringStatus.NotStarted).ToLookup(k => k.ProductId, v => v);
foreach (var productDto in productDtos)
{
var stockTakingItem = new StockTakingItem
{
StockTakingId = stockTaking.Id,
ProductId = productDto.Id,
IsMeasurable = productDto.IsMeasurable,
OriginalStockQuantity = productDto.StockQuantity,
InProcessOrdersQuantity = orderItemDtos[productDto.Id].Sum(x => x.Quantity),
//A NetWeight-et nem növeljük meg, mert az nem vonódik le automatikusan a rendelés létrehozásakor! - J.
OriginalNetWeight = productDto.NetWeight //double.Round(productDto.NetWeight + orderItemDtos[productDto.Id].Sum(x => x.NetWeight), 1)
};
await ctx.StockTakingItems.InsertAsync(stockTakingItem);
}
return true;
});
if (result) return await ctx.StockTakings.GetByIdAsync(stockTaking.Id, false);
return null;
}
[SignalR(SignalRTags.UpdateStockTaking)]
public async Task<StockTaking> UpdateStockTaking(StockTaking stockTaking)
{
if(stockTaking == null) return null;
await ctx.StockTakings.UpdateAsync(stockTaking);
return await ctx.StockTakings.GetByIdAsync(stockTaking.Id, true);
}
[SignalR(SignalRTags.GetStockTakingItems)]
public async Task<List<StockTakingItem>> GetStockTakingItems()
{
return await ctx.StockTakingItems.GetAll(true).ToListAsync();
}
[SignalR(SignalRTags.GetStockTakingItemsById)]
public async Task<StockTakingItem> GetStockTakingItemsById(int stockTakingItemId)
{
var result = await ctx.StockTakingItems.GetByIdAsync(stockTakingItemId, true);
return result;
}
[SignalR(SignalRTags.GetStockTakingItemsByProductId)]
public async Task<List<StockTakingItem>> GetStockTakingItemsByProductId(int productId)
{
return await ctx.StockTakingItems.GetAllByProductId(productId, true).ToListAsync();
}
[SignalR(SignalRTags.GetStockTakingItemsByStockTakingId)]
public async Task<List<StockTakingItem>> GetStockTakingItemsByStockTakingId(int stockTakingId)
{
return await ctx.StockTakingItems.GetAllByStockTakingId(stockTakingId, true).ToListAsync();
}
public async Task<StockTakingItem> AddStockTakingItem(StockTakingItem stockTakingItem)
{
throw new NotImplementedException();
}
public async Task<StockTakingItem> UpdateStockTakingItem(StockTakingItem stockTakingItem)
{
throw new NotImplementedException();
}
public async Task<List<StockTakingItemPallet>> GetStockTakingItemPallets()
{
throw new NotImplementedException();
}
public async Task<List<StockTakingItemPallet>> GetStockTakingItemPalletsByProductId(int productId)
{
throw new NotImplementedException();
}
public async Task<StockTakingItemPallet> AddStockTakingItemPallet(StockTakingItemPallet stockTakingItemPallet)
{
return await ctx.AddStockTakingItemPallet(stockTakingItemPallet);
}
public async Task<StockTakingItemPallet> UpdateStockTakingItemPallet(StockTakingItemPallet stockTakingItemPallet)
{
return await ctx.UpdateStockTakingItemPallet(stockTakingItemPallet);
}
[SignalR(SignalRTags.AddOrUpdateMeasuredStockTakingItemPallet)]
public async Task<StockTakingItemPallet> AddOrUpdateMeasuredStockTakingItemPallet(StockTakingItemPallet stockTakingItemPallet)
{
return await ctx.AddOrUpdateMeasuredStockTakingItemPallet(stockTakingItemPallet);
}
}