我一直在尝试将我的C#代码转换为PowerShell,但我没有成功。我尝试研究有关PowerShell的文档并尝试其中的每一个,遗憾的是,对我来说没什么用。我的C#代码在Visual Studio 2015中工作,但我无法在PowerShell中嵌入和使用它。
我的C#代码是:
using System;
using System.Collections.Concurrent;
using System.Threading;
using System.Threading.Tasks;
using NLog;
using Nop.Core.Domain;
namespace Tasks
{
public class ReleaseExpiredHoldsJob : ScheduledJob
{
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
private readonly IRepository<Shelf> _shelfRepository;
private readonly IRepository<ProductVariant> _productVariantRepository;
private readonly IRepository<OrderProductVariant> _orderProductVariantRepository;
private readonly IRepository<OrderTaskProductVariant> _orderTaskProductVariantRepository;
private readonly IRepository<OrderTask> _orderTaskRepository;
private readonly IRepository<Order> _orderRepository;
private readonly IRepository<StockWarning> _stockWarningRepository;
public ReleaseExpiredHoldsJob(IRepository<Shelf> shelfRepository, IRepository<ProductVariant> productVariantRepository, IRepository<OrderProductVariant> orderProductVariantRepository, IRepository<OrderTaskProductVariant> orderTaskProductVariantRepository, IRepository<OrderTask> orderTaskRepository, IRepository<Order> orderRepository, IRepository<StockWarning> stockWarningRepository)
{
_shelfRepository = shelfRepository;
_productVariantRepository = productVariantRepository;
_orderProductVariantRepository = orderProductVariantRepository;
_orderTaskProductVariantRepository = orderTaskProductVariantRepository;
_orderTaskRepository = orderTaskRepository;
_orderRepository = orderRepository;
_stockWarningRepository = stockWarningRepository;
}
protected override Task Run()
{
using (var scope = TransactionScopeExtensions.AsyncScope())
{
var irregularHolds = (
from s in _shelfRepository.Table
group s by s.ProductVariantId
into sg
let currentHold = sg.Sum(s => s.QuantityOnHold)
join pv in _productVariantRepository.Table on sg.Key equals pv.Id
join opv in _orderProductVariantRepository.Table on pv.Id equals opv.ProductVariantId
join otpv in _orderTaskProductVariantRepository.Table on opv.Id equals otpv.OrderProductVariantId
join ot in _orderTaskRepository.Table on otpv.OrderTaskId equals ot.Id
join o in _orderRepository.Table on ot.OrderId equals o.Id
where
ot.OrderTaskStatusId == (byte) OrderTaskStatus.Processing &&
!otpv.Completed && (ot.OrderTaskTypeId == (byte) OrderTaskType.WarehousePick ||
ot.OrderTaskTypeId == (byte) OrderTaskType.WarehouseExport) &&
o.DeliveryWindowEnd > DateTimeOffset.Now &&
(o.OrderStatusId == (int) OrderStatus.Pending || o.OrderStatusId == (int) OrderStatus.Processing)
group new {currentHold, pv, otpv} by pv.Id
into g
let expectedHold = g.Sum(t => t.otpv.Quantity - t.otpv.Fulfilled - t.otpv.Failed)
let currentHold = g.Min(t => t.currentHold)
where expectedHold != currentHold
select
new
{
ProductVariantId = g.Key,
ExpectedHold = expectedHold,
CurrentHold = currentHold
}).ToList();
Logger.Info("Processing {0} irregular holds", irregularHolds.Count);
foreach (var hold in irregularHolds)
{
Logger.Info("Processing irregular holds for product variant id {0}", hold.ProductVariantId);
var shelves =
_shelfRepository.Table.Where(
s => s.QuantityOnHold > 0 && s.ProductVariantId == hold.ProductVariantId)
.Include(s => s.ProductVariant)
.Include(s => s.ProductVariant.Product)
.ToList();
if (hold.CurrentHold > hold.ExpectedHold)
{
var excessHold = hold.CurrentHold - hold.ExpectedHold;
foreach (var shelf in shelves)
{
var toDeduct = Math.Min(excessHold, shelf.QuantityOnHold);
shelf.QuantityOnHold -= toDeduct;
_shelfRepository.Update(shelf, false);
excessHold -= toDeduct;
_stockWarningRepository.Insert(new StockWarning
{
StockWarningType = StockWarningType.ExcessHold,
ExpectedQuantity = hold.ExpectedHold,
WasQuantity = hold.CurrentHold,
AdjustedQuantity = shelf.QuantityOnHold,
ShelfName = shelf.Name,
ProductVariantId = hold.ProductVariantId
}, false);
Logger.Warn("Hold Quantity reduced from {0} to {1} on shelf {2} for product variant {3}",
hold.CurrentHold, shelf.QuantityOnHold, shelf.Name, shelf.ProductVariant.Product.Name);
}
if (excessHold > 0)
{
throw new InvalidOperationException("excessHold is greater than 0");
}
}
else if (hold.CurrentHold < hold.ExpectedHold)
{
_stockWarningRepository.Insert(new StockWarning
{
StockWarningType = StockWarningType.NotEnoughHold,
ExpectedQuantity = hold.ExpectedHold,
WasQuantity = hold.CurrentHold,
ShelfName = string.Join(", ", shelves),
ProductVariantId = hold.ProductVariantId
}, false);
Logger.Warn("Not enough hold warning for required {0}, has {1} for product variant Id {2}",
hold.CurrentHold, hold.ProductVariantId);
}
}
_shelfRepository.SaveChanges();
_stockWarningRepository.SaveChanges();
scope.Complete();
}
return Task.FromResult(true);
}
}
}
在我的PowerShell代码中,我只是把
$source = @"
在此代码的乞讨和
Add-Type -TypeDefinition $source
[Tasks.ReleaseExpiredHoldsJob]::Run()
在代码的末尾,但它不起作用。
当我执行时,我总是收到以下错误:
Add-Type : c:\Users\Team\AppData\Local\Temp\f1ng1wni.0.cs(1) : A
namespace cannot directly contain members such as fields or methods
c:\Users\Team\AppData\Local\Temp\f1ng1wni.0.cs(1) : >>> ddd
At line:1 char:1
+ Add-Type
+ ~~~~~~~~
+ CategoryInfo : InvalidData: (c:\Users\Tea...elds or methods:CompilerError) [Add-Type], Exception
+ FullyQualifiedErrorId : SOURCE_CODE_ERROR,Microsoft.PowerShell.Commands.AddTypeCommand
我的代码的每一行都有这个错误。