using System.Collections;
using System.Data;
using System.Reflection;
using SqlSugar;

namespace DataSharing.Repository.Extensions
{
    public static class SqlSugarExtensions
    {
        /// <summary>
        /// List转Dictionary
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="list"></param>
        /// <returns></returns>
        public static List<Dictionary<string, object?>> ToDictionary<T>(this List<T> list)
        {
            var result = new List<Dictionary<string, object?>>();
            if (list.Any())
            {
                foreach (var item in list)
                {
                    Dictionary<string, object?> dc = new();
                    var properties = item.GetType().GetProperties();
                    foreach (var property in properties)
                    {
                        if (IsIgnoreColumn(property)) continue;
                        if (IsNavigateColumn(property)) continue;
                        dc.Add(property.Name, property.GetValue(item));
                    }
                    result.Add(dc);
                }
            }

            return result;
        }

        public static DataTable ToDataTable<T>(this List<T> list, string tableName)
        {
            var dt = new DataTable();
            dt.TableName = tableName; //设置表名

            if (list.Any())
            {
                PropertyInfo[] properties = list[0].GetType().GetProperties();
                foreach (PropertyInfo property in properties)
                {
                    if (IsIgnoreColumn(property)) continue;
                    if (IsNavigateColumn(property)) continue;
                    Type colType = property.PropertyType;
                    if (colType.IsGenericType && colType.GetGenericTypeDefinition() == typeof(Nullable<>))
                    {
                        colType = colType.GetGenericArguments()[0];
                    }
                    dt.Columns.Add(property.Name, colType);
                }

                foreach (var item in list)
                {
                    ArrayList tempList = new();
                    //var properties = item.GetType().GetProperties();
                    foreach (var property in properties)
                    {
                        if (IsIgnoreColumn(property)) continue;
                        if (IsNavigateColumn(property)) continue;
                        var obj = property.GetValue(item, null);
                        tempList.Add(obj);
                    }
                    dt.LoadDataRow(tempList.ToArray(), true);
                }

                //for (int i = 0; i < list.Count; i++)
                //{
                //    ArrayList tempList = new();
                //    foreach (PropertyInfo pi in propertys)
                //    {
                //        if (IsIgnoreColumn(pi))
                //            continue;
                //        object obj = pi.GetValue(list[i], null);
                //        tempList.Add(obj);
                //    }
                //    object[] array = tempList.ToArray();
                //    result.LoadDataRow(array, true);
                //}
            }

            //var addRow = dt.NewRow();
            //addRow["id"] = 0;
            //addRow["price"] = 1;
            //addRow["Name"] = "a";
            //dt.Rows.Add(addRow);//添加数据

            //var x = db.Storageable(dt).WhereColumns("id").ToStorage();//id作为主键
            //x.AsInsertable.IgnoreColumns("id").ExecuteCommand();//如果是自增要添加IgnoreColumns
            //x.AsUpdateable.ExecuteCommand();
            return dt;
        }

        /// <summary>
        /// 排除SqlSugar忽略的列
        /// </summary>
        /// <param name="pi"></param>
        /// <returns></returns>
        private static bool IsIgnoreColumn(PropertyInfo property)
        {
            var sc = property.GetCustomAttributes<SugarColumn>(false).FirstOrDefault(u => u.IsIgnore);
            return sc != null;
        }

        private static bool IsNavigateColumn(PropertyInfo property) =>
            property.GetCustomAttributes<Navigate>(false).Any();
    }
}
