using cpf360.Common;
using cpf360.ModelInfo;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace cpf360.BLL
{
    public class TreeService<T> where T : TreeModel<T>
    {
        public static List<T> TreeLoop(List<T> datas)
        {
            return TreeLoop(datas, datas);
        }

        public static List<T> TreeLoop(List<T> basedatas, List<T> datas, long parentId = 0, string pathId = "0/", string pathName = "Root/")
        {
            var result = datas.Where(tm => tm.ParentId == parentId).ToList();
            result.ForEach(item =>
            {
                item.PathId = pathId.IsNullOrEmpty() ? "{0}/".FormatString(item.Id) : "{0}{1}/".FormatString(pathId, item.Id);
                item.PathName = pathName.IsNullOrEmpty() ? "{0}/".FormatString(item.Title) : "{0}{1}/".FormatString(pathName, item.Title);
                item.Childrens = basedatas.Where(tm => tm.ParentId == item.Id).ToList();
                item.HasChildrens = item.Childrens.Count > 0;
                if (item.HasChildrens)
                {
                    TreeLoop(basedatas, item.Childrens, item.Id, item.PathId, item.PathName);
                }
            });
            return result;
        }
    }
}

 

posted on 2018-04-11 10:39  chester·chen  阅读(221)  评论(0编辑  收藏  举报