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

namespace Common
{
    public class ReflexCls
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="objs"></param>
        /// <param name="iType">0 Id查询  1非Id查询</param>
        /// <param name="strId">iType是0的时候strId则有值</param>
        /// <returns></returns>
        public static Dictionary<string, object> method(object[] objs,out int iType,out string strId) 
        {
            iType = 1;
            strId = "";
            object value;
            Dictionary<string, object> dt = new Dictionary<string, object>();
            foreach (object obj in objs) 
            {
                Type tp = obj.GetType();
                if (tp.IsClass)
                {
                    PropertyInfo[] PropertyInfos1 = obj.GetType().GetProperties();
                    foreach (PropertyInfo p1 in PropertyInfos1)
                    {
                        value = p1.GetValue(obj, null);
                        if (value != null)
                        {
                            if (!p1.IsDefined(typeof(AttributeID), false))
                            {
                                dt.Add(p1.Name, value);
                            }
                            else //带了id的情况
                            {
                                if (objs.Length == 1) //如果只有一个条件且条件是主键则能调用根据主键查询的方法，否则调用全字段查询
                                {
                                    iType = 0;
                                    strId = value + "";
                                    dt.Add(p1.Name, value);
                                }
                                else 
                                {
                                    dt.Add(p1.Name, value);
                                }
                            }
                        }
                    }
                }
            }

            if (dt != null) 
            {
                if (dt.Where(s => s.Key == "pageNum").Count() == 0)
                {
                    dt.Add("pageNum", 1);
                }
                if (dt.Where(s => s.Key == "pageSize").Count() == 0) 
                {
                    dt.Add("pageSize", 999999);
                }
            }

            return dt;
        }
    }
}
