98 lines
3.7 KiB
C#
98 lines
3.7 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using TakeoutSaaS.Domain.Dictionary.Entities;
|
|
using TakeoutSaaS.Domain.Dictionary.Enums;
|
|
using TakeoutSaaS.Domain.Dictionary.Repositories;
|
|
using TakeoutSaaS.Infrastructure.Dictionary.Persistence;
|
|
|
|
namespace TakeoutSaaS.Infrastructure.Dictionary.Repositories;
|
|
|
|
/// <summary>
|
|
/// EF Core 字典仓储实现。
|
|
/// </summary>
|
|
public sealed class EfDictionaryRepository(DictionaryDbContext context) : IDictionaryRepository
|
|
{
|
|
public Task<DictionaryGroup?> FindGroupByIdAsync(long id, CancellationToken cancellationToken = default)
|
|
=> context.DictionaryGroups.FirstOrDefaultAsync(group => group.Id == id, cancellationToken);
|
|
|
|
public Task<DictionaryGroup?> FindGroupByCodeAsync(string code, CancellationToken cancellationToken = default)
|
|
=> context.DictionaryGroups.FirstOrDefaultAsync(group => group.Code == code, cancellationToken);
|
|
|
|
public async Task<IReadOnlyList<DictionaryGroup>> SearchGroupsAsync(DictionaryScope? scope, CancellationToken cancellationToken = default)
|
|
{
|
|
var query = context.DictionaryGroups.AsNoTracking();
|
|
if (scope.HasValue)
|
|
{
|
|
query = query.Where(group => group.Scope == scope.Value);
|
|
}
|
|
|
|
return await query
|
|
.OrderBy(group => group.Code)
|
|
.ToListAsync(cancellationToken);
|
|
}
|
|
|
|
public Task AddGroupAsync(DictionaryGroup group, CancellationToken cancellationToken = default)
|
|
{
|
|
context.DictionaryGroups.Add(group);
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
public Task RemoveGroupAsync(DictionaryGroup group, CancellationToken cancellationToken = default)
|
|
{
|
|
context.DictionaryGroups.Remove(group);
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
public Task<DictionaryItem?> FindItemByIdAsync(long id, CancellationToken cancellationToken = default)
|
|
=> context.DictionaryItems.FirstOrDefaultAsync(item => item.Id == id, cancellationToken);
|
|
|
|
public async Task<IReadOnlyList<DictionaryItem>> GetItemsByGroupIdAsync(long groupId, CancellationToken cancellationToken = default)
|
|
{
|
|
return await context.DictionaryItems
|
|
.AsNoTracking()
|
|
.Where(item => item.GroupId == groupId)
|
|
.OrderBy(item => item.SortOrder)
|
|
.ToListAsync(cancellationToken);
|
|
}
|
|
|
|
public Task AddItemAsync(DictionaryItem item, CancellationToken cancellationToken = default)
|
|
{
|
|
context.DictionaryItems.Add(item);
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
public Task RemoveItemAsync(DictionaryItem item, CancellationToken cancellationToken = default)
|
|
{
|
|
context.DictionaryItems.Remove(item);
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
public Task SaveChangesAsync(CancellationToken cancellationToken = default)
|
|
=> context.SaveChangesAsync(cancellationToken);
|
|
|
|
public async Task<IReadOnlyList<DictionaryItem>> GetItemsByCodesAsync(IEnumerable<string> codes, long tenantId, bool includeSystem, CancellationToken cancellationToken = default)
|
|
{
|
|
var normalizedCodes = codes
|
|
.Where(code => !string.IsNullOrWhiteSpace(code))
|
|
.Select(code => code.Trim().ToLowerInvariant())
|
|
.Distinct(StringComparer.OrdinalIgnoreCase)
|
|
.ToArray();
|
|
|
|
if (normalizedCodes.Length == 0)
|
|
{
|
|
return Array.Empty<DictionaryItem>();
|
|
}
|
|
|
|
var query = context.DictionaryItems
|
|
.AsNoTracking()
|
|
.IgnoreQueryFilters()
|
|
.Include(item => item.Group)
|
|
.Where(item => normalizedCodes.Contains(item.Group!.Code));
|
|
|
|
query = query.Where(item => item.TenantId == tenantId || (includeSystem && item.TenantId == 0));
|
|
|
|
return await query
|
|
.OrderBy(item => item.SortOrder)
|
|
.ToListAsync(cancellationToken);
|
|
}
|
|
}
|