using System;
using System.Collections.Generic;
using System.Linq;
using JLHHJSvr.Com.Model;

namespace JLHHJSvr.LJFramework.Tools
{
    public class menuRecursion
    {
        /// <summary>
        /// 递归查询
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="TList">要查找的全部内容</param>
        /// <param name="pkName">要查找内容的主ID,名称,父ID</param>
        /// <param name="ReList">输出结构</param>
        public void GetRecursions<T>(List<T> TList, PkName pkName, List<Recursion2> ReList, List<int> idList)
        {
            //if (pkName.MainField == string.Empty || pkName.MainName == string.Empty || pkName.ParentField == string.Empty)
            //{
            //    return;
            //}

            var NTList = TList.Where(t => idList.IndexOf((int)t.GetType().GetProperty(pkName.MainField).GetValue(t)) == -1).ToList();
            foreach (var mx in ReList)
            {
                var parentid = mx.value;
                var tmpList = NTList.Where(t => (int)t.GetType().GetProperty(pkName.ParentField).GetValue(t) == parentid).ToList();
                if (tmpList.Count > 0)
                {
                    mx.children = new List<Recursion2>();
                    foreach (var tmp in tmpList)
                    {
                        var Child = new Recursion2()
                        {
                            text = Convert.ToString(tmp.GetType().GetProperty(pkName.MainName).GetValue(tmp)),
                            value = Convert.ToInt32(tmp.GetType().GetProperty(pkName.MainField).GetValue(tmp))
                        };
                        mx.children.Add(Child);
                        idList.Add(Child.value);
                    }
                    GetRecursions(TList, pkName, mx.children, idList);
                }
            }
        }
    }
}
