feat: 完善库存锁定幂等与批次扣减策略

This commit is contained in:
2025-12-04 11:31:26 +08:00
parent cd8862b223
commit 7e6125c687
35 changed files with 1670 additions and 0 deletions

View File

@@ -1,6 +1,7 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using TakeoutSaaS.Domain.Deliveries.Repositories;
using TakeoutSaaS.Domain.Inventory.Repositories;
using TakeoutSaaS.Domain.Merchants.Repositories;
using TakeoutSaaS.Domain.Orders.Repositories;
using TakeoutSaaS.Domain.Payments.Repositories;
@@ -45,6 +46,7 @@ public static class AppServiceCollectionExtensions
services.AddScoped<ITenantNotificationRepository, EfTenantNotificationRepository>();
services.AddScoped<ITenantPackageRepository, EfTenantPackageRepository>();
services.AddScoped<ITenantQuotaUsageRepository, EfTenantQuotaUsageRepository>();
services.AddScoped<IInventoryRepository, EfInventoryRepository>();
services.AddOptions<AppSeedOptions>()
.Bind(configuration.GetSection(AppSeedOptions.SectionName))

View File

@@ -76,6 +76,7 @@ public sealed class TakeoutAppDbContext(
public DbSet<InventoryItem> InventoryItems => Set<InventoryItem>();
public DbSet<InventoryAdjustment> InventoryAdjustments => Set<InventoryAdjustment>();
public DbSet<InventoryBatch> InventoryBatches => Set<InventoryBatch>();
public DbSet<InventoryLockRecord> InventoryLockRecords => Set<InventoryLockRecord>();
public DbSet<ShoppingCart> ShoppingCarts => Set<ShoppingCart>();
public DbSet<CartItem> CartItems => Set<CartItem>();
@@ -170,6 +171,7 @@ public sealed class TakeoutAppDbContext(
ConfigureInventoryItem(modelBuilder.Entity<InventoryItem>());
ConfigureInventoryAdjustment(modelBuilder.Entity<InventoryAdjustment>());
ConfigureInventoryBatch(modelBuilder.Entity<InventoryBatch>());
ConfigureInventoryLockRecord(modelBuilder.Entity<InventoryLockRecord>());
ConfigureShoppingCart(modelBuilder.Entity<ShoppingCart>());
ConfigureCartItem(modelBuilder.Entity<CartItem>());
ConfigureCartItemAddon(modelBuilder.Entity<CartItemAddon>());
@@ -703,6 +705,7 @@ public sealed class TakeoutAppDbContext(
builder.Property(x => x.ProductSkuId).IsRequired();
builder.Property(x => x.BatchNumber).HasMaxLength(64);
builder.Property(x => x.Location).HasMaxLength(64);
builder.Property(x => x.RowVersion).IsRowVersion();
builder.HasIndex(x => new { x.TenantId, x.StoreId, x.ProductSkuId, x.BatchNumber });
}
@@ -723,9 +726,24 @@ public sealed class TakeoutAppDbContext(
builder.Property(x => x.StoreId).IsRequired();
builder.Property(x => x.ProductSkuId).IsRequired();
builder.Property(x => x.BatchNumber).HasMaxLength(64).IsRequired();
builder.Property(x => x.RowVersion).IsRowVersion();
builder.HasIndex(x => new { x.TenantId, x.StoreId, x.ProductSkuId, x.BatchNumber }).IsUnique();
}
private static void ConfigureInventoryLockRecord(EntityTypeBuilder<InventoryLockRecord> builder)
{
builder.ToTable("inventory_lock_records");
builder.HasKey(x => x.Id);
builder.Property(x => x.StoreId).IsRequired();
builder.Property(x => x.ProductSkuId).IsRequired();
builder.Property(x => x.Quantity).IsRequired();
builder.Property(x => x.IdempotencyKey).HasMaxLength(128).IsRequired();
builder.Property(x => x.Status).HasConversion<int>();
builder.Property(x => x.RowVersion).IsRowVersion();
builder.HasIndex(x => new { x.TenantId, x.IdempotencyKey }).IsUnique();
builder.HasIndex(x => new { x.TenantId, x.StoreId, x.ProductSkuId, x.Status });
}
private static void ConfigureShoppingCart(EntityTypeBuilder<ShoppingCart> builder)
{
builder.ToTable("shopping_carts");

View File

@@ -0,0 +1,145 @@
using Microsoft.EntityFrameworkCore;
using TakeoutSaaS.Domain.Inventory.Entities;
using TakeoutSaaS.Domain.Inventory.Enums;
using TakeoutSaaS.Domain.Inventory.Repositories;
using TakeoutSaaS.Infrastructure.App.Persistence;
namespace TakeoutSaaS.Infrastructure.App.Repositories;
/// <summary>
/// 库存仓储 EF 实现。
/// </summary>
/// <remarks>
/// 提供库存与批次的读写能力。
/// </remarks>
public sealed class EfInventoryRepository(TakeoutAppDbContext context) : IInventoryRepository
{
/// <inheritdoc />
public Task<InventoryItem?> FindByIdAsync(long inventoryItemId, long tenantId, CancellationToken cancellationToken = default)
{
return context.InventoryItems
.AsNoTracking()
.Where(x => x.TenantId == tenantId && x.Id == inventoryItemId)
.FirstOrDefaultAsync(cancellationToken);
}
/// <inheritdoc />
public Task<InventoryItem?> FindBySkuAsync(long tenantId, long storeId, long productSkuId, CancellationToken cancellationToken = default)
{
return context.InventoryItems
.AsNoTracking()
.Where(x => x.TenantId == tenantId && x.StoreId == storeId && x.ProductSkuId == productSkuId)
.FirstOrDefaultAsync(cancellationToken);
}
/// <inheritdoc />
public Task<InventoryItem?> GetForUpdateAsync(long tenantId, long storeId, long productSkuId, CancellationToken cancellationToken = default)
{
return context.InventoryItems
.Where(x => x.TenantId == tenantId && x.StoreId == storeId && x.ProductSkuId == productSkuId)
.FirstOrDefaultAsync(cancellationToken);
}
/// <inheritdoc />
public Task AddItemAsync(InventoryItem item, CancellationToken cancellationToken = default)
{
return context.InventoryItems.AddAsync(item, cancellationToken).AsTask();
}
/// <inheritdoc />
public Task UpdateItemAsync(InventoryItem item, CancellationToken cancellationToken = default)
{
context.InventoryItems.Update(item);
return Task.CompletedTask;
}
/// <inheritdoc />
public Task AddAdjustmentAsync(InventoryAdjustment adjustment, CancellationToken cancellationToken = default)
{
return context.InventoryAdjustments.AddAsync(adjustment, cancellationToken).AsTask();
}
/// <inheritdoc />
public Task AddLockAsync(InventoryLockRecord lockRecord, CancellationToken cancellationToken = default)
{
return context.InventoryLockRecords.AddAsync(lockRecord, cancellationToken).AsTask();
}
/// <inheritdoc />
public Task<InventoryLockRecord?> FindLockByKeyAsync(long tenantId, string idempotencyKey, CancellationToken cancellationToken = default)
{
return context.InventoryLockRecords
.Where(x => x.TenantId == tenantId && x.IdempotencyKey == idempotencyKey)
.FirstOrDefaultAsync(cancellationToken);
}
/// <inheritdoc />
public Task MarkLockStatusAsync(InventoryLockRecord lockRecord, InventoryLockStatus status, CancellationToken cancellationToken = default)
{
lockRecord.Status = status;
context.InventoryLockRecords.Update(lockRecord);
return Task.CompletedTask;
}
/// <inheritdoc />
public async Task<IReadOnlyList<InventoryLockRecord>> FindExpiredLocksAsync(long tenantId, DateTime utcNow, CancellationToken cancellationToken = default)
{
var locks = await context.InventoryLockRecords
.Where(x => x.TenantId == tenantId && x.Status == InventoryLockStatus.Locked && x.ExpiresAt != null && x.ExpiresAt <= utcNow)
.ToListAsync(cancellationToken);
return locks;
}
/// <inheritdoc />
public async Task<IReadOnlyList<InventoryBatch>> GetBatchesForConsumeAsync(long tenantId, long storeId, long productSkuId, InventoryBatchConsumeStrategy strategy, CancellationToken cancellationToken = default)
{
var query = context.InventoryBatches
.Where(x => x.TenantId == tenantId && x.StoreId == storeId && x.ProductSkuId == productSkuId);
query = strategy == InventoryBatchConsumeStrategy.Fefo
? query.OrderBy(x => x.ExpireDate ?? DateTime.MaxValue).ThenBy(x => x.BatchNumber)
: query.OrderBy(x => x.BatchNumber);
return await query.ToListAsync(cancellationToken);
}
/// <inheritdoc />
public async Task<IReadOnlyList<InventoryBatch>> GetBatchesAsync(long tenantId, long storeId, long productSkuId, CancellationToken cancellationToken = default)
{
var batches = await context.InventoryBatches
.AsNoTracking()
.Where(x => x.TenantId == tenantId && x.StoreId == storeId && x.ProductSkuId == productSkuId)
.OrderBy(x => x.ExpireDate ?? DateTime.MaxValue)
.ThenBy(x => x.BatchNumber)
.ToListAsync(cancellationToken);
return batches;
}
/// <inheritdoc />
public Task<InventoryBatch?> GetBatchForUpdateAsync(long tenantId, long storeId, long productSkuId, string batchNumber, CancellationToken cancellationToken = default)
{
return context.InventoryBatches
.Where(x => x.TenantId == tenantId && x.StoreId == storeId && x.ProductSkuId == productSkuId && x.BatchNumber == batchNumber)
.FirstOrDefaultAsync(cancellationToken);
}
/// <inheritdoc />
public Task AddBatchAsync(InventoryBatch batch, CancellationToken cancellationToken = default)
{
return context.InventoryBatches.AddAsync(batch, cancellationToken).AsTask();
}
/// <inheritdoc />
public Task UpdateBatchAsync(InventoryBatch batch, CancellationToken cancellationToken = default)
{
context.InventoryBatches.Update(batch);
return Task.CompletedTask;
}
/// <inheritdoc />
public Task SaveChangesAsync(CancellationToken cancellationToken = default)
{
return context.SaveChangesAsync(cancellationToken);
}
}