diff --git a/src/Infrastructure/TakeoutSaaS.Infrastructure/App/Repositories/EfStoreRepository.cs b/src/Infrastructure/TakeoutSaaS.Infrastructure/App/Repositories/EfStoreRepository.cs index 72952f1..a21d40a 100644 --- a/src/Infrastructure/TakeoutSaaS.Infrastructure/App/Repositories/EfStoreRepository.cs +++ b/src/Infrastructure/TakeoutSaaS.Infrastructure/App/Repositories/EfStoreRepository.cs @@ -17,11 +17,25 @@ namespace TakeoutSaaS.Infrastructure.App.Repositories; public sealed class EfStoreRepository(TakeoutAppDbContext context) : IStoreRepository { /// - public Task FindByIdAsync(long storeId, long tenantId, CancellationToken cancellationToken = default) + public Task FindByIdAsync(long storeId, long? tenantId, CancellationToken cancellationToken = default, bool includeDeleted = false) { - return context.Stores - .AsNoTracking() - .Where(x => x.TenantId == tenantId && x.Id == storeId) + var query = context.Stores.AsNoTracking(); + + // 1. 包含软删除数据时忽略全局过滤 + if (includeDeleted) + { + query = query.IgnoreQueryFilters(); + } + + // 2. (空行后) 可选租户过滤 + if (tenantId.HasValue) + { + query = query.Where(x => x.TenantId == tenantId.Value); + } + + // 3. (空行后) 返回门店实体 + return query + .Where(x => x.Id == storeId) .FirstOrDefaultAsync(cancellationToken); } @@ -37,50 +51,71 @@ public sealed class EfStoreRepository(TakeoutAppDbContext context) : IStoreRepos /// public async Task> SearchAsync( - long tenantId, + long? tenantId, long? merchantId, StoreStatus? status, StoreAuditStatus? auditStatus, StoreBusinessStatus? businessStatus, StoreOwnershipType? ownershipType, string? keyword, + bool includeDeleted = false, CancellationToken cancellationToken = default) { - var query = context.Stores - .AsNoTracking() - .Where(x => x.TenantId == tenantId); + var query = context.Stores.AsNoTracking(); + // 1. 包含软删除数据时忽略全局过滤 + if (includeDeleted) + { + query = query.IgnoreQueryFilters(); + } + + // 2. (空行后) 可选租户过滤 + if (tenantId.HasValue) + { + query = query.Where(x => x.TenantId == tenantId.Value); + } + + // 3. (空行后) 可选过滤:商户 if (merchantId.HasValue) { query = query.Where(x => x.MerchantId == merchantId.Value); } + // 4. (空行后) 可选过滤:状态 if (status.HasValue) { query = query.Where(x => x.Status == status.Value); } + // 5. (空行后) 可选过滤:审核状态 if (auditStatus.HasValue) { query = query.Where(x => x.AuditStatus == auditStatus.Value); } + // 6. (空行后) 可选过滤:经营状态 if (businessStatus.HasValue) { query = query.Where(x => x.BusinessStatus == businessStatus.Value); } + // 7. (空行后) 可选过滤:主体类型 if (ownershipType.HasValue) { query = query.Where(x => x.OwnershipType == ownershipType.Value); } + // 8. (空行后) 可选过滤:关键词 if (!string.IsNullOrWhiteSpace(keyword)) { var trimmed = keyword.Trim(); - query = query.Where(x => x.Name.Contains(trimmed) || x.Code.Contains(trimmed)); + query = query.Where(x => + x.Name.Contains(trimmed) || + x.Code.Contains(trimmed) || + (x.Phone != null && x.Phone.Contains(trimmed))); } + // 9. (空行后) 查询并返回结果 var stores = await query .OrderBy(x => x.Name) .ToListAsync(cancellationToken); @@ -126,17 +161,22 @@ public sealed class EfStoreRepository(TakeoutAppDbContext context) : IStoreRepos } /// - public async Task> GetStoreCountsAsync(long tenantId, IReadOnlyCollection merchantIds, CancellationToken cancellationToken = default) + public async Task> GetStoreCountsAsync(long? tenantId, IReadOnlyCollection merchantIds, CancellationToken cancellationToken = default) { if (merchantIds.Count == 0) { return new Dictionary(); } - var query = context.Stores - .AsNoTracking() - .Where(x => x.TenantId == tenantId); + var query = context.Stores.AsNoTracking(); + // 1. 可选租户过滤 + if (tenantId.HasValue) + { + query = query.Where(x => x.TenantId == tenantId.Value); + } + + // 2. (空行后) 分组统计门店数量 return await query .Where(x => merchantIds.Contains(x.MerchantId)) .GroupBy(x => x.MerchantId) @@ -145,11 +185,25 @@ public sealed class EfStoreRepository(TakeoutAppDbContext context) : IStoreRepos } /// - public async Task> GetBusinessHoursAsync(long storeId, long tenantId, CancellationToken cancellationToken = default) + public async Task> GetBusinessHoursAsync(long storeId, long? tenantId, CancellationToken cancellationToken = default, bool includeDeleted = false) { - var hours = await context.StoreBusinessHours - .AsNoTracking() - .Where(x => x.TenantId == tenantId && x.StoreId == storeId) + var query = context.StoreBusinessHours.AsNoTracking(); + + // 1. 包含软删除数据时忽略全局过滤 + if (includeDeleted) + { + query = query.IgnoreQueryFilters(); + } + + // 2. (空行后) 可选租户过滤 + if (tenantId.HasValue) + { + query = query.Where(x => x.TenantId == tenantId.Value); + } + + // 3. (空行后) 查询并返回营业时段 + var hours = await query + .Where(x => x.StoreId == storeId) .OrderBy(x => x.DayOfWeek) .ThenBy(x => x.StartTime) .ToListAsync(cancellationToken); @@ -158,11 +212,25 @@ public sealed class EfStoreRepository(TakeoutAppDbContext context) : IStoreRepos } /// - public Task GetStoreFeeAsync(long storeId, long tenantId, CancellationToken cancellationToken = default) + public Task GetStoreFeeAsync(long storeId, long? tenantId, CancellationToken cancellationToken = default, bool includeDeleted = false) { - return context.StoreFees - .AsNoTracking() - .Where(x => x.TenantId == tenantId && x.StoreId == storeId) + var query = context.StoreFees.AsNoTracking(); + + // 1. 包含软删除数据时忽略全局过滤 + if (includeDeleted) + { + query = query.IgnoreQueryFilters(); + } + + // 2. (空行后) 可选租户过滤 + if (tenantId.HasValue) + { + query = query.Where(x => x.TenantId == tenantId.Value); + } + + // 3. (空行后) 返回费用配置 + return query + .Where(x => x.StoreId == storeId) .FirstOrDefaultAsync(cancellationToken); } @@ -180,11 +248,25 @@ public sealed class EfStoreRepository(TakeoutAppDbContext context) : IStoreRepos } /// - public async Task> GetQualificationsAsync(long storeId, long tenantId, CancellationToken cancellationToken = default) + public async Task> GetQualificationsAsync(long storeId, long? tenantId, CancellationToken cancellationToken = default, bool includeDeleted = false) { - var qualifications = await context.StoreQualifications - .AsNoTracking() - .Where(x => x.TenantId == tenantId && x.StoreId == storeId) + var query = context.StoreQualifications.AsNoTracking(); + + // 1. 包含软删除数据时忽略全局过滤 + if (includeDeleted) + { + query = query.IgnoreQueryFilters(); + } + + // 2. (空行后) 可选租户过滤 + if (tenantId.HasValue) + { + query = query.Where(x => x.TenantId == tenantId.Value); + } + + // 3. (空行后) 查询并返回资质列表 + var qualifications = await query + .Where(x => x.StoreId == storeId) .OrderBy(x => x.SortOrder) .ThenBy(x => x.QualificationType) .ToListAsync(cancellationToken); @@ -251,11 +333,25 @@ public sealed class EfStoreRepository(TakeoutAppDbContext context) : IStoreRepos } /// - public async Task> GetDeliveryZonesAsync(long storeId, long tenantId, CancellationToken cancellationToken = default) + public async Task> GetDeliveryZonesAsync(long storeId, long? tenantId, CancellationToken cancellationToken = default, bool includeDeleted = false) { - var zones = await context.StoreDeliveryZones - .AsNoTracking() - .Where(x => x.TenantId == tenantId && x.StoreId == storeId) + var query = context.StoreDeliveryZones.AsNoTracking(); + + // 1. 包含软删除数据时忽略全局过滤 + if (includeDeleted) + { + query = query.IgnoreQueryFilters(); + } + + // 2. (空行后) 可选租户过滤 + if (tenantId.HasValue) + { + query = query.Where(x => x.TenantId == tenantId.Value); + } + + // 3. (空行后) 查询并返回配送区域 + var zones = await query + .Where(x => x.StoreId == storeId) .OrderBy(x => x.SortOrder) .ToListAsync(cancellationToken); @@ -263,19 +359,48 @@ public sealed class EfStoreRepository(TakeoutAppDbContext context) : IStoreRepos } /// - public Task FindDeliveryZoneByIdAsync(long deliveryZoneId, long tenantId, CancellationToken cancellationToken = default) + public Task FindDeliveryZoneByIdAsync(long deliveryZoneId, long? tenantId, CancellationToken cancellationToken = default, bool includeDeleted = false) { - return context.StoreDeliveryZones - .Where(x => x.TenantId == tenantId && x.Id == deliveryZoneId) + var query = context.StoreDeliveryZones.AsQueryable(); + + // 1. 包含软删除数据时忽略全局过滤 + if (includeDeleted) + { + query = query.IgnoreQueryFilters(); + } + + // 2. (空行后) 可选租户过滤 + if (tenantId.HasValue) + { + query = query.Where(x => x.TenantId == tenantId.Value); + } + + // 3. (空行后) 返回配送区域实体 + return query + .Where(x => x.Id == deliveryZoneId) .FirstOrDefaultAsync(cancellationToken); } /// - public async Task> GetHolidaysAsync(long storeId, long tenantId, CancellationToken cancellationToken = default) + public async Task> GetHolidaysAsync(long storeId, long? tenantId, CancellationToken cancellationToken = default, bool includeDeleted = false) { - var holidays = await context.StoreHolidays - .AsNoTracking() - .Where(x => x.TenantId == tenantId && x.StoreId == storeId) + var query = context.StoreHolidays.AsNoTracking(); + + // 1. 包含软删除数据时忽略全局过滤 + if (includeDeleted) + { + query = query.IgnoreQueryFilters(); + } + + // 2. (空行后) 可选租户过滤 + if (tenantId.HasValue) + { + query = query.Where(x => x.TenantId == tenantId.Value); + } + + // 3. (空行后) 查询并返回节假日 + var holidays = await query + .Where(x => x.StoreId == storeId) .OrderBy(x => x.Date) .ToListAsync(cancellationToken); @@ -283,10 +408,25 @@ public sealed class EfStoreRepository(TakeoutAppDbContext context) : IStoreRepos } /// - public Task FindHolidayByIdAsync(long holidayId, long tenantId, CancellationToken cancellationToken = default) + public Task FindHolidayByIdAsync(long holidayId, long? tenantId, CancellationToken cancellationToken = default, bool includeDeleted = false) { - return context.StoreHolidays - .Where(x => x.TenantId == tenantId && x.Id == holidayId) + var query = context.StoreHolidays.AsQueryable(); + + // 1. 包含软删除数据时忽略全局过滤 + if (includeDeleted) + { + query = query.IgnoreQueryFilters(); + } + + // 2. (空行后) 可选租户过滤 + if (tenantId.HasValue) + { + query = query.Where(x => x.TenantId == tenantId.Value); + } + + // 3. (空行后) 返回节假日实体 + return query + .Where(x => x.Id == holidayId) .FirstOrDefaultAsync(cancellationToken); } @@ -528,10 +668,18 @@ public sealed class EfStoreRepository(TakeoutAppDbContext context) : IStoreRepos } /// - public async Task DeleteDeliveryZoneAsync(long deliveryZoneId, long tenantId, CancellationToken cancellationToken = default) + public async Task DeleteDeliveryZoneAsync(long deliveryZoneId, long? tenantId, CancellationToken cancellationToken = default) { - var existing = await context.StoreDeliveryZones - .Where(x => x.TenantId == tenantId && x.Id == deliveryZoneId) + // 1. 查询目标配送区域 + var query = context.StoreDeliveryZones.AsQueryable(); + if (tenantId.HasValue) + { + query = query.Where(x => x.TenantId == tenantId.Value); + } + + // 2. (空行后) 执行软删除 + var existing = await query + .Where(x => x.Id == deliveryZoneId) .FirstOrDefaultAsync(cancellationToken); if (existing != null) @@ -541,10 +689,18 @@ public sealed class EfStoreRepository(TakeoutAppDbContext context) : IStoreRepos } /// - public async Task DeleteHolidayAsync(long holidayId, long tenantId, CancellationToken cancellationToken = default) + public async Task DeleteHolidayAsync(long holidayId, long? tenantId, CancellationToken cancellationToken = default) { - var existing = await context.StoreHolidays - .Where(x => x.TenantId == tenantId && x.Id == holidayId) + // 1. 查询目标节假日 + var query = context.StoreHolidays.AsQueryable(); + if (tenantId.HasValue) + { + query = query.Where(x => x.TenantId == tenantId.Value); + } + + // 2. (空行后) 执行软删除 + var existing = await query + .Where(x => x.Id == holidayId) .FirstOrDefaultAsync(cancellationToken); if (existing != null)