using FluentNHibernate.Cfg;
using FluentNHibernate.Mapping;
using NHibernate;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace ProjectManagementSystem.NHibernateDBHelper
{
    class WMSDBModelHelper
    {
        static private volatile ISessionFactory m_factory = null;
        static private object obj = new object();
        static private string _connectionString;
        static public ISession Session
        {
            get
            {
                if (m_factory == null)
                {
                    lock (obj)
                    {
                        try
                        {
                            if (m_factory == null) m_factory = createSessionFactory();
                        }
                        catch { throw; }
                    }
                }
                return m_factory.OpenSession();
            }
        }

        static public void initialize(string path)
        {
            _connectionString = "Data Source =" + path;
            try
            {
                if (m_factory != null) m_factory.Dispose();
                m_factory = createSessionFactory();
            }
            catch (Exception ex) { Console.Write(ex.ToString()); throw; }
        }

        public static void initialize(string strDatabaseServer,
                                      int databasePort,
                                      string strDatabaseName,
                                      string strUserName,
                                      string strPassword)
        {
            //if (m_sessionFactory != null) return;

            try
            {
                string connectionString = string.Format("Server={0};Port={1};Database={2};Uid={3};Pwd={4};",
                    strDatabaseServer, databasePort, strDatabaseName, strUserName, strPassword);
                FluentConfiguration config = Fluently.Configure()
                        .Database(FluentNHibernate.Cfg.Db.MsSqlConfiguration.MsSql2008
                        .ConnectionString(connectionString));

                Assembly curAssembly = Assembly.GetExecutingAssembly();
                NHibernate.Cfg.Configuration cfg = config.Mappings(m => m.FluentMappings.AddFromAssembly(curAssembly)).BuildConfiguration();
                m_factory = cfg.BuildSessionFactory();

                //  m_mappingSet.buildMappings(cfg);
            }
            catch { throw; }
        }

        internal static void InitWareHousesStatus()
        {
            using (var session = Session)
            {
                using (var transaction = session.BeginTransaction())
                {
                    try
                    {
                        ISQLQuery qry1 = session.CreateSQLQuery("update t_wh_locationProperty set Status='2'");
                        qry1.ExecuteUpdate();
                        //ISQLQuery qry2 = session.CreateSQLQuery("delete from OperationRecord");
                        //qry2.ExecuteUpdate();
                        //ISQLQuery qry3 = session.CreateSQLQuery("delete from DemandStation");
                        //qry3.ExecuteUpdate();
                        //ISQLQuery qry4 = session.CreateSQLQuery("delete from Station");
                        //qry4.ExecuteUpdate();
                        //ISQLQuery qry5 = session.CreateSQLQuery("delete from NavigatePath");
                        //qry5.ExecuteUpdate();
                        //ISQLQuery qry6 = session.CreateSQLQuery("delete from NavigateNode");
                        //qry6.ExecuteUpdate();
                    }
                    catch (Exception ex)
                    {
                        transaction.Rollback();
                        throw ex;
                    }
                }
            }
        }
        static public void close()
        {
            if (m_factory != null) { m_factory.Close(); m_factory = null; }
        }
        static private ISessionFactory createSessionFactory()
        {
            try
            {
                string connectionString = _connectionString;
                FluentConfiguration config = Fluently.Configure()
                        .Database(FluentNHibernate.Cfg.Db.MsSqlConfiguration.MsSql2008
                        .ConnectionString(connectionString));
                Assembly curAssembly = Assembly.GetExecutingAssembly();
                NHibernate.Cfg.Configuration cfg = config.Mappings(m => m.FluentMappings.AddFromAssembly(curAssembly))
                                                         .ExposeConfiguration(f => f.SetInterceptor(new SqlStatementInterceptor()))
                                                         .BuildConfiguration();
                return cfg.BuildSessionFactory();
            }
            catch { throw; }
        }

        public class SqlStatementInterceptor : EmptyInterceptor
        {
            public override NHibernate.SqlCommand.SqlString OnPrepareStatement(NHibernate.SqlCommand.SqlString sql)
            {
                //#if DEBUG
                try
                {
                    //File.AppendAllText("c:\\log.txt", string.Format("{0}\n", sql.ToString()));
                }
                catch
                {

                }
                return sql;
            }
        }
    }

    #region WareHouseLocation
    public class WareHouseLocation
    {
        private string _WareHouseCode;
        private string _LocationCode;
        private string _Area;
        private string _Status;
        private string _LocationName;
        //private string _WareHouseType;
        //private string _CallCount;
        private string _Material;
        private string _Time;
        private string _Column;
        private string _Row;
        //private int _Mark;


        public virtual string LocationCode
        {
            get
            {
                return _LocationCode;
            }

            set
            {
                _LocationCode = value;
            }
        }

        public virtual string Attribute1
        {
            get
            {
                return _LocationName;
            }

            set
            {
                _LocationName = value;
            }
        }

        public virtual string Attribute2
        {
            get
            {
                return _Column;
            }

            set
            {
                _Column = value;
            }
        }

        public virtual string Attribute3
        {
            get
            {
                return _Row;
            }

            set
            {
                _Row = value;
            }
        }

        public virtual string Attribute4
        {
            get
            {
                return _Material;
            }

            set
            {
                _Material = value;
            }
        }

        public virtual string Attribute5
        {
            get
            {
                return _Time;
            }

            set
            {
                _Time = value;
            }
        }
        public virtual string Area
        {
            get
            {
                return _Area;
            }

            set
            {
                _Area = value;
            }
        }


        public virtual string WareHouseCode
        {
            get
            {
                return _WareHouseCode;
            }

            set
            {
                _WareHouseCode = value;
            }
        }

        public virtual string Status
        {
            get
            {
                return _Status;
            }

            set
            {
                _Status = value;
            }
        }
        //public virtual int Mark
        //{
        //    get
        //    {
        //        return _Mark;
        //    }

        //    set
        //    {
        //        _Mark = value;
        //    }
        //}
    }

    class MappingWareHouseLocation : ClassMap<WareHouseLocation>
    {
        public MappingWareHouseLocation()
        {
            Table("t_wh_locationProperty");
            Id(m => m.LocationCode).GeneratedBy.Assigned();
            Map(m => m.WareHouseCode);//.Increment();
            Map(m => m.Status);
            Map(m => m.Area);
            Map(m => m.Attribute1);
            Map(m => m.Attribute2);
            Map(m => m.Attribute3);
            Map(m => m.Attribute4);
            Map(m => m.Attribute5);
            //Map(m => m.Mark);
        }


    }
    #endregion
}

