using JLHHJSvr.Com.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Linq;
using System.Text;

namespace DirectService.Tools
{
    internal static class ListEx
    {
        /// <summary>
        /// 把列表转换为(*,*,*,*,...)字符串，空列表返回null
        /// </summary>
        public static string getString<T>(IEnumerable<T> list)
        {
            string listString;

            if (list == null || !list.Any())
            {
                return null;
            }
            StringBuilder sb = new StringBuilder("(");
            if (typeof(T).IsAssignableFrom(typeof(string)) || typeof(T).IsAssignableFrom(typeof(DateTime)))
            {
                foreach (var item in list)
                {
                    sb.Append("'" + item + "',");
                }
            }
            else
            {
                foreach (var item in list)
                {
                    sb.Append(item + ",");
                }
            }
            listString = sb.ToString().TrimEnd(',') + ')';
            return listString;
        }

        public static string GetWhereStr(List<string> whereList, string connector = "AND")
        {
            string connectStr = " " + connector + " ";
            var whereStr = string.Empty;
            for (int i = 0; i < whereList.Count; i++)
            {
                if (i == 0)
                {
                    whereStr += "(" + whereList[i] + ")";
                }
                else
                {
                    whereStr += connectStr + "(" + whereList[i] + ")";
                }
            }
            return whereStr;
        }

        public static void GetRecursions<T>(List<T> TList, PkName pkName, out List<Recursion<T>> ReList)
        {
            ReList = new List<Recursion<T>>();
            var nodeDict = new Dictionary<int, Recursion<T>>();

            foreach(var parent in TList)
            {
                int value = Convert.ToInt32(parent.GetType().GetProperty(pkName.MainField).GetValue(parent));
                var recursion = new Recursion<T>()
                {
                    data = parent
                };
                recursion.value = Convert.ToInt32(parent.GetType().GetProperty(pkName.MainField).GetValue(parent));
                recursion.text = Convert.ToString(parent.GetType().GetProperty(pkName.MainName).GetValue(parent));

                nodeDict[value] = recursion;
            }

            // 添加子节点
            foreach (var child in TList)
            {
                int value = Convert.ToInt32(child.GetType().GetProperty(pkName.MainField).GetValue(child));
                int parentid = Convert.ToInt32(child.GetType().GetProperty(pkName.ParentField).GetValue(child));

                if (parentid == 0)
                {
                    ReList.Add(nodeDict[value]);
                }else if (nodeDict.TryGetValue(parentid, out var parentRecursion))
                {
                    parentRecursion.AddChild(nodeDict[value]);
                }
            }
        }
    }
}
