//using DBFactory;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace RGDWCSServices
{
    public class PlatformBase : Base
    {
        [Serializable]
        public class LANE
        {
            private string _lane_no;
            private string _free_loc_num;

            public string LANE_NO
            {
                get { return this._lane_no; }
                set { this._lane_no = value; }
            }
            public string FREE_LOC_NUM
            {
                get { return this._free_loc_num; }
                set { this._free_loc_num = value; }
            }
        }
        private static string _PlatformUrl;

        public PlatformBase()
        {
            if (string.IsNullOrEmpty(_PlatformUrl))
            {
                _PlatformUrl = Common.StringUtil.GetConfig("PlatformUrl");
            }
        }
        /// <summary>调用平台
        /// 调用平台
        /// </summary>
        /// <param name="sMethodName">方法名</param>
        /// <param name="sXmlIn">入参</param>
        /// <param name="sXmlOut">出参</param>
        /// <returns></returns>
        public bool InvokePlatForm(String sMethodName, String sXmlIn, out string sXmlOut)
        {
            bool bResult = true;

            sXmlOut = string.Empty;

            DateTime dtBegin = System.DateTime.Now;

            DateTime dtEnd;

            try
            {
                bResult = Common.WebServiceHelper.Invoke(_PlatformUrl, "", sMethodName, new object[] { sXmlIn }, out sXmlOut);
            }
            catch (Exception ex)
            {
                bResult = false;

                sXmlOut = ex.Message;
            }
            finally
            {
                dtEnd = System.DateTime.Now;
                string str = string.Format("WCS调用WMS开始时间：{3}\n\r方法：{0}\n\r入参：{1}\n\r出参：{2}\n\r", sMethodName, sXmlIn, sXmlOut, dtBegin.ToString("yyyyMMdd HH:mm:ss"));
                this._log.Info(str);
                System.Diagnostics.Debug.WriteLine(str);
            }

            return bResult;
        }

        /// <summary>反射调用
        /// 反射调用
        /// </summary>
        /// <param name="sClassFullName">类名（全）</param>
        /// <param name="sMethodName">方法</param>
        /// <param name="sXmlIn">入参</param>
        /// <param name="sXmlOut">出参</param>
        /// <returns>返回结果</returns>
        public bool Invoke(string sClassFullName, string sMethodName, string sXmlIn, out string sXmlOut)
        {
            bool bResult = true;

            sXmlOut = string.Empty;

            Type t = this.GetType();

            Assembly complierAssembly = t.Assembly;

            object complierInstance = complierAssembly.CreateInstance(sClassFullName);

            Type type = complierInstance.GetType();

            MethodInfo Method = type.GetMethod(sMethodName);

            object[] obj = new object[] { sXmlIn };

            object oResult = Method.Invoke(complierInstance, obj);

            sXmlOut = oResult.ToString();

            return bResult;
        }
        public bool getInOutCheckInfo(string sXmlIn, out string sXmlOut)
        {
            return this.InvokePlatForm(System.Reflection.MethodBase.GetCurrentMethod().Name, sXmlIn, out sXmlOut);
        }
        public bool getInStackLocation(string sXmlIn, out string sXmlOut)
        {
            return this.InvokePlatForm(System.Reflection.MethodBase.GetCurrentMethod().Name, sXmlIn, out sXmlOut);
        }
        public bool uploadStackIODet(string sXmlIn, out string sXmlOut)
        {
            return this.InvokePlatForm(System.Reflection.MethodBase.GetCurrentMethod().Name, sXmlIn, out sXmlOut);
        }
        public bool uploadWhExceptionInfo(string sXmlIn, out string sXmlOut)
        {
            return this.InvokePlatForm(System.Reflection.MethodBase.GetCurrentMethod().Name, sXmlIn, out sXmlOut);
        }
        public bool getUserBind(string sXmlIn, out string sXmlOut)
        {
            return this.InvokePlatForm(System.Reflection.MethodBase.GetCurrentMethod().Name, sXmlIn, out sXmlOut);
        }
        public bool getEquipBoxBind(string sXmlIn, out string sXmlOut)
        {
            return this.InvokePlatForm(System.Reflection.MethodBase.GetCurrentMethod().Name, sXmlIn, out sXmlOut);
        }
        public bool getEmptyBoxPos(string sXmlIn, out string sXmlOut)
        {
            return this.InvokePlatForm(System.Reflection.MethodBase.GetCurrentMethod().Name, sXmlIn, out sXmlOut);
        }
        public bool getBoxBarCode(string sXmlIn, out string sXmlOut)
        {
            return this.InvokePlatForm(System.Reflection.MethodBase.GetCurrentMethod().Name, sXmlIn, out sXmlOut);
        }
        public bool taskStart(string sXmlIn, out string sXmlOut)
        {
            return this.InvokePlatForm(System.Reflection.MethodBase.GetCurrentMethod().Name, sXmlIn, out sXmlOut);
        }
        public bool getFreeLocNum(string sXmlIn, out string sXmlOut)
        {
            return this.InvokePlatForm(System.Reflection.MethodBase.GetCurrentMethod().Name, sXmlIn, out sXmlOut);
        }
        //DbHelperSQLperator DbHelperSQL = new DbHelperSQLperator();
        public string GetOperNo()
        {
            return DateTime.Now.Ticks.ToString();

            //int OPER_NO;

            //DataView dv = DbHelperSQL.Query("select * from OPER_NO_INDEX").Tables[0].DefaultView;

            //if(dv.Count==0)
            //{
            //    DbHelperSQL.ExecuteSql("insert into OPER_NO_INDEX values(1)");
            //    OPER_NO = 1;
            //}
            //else
            //{
            //    OPER_NO = (int)dv[0]["OPER_NO"]+1;
            //    if (OPER_NO == 10000)
            //        OPER_NO = 1;
            //}

            //return DateTime.Now.ToString("yyyyMMdd") + OPER_NO.ToString().PadLeft(5, '0');
        }
    }
}
