• 树型结构递归 实体递归 JSON格式


    用递归实现无限级菜单,产品分类,盖楼式评论、留言等功能。

    下列代码不能直接使用

    CREATE TABLE [dbo].[P_Category](
    	[Code] [varchar](36) NOT NULL PRIMARY KEY,
    	[Parent_Code] [varchar](36) NULL,
    	[Depth] [int] NULL,
    	[Name] [varchar](50) NULL
    ) 
    GO
    
    INSERT [dbo].[P_Category] ([Code], [Parent_Code], [Depth], [Name]) VALUES (N'101', N'1', 2, N'木门')
    INSERT [dbo].[P_Category] ([Code], [Parent_Code], [Depth], [Name]) VALUES (N'10101', N'101', 3, N'室内木门')
    INSERT [dbo].[P_Category] ([Code], [Parent_Code], [Depth], [Name]) VALUES (N'1010101', N'10101', 4, N'A02')
    INSERT [dbo].[P_Category] ([Code], [Parent_Code], [Depth], [Name]) VALUES (N'101010101', N'10101', 5, N'A01')
    INSERT [dbo].[P_Category] ([Code], [Parent_Code], [Depth], [Name]) VALUES (N'10101010101', N'101010101', 6, N'A0101')
    INSERT [dbo].[P_Category] ([Code], [Parent_Code], [Depth], [Name]) VALUES (N'102', N'1', 2, N'B')
    INSERT [dbo].[P_Category] ([Code], [Parent_Code], [Depth], [Name]) VALUES (N'10201', N'102', 3, N'B1')
    INSERT [dbo].[P_Category] ([Code], [Parent_Code], [Depth], [Name]) VALUES (N'1020101', N'10201', 4, N'B2')
    

      

    private List<Category> CategoryCacheAllList { get; set; }
    
    [Route("")]
    public HttpResponseMessage Get()
    {
    	var list = CacheHelper<List<Category>>.GetCache(CategoryAllListCacheKEY);
    	if (list == null)
    	{
    		CategoryCacheAllList = CategoryService.GetCacheList(); //取得数据库里面所有数据
    		list = new List<Category>();
    		CategoryJson(list, "1"); 
    		CacheHelper<List<Category>>.SetCache(CategoryAllListCacheKEY, list);
    	}
    	return Request.CreateResponse(HttpStatusCode.OK, list);
     	//下面的代码这个没试
    	//string json = JsonConvert.SerializeObject(categoryList, Formatting.Indented);
    	//return json;	
    }
    
    
    /// <summary>
    /// 取得兄弟节点
    /// </summary>
    /// <param name="categoryList"></param>
    /// <param name="parentCode"></param>
    public void CategoryJson(List<Category> categoryList, string parentCode)
    {
    	var list = CategoryCacheAllList.FindAll(p => p.ParentCode == parentCode);
    	if (list.Count > 0)
    	{
    		foreach (var item in list)
    		{
    			CategoryTreeJson(item, item.Code);
    			categoryList.Add(item);
    		}
    	}
    }
    
    /// <summary>
    /// 递归出子对象
    /// </summary>
    /// <param name="sbCategory"></param>
    /// <param name="parentCode"></param>
    private void CategoryTreeJson(Category sbCategory, string parentCode)
    {
    	var list = CategoryCacheAllList.FindAll(p => p.ParentCode == parentCode);
    	if (list.Count > 0)
    	{
    		sbCategory.Children = new List<Category>();
    		foreach (var item in list)
    		{
    			CategoryTreeJson(item, item.Code);
    			sbCategory.Children.Add(item);
    		}
    	}
    }
    

      

    namespace VipSoft.Base.Core.Entity
    {
        /// <summary> 
        ///  产品分类
        /// </summary>
        [Table("VipSoft_Category")]
        public class Category
        {
            /// <summary> 
            /// 编码
            /// </summary>
            [Column(ColumnType.IncrementPrimary, Name = "Code")]
            public string Code { get; set; }
    
            /// <summary> 
            /// 父级编码
            /// </summary>
            [Column(Name = "Parent_Code")]
            public string ParentCode { get; set; }
    
            /// <summary> 
            /// 深度
            /// </summary>
            [Column(Name = "Depth")]
            public int? Depth { get; set; }
    
            /// <summary> 
            /// 分类名称
            /// </summary>
            [Column(Name = "Name")]
            public string Name { get; set; }
     
            /// <summary> 
            /// 排序
            /// </summary>
            [Column(Name = "Sequence")]
            public int? Sequence { get; set; }
    
            /// <summary> 
            /// 状态
            /// </summary>
            [Column(Name = "Status")]
            public int? Status { get; set; }
    
            /// <summary> 
            /// 创建时间
            /// </summary>
            [Column(Name = "Create_Date")]
            public DateTime? CreateDate { get; set; }
    
    
            public List<Category> Children { get; set; }
            
        }
    }
  • 相关阅读:
    python中类方法、类实例方法、静态方法的使用与区别
    在python里如何动态添加类的动态属性呢?
    PYTHON基础
    EXCEL 写入
    thread 多线程
    Python 常用函数
    列表减列表
    04_Linux搭建Jdk和tomcat环境
    自动生成和安装requirements.txt依赖
    python+selenium面试题
  • 原文地址:https://www.cnblogs.com/vipsoft/p/4343493.html
Copyright © 2020-2023  润新知