美文网首页
C# 封装树状

C# 封装树状

作者: Rinaloving | 来源:发表于2022-09-19 00:42 被阅读0次
    
    using System;
    using System.Collections.Generic;
    using System.Reflection;
    using System.Text;
    
    namespace com.uplus.tree
    {
        public class TreeHelper
        {
    
            #region 获取所有根
    
            public static List<T>  GetList<T>(List<T> t, int nRootLevel) 
            {
                List<T> reList = new List<T>();
                foreach (var item in t)
                {
                    var newType = item.GetType();
                    string code = "";
                    string name = "";
                    int nType = 0;
                    foreach (var itemProperty in newType.GetRuntimeProperties())
                    {
    
                        var dtlName = itemProperty.Name.ToUpper();
                        var dtlType = itemProperty.PropertyType.Name;
                        var oldValue = itemProperty.GetValue(item);
                        switch (dtlName)
                        {
                            case "CODE":
                                code = oldValue.ToString();
                                break;
                            case "SNAME":
                                name = oldValue.ToString();
                                break;
                            case "NTYPE":
                                nType = Convert.ToInt32(oldValue);
                                break;
                            case "NLEVEL":
                                nType = Convert.ToInt32(oldValue);
                                break;
    
                            default:
                                break;
                        }
                       
                    }
                    if (nType == nRootLevel)
                    {
                        reList.Add(item);
                    }
                }
                return reList;
    
            }
    
    
            #endregion
    
            #region 获取所有子节点
    
            public static void GetSonList<T>(List<T> t, int nLevel, string parentId,  out List<T> tResList) where T : class
            {
                List<T> sonList = new List<T>();
                foreach (var item in t)
                {
                    var newType = item.GetType();
                    string code = "";
                    string parentCode = "";
                    string name = "";
                    int nType = 0;
                    foreach (var itemProperty in newType.GetRuntimeProperties())
                    {
    
                        var dtlName = itemProperty.Name.ToUpper();
                        var dtlType = itemProperty.PropertyType.Name;
                        var oldValue = itemProperty.GetValue(item);
                        switch (dtlName)
                        {
                            case "CODE":
                                code = oldValue.ToString();
                                break;
                            case "SPARENTCODE":
                                parentCode = oldValue?.ToString();
                                break;
                            case "SNAME":
                                name = oldValue.ToString();
                                break;
                            case "NTYPE":
                                nType = Convert.ToInt32(oldValue);
                                break;
                            case "NLEVEL":
                                nType = Convert.ToInt32(oldValue);
                                break;
    
                            default:
                                break;
                        }
                        
                    }
                    if (parentCode == parentId&& nLevel+1== nType)
                    {
                        sonList.Add(item);
                    }
                }
                tResList =  sonList;
    
            }
    
    
            #endregion
    
    
            #region 获取树形数据
            /// <summary>
            ///  获取树形数据 
            /// </summary>
            /// <returns></returns>
            /// <remarks> add by cfl 2022-09-19</remarks>
            public static List<TreeData> GetTreeList<T>(List<T> t,int nRootLevel) where T : class, new()
            {
                 //List<T> reList = new List<T>();
    
                List<T> resModelList = GetList<T>(t, nRootLevel);
                List<TreeData> trees = new List<TreeData>();
                if (t != null && resModelList.Count > 0)
                {
                    //获取顶级节点和所有子节点
                    foreach (var item in resModelList)
                    {
                        var newType = item.GetType();
                        string code = "";
                        string name = "";
                        int nType = 0;
                        foreach (var itemProperty in newType.GetRuntimeProperties())
                        {
    
                                var dtlName = itemProperty.Name.ToUpper();
                                var dtlType = itemProperty.PropertyType.Name;
                                var oldValue = itemProperty.GetValue(item);
                                switch (dtlName)
                                {
                                    case "CODE":
                                        code = oldValue.ToString();
                                        break;
                                    case "SNAME":
                                        name = oldValue.ToString();
                                        break;
                                    case "NTYPE":
                                        nType = Convert.ToInt32(oldValue);
                                        break;
                                    case "NLEVEL":
                                        nType = Convert.ToInt32(oldValue);
                                        break;
    
                                    default:
                                        break;
                                }
    
    
                        }
                        //获取顶级节点
                        TreeData treeData = new TreeData();
                        treeData.Children = new List<TreeData>();
                        treeData.Id = code;
                        treeData.Label = name;
                        treeData.Level = nType;
    
                        List<TreeData> treeInfo = new List<TreeData>();
                        //获取子节点
                        getTree<T>(t, nType, code,out treeInfo);
                        if (treeInfo != null && treeInfo.Count > 0)
                        {
                            treeData.Children.AddRange(treeInfo);
                        }
                        trees.Add(treeData);
    
                    }
                }
                return trees;
            }
            #endregion
    
            #region 获取分级后的父子数据
    
            /// <summary>
            /// 递归获取数据
            /// </summary>
            /// <param name="parentId"></param>
            /// <returns></returns>
            public static void getTree<T>(List<T> tList, int nLevel, string parentId,out List<TreeData> treeList) where T : class, new()
            {
                //转换成Easyui数据
                List<TreeData> modelList = new List<TreeData>();
                List<T> smodelList = new List<T>();
                GetSonList(tList, nLevel, parentId, out smodelList);
    
                if (smodelList.Count > 0)
                {
                    foreach (var item in smodelList)
                    {
                        var newType = item.GetType();
                        string code = "";
                        string name = "";
                        int nType = 0;
                        foreach (var itemProperty in newType.GetRuntimeProperties())
                        {
    
                            var dtlName = itemProperty.Name.ToUpper();
                            var dtlType = itemProperty.PropertyType.Name;
                            var oldValue = itemProperty.GetValue(item);
                            switch (dtlName)
                            {
                                case "CODE":
                                    code = oldValue.ToString();
                                    break;
                                case "SNAME":
                                    name = oldValue.ToString();
                                    break;
                                case "NTYPE":
                                    nType = Convert.ToInt32(oldValue);
                                    break;
                                case "NLEVEL":
                                    nType = Convert.ToInt32(oldValue);
                                    break;
    
                                default:
                                    break;
                            }
    
    
                        }
                        TreeData model = new TreeData();
                        model.Children = new List<TreeData>();
                        model.Id = code;
                        model.Label = name;
                        model.Level = nType;
                        List<TreeData> sonTreeInfo = new List<TreeData>();
                        //递归子节点
                        getTree<T>(smodelList, nType,code, out sonTreeInfo);
                        if (sonTreeInfo != null)
                        {
                            model.Children.AddRange(sonTreeInfo);
                        }
                        modelList.Add(model);
                    }
                }
                treeList =  modelList;
            }
    
    
    
            #endregion
    
    
        }
    }
    
    

    相关文章

      网友评论

          本文标题:C# 封装树状

          本文链接:https://www.haomeiwen.com/subject/hcymortx.html