namespace Admin.NET.Core.Service; /// /// 系统机构服务 /// [ApiDescriptionSettings(Order = 470)] public class SysOrgService : IDynamicApiController, ITransient { private readonly UserManager _userManager; private readonly SqlSugarRepository _sysOrgRep; private readonly SysCacheService _sysCacheService; private readonly SysUserExtOrgService _sysUserExtOrgService; private readonly SysUserRoleService _sysUserRoleService; private readonly SysRoleOrgService _sysRoleOrgService; public SysOrgService(UserManager userManager, SqlSugarRepository sysOrgRep, SysCacheService sysCacheService, SysUserExtOrgService sysUserExtOrgService, SysUserRoleService sysUserRoleService, SysRoleOrgService sysRoleOrgService) { _sysOrgRep = sysOrgRep; _userManager = userManager; _sysCacheService = sysCacheService; _sysUserExtOrgService = sysUserExtOrgService; _sysUserRoleService = sysUserRoleService; _sysRoleOrgService = sysRoleOrgService; } /// /// 获取机构列表 /// /// [DisplayName("获取机构列表")] public async Task> GetList([FromQuery] OrgInput input) { var orgIdList = await GetUserOrgIdList(); var iSugarQueryable = _sysOrgRep.AsQueryable().OrderBy(u => u.OrderNo); // 条件筛选可能造成无法构造树(列表数据) if (!string.IsNullOrWhiteSpace(input.Name) || !string.IsNullOrWhiteSpace(input.Code) || !string.IsNullOrWhiteSpace(input.OrgType)) { return await iSugarQueryable.WhereIF(orgIdList.Count > 0, u => orgIdList.Contains(u.Id)) .WhereIF(!string.IsNullOrWhiteSpace(input.Name), u => u.Name.Contains(input.Name)) .WhereIF(!string.IsNullOrWhiteSpace(input.Code), u => u.Code.Contains(input.Code)) .WhereIF(!string.IsNullOrWhiteSpace(input.OrgType), u => u.OrgType.Contains(input.OrgType)) .ToListAsync(); } if (input.Id > 0) { return await iSugarQueryable.WhereIF(orgIdList.Count > 0, u => orgIdList.Contains(u.Id)).ToChildListAsync(u => u.Pid, input.Id, true); } else { return _userManager.SuperAdmin ? await iSugarQueryable.ToTreeAsync(u => u.Children, u => u.Pid, 0) : await iSugarQueryable.ToTreeAsync(u => u.Children, u => u.Pid, 0, orgIdList.Select(d => (object)d).ToArray()); } } /// /// 增加机构 /// /// /// [ApiDescriptionSettings(Name = "Add"), HttpPost] [DisplayName("增加机构")] public async Task AddOrg(AddOrgInput input) { var isExist = await _sysOrgRep.IsAnyAsync(u => u.Name == input.Name && u.Code == input.Code); if (isExist) throw Oops.Oh(ErrorCodeEnum.D2002); var orgIdList = await GetUserOrgIdList(); if (!_userManager.SuperAdmin) { // 新增机构父Id不是0,则进行权限校验 if (input.Pid != 0) { // 新增机构的父机构不在自己的数据范围内 if (orgIdList.Count < 1 || !orgIdList.Contains(input.Pid)) throw Oops.Oh(ErrorCodeEnum.D2003); } //else // throw Oops.Oh(ErrorCodeEnum.D2006); // 删除当前用户的机构缓存 _sysCacheService.Remove(CacheConst.KeyOrgIdList + _userManager.UserId); } var sysOrg = input.Adapt(); var newOrg = await _sysOrgRep.AsInsertable(sysOrg).ExecuteReturnEntityAsync(); return newOrg.Id; } /// /// 更新机构 /// /// /// [UnitOfWork] [ApiDescriptionSettings(Name = "Update"), HttpPost] [DisplayName("更新机构")] public async Task UpdateOrg(UpdateOrgInput input) { if (input.Pid != 0) { var pOrg = await _sysOrgRep.GetFirstAsync(u => u.Id == input.Pid); _ = pOrg ?? throw Oops.Oh(ErrorCodeEnum.D2000); } if (input.Id == input.Pid) throw Oops.Oh(ErrorCodeEnum.D2001); var isExist = await _sysOrgRep.IsAnyAsync(u => u.Name == input.Name && u.Code == input.Code && u.Id != input.Id); if (isExist) throw Oops.Oh(ErrorCodeEnum.D2002); // 父Id不能为自己的子节点 var childIdList = await GetChildIdListWithSelfById(input.Id); if (childIdList.Contains(input.Pid)) throw Oops.Oh(ErrorCodeEnum.D2001); // 是否有权限操作此机构 var dataScopes = await GetUserOrgIdList(); if (!_userManager.SuperAdmin && (dataScopes.Count < 1 || !dataScopes.Contains(input.Id))) throw Oops.Oh(ErrorCodeEnum.D2003); await _sysOrgRep.AsUpdateable(input.Adapt()).IgnoreColumns(true).ExecuteCommandAsync(); } /// /// 删除机构 /// /// /// [UnitOfWork] [ApiDescriptionSettings(Name = "Delete"), HttpPost] [DisplayName("删除机构")] public async Task DeleteOrg(DeleteOrgInput input) { var sysOrg = await _sysOrgRep.GetFirstAsync(u => u.Id == input.Id); // 是否有权限操作此机构 if (!_userManager.SuperAdmin) { var dataScopes = await GetUserOrgIdList(); if (dataScopes.Count < 1 || !dataScopes.Contains(sysOrg.Id)) throw Oops.Oh(ErrorCodeEnum.D2003); } // 若机构为租户默认机构禁止删除 var isTenantOrg = await _sysOrgRep.ChangeRepository>() .IsAnyAsync(u => u.OrgId == input.Id); if (isTenantOrg) throw Oops.Oh(ErrorCodeEnum.D2008); // 若机构有用户则禁止删除 var orgHasEmp = await _sysOrgRep.ChangeRepository>() .IsAnyAsync(u => u.OrgId == input.Id); if (orgHasEmp) throw Oops.Oh(ErrorCodeEnum.D2004); // 若扩展机构有用户则禁止删除 var hasExtOrgEmp = await _sysUserExtOrgService.HasUserOrg(sysOrg.Id); if (hasExtOrgEmp) throw Oops.Oh(ErrorCodeEnum.D2005); // 若子机构有用户则禁止删除 var orgTreeList = await _sysOrgRep.AsQueryable().ToChildListAsync(u => u.Pid, input.Id, true); var orgIdList = orgTreeList.Select(u => u.Id).ToList(); // 若子机构有用户则禁止删除 var cOrgHasEmp = await _sysOrgRep.ChangeRepository>() .IsAnyAsync(u => orgIdList.Contains(u.OrgId)); if (cOrgHasEmp) throw Oops.Oh(ErrorCodeEnum.D2007); // 级联删除机构子节点 await _sysOrgRep.DeleteAsync(u => orgIdList.Contains(u.Id)); // 级联删除角色机构数据 await _sysRoleOrgService.DeleteRoleOrgByOrgIdList(orgIdList); // 级联删除用户机构数据 await _sysUserExtOrgService.DeleteUserExtOrgByOrgIdList(orgIdList); } /// /// 根据用户Id获取机构Id集合 /// /// [ApiDescriptionSettings(false)] public async Task> GetUserOrgIdList() { if (_userManager.SuperAdmin) return new List(); var userId = _userManager.UserId; var orgIdList = _sysCacheService.Get>(CacheConst.KeyOrgIdList + userId); // 取缓存 if (orgIdList == null || orgIdList.Count < 1) { // 扩展机构集合 var orgList1 = await _sysUserExtOrgService.GetUserExtOrgList(userId); // 角色机构集合 var orgList2 = await GetUserRoleOrgIdList(userId); // 机构并集 orgIdList = orgList1.Select(u => u.OrgId).Union(orgList2).ToList(); // 当前所属机构 if (!orgIdList.Contains(_userManager.OrgId)) orgIdList.Add(_userManager.OrgId); _sysCacheService.Set(CacheConst.KeyOrgIdList + userId, orgIdList); // 存缓存 } return orgIdList; } /// /// 获取用户角色机构Id集合 /// /// /// private async Task> GetUserRoleOrgIdList(long userId) { var roleList = await _sysUserRoleService.GetUserRoleList(userId); if (roleList.Count < 1) return new List(); // 空机构Id集合 return await GetUserOrgIdList(roleList); } /// /// 根据角色Id集合获取机构Id集合 /// /// /// private async Task> GetUserOrgIdList(List roleList) { // 按最大范围策略设定(若同时拥有ALL和SELF权限,则结果ALL) int strongerDataScopeType = (int)DataScopeEnum.Self; // 角色集合拥有的数据范围 var customDataScopeRoleIdList = new List(); if (roleList != null && roleList.Count > 0) { roleList.ForEach(u => { if (u.DataScope == DataScopeEnum.Define) customDataScopeRoleIdList.Add(u.Id); else if ((int)u.DataScope <= strongerDataScopeType) strongerDataScopeType = (int)u.DataScope; }); } // 根据角色集合获取机构集合 var orgIdList1 = await _sysRoleOrgService.GetRoleOrgIdList(customDataScopeRoleIdList); // 根据数据范围获取机构集合 var orgIdList2 = await GetOrgIdListByDataScope(strongerDataScopeType); // 缓存当前用户最大角色数据范围 _sysCacheService.Set(CacheConst.KeyMaxDataScope + _userManager.UserId, strongerDataScopeType); // 并集机构集合 return orgIdList1.Union(orgIdList2).ToList(); } /// /// 根据数据范围获取机构Id集合 /// /// /// private async Task> GetOrgIdListByDataScope(int dataScope) { var orgId = _userManager.OrgId; var orgIdList = new List(); // 若数据范围是全部,则获取所有机构Id集合 if (dataScope == (int)DataScopeEnum.All) { orgIdList = await _sysOrgRep.AsQueryable().Select(u => u.Id).ToListAsync(); } // 若数据范围是本部门及以下,则获取本节点和子节点集合 else if (dataScope == (int)DataScopeEnum.DeptChild) { orgIdList = await GetChildIdListWithSelfById(orgId); } // 若数据范围是本部门不含子节点,则直接返回本部门 else if (dataScope == (int)DataScopeEnum.Dept) { orgIdList.Add(orgId); } return orgIdList; } /// /// 根据节点Id获取子节点Id集合(包含自己) /// /// /// [ApiDescriptionSettings(false)] public async Task> GetChildIdListWithSelfById(long pid) { var orgTreeList = await _sysOrgRep.AsQueryable().ToChildListAsync(u => u.Pid, pid, true); return orgTreeList.Select(u => u.Id).ToList(); } }