/// <summary> /// 查询根节点 /// </summary> /// <returns></returns> private SysDepartEntity GetRoot() { string Key = string.Format(CacheKey.JOOSHOW_SYSDEPART_CACHE, this.CompanyID); List <SysDepartEntity> listSource = GetList(); if (listSource.IsNullOrEmpty()) { SysDepartEntity entity = new SysDepartEntity(); entity.SnNum = ConvertHelper.NewGuid(); entity.DepartNum = new TNumProvider(this.CompanyID).GetSwiftNum(typeof(SysDepartEntity), 5); entity.IsDelete = (int)EIsDelete.NotDelete; entity.CreateTime = DateTime.Now; entity.DepartName = "Root"; entity.ParentNum = ""; entity.CompanyID = this.CompanyID; entity.Left = 1; entity.Right = 2; entity.IncludeAll(); int line = this.SysDepart.Add(entity); if (line > 0) { CacheHelper.Remove(Key); } } listSource = GetList(); SysDepartEntity result = listSource.First(item => item.ParentNum.IsEmpty()); return(result); }
/// <summary> /// 获得所有的部门信息 /// </summary> /// <returns></returns> public List <SysDepartEntity> GetList() { string key = string.Format(CacheKey.JOOSHOW_SYSDEPART_CACHE, this.CompanyID); List <SysDepartEntity> listResult = CacheHelper.Get(key) as List <SysDepartEntity>; if (!listResult.IsNullOrEmpty()) { return(listResult); } SysDepartEntity temp = new SysDepartEntity(); temp.IncludeAll(); temp.Where(a => a.IsDelete == (int)EIsDelete.NotDelete) .And(item => item.CompanyID == this.CompanyID); listResult = this.SysDepart.GetList(temp); if (!listResult.IsNullOrEmpty()) { foreach (SysDepartEntity item in listResult) { int depth = listResult.Where(a => a.Left <= item.Left && a.Right >= item.Right).Count(); item.Depth = depth; } foreach (SysDepartEntity entity in listResult.Where(itemParent => !string.IsNullOrEmpty(itemParent.ParentNum))) { SysDepartEntity tempEntity = listResult.SingleOrDefault(item => item.SnNum == entity.ParentNum); if (!tempEntity.IsNull()) { entity.ParentName = tempEntity.DepartName; } } CacheHelper.Insert(key, listResult, null, DateTime.Now.AddDays(1)); } return(listResult); }
/// <summary> /// 新增部门 /// </summary> /// <returns></returns> public int Add(SysDepartEntity entity) { string Key = string.Format(CacheKey.JOOSHOW_SYSDEPART_CACHE, this.CompanyID); if (entity.ParentNum.IsEmpty()) { SysDepartEntity parent = GetRoot(); entity.ParentNum = GetRoot().SnNum; } List <SysDepartEntity> listSource = GetList(); listSource = listSource.IsNull() ? new List <SysDepartEntity>() : listSource; using (TransactionScope ts = new TransactionScope()) { //查询新增节点的上一个节点 SysDepartEntity parent = listSource.FirstOrDefault(item => item.SnNum == entity.ParentNum); int rightValue = parent.Right; List <SysDepartEntity> listNodes = listSource.Where(item => item.Right >= rightValue).ToList(); if (!listNodes.IsNullOrEmpty()) { foreach (SysDepartEntity item in listNodes) { item.Right += 2; item.IncludeRight(true); item.Where(b => b.SnNum == item.SnNum).And(b => item.CompanyID == this.CompanyID); this.SysDepart.Update(item); } } listNodes = listSource.Where(item => item.Left >= rightValue).ToList(); if (!listNodes.IsNullOrEmpty()) { foreach (SysDepartEntity item in listNodes) { item.Left += 2; item.IncludeLeft(true); item.Where(b => b.SnNum == item.SnNum).And(b => item.CompanyID == this.CompanyID); this.SysDepart.Update(item); } } entity.SnNum = ConvertHelper.NewGuid(); entity.DepartNum = entity.DepartNum.IsEmpty() ? new TNumProvider(this.CompanyID).GetSwiftNum(typeof(SysDepartEntity), 5) : string.Empty; entity.CompanyID = this.CompanyID; entity.IsDelete = (int)EIsDelete.NotDelete; entity.CreateTime = DateTime.Now; entity.Left = rightValue; entity.Right = rightValue + 1; entity.IncludeAll(); int line = this.SysDepart.Add(entity); if (line > 0) { CacheHelper.Remove(Key); } ts.Complete(); return(line); } }