递归数据树结构

递归

树结构

        /// <summary>
        /// 组织结构树
        /// </summary>
        public class TreeStructure
        {
            public string ID { get; set; }
            public string ParentId { get; set; }

            public string Name { get; set; }

            public string IsProject { get; set; }
            public string Flag { get; set; }

            public int Rowno { get; set; }

            private List<TreeStructure> _childNodes = new List<TreeStructure>();
            public List<TreeStructure> ChildNodes  {
                get { return _childNodes; } 
                set { _childNodes = value; }
            }
        }
树结构

递归出树结构,第一级开始

        /// <summary>
        /// 组织树结构
        /// </summary>
        /// <param name="lstTreeStructure">所有数据</param>
        /// <param name="parentId">父Id</param>
        /// <returns></returns>
        public static IEnumerable<TreeStructure> Recursive(List<TreeStructure> lstTreeStructure,string parentId)
        {
            var lstTreeStructureRec = lstTreeStructure.Where(m => m.ParentId == parentId).ToList();
            lstTreeStructureRec.ForEach(m => m.ChildNodes.AddRange(Recursive(lstTreeStructure, m.ID)));
            return lstTreeStructureRec;
        }
View Code

后添加的:递归出树结构,某节点开始 ,跟第一级开始的关联

        /// <summary>
        /// 树结构(自己)
        /// </summary>
        /// <param name="lstDataAll">全部数据</param>
        /// <param name="id">Id</param>
        /// <returns></returns>
        public IEnumerable<TreeRecord> RecursiveSelf(List<TreeRecord> lstDataAll, string id)
        {
            var lstTreeRecordRec = lstDataAll.FindAll(m => m.Id == id);
            lstTreeRecordRec.ForEach(m => m.ChildNodes.AddRange(Recursive(lstDataAll, m.Id)));
            return lstTreeRecordRec;
        }
View Code

 

查找符合条件的上级

        /// <summary>
        /// 查找公司
        /// </summary>
        /// <param name="lstTStructureAll">所有组织</param>
        /// <param name="struId">当前组织id</param>
        /// <returns></returns>
        public static TStructure GetCompany(List<TStructure> lstTStructureAll, Guid? struId)
        {
            TStructure modTStructure = lstTStructureAll.FirstOrDefault(m => m.StruID == struId);
            if (null != modTStructure && modTStructure.IsDept.HasValue)
            {
                if (!(Boolean)modTStructure.IsDept)
                {
                    return modTStructure;
                }
            }
            else
            {
                return modTStructure;
            }

            return GetCompany(lstTStructureAll, modTStructure.ParentStruID);
        }
View Code

 

查找所有符合条件的子级

        /// <summary>
        /// 获取子级公司(不包括自己)
        /// </summary>
        /// <param name="lstTStructureAll">所有组织</param>
        /// <param name="struId">当前组织id</param>
        /// <param name="lstTStructure">子集</param>
        public static void ChildTStructure(List<TStructure> lstTStructureAll, Guid? struId, List<TStructure> lstTStructure)
        {
            List<TStructure> lstStructureChild = lstTStructureAll.FindAll(m => m.ParentStruID == struId);
            foreach (var item in lstStructureChild)
            {
                if (item.IsDept.HasValue && !(Boolean)item.IsDept)
                {
                    if (!lstTStructure.Any(m => m.StruID == item.StruID))
                    {
                        lstTStructure.Add(item);
                    }
                }
                ChildTStructure(lstTStructureAll, item.StruID, lstTStructure);
            }

            return;
        }
View Code


查找所有符合条件的父级

        /// <summary>
        /// 获取父级公司(包括自己)
        /// </summary>
        /// <param name="lstTStructureAll"></param>
        /// <param name="struId">当前组织id</param>
        /// <param name="lstTStructure"></param>
        /// <returns></returns>
        public static TStructure ParentTStructure(List<TStructure> lstTStructureAll, Guid? struId, List<TStructure> lstTStructure)
        {
            TStructure modTStructure = lstTStructureAll.FirstOrDefault(m => m.StruID == struId);
            if (null == modTStructure)
            {
                return modTStructure;
            }
            if (modTStructure.IsDept.HasValue && !(Boolean)modTStructure.IsDept)
            {
                if (!lstTStructure.Any(m => m.StruID == modTStructure.StruID))
                {
                    lstTStructure.Add(modTStructure);
                }
            }
            if (!modTStructure.ParentStruID.HasValue || modTStructure.ParentStruID.Equals(Guid.Empty))
            {
                return modTStructure;
            }

            return ParentTStructure(lstTStructureAll, modTStructure.ParentStruID, lstTStructure);
        }
View Code

 ----------------------------------

