All checks were successful
Build and Deploy / deploy (push) Successful in 1m17s
Co-authored-by: Cursor <cursoragent@cursor.com>
274 lines
8.0 KiB
C#
274 lines
8.0 KiB
C#
using Fake.DependencyInjection;
|
||
using Microsoft.Extensions.Logging;
|
||
using Oms.Domain.WarehouseAggregate.Consts;
|
||
using Oms.Domain.WarehouseAggregate.Entities;
|
||
using Oms.Domain.WarehouseAggregate.Repositories;
|
||
|
||
namespace Oms.Application.Shared;
|
||
|
||
/// <summary>
|
||
/// 仓租库龄批次维护:入库增加、出库 FIFO 扣减、移位。
|
||
/// </summary>
|
||
public class WarehouseRentLotManager : IScopedDependency
|
||
{
|
||
private readonly ICWarehouseRentLotRepository _lotRepository;
|
||
private readonly ICSkuRepository _skuRepository;
|
||
private readonly ILogger<WarehouseRentLotManager> _logger;
|
||
|
||
public WarehouseRentLotManager(
|
||
ICWarehouseRentLotRepository lotRepository,
|
||
ICSkuRepository skuRepository,
|
||
ILogger<WarehouseRentLotManager> logger)
|
||
{
|
||
_lotRepository = lotRepository;
|
||
_skuRepository = skuRepository;
|
||
_logger = logger;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 按 SKU 尺寸计算单件 CBM(cm³ / 1e6);缺尺寸返回 0。
|
||
/// </summary>
|
||
public static decimal CalcUnitCbm(CSku? sku)
|
||
{
|
||
if (sku?.LengthCm is null or <= 0
|
||
|| sku.WidthCm is null or <= 0
|
||
|| sku.HeightCm is null or <= 0)
|
||
{
|
||
return 0;
|
||
}
|
||
|
||
return Math.Round(
|
||
sku.LengthCm.Value * sku.WidthCm.Value * sku.HeightCm.Value / 1_000_000M,
|
||
6,
|
||
MidpointRounding.AwayFromZero);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 按 SKU 当前尺寸回刷仍缺体积的在库批次 UnitCbm。
|
||
/// </summary>
|
||
public async Task<int> RefreshUnitCbmForSkuAsync(int skuId, CancellationToken cancellationToken = default)
|
||
{
|
||
if (skuId <= 0)
|
||
{
|
||
return 0;
|
||
}
|
||
|
||
var sku = await _skuRepository.FirstOrDefaultAsync(x => x.Id == skuId);
|
||
var cbm = CalcUnitCbm(sku);
|
||
if (cbm <= 0)
|
||
{
|
||
return 0;
|
||
}
|
||
|
||
var lots = await _lotRepository.GetListAsync(x =>
|
||
x.SkuId == skuId && x.RemainQty > 0 && x.UnitCbm <= 0);
|
||
foreach (var lot in lots)
|
||
{
|
||
lot.UnitCbm = cbm;
|
||
await _lotRepository.UpdateAsync(lot, cancellationToken);
|
||
}
|
||
|
||
return lots.Count;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 对给定批次列表回刷 UnitCbm=0(就地更新实体,便于日结直接用)。
|
||
/// </summary>
|
||
public async Task RefreshUnitCbmForLotsAsync(
|
||
IReadOnlyList<CWarehouseRentLot> lots,
|
||
CancellationToken cancellationToken = default)
|
||
{
|
||
var need = lots.Where(x => x.UnitCbm <= 0 && x.RemainQty > 0).ToList();
|
||
if (need.Count == 0)
|
||
{
|
||
return;
|
||
}
|
||
|
||
var skuIds = need.Select(x => x.SkuId).Distinct().ToArray();
|
||
var skus = await _skuRepository.GetListAsync(x => skuIds.Contains(x.Id));
|
||
var skuMap = skus.ToDictionary(x => x.Id);
|
||
foreach (var lot in need)
|
||
{
|
||
if (!skuMap.TryGetValue(lot.SkuId, out var sku))
|
||
{
|
||
continue;
|
||
}
|
||
|
||
var cbm = CalcUnitCbm(sku);
|
||
if (cbm <= 0)
|
||
{
|
||
continue;
|
||
}
|
||
|
||
lot.UnitCbm = cbm;
|
||
await _lotRepository.UpdateAsync(lot, cancellationToken);
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 入库/上架新增批次。
|
||
/// </summary>
|
||
public async Task AddLotAsync(
|
||
int customerId,
|
||
int warehouseId,
|
||
int skuId,
|
||
int? binLocationId,
|
||
int qty,
|
||
string sourceType,
|
||
int sourceId,
|
||
DateTime? inboundTime = null,
|
||
CancellationToken cancellationToken = default)
|
||
{
|
||
if (qty <= 0)
|
||
{
|
||
return;
|
||
}
|
||
|
||
var sku = await _skuRepository.FirstOrDefaultAsync(x => x.Id == skuId);
|
||
var lot = new CWarehouseRentLot
|
||
{
|
||
CustomerId = customerId,
|
||
WarehouseId = warehouseId,
|
||
SkuId = skuId,
|
||
BinLocationId = binLocationId,
|
||
InboundTime = inboundTime ?? DateTime.Now,
|
||
RemainQty = qty,
|
||
UnitCbm = CalcUnitCbm(sku),
|
||
SourceType = sourceType,
|
||
SourceId = sourceId
|
||
};
|
||
await _lotRepository.InsertAsync(lot, cancellationToken);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 按客户+仓+SKU FIFO 扣减剩余量(可选限定库位)。返回未能扣减的数量(>0 表示影子账不足)。
|
||
/// </summary>
|
||
public async Task<int> DeductFifoAsync(
|
||
int customerId,
|
||
int warehouseId,
|
||
int skuId,
|
||
int qty,
|
||
int? binLocationId = null,
|
||
CancellationToken cancellationToken = default)
|
||
{
|
||
if (qty <= 0)
|
||
{
|
||
return 0;
|
||
}
|
||
|
||
var hasBin = binLocationId is > 0;
|
||
var binId = binLocationId.GetValueOrDefault();
|
||
var lots = await _lotRepository.GetListAsync(x =>
|
||
x.CustomerId == customerId
|
||
&& x.WarehouseId == warehouseId
|
||
&& x.SkuId == skuId
|
||
&& x.RemainQty > 0
|
||
&& (!hasBin || x.BinLocationId == binId));
|
||
lots = lots.OrderBy(x => x.InboundTime).ThenBy(x => x.Id).ToList();
|
||
|
||
var remaining = qty;
|
||
foreach (var lot in lots)
|
||
{
|
||
if (remaining <= 0)
|
||
{
|
||
break;
|
||
}
|
||
|
||
var take = Math.Min(lot.RemainQty, remaining);
|
||
if (take <= 0)
|
||
{
|
||
continue;
|
||
}
|
||
|
||
lot.RemainQty -= take;
|
||
remaining -= take;
|
||
await _lotRepository.UpdateAsync(lot, cancellationToken);
|
||
}
|
||
|
||
if (remaining > 0)
|
||
{
|
||
_logger.LogWarning(
|
||
"仓租批次 FIFO 不足:客户 {CustomerId} 仓 {WarehouseId} SKU {SkuId} 库位 {BinLocationId} 仍缺 {Shortfall}(请求扣 {Qty})",
|
||
customerId,
|
||
warehouseId,
|
||
skuId,
|
||
hasBin ? binId : (int?)null,
|
||
remaining,
|
||
qty);
|
||
}
|
||
|
||
return remaining;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 库位移位:从源库位 FIFO 扣减并在目标库位写入同库龄批次。
|
||
/// </summary>
|
||
public async Task RelocateAsync(
|
||
int customerId,
|
||
int warehouseId,
|
||
int skuId,
|
||
int sourceBinLocationId,
|
||
int targetBinLocationId,
|
||
int qty,
|
||
CancellationToken cancellationToken = default)
|
||
{
|
||
if (qty <= 0 || sourceBinLocationId == targetBinLocationId)
|
||
{
|
||
return;
|
||
}
|
||
|
||
var lots = await _lotRepository.GetListAsync(x =>
|
||
x.CustomerId == customerId
|
||
&& x.WarehouseId == warehouseId
|
||
&& x.SkuId == skuId
|
||
&& x.BinLocationId == sourceBinLocationId
|
||
&& x.RemainQty > 0);
|
||
lots = lots.OrderBy(x => x.InboundTime).ThenBy(x => x.Id).ToList();
|
||
|
||
var remaining = qty;
|
||
foreach (var lot in lots)
|
||
{
|
||
if (remaining <= 0)
|
||
{
|
||
break;
|
||
}
|
||
|
||
var take = Math.Min(lot.RemainQty, remaining);
|
||
if (take <= 0)
|
||
{
|
||
continue;
|
||
}
|
||
|
||
lot.RemainQty -= take;
|
||
remaining -= take;
|
||
await _lotRepository.UpdateAsync(lot, cancellationToken);
|
||
|
||
var moved = new CWarehouseRentLot
|
||
{
|
||
CustomerId = customerId,
|
||
WarehouseId = warehouseId,
|
||
SkuId = skuId,
|
||
BinLocationId = targetBinLocationId,
|
||
InboundTime = lot.InboundTime,
|
||
RemainQty = take,
|
||
UnitCbm = lot.UnitCbm,
|
||
SourceType = lot.SourceType,
|
||
SourceId = lot.SourceId
|
||
};
|
||
await _lotRepository.InsertAsync(moved, cancellationToken);
|
||
}
|
||
|
||
if (remaining > 0)
|
||
{
|
||
_logger.LogWarning(
|
||
"仓租批次移位不足:客户 {CustomerId} 仓 {WarehouseId} SKU {SkuId} {SourceBin}->{TargetBin} 仍缺 {Shortfall}",
|
||
customerId,
|
||
warehouseId,
|
||
skuId,
|
||
sourceBinLocationId,
|
||
targetBinLocationId,
|
||
remaining);
|
||
}
|
||
}
|
||
}
|