chore: add documentation comments and stylecop rules
This commit is contained in:
@@ -21,18 +21,20 @@ public sealed class DictionaryAppService(
|
||||
ITenantProvider tenantProvider,
|
||||
ILogger<DictionaryAppService> logger) : IDictionaryAppService
|
||||
{
|
||||
|
||||
public async Task<DictionaryGroupDto> CreateGroupAsync(CreateDictionaryGroupRequest request, CancellationToken cancellationToken = default)
|
||||
{
|
||||
// 1. 规范化编码并确定租户
|
||||
var normalizedCode = NormalizeCode(request.Code);
|
||||
var targetTenant = ResolveTargetTenant(request.Scope);
|
||||
|
||||
// 2. 校验编码唯一
|
||||
var existing = await repository.FindGroupByCodeAsync(normalizedCode, cancellationToken);
|
||||
if (existing != null)
|
||||
{
|
||||
throw new BusinessException(ErrorCodes.Conflict, $"字典分组编码 {normalizedCode} 已存在");
|
||||
}
|
||||
|
||||
// 3. 构建分组实体
|
||||
var group = new DictionaryGroup
|
||||
{
|
||||
Id = 0,
|
||||
@@ -44,6 +46,7 @@ public sealed class DictionaryAppService(
|
||||
IsEnabled = true
|
||||
};
|
||||
|
||||
// 4. 持久化并返回
|
||||
await repository.AddGroupAsync(group, cancellationToken);
|
||||
await repository.SaveChangesAsync(cancellationToken);
|
||||
logger.LogInformation("创建字典分组:{Code}({Scope})", group.Code, group.Scope);
|
||||
@@ -52,13 +55,16 @@ public sealed class DictionaryAppService(
|
||||
|
||||
public async Task<DictionaryGroupDto> UpdateGroupAsync(long groupId, UpdateDictionaryGroupRequest request, CancellationToken cancellationToken = default)
|
||||
{
|
||||
// 1. 读取分组并校验权限
|
||||
var group = await RequireGroupAsync(groupId, cancellationToken);
|
||||
EnsureScopePermission(group.Scope);
|
||||
|
||||
// 2. 更新字段
|
||||
group.Name = request.Name.Trim();
|
||||
group.Description = request.Description?.Trim();
|
||||
group.IsEnabled = request.IsEnabled;
|
||||
|
||||
// 3. 持久化并失效缓存
|
||||
await repository.SaveChangesAsync(cancellationToken);
|
||||
await InvalidateCacheAsync(group, cancellationToken);
|
||||
logger.LogInformation("更新字典分组:{GroupId}", group.Id);
|
||||
@@ -67,9 +73,11 @@ public sealed class DictionaryAppService(
|
||||
|
||||
public async Task DeleteGroupAsync(long groupId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
// 1. 读取分组并校验权限
|
||||
var group = await RequireGroupAsync(groupId, cancellationToken);
|
||||
EnsureScopePermission(group.Scope);
|
||||
|
||||
// 2. 删除并失效缓存
|
||||
await repository.RemoveGroupAsync(group, cancellationToken);
|
||||
await repository.SaveChangesAsync(cancellationToken);
|
||||
await InvalidateCacheAsync(group, cancellationToken);
|
||||
@@ -78,10 +86,12 @@ public sealed class DictionaryAppService(
|
||||
|
||||
public async Task<IReadOnlyList<DictionaryGroupDto>> SearchGroupsAsync(DictionaryGroupQuery request, CancellationToken cancellationToken = default)
|
||||
{
|
||||
// 1. 确定查询范围并校验权限
|
||||
var tenantId = tenantProvider.GetCurrentTenantId();
|
||||
var scope = ResolveScopeForQuery(request.Scope, tenantId);
|
||||
EnsureScopePermission(scope);
|
||||
|
||||
// 2. 查询分组及可选项
|
||||
var groups = await repository.SearchGroupsAsync(scope, cancellationToken);
|
||||
var includeItems = request.IncludeItems;
|
||||
var result = new List<DictionaryGroupDto>(groups.Count);
|
||||
@@ -91,6 +101,7 @@ public sealed class DictionaryAppService(
|
||||
IReadOnlyList<DictionaryItemDto> items = Array.Empty<DictionaryItemDto>();
|
||||
if (includeItems)
|
||||
{
|
||||
// 查询分组下字典项
|
||||
var itemEntities = await repository.GetItemsByGroupIdAsync(group.Id, cancellationToken);
|
||||
items = itemEntities.Select(MapItem).ToList();
|
||||
}
|
||||
@@ -103,9 +114,11 @@ public sealed class DictionaryAppService(
|
||||
|
||||
public async Task<DictionaryItemDto> CreateItemAsync(CreateDictionaryItemRequest request, CancellationToken cancellationToken = default)
|
||||
{
|
||||
// 1. 校验分组与权限
|
||||
var group = await RequireGroupAsync(request.GroupId, cancellationToken);
|
||||
EnsureScopePermission(group.Scope);
|
||||
|
||||
// 2. 构建字典项
|
||||
var item = new DictionaryItem
|
||||
{
|
||||
Id = 0,
|
||||
@@ -119,6 +132,7 @@ public sealed class DictionaryAppService(
|
||||
IsEnabled = request.IsEnabled
|
||||
};
|
||||
|
||||
// 3. 持久化并失效缓存
|
||||
await repository.AddItemAsync(item, cancellationToken);
|
||||
await repository.SaveChangesAsync(cancellationToken);
|
||||
await InvalidateCacheAsync(group, cancellationToken);
|
||||
@@ -128,16 +142,19 @@ public sealed class DictionaryAppService(
|
||||
|
||||
public async Task<DictionaryItemDto> UpdateItemAsync(long itemId, UpdateDictionaryItemRequest request, CancellationToken cancellationToken = default)
|
||||
{
|
||||
// 1. 读取字典项与分组并校验权限
|
||||
var item = await RequireItemAsync(itemId, cancellationToken);
|
||||
var group = await RequireGroupAsync(item.GroupId, cancellationToken);
|
||||
EnsureScopePermission(group.Scope);
|
||||
|
||||
// 2. 更新字段
|
||||
item.Value = request.Value.Trim();
|
||||
item.Description = request.Description?.Trim();
|
||||
item.SortOrder = request.SortOrder;
|
||||
item.IsDefault = request.IsDefault;
|
||||
item.IsEnabled = request.IsEnabled;
|
||||
|
||||
// 3. 持久化并失效缓存
|
||||
await repository.SaveChangesAsync(cancellationToken);
|
||||
await InvalidateCacheAsync(group, cancellationToken);
|
||||
logger.LogInformation("更新字典项:{ItemId}", item.Id);
|
||||
@@ -146,10 +163,12 @@ public sealed class DictionaryAppService(
|
||||
|
||||
public async Task DeleteItemAsync(long itemId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
// 1. 读取字典项与分组并校验权限
|
||||
var item = await RequireItemAsync(itemId, cancellationToken);
|
||||
var group = await RequireGroupAsync(item.GroupId, cancellationToken);
|
||||
EnsureScopePermission(group.Scope);
|
||||
|
||||
// 2. 删除并失效缓存
|
||||
await repository.RemoveItemAsync(item, cancellationToken);
|
||||
await repository.SaveChangesAsync(cancellationToken);
|
||||
await InvalidateCacheAsync(group, cancellationToken);
|
||||
@@ -158,6 +177,7 @@ public sealed class DictionaryAppService(
|
||||
|
||||
public async Task<IReadOnlyDictionary<string, IReadOnlyList<DictionaryItemDto>>> GetCachedItemsAsync(DictionaryBatchQueryRequest request, CancellationToken cancellationToken = default)
|
||||
{
|
||||
// 1. 规范化编码
|
||||
var normalizedCodes = request.Codes
|
||||
.Where(code => !string.IsNullOrWhiteSpace(code))
|
||||
.Select(NormalizeCode)
|
||||
@@ -169,6 +189,7 @@ public sealed class DictionaryAppService(
|
||||
return new Dictionary<string, IReadOnlyList<DictionaryItemDto>>(StringComparer.OrdinalIgnoreCase);
|
||||
}
|
||||
|
||||
// 2. 按租户合并系统与业务字典
|
||||
var tenantId = tenantProvider.GetCurrentTenantId();
|
||||
var result = new Dictionary<string, IReadOnlyList<DictionaryItemDto>>(StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
@@ -190,6 +211,7 @@ public sealed class DictionaryAppService(
|
||||
|
||||
private async Task<DictionaryGroup> RequireGroupAsync(long groupId, CancellationToken cancellationToken)
|
||||
{
|
||||
// 1. 读取分组,找不到抛异常
|
||||
var group = await repository.FindGroupByIdAsync(groupId, cancellationToken);
|
||||
if (group == null)
|
||||
{
|
||||
@@ -201,6 +223,7 @@ public sealed class DictionaryAppService(
|
||||
|
||||
private async Task<DictionaryItem> RequireItemAsync(long itemId, CancellationToken cancellationToken)
|
||||
{
|
||||
// 1. 读取字典项,找不到抛异常
|
||||
var item = await repository.FindItemByIdAsync(itemId, cancellationToken);
|
||||
if (item == null)
|
||||
{
|
||||
@@ -269,12 +292,14 @@ public sealed class DictionaryAppService(
|
||||
|
||||
private async Task<IReadOnlyList<DictionaryItemDto>> GetOrLoadCacheAsync(long tenantId, string code, CancellationToken cancellationToken)
|
||||
{
|
||||
// 1. 先查缓存
|
||||
var cached = await cache.GetAsync(tenantId, code, cancellationToken);
|
||||
if (cached != null)
|
||||
{
|
||||
return cached;
|
||||
}
|
||||
|
||||
// 2. 从仓储加载并写入缓存
|
||||
var entities = await repository.GetItemsByCodesAsync(new[] { code }, tenantId, includeSystem: false, cancellationToken);
|
||||
var items = entities
|
||||
.Where(item => item.IsEnabled && (item.Group?.IsEnabled ?? true))
|
||||
|
||||
Reference in New Issue
Block a user