diff --git a/src/Oms.Application/Dtos/DataPerm/DataPermDtos.cs b/src/Oms.Application/Dtos/DataPerm/DataPermDtos.cs index f3e0102..30ca208 100644 --- a/src/Oms.Application/Dtos/DataPerm/DataPermDtos.cs +++ b/src/Oms.Application/Dtos/DataPerm/DataPermDtos.cs @@ -18,6 +18,40 @@ public class DataResourceMetaDto public string? MainTable { get; set; } public List? FilterableFields { get; set; } + + /// + /// 业务列表查询是否已真正接入数据权限过滤(未接入时配置仅作预留)。 + /// + public bool IsEnforced { get; set; } +} + +/// +/// 按目标用户生效范围预览订单可见量(运营验收用)。 +/// +public class OrderScopePreviewDto +{ + public string UserId { get; set; } = string.Empty; + + public bool IsAdmin { get; set; } + + public bool Enforced { get; set; } = true; + + public int TotalCount { get; set; } + + public List Samples { get; set; } = []; + + public string? SummaryText { get; set; } +} + +public class OrderScopePreviewItemDto +{ + public int OrderId { get; set; } + + public string ServiceNo { get; set; } = string.Empty; + + public string? ClientNo { get; set; } + + public string? OrderStatus { get; set; } } public class DataPermMetaDto @@ -217,12 +251,19 @@ public class EffectiveDataScopeDto public string? DataResourceCode { get; set; } + /// + /// 当前预览模块是否已接入业务过滤。 + /// + public bool IsEnforced { get; set; } + public List? OrganizationIds { get; set; } public List? OrganizationNames { get; set; } public int? DataScope { get; set; } + public string? DataScopeLabel { get; set; } + public List? BizFilters { get; set; } public List? SecretLevels { get; set; } diff --git a/src/Oms.Application/Services/DataPerm/DataPermService.cs b/src/Oms.Application/Services/DataPerm/DataPermService.cs index cb9c7f2..31f0e68 100644 --- a/src/Oms.Application/Services/DataPerm/DataPermService.cs +++ b/src/Oms.Application/Services/DataPerm/DataPermService.cs @@ -9,6 +9,8 @@ using Oms.Application.Dtos.DataPerm; using Oms.Application.Shared; using Oms.Domain.ChannelsAggregate.Entities; using Oms.Domain.ChannelsAggregate.Repositories; +using Oms.Domain.OrdersAggregate; +using Oms.Domain.OrdersAggregate.Repositories; using Oms.Domain.SystemAggregate.Entities; using Oms.Domain.SystemAggregate.Repositories; @@ -28,6 +30,10 @@ public partial class DataPermService : ApplicationService private ISalesProductRepository SalesProductRepository => LazyServiceProvider.GetRequiredService(); private IKpiAreaRepository KpiAreaRepository => LazyServiceProvider.GetRequiredService(); private IWarehouseAddressRepository WarehouseRepository => LazyServiceProvider.GetRequiredService(); + private IOrderRepository OrderRepository => LazyServiceProvider.GetRequiredService(); + + private static readonly HashSet EnforcedResourceCodes = + new(StringComparer.OrdinalIgnoreCase) { DataScopeApplier.OrderResourceCode }; /// /// 获取数据权限元数据 @@ -106,27 +112,36 @@ public partial class DataPermService : ApplicationService CancellationToken cancellationToken = default) { var id = ParseGuid(userId, "userId"); - var context = await ScopeBuilder.BuildAsync(id, dataResourceCode, useCache: true, cancellationToken); + var resourceCode = string.IsNullOrWhiteSpace(dataResourceCode) + ? DataScopeApplier.OrderResourceCode + : dataResourceCode.Trim(); + var isEnforced = EnforcedResourceCodes.Contains(resourceCode); + var context = await ScopeBuilder.BuildAsync(id, resourceCode, useCache: true, cancellationToken); if (context.IsAdmin) { return new EffectiveDataScopeDto { UserId = id.ToString(), IsAdmin = true, - DataResourceCode = dataResourceCode, + DataResourceCode = resourceCode, + IsEnforced = isEnforced, SummaryText = "管理员:跳过五维数据权限" }; } var orgNames = await Repository.GetOrganizationNamesAsync(context.OrganizationIds, cancellationToken); + var scopeLabel = DataPermConstants.DataScopeOptions + .FirstOrDefault(x => x.Value == context.DataScope).Label; return new EffectiveDataScopeDto { UserId = id.ToString(), IsAdmin = false, - DataResourceCode = dataResourceCode, + DataResourceCode = resourceCode, + IsEnforced = isEnforced, OrganizationIds = context.OrganizationIds.Select(x => x.ToString()).ToList(), OrganizationNames = orgNames.Values.ToList(), DataScope = context.DataScope, + DataScopeLabel = scopeLabel, SecretLevels = context.SecretLevels.OrderBy(x => x).ToList(), BizFilters = context.BizFilters?.Select(x => new BizFilterGroupDto { @@ -137,6 +152,56 @@ public partial class DataPermService : ApplicationService }; } + /// + /// 按目标用户生效范围预览订单可见量(抽样,供运营验收配置是否生效)。 + /// + /// 目标用户 GUID。 + /// 抽样条数,默认 5,最大 20。 + /// 取消令牌。 + /// 可见订单总数与抽样服务单号;非订单模块不执行计数。 + [HttpGet("/rbac/data-perm/preview-order-scope")] + public async Task PreviewOrderScopeAsync( + [FromQuery] string userId, + [FromQuery] int sampleSize = 5, + CancellationToken cancellationToken = default) + { + var id = ParseGuid(userId, "userId"); + sampleSize = Math.Clamp(sampleSize, 1, 20); + var context = await ScopeBuilder.BuildAsync(id, DataScopeApplier.OrderResourceCode, useCache: true, cancellationToken); + var summary = await ScopeBuilder.BuildSummaryTextAsync(context, cancellationToken); + + if (context.IsAdmin) + { + var (adminItems, adminTotal) = await OrderRepository.GetFilteredPagedListAsync( + 1, + sampleSize, + new OrderListFilter(), + cancellationToken); + return new OrderScopePreviewDto + { + UserId = id.ToString(), + IsAdmin = true, + Enforced = true, + TotalCount = adminTotal, + SummaryText = summary, + Samples = adminItems.Select(MapPreviewItem).ToList() + }; + } + + var filter = new OrderListFilter(); + DataScopeApplier.ApplyToOrderListFilter(filter, context); + var (items, total) = await OrderRepository.GetFilteredPagedListAsync(1, sampleSize, filter, cancellationToken); + return new OrderScopePreviewDto + { + UserId = id.ToString(), + IsAdmin = false, + Enforced = true, + TotalCount = total, + SummaryText = summary, + Samples = items.Select(MapPreviewItem).ToList() + }; + } + /// /// 保存用户数据权限 /// @@ -275,6 +340,11 @@ public partial class DataPermService : ApplicationService var rows = await Repository.GetDataResourcesAsync(cancellationToken); if (rows.Count > 0) { + foreach (var row in rows) + { + row.IsEnforced = EnforcedResourceCodes.Contains(row.Code); + } + return rows; } } @@ -287,12 +357,33 @@ public partial class DataPermService : ApplicationService .Select(kv => new DataResourceMetaDto { Code = kv.Key, - Name = kv.Key, - FilterableFields = kv.Value.ToList() + Name = ResolveResourceDisplayName(kv.Key), + FilterableFields = kv.Value.ToList(), + IsEnforced = EnforcedResourceCodes.Contains(kv.Key) }) .ToList(); } + private static string ResolveResourceDisplayName(string code) => + code.ToLowerInvariant() switch + { + "order" => "订单", + "customer" => "客户", + "recharge" => "充值", + "channelproduct" => "销售产品", + "xhs" => "小红书", + _ => code + }; + + private static OrderScopePreviewItemDto MapPreviewItem(Domain.OrdersAggregate.Entities.Order order) => + new() + { + OrderId = order.Id, + ServiceNo = order.ServiceNo, + ClientNo = order.ClientNo, + OrderStatus = order.OrderStatus + }; + private Guid GetCurrentUserGuid() { var raw = CurrentUser.FindClaimValueOrNull(ClaimTypes.NameIdentifier);