using System; using System.Collections.Generic; using System.Linq; using OpenAuth.App.Response; using OpenAuth.Repository.Domain; using OpenAuth.Repository.Interface; namespace OpenAuth.App { public class OrgManagerApp { public IUnitWork _unitWork { get; set; } /// /// 添加部门 /// /// The org. /// System.Int32. /// 未能找到该组织的父节点信息 public string AddOrUpdate(Org org) { ChangeModuleCascade(org); if (org.Id == string.Empty) { _unitWork.Add(org); } else { //获取旧的的CascadeId var CascadeId = _unitWork.FindSingle(o => o.Id == org.Id).CascadeId; //根据CascadeId查询子部门 var orgs = _unitWork.Find(u => u.CascadeId.Contains(CascadeId) && u.Id != org.Id).OrderBy(u => u.CascadeId).ToList(); //更新操作 _unitWork.Update(org); //更新子部门的CascadeId foreach (var a in orgs) { ChangeModuleCascade(a); _unitWork.Update(a); } } return org.Id; } /// /// 删除指定ID的部门及其所有子部门 /// public void DelOrg(string[] ids) { var delOrg = _unitWork.Find(u => ids.Contains(u.Id)).ToList(); foreach (var org in delOrg) { _unitWork.Delete(u => u.CascadeId.Contains(org.CascadeId)); } } /// /// 加载特定用户的角色 /// TODO:这里会加载用户及用户角色的所有角色,“为用户分配角色”功能会给人一种混乱的感觉,但可以接受 /// /// The user unique identifier. public List LoadForUser(string userId) { //用户角色 var userRoleIds = _unitWork.Find(u => u.FirstId == userId && u.Key == "UserRole").Select(u => u.SecondId).ToList(); //用户角色与自己分配到的角色ID var moduleIds = _unitWork.Find( u => (u.FirstId == userId && u.Key == "UserOrg") || (u.Key == "RoleOrg" && userRoleIds.Contains(u.FirstId))).Select(u => u.SecondId).ToList(); if (!moduleIds.Any()) return new List(); return _unitWork.Find(u => moduleIds.Contains(u.Id)).ToList(); } /// /// 加载特定角色的角色 /// /// The role unique identifier. public List LoadForRole(string roleId) { var moduleIds = _unitWork.Find(u => u.FirstId == roleId && u.Key == "RoleOrg") .Select(u => u.SecondId) .ToList(); if (!moduleIds.Any()) return new List(); return _unitWork.Find(u => moduleIds.Contains(u.Id)).ToList(); } /// /// 得到部门的所有子部门 /// 如果orgId为0,表示取得所有部门 /// public TableData LoadAllChildren(string orgId) { var query = GetSubOrgs(orgId); return new TableData { data = query.ToList(), count = query.Count(), }; } public IEnumerable GetSubOrgs(string orgId) { string cascadeId = "0."; if (!string.IsNullOrEmpty(orgId)) { var org = _unitWork.FindSingle(u => u.Id == orgId); if (org == null) throw new Exception("未能找到指定对象信息"); cascadeId = org.CascadeId; } return _unitWork.Find(u => u.CascadeId.Contains(cascadeId)); } #region 私有方法 //修改对象的级联ID,生成类似XXX.XXX.X.XX private void ChangeModuleCascade(Org org) { string cascadeId; int currentCascadeId = 1; //当前结点的级联节点最后一位 var sameLevels = _unitWork.Find(o => o.ParentId == org.ParentId && o.Id != org.Id); foreach (var obj in sameLevels) { int objCascadeId = int.Parse(obj.CascadeId.TrimEnd('.').Split('.').Last()); if (currentCascadeId <= objCascadeId) currentCascadeId = objCascadeId + 1; } if (org.ParentId != null && org.ParentId != string.Empty) { var parentOrg = _unitWork.FindSingle(o => o.Id == org.ParentId); if (parentOrg != null) { cascadeId = parentOrg.CascadeId + currentCascadeId+"."; org.ParentName = parentOrg.Name; } else { throw new Exception("未能找到该组织的父节点信息"); } } else { cascadeId = ".0." + currentCascadeId+"."; org.ParentName = "根节点"; } org.CascadeId = cascadeId; } #endregion 私有方法 } }