using FoodLabeling.Application.Services.DbModels; using FoodLabeling.Domain.Entities; using SqlSugar; using Volo.Abp.Users; using Yi.Framework.Rbac.Domain.Entities; using Yi.Framework.Rbac.Domain.Shared.Consts; using Yi.Framework.SqlSugarCore.Abstractions; namespace FoodLabeling.Application.Helpers; /// /// Dashboard 数据范围:管理员看全平台(用户指标排除内置系统 admin);非管理员仅看绑定门店所属公司范围。 /// public static class DashboardScopeHelper { /// /// 解析 Dashboard 统计适用的门店 Id 集合。 /// null 表示不限制门店(系统管理员);空列表表示无可见范围。 /// public static Task?> ResolveDashboardLocationIdsAsync( ICurrentUser currentUser, ISqlSugarDbContext dbContext) => TeamMemberListScopeHelper.ResolveListLocationIdsAsync(currentUser, dbContext, null, null, null); /// /// 平台系统管理员用户 Id(不计入 People / Active Users,也不视为某公司成员)。 /// 含内置用户名 及绑定角色码 的账号。 /// public static async Task> GetPlatformAdminUserIdStringsAsync(ISqlSugarClient db) { var result = new HashSet(StringComparer.Ordinal); var nameIds = await db.Queryable() .Where(u => !u.IsDeleted && u.UserName == UserConst.Admin) .Select(u => u.Id) .ToListAsync(); foreach (var id in nameIds) { result.Add(id.ToString()); } var adminRoleIds = await db.Queryable() .Where(r => !r.IsDeleted && r.RoleCode == UserConst.AdminRolesCode) .Select(r => r.Id) .ToListAsync(); if (adminRoleIds.Count > 0) { var roleUserIds = await db.Queryable() .Where(ur => adminRoleIds.Contains(ur.RoleId)) .Select(ur => ur.UserId) .ToListAsync(); foreach (var id in roleUserIds) { result.Add(id.ToString()); } } return result; } /// /// 统计范围内成员数:须在 userlocation 中绑定范围内门店;排除平台系统 admin。 /// public static async Task CountScopedTeamMembersAsync( ISqlSugarClient db, List? scopeLocationIds, HashSet platformAdminUserIds, bool activeOnly, DateTime? createdBefore) { if (scopeLocationIds is { Count: 0 }) { return 0; } var userLocQuery = db.Queryable() .Where(ul => !ul.IsDeleted); if (scopeLocationIds is not null) { userLocQuery = userLocQuery.Where(ul => scopeLocationIds.Contains(ul.LocationId)); } var scopedUserIdStrings = await userLocQuery .Select(ul => ul.UserId) .Distinct() .ToListAsync(); if (scopedUserIdStrings.Count == 0) { return 0; } var scopedUserIds = new List(); foreach (var s in scopedUserIdStrings) { if (platformAdminUserIds.Contains(s)) { continue; } if (Guid.TryParse(s, out var gid) && gid != Guid.Empty) { scopedUserIds.Add(gid); } } if (scopedUserIds.Count == 0) { return 0; } var userQuery = db.Queryable() .Where(u => !u.IsDeleted && scopedUserIds.Contains(u.Id)); if (activeOnly) { userQuery = userQuery.Where(u => u.State); } if (createdBefore.HasValue) { userQuery = userQuery.Where(u => u.CreationTime < createdBefore.Value); } return await userQuery.CountAsync(); } /// /// 统计范围内门店数。 /// public static async Task CountScopedLocationsAsync( ISqlSugarClient db, List? scopeLocationIds, DateTime? createdBefore) { if (scopeLocationIds is { Count: 0 }) { return 0; } var q = db.Queryable().Where(x => !x.IsDeleted); if (scopeLocationIds is not null) { q = q.Where(x => scopeLocationIds.Contains(SqlFunc.ToString(x.Id))); } if (createdBefore.HasValue) { q = q.Where(x => x.CreationTime < createdBefore.Value); } return await q.CountAsync(); } /// /// 为打印任务查询附加门店范围(scopeLocationIds == null 不限制)。 /// public static ISugarQueryable ApplyPrintTaskLocationScope( ISugarQueryable query, List? scopeLocationIds) { if (scopeLocationIds is null) { return query; } if (scopeLocationIds.Count == 0) { return query.Where(_ => false); } return query.Where(t => t.LocationId != null && scopeLocationIds.Contains(t.LocationId)); } }