using SqlSugar; using ZR.Model.System; namespace ZR.Service.Business { /// /// 部门数据隔离工具类 /// 根据 DeptId 递归查询本级及所有下级部门 ID 列表 /// public static class DeptDataScopeHelper { /// /// 获取当前用户可见的部门ID列表(本级 + 所有下级) /// /// SqlSugar 客户端实例 /// 当前用户所属部门ID /// 可见部门ID列表 public static List GetVisibleDeptIds(ISqlSugarClient db, long deptId) { var allDepts = db.Queryable().ToList(); var result = new List { deptId }; CollectChildDeptIds(allDepts, deptId, result); return result; } /// /// 递归收集所有下级部门ID /// private static void CollectChildDeptIds(List all, long parentId, List result) { var children = all.Where(d => d.ParentId == parentId).ToList(); foreach (var child in children) { result.Add(child.DeptId); CollectChildDeptIds(all, child.DeptId, result); } } } }