/// <summary> /// 添加部门 /// </summary> /// <param name="org">The org.</param> /// <returns>System.Int32.</returns> /// <exception cref="System.Exception">未能找到该组织的父节点信息</exception> public Guid AddOrUpdate(Org org) { ChangeModuleCascade(org); if (org.Id == Guid.Empty) { _repository.Add(org); } else { //获取旧的的CascadeId var CascadeId = _repository.FindSingle(o => o.Id == org.Id).CascadeId; //根据CascadeId查询子部门 var orgs = _repository.Find(u => u.CascadeId.Contains(CascadeId) && u.Id != org.Id).OrderBy(u => u.CascadeId).ToList(); //更新操作 _repository.Update(org); //更新子部门的CascadeId foreach (var a in orgs) { ChangeModuleCascade(a); _repository.Update(a); } } return(org.Id); }
/// <summary> /// 删除指定ID的部门及其所有子部门 /// </summary> public void DelOrg(int id) { var delOrg = _repository.FindSingle(u => u.Id == id); if (delOrg == null) { return; } _repository.Delete(u => u.CascadeId.Contains(delOrg.CascadeId)); }
/// <summary> /// 获取当前组织的所有下级组织 /// </summary> private int[] GetSubOrgIds(int orgId) { var org = _orgRepository.FindSingle(u => u.Id == orgId); var orgs = _orgRepository.Find(u => u.CascadeId.Contains(org.CascadeId)).Select(u => u.Id).ToArray(); return(orgs); }
//修改对象的级联ID,生成类似XXX.XXX.X.XX private void ChangeModuleCascade(Org org) { string cascadeId; int currentCascadeId = 1; //当前结点的级联节点最后一位 var sameLevels = _repository.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 != Guid.Empty) { var parentOrg = _repository.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; }