using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Collections;
using System.Configuration;
using System.Xml;

namespace SSLMS.Common
{
    public class StringUtil
    {
        /// <summary>
        /// 获得日期
        /// </summary>
        /// <returns></returns>
        public static string GetCurDateTimeString()
        {
            return System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
        }

        /// <summary>
        /// 获得GUID
        /// </summary>
        /// <returns></returns>
        public static string GetGUID()
        {
            return System.Guid.NewGuid().ToString();
        }

        /// <summary>
        /// 获得计算机名
        /// </summary>
        /// <returns></returns>
        public static string GetHostName()
        {
            return System.Net.Dns.GetHostName().ToString();
        }

        /// <summary>
        /// 获得分离字符串中的某组字符
        /// </summary>
        /// <param name="ControlName"></param>
        /// <param name="ch"></param>
        /// <param name="index"></param>
        /// <returns></returns>
        public static string GetSplitStr(string ControlName, char[] ch, int index)
        {
            string[] arrTmp = ControlName.Split(ch);

            return arrTmp[(1 == arrTmp.Length) ? 0 : index];
        }

        public static string strFormat(string str, string[] strPara)
        {
            return string.Format(str, strPara);
        }

        public static DataTable AddAll(DataTable dt, Hashtable ht)
        {
            if (0 == ht.Count)
            {
                ht.Add("name", "-");

                ht.Add("value", "");
            }

            DataTable dtResult = dt;

            if (null == dtResult)
            {
                dtResult = new DataTable();

                foreach (DictionaryEntry d in ht)
                {
                    dtResult.Columns.Add(d.Key.ToString());
                }
            }

            if (0 == dtResult.Columns.Count)
            {
                dtResult = new DataTable();

                foreach (DictionaryEntry d in ht)
                {
                    dtResult.Columns.Add(d.Key.ToString());
                }
            }

            DataRow dr = dtResult.NewRow();

            foreach (DictionaryEntry d in ht)
            {
                dr[d.Key.ToString()] = d.Value;
            }

            dtResult.Rows.InsertAt(dr, 0);

            return dtResult;
        }

        public static string ConfigGet(string str)
        {
            string sResult = str;

            sResult = new AppSettingsReader().GetValue(str, typeof(string)).ToString().Trim();

            return sResult;
        }

        /// <summary>
        /// 设置配置文件
        /// </summary>
        /// <param name="connString"></param>
        public static bool ConfigSet(string sFileName, string sKey, string sValue, out string sResult)
        {
            bool bResult = true;

            sResult = string.Empty;

            try
            {
                string sPath = string.Empty;

                Assembly Asm = Assembly.GetExecutingAssembly();

                XmlDocument xmlDoc = new XmlDocument();

                sPath = Asm.Location.Substring(0, (Asm.Location.LastIndexOf("\\") + 1)) + sFileName;

                xmlDoc.Load(sPath);

                XmlNodeList nodeList = xmlDoc.SelectSingleNode("/configuration/appSettings").ChildNodes;

                foreach (XmlNode xn in nodeList)//遍历所有子节点
                {
                    try
                    {
                        XmlElement xe = (XmlElement)xn;

                        if (xe.GetAttribute("key").IndexOf(sKey) != -1)
                        {
                            xe.SetAttribute("value", sValue);
                        }
                    }
                    catch
                    {
                    }
                }

                xmlDoc.Save(sPath);
            }
            catch (Exception ex)
            {
                bResult = false;

                sResult = ex.Message;
            }

            return bResult;
        }

        /// <summary>
        /// IList转DataTable
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public static DataTable ListToDataTable(IList list)
        {
            DataTable dtResult = new DataTable();

            if (list.Count > 0)
            {
                PropertyInfo[] propertys = list[0].GetType().GetProperties();

                foreach (PropertyInfo pi in propertys)
                {
                    dtResult.Columns.Add(pi.Name, pi.PropertyType);
                }

                for (int i = 0; i < list.Count; i++)
                {
                    ArrayList tempList = new ArrayList();

                    foreach (PropertyInfo pi in propertys)
                    {
                        object obj = pi.GetValue(list[i], null);

                        tempList.Add(obj);
                    }

                    object[] array = tempList.ToArray();

                    dtResult.LoadDataRow(array, true);
                }
            }

            return dtResult;
        }

        /// <summary>
        ///DataTable转IList
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static IList<T> DataTableToList<T>(DataTable dt, Hashtable ht)
        {
            IList<T> lsResult = new List<T>();

            DataTable dtTmp = dt.Clone();

            PropertyInfo[] propertys = typeof(T).GetProperties();

            foreach (PropertyInfo pi in propertys)
            {
                try
                {
                    dtTmp.Columns[pi.Name].DataType = pi.PropertyType;
                }
                catch
                { 
                }
            }

            foreach (DataRow dr in dt.Rows)
            {
                dtTmp.Rows.Add(dr.ItemArray);

                T t = Activator.CreateInstance<T>();

                string sKey = string.Empty;

                foreach (PropertyInfo pi in propertys)
                {
                    sKey = pi.Name;

                    try
                    {
                        if (ht.ContainsKey(sKey))
                        {
                            pi.SetValue(t, dtTmp.Rows[dtTmp.Rows.Count - 1][ht[sKey].ToString()], null);
                        }
                        else
                        {
                            pi.SetValue(t, dtTmp.Rows[dtTmp.Rows.Count - 1][sKey], null);
                        }
                    }
                    catch
                    {
                    }
                }



                lsResult.Add(t);
            }

            return lsResult;
        }

        /// <summary>
        ///IList转IList
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static IList<T> ListToList<T>(IList<T> lsIn, Hashtable ht)
        {
            IList<T> lsResult = new List<T>();

            //DataTable dtTmp = dt.Clone();

            //PropertyInfo[] propertys = typeof(T).GetProperties();

            //foreach (PropertyInfo pi in propertys)
            //{
            //    try
            //    {
            //        dtTmp.Columns[pi.Name].DataType = pi.PropertyType;
            //    }
            //    catch
            //    {
            //    }
            //}

            //foreach (DataRow dr in dt.Rows)
            //{
            //    dtTmp.Rows.Add(dr.ItemArray);

            //    T t = Activator.CreateInstance<T>();

            //    string sKey = string.Empty;

            //    foreach (PropertyInfo pi in propertys)
            //    {
            //        sKey = pi.Name;

            //        try
            //        {
            //            if (ht.ContainsKey(sKey))
            //            {
            //                pi.SetValue(t, dtTmp.Rows[dtTmp.Rows.Count - 1][ht[sKey].ToString()], null);
            //            }
            //            else
            //            {
            //                pi.SetValue(t, dtTmp.Rows[dtTmp.Rows.Count - 1][sKey], null);
            //            }
            //        }
            //        catch
            //        {
            //        }
            //    }



            //    lsResult.Add(t);
            //}

            return lsResult;
        }
    }
}