另一个,整理的

 

        #region ***

        /// <summary>
        /// 根据用户Id获取自己公司和下级所有公司Id
        /// </summary>
        /// <param name="userId">用户Id</param>
        /// <returns></returns>
        public List<string> GetStructIdByUserId(string userId)
        {
            List<string> lstStruId = new List<string>();
            var modUser = _userRepository.Get(m => m.IsDelete == false && m.Enabled == true && m.UserId == userId);
            if (null == modUser)
            {
                return lstStruId;
            }

            lstStruId = GetStructIdByStruId(modUser.StruId, "0");

            return lstStruId;
        }

        /// <summary>
        /// 根据节点获取自己和下级所有公司Id
        /// </summary>
        /// <param name="struId">节点Id</param>
        /// <param name="flag">标识(0:公司)</param>
        /// <returns></returns>
        public List<string> GetStructIdByStruId(string struId, string flag = "0")
        {
            List<TreeRecord> lstDataAll = _structureRepository.Fetch(m => m.Enabled == true).OrderBy(m => m.Depth).ThenBy(m => m.StruType).ThenBy(m => m.StruName).Select(m => new TreeRecord()
            {
                Id = m.StruId,
                Name = m.StruName,
                ParentId = m.ParentStruId,
                Level = m.Depth,
                Rowno = m.DepCode,
                Flag = m.StruType
            }).ToList();

            List<string> lstStruId = new List<string>();
            string strCompanyId = string.Empty; //公司Id
            //自己
            var modStruct = lstDataAll.Find(m => m.Id == struId);
            if (null == modStruct)
            {
                return lstStruId;
            }
            if (modStruct.Flag == flag)
            {
                strCompanyId = modStruct.Id;
            }
            else
            {
                var modTreeRecord = GetCompany(lstDataAll, struId, flag); //获取公司
                strCompanyId = modTreeRecord.Id;
            }
            List<TreeRecord> lstDataSource = new List<TreeRecord>();
            RecursiveChild(lstDataAll, strCompanyId, ref lstDataSource, flag);
            lstStruId = lstDataSource.Select(m => m.Id).ToList();
            lstStruId.Add(strCompanyId);

            return lstStruId;
        }

        /// <summary>
        /// 根据用户Id获取组织树结构
        /// </summary>
        /// <param name="userId">用户Id</param>
        /// <returns></returns>
        public IEnumerable<TreeRecord> GetStructTreeByUserId(string userId)
        {
            List<TreeRecord> lstTreeRecord = new List<TreeRecord>();
            var modUser = _userRepository.Get(m => m.IsDelete == false && m.Enabled == true && m.UserId == userId);
            if (null == modUser)
            {
                return lstTreeRecord;
            }
            var strStruId = modUser.StruId;
            List<TreeRecord> lstDataAll = _structureRepository.Fetch(m => m.Enabled == true).OrderBy(m => m.Depth).ThenBy(m => m.StruType).ThenBy(m => m.StruName).Select(m => new TreeRecord()
            {
                Id = m.StruId,
                Name = m.StruName,
                ParentId = m.ParentStruId,
                Level = m.Depth,
                Rowno = m.DepCode,
                Flag = m.StruType
            }).ToList();
            lstTreeRecord = GetStructTreeByStruId(lstDataAll, strStruId).ToList();

            return lstTreeRecord;
        }

        /// <summary>
        /// 根据节点获取组织树结构(所在标识节点和下级)
        /// </summary>
        /// <param name="lstDataAll">全部数据</param>
        /// <param name="struId">节点组织id</param>
        /// <param name="flag">标识</param>
        /// <returns></returns>
        public IEnumerable<TreeRecord> GetStructTreeByStruId(List<TreeRecord> lstDataAll, string struId, string flag = "0")
        {
            List<TreeRecord> lstTreeRecord = new List<TreeRecord>();
            List<TreeRecord> lstDataSource = new List<TreeRecord>();    //需要的数据
            string strCompanyId = string.Empty; //公司Id
            //自己
            var modStruct = lstDataAll.Find(m => m.Id == struId);
            if (null != modStruct)
            {
                if (modStruct.Flag == flag)
                {
                    lstDataSource.Add(modStruct);
                    strCompanyId = modStruct.Id;
                }
                else
                {
                    var modTreeRecord = GetCompany(lstDataAll, struId, flag); //获取公司
                    lstDataSource.Add(modTreeRecord);
                    strCompanyId = modTreeRecord.Id;
                }
            }
            RecursiveChild(lstDataAll, strCompanyId, ref lstDataSource, flag); //id:用所在公司的id
            lstTreeRecord = RecursiveSelf(lstDataSource.OrderBy(m => m.Level).ThenBy(m => m.Name).ToList(), strCompanyId).ToList();

            return lstTreeRecord;
        }


        /// <summary>
        /// 根据节点获取组织树结构(包含上级)
        /// </summary>
        /// <param name="lstDataAll">全部数据</param>
        /// <param name="struId">节点组织id</param>
        /// <param name="flag">标识</param>
        /// <returns></returns>
        public IEnumerable<TreeRecord> GetStructTreeHasParentByStruId(List<TreeRecord> lstDataAll, string struId, string flag = "0")
        {
            List<TreeRecord> lstTreeRecord = new List<TreeRecord>();
            List<TreeRecord> lstDataSource = new List<TreeRecord>();    //需要的数据
            RecursiveParent(lstDataAll, struId, ref lstDataSource, flag);
            RecursiveChild(lstDataAll, struId, ref lstDataSource, flag);
            lstTreeRecord = Recursive(lstDataSource.OrderBy(m => m.Level).ThenBy(m => m.Name).ToList(), "").ToList();

            return lstTreeRecord;
        }

        /// <summary>
        /// 根据组织Id获取所在flag组织
        /// </summary>
        /// <param name="lstDataAll">全部数据</param>
        /// <param name="struId">组织Id</param>
        /// <param name="flag">标识</param>
        /// <returns></returns>
        public TreeRecord GetCompany(List<TreeRecord> lstDataAll, string struId, string flag = "0")
        {
            var modTreeRecord = lstDataAll.Find(m => m.Id == struId);
            if (null != modTreeRecord)
            {
                if (modTreeRecord.Flag == flag)
                {
                    return modTreeRecord;
                }
                else
                {
                    return GetCompany(lstDataAll, modTreeRecord.ParentId, flag);
                }
            }
            else
            {
                return modTreeRecord;
            }
        }

        /// <summary>
        /// 获取父级数据(包含自己)
        /// </summary>
        /// <param name="lstDataAll">全部数据</param>
        /// <param name="id">节点Id</param>
        /// <param name="lstDataParent">返回的全部数据(加上父级)</param>
        /// <param name="flag">标识</param>
        /// <returns></returns>
        public IEnumerable<TreeRecord> RecursiveParent(List<TreeRecord> lstDataAll, string id, ref List<TreeRecord> lstDataParent, string flag = "0")
        {
            var modTreeRecord = lstDataAll.FirstOrDefault(m => m.Id == id);
            if (null == modTreeRecord)
            {
                return lstDataParent;
            }
            if (modTreeRecord.Flag == flag)
            {
                if (!lstDataParent.Any(m => m.Id == modTreeRecord.Id))
                {
                    lstDataParent.Add(modTreeRecord);
                }
            }
            return RecursiveParent(lstDataAll, modTreeRecord.ParentId, ref lstDataParent);
        }

        /// <summary>
        /// 获取子集数据(不包括自己)
        /// </summary>
        /// <param name="lstDataAll">全部数据</param>
        /// <param name="id">节点Id</param>
        /// <param name="lstDataChild">返回的全部数据(加上子级)</param>
        /// <param name="flag"></param>
        /// <returns></returns>
        public IEnumerable<TreeRecord> RecursiveChild(List<TreeRecord> lstDataAll, string id, ref List<TreeRecord> lstDataChild, string flag = "0")
        {
            List<TreeRecord> lstTreeRecord = lstDataAll.FindAll(m => m.ParentId == id);
            foreach (var item in lstTreeRecord)
            {
                if (item.Flag == flag)
                {
                    if (!lstDataChild.Any(m => m.Id == item.Id))
                    {
                        lstDataChild.Add(item);
                    }
                }
                RecursiveChild(lstDataAll, item.Id, ref lstDataChild, flag);
            }
            return lstDataChild;
        }

        /// <summary>
        /// 树结构
        /// </summary>
        /// <param name="lstDataAll">全部数据</param>
        /// <param name="parentId">父Id</param>
        /// <returns></returns>
        public IEnumerable<TreeRecord> Recursive(List<TreeRecord> lstDataAll, string parentId)
        {
            var lstTreeRecordRec = lstDataAll.FindAll(m => m.ParentId == parentId);
            lstTreeRecordRec.ForEach(m => m.ChildNodes.AddRange(Recursive(lstDataAll, m.Id)));
            return lstTreeRecordRec;
        }

        /// <summary>
        /// 树结构(自己)
        /// </summary>
        /// <param name="lstDataAll">全部数据</param>
        /// <param name="id">Id</param>
        /// <returns></returns>
        public IEnumerable<TreeRecord> RecursiveSelf(List<TreeRecord> lstDataAll, string id)
        {
            var lstTreeRecordRec = lstDataAll.FindAll(m => m.Id == id);
            lstTreeRecordRec.ForEach(m => m.ChildNodes.AddRange(Recursive(lstDataAll, m.Id)));
            return lstTreeRecordRec;
        }

        #endregion
View Code

 

    /// <summary>
    /// 树形结构
    /// </summary>
    public class TreeRecord
    {
        /// <summary>
        /// Id
        /// </summary>
        public string Id { get; set; }

        /// <summary>
        /// 父级id
        /// </summary>
        public string ParentId { get; set; }

        /// <summary>
        /// 名称
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 标识
        /// </summary>
        public string Flag { get; set; }

        /// <summary>
        /// 层级
        /// </summary>
        public int Level { get; set; }

        /// <summary>
        /// 是否选中
        /// </summary>
        public bool IsChecked { get; set; }

        /// <summary>
        /// 排序号
        /// </summary>
        public string Rowno { get; set; }

        /// <summary>
        /// 子集
        /// </summary>
        private List<TreeRecord> _childNodes = new List<TreeRecord>();

        /// <summary>
        /// 子集
        /// </summary>
        public List<TreeRecord> ChildNodes
        {
            get { return _childNodes; }
            set { _childNodes = value; }
        }
    }
View Code

 

posted @ 2017-06-05 14:22  bxzjzg  阅读(265)  评论(0编辑  收藏  举报