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
{
///
/// 把列表转换为(*,*,*,*,...)字符串,空列表返回null
///
public static string getString(List list)
{
string listString;
if (list==null||list.Count<=0)
{
return null;
}
StringBuilder sb = new StringBuilder("(");
switch (typeof(T).Name)
{
case "DateTime":
case "String":
foreach (var item in list)
{
sb.Append("'" + item + "',");
}
break;
default:
foreach (var item in list)
{
sb.Append(item + ",");
}
break;
}
listString = sb.ToString().TrimEnd(',')+')';
return listString;
}
public static string GetWhereStr(List 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(List TList, PkName pkName, out List> ReList)
{
ReList = new List>();
var nodeDict = new Dictionary>();
foreach(var parent in TList)
{
int value = Convert.ToInt32(parent.GetType().GetProperty(pkName.MainField).GetValue(parent));
var recursion = new Recursion()
{
data = 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]);
}
}
}
}
}