新增仓储领域实体、仓储库、WarehouseService 全套接口及数据库迁移脚本; 实现店铺授权、店铺订单服务与库存匹配创建小包单;补充仓储模块 RBAC 权限种子数据。 Co-authored-by: Cursor <cursoragent@cursor.com>
110 lines
2.9 KiB
C#
110 lines
2.9 KiB
C#
using Fake.Application;
|
|
using Oms.Application.Shared;
|
|
using Oms.Domain.WarehouseAggregate.Entities;
|
|
|
|
namespace Oms.Application.Services.Warehouse;
|
|
|
|
public partial class WarehouseService
|
|
{
|
|
private async Task<int> ResolveCustomerIdAsync(int? requestedCustomerId) =>
|
|
await WarehouseCustomerHelper.ResolveRequiredCustomerIdAsync(
|
|
CustomerRepository,
|
|
CurrentUser,
|
|
requestedCustomerId);
|
|
|
|
private async Task<CSku> GetSkuOrThrowAsync(int customerId, int skuId)
|
|
{
|
|
var entity = await SkuRepository.FirstOrDefaultAsync(x => x.Id == skuId && x.CustomerId == customerId);
|
|
if (entity == null)
|
|
{
|
|
throw new BusinessException("SKU 不存在");
|
|
}
|
|
|
|
return entity;
|
|
}
|
|
|
|
private async Task UpsertInventoryQtyAsync(
|
|
int customerId,
|
|
int warehouseId,
|
|
int skuId,
|
|
int binLocationId,
|
|
int deltaQty)
|
|
{
|
|
if (deltaQty == 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
var inventory = await InventoryRepository.FirstOrDefaultAsync(x =>
|
|
x.CustomerId == customerId
|
|
&& x.WarehouseId == warehouseId
|
|
&& x.SkuId == skuId
|
|
&& x.BinLocationId == binLocationId);
|
|
if (inventory == null)
|
|
{
|
|
if (deltaQty < 0)
|
|
{
|
|
throw new BusinessException("库存不足");
|
|
}
|
|
|
|
inventory = new CInventory
|
|
{
|
|
CustomerId = customerId,
|
|
WarehouseId = warehouseId,
|
|
SkuId = skuId,
|
|
BinLocationId = binLocationId,
|
|
Qty = deltaQty
|
|
};
|
|
await InventoryRepository.InsertAsync(inventory);
|
|
return;
|
|
}
|
|
|
|
var newQty = inventory.Qty + deltaQty;
|
|
if (newQty < 0)
|
|
{
|
|
throw new BusinessException("库存不足");
|
|
}
|
|
|
|
inventory.Qty = newQty;
|
|
await InventoryRepository.UpdateAsync(inventory);
|
|
}
|
|
|
|
private async Task DeductInventoryAsync(
|
|
int customerId,
|
|
int warehouseId,
|
|
int skuId,
|
|
int requiredQty)
|
|
{
|
|
if (requiredQty <= 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
var rows = await InventoryRepository.GetListAsync(x =>
|
|
x.CustomerId == customerId
|
|
&& x.WarehouseId == warehouseId
|
|
&& x.SkuId == skuId
|
|
&& x.Qty > 0);
|
|
rows = rows.OrderBy(x => x.BinLocationId).ToList();
|
|
|
|
var remaining = requiredQty;
|
|
foreach (var row in rows)
|
|
{
|
|
if (remaining <= 0)
|
|
{
|
|
break;
|
|
}
|
|
|
|
var deduct = Math.Min(row.Qty, remaining);
|
|
row.Qty -= deduct;
|
|
remaining -= deduct;
|
|
await InventoryRepository.UpdateAsync(row);
|
|
}
|
|
|
|
if (remaining > 0)
|
|
{
|
|
throw new BusinessException($"SKU {skuId} 库存不足,缺少 {remaining} 件");
|
|
}
|
|
}
|
|
}
|