using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

/// <summary>
/// 转为树形结构
/// </summary>
namespace Nirvana.Common
{
    public class ToTree<T> where T:IToTreeModel
    {
        public static List<T> ToDo(List<T> models)
        {
            var dtoMap = new Dictionary<int, T>();
            foreach (var item in models)
            {
                dtoMap.Add(item.Id, item);
            }
            List<T> result = new List<T>();
            foreach (var item in dtoMap.Values)
            {
                if (item.ParentId == 0)
                {
                    result.Add(item);
                }
                else
                {
                    if (dtoMap.ContainsKey(item.ParentId))
                    {
                        dtoMap[item.ParentId].AddChilrden(item);
                    }
                }
            }
            return result;
        }
    }

    public interface IToTreeModel
    {
        int Id { get; set; }
        int ParentId { get; set; }
        public string Name { get; set; }
        List<IToTreeModel> treeList { get; set; }
        void AddChilrden(IToTreeModel node);
    }

    public class ShowMessageUpdatesViewModel : IToTreeModel
    {
        public int Id { get; set; }
        public int ParentId { get; set; }
        public string Name { get; set; }
        public int status { get; set; }
        public List<IToTreeModel> treeList { get; set; }
        public void AddChilrden(IToTreeModel node)
        {
            if (treeList == null)
                treeList = new List<IToTreeModel>();
            this.treeList.Add(node);
        }
    }
}
