using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace ProjectManagementSystem.NHibernateDBHelper
{
    public class CDBSettingData
    {
        public string DataSource = ".";
        public string InitialCatalog = "PmsCoreDB_SDGK";
        public string uid = "sa";
        public string pwd = "123";
    }
    public class WMSAccessSetting
    {
        private static CDBSettingData _DBSetting = new CDBSettingData() { InitialCatalog = "WMS_JLYC" };
        private static string _ConnectionString = "";

        public static string ConnectionString
        {
            get
            {
                if (_ConnectionString == "")
                {
                    InitDBSetting();

                    try
                    {
                        XmlSerializer serializer1 = new XmlSerializer(WMSAccessSetting._DBSetting.GetType());
                        string strPath = AppDomain.CurrentDomain.BaseDirectory + "PMS\\Config";
                        string strFileName = strPath + "\\CDBSettingWMS.xml";

                        DirectoryInfo dir = new DirectoryInfo(strPath);
                        if (!dir.Exists)
                        {
                            dir.Create();
                        }

                        using (TextWriter tw = new StreamWriter(strFileName))
                        {
                            serializer1.Serialize(tw, WMSAccessSetting._DBSetting);
                        }
                    }
                    catch
                    { }
                }
                return _ConnectionString;
            }
        }

        private static void InitDBSetting()
        {
            //反序列化
            XmlSerializer serializer = new XmlSerializer(WMSAccessSetting._DBSetting.GetType());
            try
            {
                string path = AppDomain.CurrentDomain.BaseDirectory + "PMS\\Config\\CDBSettingWMS.xml";
                using (TextReader tr = new StreamReader(path))
                {
                    WMSAccessSetting._DBSetting = (CDBSettingData)serializer.Deserialize(tr);
                }
            }
            catch (Exception e)
            { }

            _ConnectionString = string.Format("{0};Initial Catalog={1};Integrated Security=false;uid={2};pwd={3}"
                   , _DBSetting.DataSource
                   , _DBSetting.InitialCatalog
                   , _DBSetting.uid
                   , _DBSetting.pwd
                   );
            //_ConnectionString = string.Format("server={0};database={1};uid={2};pwd={3}"
            //    , _DBSetting.DataSource
            //    , _DBSetting.InitialCatalog
            //    , _DBSetting.uid
            //    , _DBSetting.pwd
            //    );
        }
    }

    public class PMSCoreDBAccessSetting
    {
        private static CDBSettingData _DBSetting = new CDBSettingData() { InitialCatalog = "PmsCoreDB_SDGK" };
        private static string _ConnectionString = "";

        public static string ConnectionString
        {
            get
            {
                if (_ConnectionString == "")
                {
                    InitDBSetting();

                    try
                    {
                        XmlSerializer serializer1 = new XmlSerializer(PMSCoreDBAccessSetting._DBSetting.GetType());
                        string strPath = AppDomain.CurrentDomain.BaseDirectory + "PMS\\Config";
                        string strFileName = strPath + "\\CDBSetting.xml";

                        DirectoryInfo dir = new DirectoryInfo(strPath);
                        if (!dir.Exists)
                        {
                            dir.Create();
                        }

                        using (TextWriter tw = new StreamWriter(strFileName))
                        {
                            serializer1.Serialize(tw, PMSCoreDBAccessSetting._DBSetting);
                        }
                    }
                    catch
                    { }
                }
                return _ConnectionString;
            }
        }

        private static void InitDBSetting()
        {
            //反序列化
            XmlSerializer serializer = new XmlSerializer(PMSCoreDBAccessSetting._DBSetting.GetType());
            try
            {
                string path = AppDomain.CurrentDomain.BaseDirectory + "PMS\\Config\\CDBSetting.xml";
                using (TextReader tr = new StreamReader(path))
                {
                    PMSCoreDBAccessSetting._DBSetting = (CDBSettingData)serializer.Deserialize(tr);
                }
            }
            catch (Exception e)
            { }

            _ConnectionString = string.Format("{0};Initial Catalog={1};Integrated Security=false;uid={2};pwd={3}"
                   , _DBSetting.DataSource
                   , _DBSetting.InitialCatalog
                   , _DBSetting.uid
                   , _DBSetting.pwd
                   );
            //_ConnectionString = string.Format("server={0};database={1};uid={2};pwd={3}"
            //    , _DBSetting.DataSource
            //    , _DBSetting.InitialCatalog
            //    , _DBSetting.uid
            //    , _DBSetting.pwd
            //    );
        }
    }

    public class MiddleDBAccessSetting
    {
        private static CDBSettingData _DBSetting = new CDBSettingData() { InitialCatalog = "SDGK_MiddleTransForm" };
        private static string _ConnectionString = "";

        public static string ConnectionString
        {
            get
            {
                if (_ConnectionString == "")
                {
                    InitDBSetting();

                    try
                    {
                        XmlSerializer serializer1 = new XmlSerializer(MiddleDBAccessSetting._DBSetting.GetType());
                        string strPath = AppDomain.CurrentDomain.BaseDirectory + "PMS\\Config";
                        string strFileName = strPath + "\\CDBSettingMiddle.xml";

                        DirectoryInfo dir = new DirectoryInfo(strPath);
                        if (!dir.Exists)
                        {
                            dir.Create();
                        }

                        using (TextWriter tw = new StreamWriter(strFileName))
                        {
                            serializer1.Serialize(tw, MiddleDBAccessSetting._DBSetting);
                        }
                    }
                    catch
                    { }
                }
                return _ConnectionString;
            }
        }

        private static void InitDBSetting()
        {
            //反序列化
            XmlSerializer serializer = new XmlSerializer(MiddleDBAccessSetting._DBSetting.GetType());
            try
            {
                string path = AppDomain.CurrentDomain.BaseDirectory + "PMS\\Config\\CDBSettingMiddle.xml";
                using (TextReader tr = new StreamReader(path))
                {
                    MiddleDBAccessSetting._DBSetting = (CDBSettingData)serializer.Deserialize(tr);
                }
            }
            catch (Exception e)
            { }

            _ConnectionString = string.Format("{0};Initial Catalog={1};Integrated Security=false;uid={2};pwd={3}"
                   , _DBSetting.DataSource
                   , _DBSetting.InitialCatalog
                   , _DBSetting.uid
                   , _DBSetting.pwd
                   );
            //_ConnectionString = string.Format("server={0};database={1};uid={2};pwd={3}"
            //    , _DBSetting.DataSource
            //    , _DBSetting.InitialCatalog
            //    , _DBSetting.uid
            //    , _DBSetting.pwd
            //    );
        }
    }
}

