refactor: AdminApi 剔除租户侧能力
This commit is contained in:
@@ -15,29 +15,50 @@ namespace TakeoutSaaS.Infrastructure.App.Repositories;
|
||||
public sealed class EfInventoryRepository(TakeoutAdminDbContext context) : IInventoryRepository
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public Task<InventoryItem?> FindByIdAsync(long inventoryItemId, long tenantId, CancellationToken cancellationToken = default)
|
||||
public Task<InventoryItem?> FindByIdAsync(long inventoryItemId, long? tenantId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return context.InventoryItems
|
||||
var query = context.InventoryItems
|
||||
.AsNoTracking()
|
||||
.Where(x => x.TenantId == tenantId && x.Id == inventoryItemId)
|
||||
.FirstOrDefaultAsync(cancellationToken);
|
||||
.Where(x => x.Id == inventoryItemId);
|
||||
|
||||
// 1. (空行后) 可选租户过滤
|
||||
if (tenantId.HasValue)
|
||||
{
|
||||
query = query.Where(x => x.TenantId == tenantId.Value);
|
||||
}
|
||||
|
||||
return query.FirstOrDefaultAsync(cancellationToken);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public Task<InventoryItem?> FindBySkuAsync(long tenantId, long storeId, long productSkuId, CancellationToken cancellationToken = default)
|
||||
public Task<InventoryItem?> FindBySkuAsync(long? tenantId, long storeId, long productSkuId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return context.InventoryItems
|
||||
var query = context.InventoryItems
|
||||
.AsNoTracking()
|
||||
.Where(x => x.TenantId == tenantId && x.StoreId == storeId && x.ProductSkuId == productSkuId)
|
||||
.FirstOrDefaultAsync(cancellationToken);
|
||||
.Where(x => x.StoreId == storeId && x.ProductSkuId == productSkuId);
|
||||
|
||||
// 1. (空行后) 可选租户过滤
|
||||
if (tenantId.HasValue)
|
||||
{
|
||||
query = query.Where(x => x.TenantId == tenantId.Value);
|
||||
}
|
||||
|
||||
return query.FirstOrDefaultAsync(cancellationToken);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public Task<InventoryItem?> GetForUpdateAsync(long tenantId, long storeId, long productSkuId, CancellationToken cancellationToken = default)
|
||||
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);
|
||||
var query = context.InventoryItems
|
||||
.Where(x => x.StoreId == storeId && x.ProductSkuId == productSkuId);
|
||||
|
||||
// 1. (空行后) 可选租户过滤
|
||||
if (tenantId.HasValue)
|
||||
{
|
||||
query = query.Where(x => x.TenantId == tenantId.Value);
|
||||
}
|
||||
|
||||
return query.FirstOrDefaultAsync(cancellationToken);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
@@ -66,11 +87,18 @@ public sealed class EfInventoryRepository(TakeoutAdminDbContext context) : IInve
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public Task<InventoryLockRecord?> FindLockByKeyAsync(long tenantId, string idempotencyKey, CancellationToken cancellationToken = default)
|
||||
public Task<InventoryLockRecord?> FindLockByKeyAsync(long? tenantId, string idempotencyKey, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return context.InventoryLockRecords
|
||||
.Where(x => x.TenantId == tenantId && x.IdempotencyKey == idempotencyKey)
|
||||
.FirstOrDefaultAsync(cancellationToken);
|
||||
var query = context.InventoryLockRecords
|
||||
.Where(x => x.IdempotencyKey == idempotencyKey);
|
||||
|
||||
// 1. (空行后) 可选租户过滤
|
||||
if (tenantId.HasValue)
|
||||
{
|
||||
query = query.Where(x => x.TenantId == tenantId.Value);
|
||||
}
|
||||
|
||||
return query.FirstOrDefaultAsync(cancellationToken);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
@@ -82,19 +110,32 @@ public sealed class EfInventoryRepository(TakeoutAdminDbContext context) : IInve
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<IReadOnlyList<InventoryLockRecord>> FindExpiredLocksAsync(long tenantId, DateTime utcNow, CancellationToken cancellationToken = default)
|
||||
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);
|
||||
var query = context.InventoryLockRecords
|
||||
.Where(x => x.Status == InventoryLockStatus.Locked && x.ExpiresAt != null && x.ExpiresAt <= utcNow);
|
||||
|
||||
// 1. (空行后) 可选租户过滤
|
||||
if (tenantId.HasValue)
|
||||
{
|
||||
query = query.Where(x => x.TenantId == tenantId.Value);
|
||||
}
|
||||
|
||||
var locks = await query.ToListAsync(cancellationToken);
|
||||
return locks;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<IReadOnlyList<InventoryBatch>> GetBatchesForConsumeAsync(long tenantId, long storeId, long productSkuId, InventoryBatchConsumeStrategy strategy, CancellationToken cancellationToken = default)
|
||||
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);
|
||||
.Where(x => x.StoreId == storeId && x.ProductSkuId == productSkuId);
|
||||
|
||||
// 1. (空行后) 可选租户过滤
|
||||
if (tenantId.HasValue)
|
||||
{
|
||||
query = query.Where(x => x.TenantId == tenantId.Value);
|
||||
}
|
||||
|
||||
query = strategy == InventoryBatchConsumeStrategy.Fefo
|
||||
? query.OrderBy(x => x.ExpireDate ?? DateTime.MaxValue).ThenBy(x => x.BatchNumber)
|
||||
@@ -104,11 +145,19 @@ public sealed class EfInventoryRepository(TakeoutAdminDbContext context) : IInve
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<IReadOnlyList<InventoryBatch>> GetBatchesAsync(long tenantId, long storeId, long productSkuId, CancellationToken cancellationToken = default)
|
||||
public async Task<IReadOnlyList<InventoryBatch>> GetBatchesAsync(long? tenantId, long storeId, long productSkuId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var batches = await context.InventoryBatches
|
||||
var query = context.InventoryBatches
|
||||
.AsNoTracking()
|
||||
.Where(x => x.TenantId == tenantId && x.StoreId == storeId && x.ProductSkuId == productSkuId)
|
||||
.Where(x => x.StoreId == storeId && x.ProductSkuId == productSkuId);
|
||||
|
||||
// 1. (空行后) 可选租户过滤
|
||||
if (tenantId.HasValue)
|
||||
{
|
||||
query = query.Where(x => x.TenantId == tenantId.Value);
|
||||
}
|
||||
|
||||
var batches = await query
|
||||
.OrderBy(x => x.ExpireDate ?? DateTime.MaxValue)
|
||||
.ThenBy(x => x.BatchNumber)
|
||||
.ToListAsync(cancellationToken);
|
||||
@@ -117,11 +166,18 @@ public sealed class EfInventoryRepository(TakeoutAdminDbContext context) : IInve
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public Task<InventoryBatch?> GetBatchForUpdateAsync(long tenantId, long storeId, long productSkuId, string batchNumber, CancellationToken cancellationToken = default)
|
||||
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);
|
||||
var query = context.InventoryBatches
|
||||
.Where(x => x.StoreId == storeId && x.ProductSkuId == productSkuId && x.BatchNumber == batchNumber);
|
||||
|
||||
// 1. (空行后) 可选租户过滤
|
||||
if (tenantId.HasValue)
|
||||
{
|
||||
query = query.Where(x => x.TenantId == tenantId.Value);
|
||||
}
|
||||
|
||||
return query.FirstOrDefaultAsync(cancellationToken);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
|
||||
@@ -1,68 +0,0 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using TakeoutSaaS.Domain.Tenants.Entities;
|
||||
using TakeoutSaaS.Domain.Tenants.Repositories;
|
||||
using TakeoutSaaS.Infrastructure.App.Persistence;
|
||||
|
||||
namespace TakeoutSaaS.Infrastructure.App.Repositories;
|
||||
|
||||
/// <summary>
|
||||
/// EF 公告已读仓储。
|
||||
/// </summary>
|
||||
public sealed class EfTenantAnnouncementReadRepository(TakeoutAdminDbContext context) : ITenantAnnouncementReadRepository
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public Task<IReadOnlyList<TenantAnnouncementRead>> GetByAnnouncementAsync(long tenantId, long announcementId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return context.TenantAnnouncementReads.AsNoTracking()
|
||||
.Where(x => x.TenantId == tenantId && x.AnnouncementId == announcementId)
|
||||
.OrderBy(x => x.ReadAt)
|
||||
.ToListAsync(cancellationToken)
|
||||
.ContinueWith(t => (IReadOnlyList<TenantAnnouncementRead>)t.Result, cancellationToken);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public Task<IReadOnlyList<TenantAnnouncementRead>> GetByAnnouncementAsync(long tenantId, IEnumerable<long> announcementIds, long? userId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var ids = announcementIds.Distinct().ToArray();
|
||||
if (ids.Length == 0)
|
||||
{
|
||||
return Task.FromResult<IReadOnlyList<TenantAnnouncementRead>>(Array.Empty<TenantAnnouncementRead>());
|
||||
}
|
||||
|
||||
var query = context.TenantAnnouncementReads.AsNoTracking()
|
||||
.Where(x => x.TenantId == tenantId && ids.Contains(x.AnnouncementId));
|
||||
|
||||
if (userId.HasValue)
|
||||
{
|
||||
query = query.Where(x => x.UserId == userId.Value);
|
||||
}
|
||||
else
|
||||
{
|
||||
query = query.Where(x => x.UserId == null);
|
||||
}
|
||||
|
||||
return query
|
||||
.OrderByDescending(x => x.ReadAt)
|
||||
.ToListAsync(cancellationToken)
|
||||
.ContinueWith(t => (IReadOnlyList<TenantAnnouncementRead>)t.Result, cancellationToken);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public Task<TenantAnnouncementRead?> FindAsync(long tenantId, long announcementId, long? userId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return context.TenantAnnouncementReads
|
||||
.FirstOrDefaultAsync(x => x.TenantId == tenantId && x.AnnouncementId == announcementId && x.UserId == userId, cancellationToken);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public Task AddAsync(TenantAnnouncementRead record, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return context.TenantAnnouncementReads.AddAsync(record, cancellationToken).AsTask();
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public Task SaveChangesAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
return context.SaveChangesAsync(cancellationToken);
|
||||
}
|
||||
}
|
||||
@@ -86,63 +86,6 @@ public sealed class EfTenantAnnouncementRepository(TakeoutAdminDbContext context
|
||||
return await query.ToListAsync(cancellationToken);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public Task<TenantAnnouncement?> FindByIdInScopeAsync(long tenantId, long announcementId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var tenantIds = new[] { tenantId, 0L };
|
||||
return context.TenantAnnouncements.AsNoTracking()
|
||||
.IgnoreQueryFilters()
|
||||
.FirstOrDefaultAsync(x => tenantIds.Contains(x.TenantId) && x.Id == announcementId, cancellationToken);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<IReadOnlyList<TenantAnnouncement>> SearchUnreadAsync(
|
||||
long tenantId,
|
||||
long? userId,
|
||||
AnnouncementStatus? status,
|
||||
bool? isActive,
|
||||
DateTime? effectiveAt,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
var tenantIds = new[] { tenantId, 0L };
|
||||
var announcementQuery = context.TenantAnnouncements.AsNoTracking()
|
||||
.IgnoreQueryFilters()
|
||||
.Where(x => tenantIds.Contains(x.TenantId));
|
||||
|
||||
if (status.HasValue)
|
||||
{
|
||||
announcementQuery = announcementQuery.Where(x => x.Status == status.Value);
|
||||
}
|
||||
|
||||
if (isActive.HasValue)
|
||||
{
|
||||
announcementQuery = isActive.Value
|
||||
? announcementQuery.Where(x => x.Status == AnnouncementStatus.Published)
|
||||
: announcementQuery.Where(x => x.Status != AnnouncementStatus.Published);
|
||||
}
|
||||
|
||||
if (effectiveAt.HasValue)
|
||||
{
|
||||
var at = effectiveAt.Value;
|
||||
announcementQuery = announcementQuery.Where(x => x.EffectiveFrom <= at && (x.EffectiveTo == null || x.EffectiveTo >= at));
|
||||
}
|
||||
|
||||
var readQuery = context.TenantAnnouncementReads.AsNoTracking()
|
||||
.IgnoreQueryFilters()
|
||||
.Where(x => x.TenantId == tenantId);
|
||||
|
||||
readQuery = userId.HasValue
|
||||
? readQuery.Where(x => x.UserId == null || x.UserId == userId.Value)
|
||||
: readQuery.Where(x => x.UserId == null);
|
||||
|
||||
var query = from announcement in announcementQuery
|
||||
join read in readQuery on announcement.Id equals read.AnnouncementId into readGroup
|
||||
where !readGroup.Any()
|
||||
select announcement;
|
||||
|
||||
return await query.ToListAsync(cancellationToken);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public Task<TenantAnnouncement?> FindByIdAsync(long tenantId, long announcementId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user