using DataLinkMesWork;
using DoWhatPlc;
using MesServerWork;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MisDataFunDll
{
    public partial class MisDataFun
    {
        static object locker = new object();
        /// <summary>
        ///引用程序集Hashtable
        /// </summary>
        public static Hashtable Hashtable_assembly = new Hashtable();
        public static Hashtable hashtable_MesServerCode = new Hashtable();
        public static Hashtable hashtable_MesDll = new Hashtable();
        public static Hashtable hashtable_TagTypeTagName = new Hashtable();
        public static Hashtable hashtable_TagName = new Hashtable();


        ///// <summary>
        ///// 保存质量数据和过站信息
        ///// 处理function
        ///// </summary>
        ///// <param name="text"></param>
        //public static void TransferInfToMES_Bak(object text)
        //{



        //    try
        //    {
        //        //try
        //        //{
        //        //    //保存质量数据和过站信息
        //        //    MisDataSaveDateMesRead.Funtion.MIS_Funtion(text);
        //        //}
        //        //catch
        //        //{

        //        //}
        //        string recData = text.ToString();
        //        List<string> dataArray = DoWhatPlc.MessageText.GetCmdStringList(recData);
        //        //string[] dataArray = recData.Split('|');
        //        //if (dataArray.Count != 4)
        //            //return;
        //        //消息来源
        //        string m_Source = dataArray[0].ToString();
        //        //消息类型 Barcode
        //        string m_Command = dataArray[1].ToString();
        //        //工位号
        //        string m_OpName = dataArray[2].ToString();
        //        //消息参数
        //        string m_Value = dataArray[3].ToString();

        //        //if (!m_WorkStart_OpName.ContainsKey(m_OpName)) return;  //当前系统是否存在该工位 

        //        switch (m_Source)
        //        {
        //            case "BarCode":
        //                //处理条码逻辑
        //                if (m_Command == "Bar")
        //                {
        //                    MesServerFunction(m_OpName, m_Value, "fun_Barcode");
        //                }
        //                break;
        //            //case "WEB":
        //            //    if (m_Command == "Barcode")
        //            //    {
        //            //        MesServerFunction(m_OpName, m_Value, "fun_Barcode");

        //            //    }
        //            //    else
        //            //    {
        //            //        string Content = m_Command + "|" + m_Value;
        //            //        MesServerFunction(m_OpName, Content, "fun_WebSubmit");

        //            //    }
        //            //    break;
        //            case "PLC":

        //                MesServerFunction(m_Value);
        //                break;
        //        }

        //    }
        //    catch (Exception err)
        //    {
        //        ////ApplicationLog.WriteLog(err, "TransferInf");
        //    }

        //}

        /// <summary>
        /// 保存质量数据和过站信息
        /// 处理function
        /// </summary>
        /// <param name="text"></param>
        //public static void TransferInfToMES_WebSubmit(object text)
        //{



        //    try
        //    {
        //        string recData = text.ToString();
        //        List<string> dataArray = DoWhatPlc.MessageText.GetCmdStringList(recData);

        //        //string[] dataArray = recData.Split('|');
        //        if (dataArray.Count != 4)
        //            return;
        //        //消息来源
        //        string m_Source = dataArray[0].ToString();
        //        //消息类型 Barcode
        //        string m_Command = dataArray[1].ToString();
        //        //工位号
        //        string m_OpName = dataArray[2].ToString();
        //        //消息参数
        //        string m_Value = dataArray[3].ToString();

        //        //if (!m_WorkStart_OpName.ContainsKey(m_OpName)) return;  //当前系统是否存在该工位 

        //        switch (m_Source)
        //        {
        //            //case "BarCode":
        //            //    //处理条码逻辑
        //            //    if (m_Command == "Bar")
        //            //    {
        //            //        MesServerFunction(m_OpName, m_Value, "fun_Barcode");
        //            //    }
        //            //    break;
        //            case "WEB":
        //                if (m_Command == "Barcode")
        //                {
        //                    MesServerFunction(m_OpName, m_Value, "fun_Barcode");

        //                }
        //                else
        //                {
        //                    string Content = m_Command + "|" + m_Value;
        //                    MesServerFunction(m_OpName, Content, "fun_WebSubmit");

        //                }
        //                break;
        //        }

        //    }
        //    catch (Exception err)
        //    {
        //        ////ApplicationLog.WriteLog(err, "TransferInf");
        //    }

        //}


        ///// <summary>
        ///// MesServerFunction
        ///// </summary>
        ///// <param name="OpName"></param>
        ///// <param name="tagValue"></param>
        ///// <param name="fun"></param>
        //private static void MesServerFunction(string OpName, object tagValue, string fun)
        //{
        //    try
        //    {

        //        CustomeEvetnArgs e = new CustomeEvetnArgs();

        //        string ResultOK = "";
        //        string mesServerCode;
        //        //mesServerCode = GetMesServerCodeByOpName(OpName);
        //        string mesServerdllName;
        //        //mesServerdllName = GetMesServeDllByOpName(OpName);
        //        // TagType
        //        if (!GetMesServerCodeByOpName(out mesServerdllName, out mesServerCode, OpName)) return;
        //        if (mesServerdllName == null) return;
        //        if (mesServerdllName.Length < 1) return;
        //        e.OpName = OpName;
        //        e.TagValue = tagValue;
        //        switch (fun)
        //        {
        //            case "fun_Barcode":
        //                e.TagTypeCodeID = "Barcode";
        //                break;
        //            case "fun_WebSubmit":
        //                e.TagTypeCodeID = "WebSubmit";
        //                break;
        //        }


        //        object[] Paramsters = new object[1];
        //        Paramsters[0] = e;

        //        System.Reflection.Assembly assembly;

        //        //自动工位不调用function
        //        //if (mesServerCode == "00")
        //        //{
        //        //    return;
        //        //}
        //        bool IsOK = false;

        //        {
        //            IsOK = Hashtable_assembly.ContainsKey(mesServerdllName);
        //        }
        //        if (IsOK)
        //        {
        //            assembly = (System.Reflection.Assembly)Hashtable_assembly[mesServerdllName];
        //        }
        //        else
        //        {
        //            assembly = System.Reflection.Assembly.Load(mesServerdllName);
        //            if (assembly == null)
        //            {
        //                return;
        //            }
        //            Hashtable_assembly.Add(mesServerdllName, assembly);
        //        }
        //        if(mesServerCode =="00")
        //        {
        //            fun = "FunWork";
        //            ResultOK = Reflection.ReflectionMethod(assembly, "MesServerFunctions", " FunctionMES", "FunWork", Paramsters);

        //        }
        //        else
        //        {
        //            ResultOK = Reflection.ReflectionMethod(assembly, "MesServerFunctions", "Functions_BASE_" + mesServerCode, fun, Paramsters);

        //        }
        //        if (ResultOK != "ok")
        //        {
        //            // ApplicationLog.WriteLog("ReflectionMethod is Error:" + OpName + fun + ResultOK);
        //        }
        //    }
        //    catch (Exception err)
        //    {
        //        //ApplicationLog.WriteLog(err, "MesServerFunction");
        //    }
        //}
        //static bool GetMesServerCodeByOpName(out string mesServerdllName, out string mesServerCode,string opName)
        //{
        //    try
        //    {
        //        //HashtableList hsList = DtWebApi.OpcDemo.GetHashtableList();

        //        mesServerCode = Convert.ToInt32(hashtable_MesServerCode[opName]).ToString("00");
        //        mesServerdllName = hashtable_MesDll[opName].ToString();
        //        return true;

        //    }
        //    catch
        //    {
        //        mesServerdllName = null;
        //        mesServerCode = null;
        //        opName = null;
        //        return false;
        //    }
        //}
        private static bool GetMesServerCodeByOpName(out string mesServerdllName, out string mesServerCode, out string fun, string opName, string tagType = "1")
        {
            try
            {
                //HashtableList hsList = DtWebApi.OpcDemo.GetHashtableList();
                try
                {
                    mesServerCode = Convert.ToInt32(hashtable_MesServerCode[opName]).ToString("00");

                }
                catch
                {
                    mesServerCode = "00";
                }
                mesServerdllName = hashtable_MesDll[opName].ToString();
                fun = "fun_" + hashtable_TagTypeTagName[tagType].ToString();
                return true;

            }
            catch
            {
                mesServerdllName = null;
                mesServerCode = null;
                fun = null;
                opName = null;
                return false;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="hashtable_TagName">变量名称对应的变量类型代码</param>
        /// <param name="hashtable_TagTypeTagName">变量类型代码对应的变量名称</param>
        public static void GetBaseData_tagTable_TagName(out Hashtable hashtable_TagName, out Hashtable hashtable_TagTypeTagName)
        {
            hashtable_TagName = new Hashtable();
            hashtable_TagTypeTagName = new Hashtable();

            string tagName;
            string tagTypeCode;
            DataTable dt;//GetMesServerCodeFromMIS
            dt = BaseData_tagTable_TagName();
            if (dt != null)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    tagName = dt.Rows[i]["TagName"].ToString();
                    tagTypeCode = dt.Rows[i]["变量类型代码"].ToString();
                    if (!hashtable_TagName.Contains(tagName))
                    {
                        hashtable_TagName.Add(tagName, tagTypeCode);
                    }
                    if (!hashtable_TagTypeTagName.Contains(tagTypeCode))
                    {
                        hashtable_TagTypeTagName.Add(tagTypeCode, tagName);
                    }
                }
            }
        }
        public static DataTable BaseData_tagTable_TagName()
        {
            DataTable dt = null;
            string sql;
            sql = "SELECT distinct[TagName],[变量类型代码] FROM[MES_基本变量_原始数据] ";
            DataAccess.ExecuteDataTable(sql, out dt);
            return dt;
        }
        public static void GetMesServerCodeAndMesDll(out Hashtable hashtable_MesServerCode, out Hashtable hashtable_MesDll)
        {
            hashtable_MesServerCode = new Hashtable();
            hashtable_MesDll = new Hashtable();

            string OpName;
            string MesServerCode;
            string MesDll;
            DataTable dt;//GetMesServerCodeFromMIS
            dt = GetMesServerCodeFromMIS();
            if (dt != null)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    OpName = dt.Rows[i]["工位号"].ToString();
                    MesServerCode = dt.Rows[i]["MesServerCode"].ToString();
                    MesDll = dt.Rows[i]["MesDll"].ToString();
                    if (!hashtable_MesDll.Contains(OpName))
                    {
                        hashtable_MesDll.Add(OpName, MesDll);
                    }
                    if (!hashtable_MesServerCode.Contains(OpName))
                    {
                        hashtable_MesServerCode.Add(OpName, MesServerCode);
                    }
                }
            }
        }
        public static DataTable GetMesServerCodeFromMIS()
        {
            DataTable dt;
            string sql;
            sql = "exec MES_计划BOM_工位与名称_查询应用程序";
            DataAccess.ExecuteDataTable(sql, out dt);
            return dt;
        }
        //

        /// <summary>
        /// MesServerFunction
        /// </summary>
        /// <param name="OpName"></param>
        /// <param name="tagValue"></param>
        /// <param name="fun"></param>
        private static void MesServerFunction(string msgValue)
        {
            try
            {
                bool IsOK = false;
                //lock (locker)
                {
                    DoWhatPlc.CustomeEvetnArgs e = Newtonsoft.Json.JsonConvert.DeserializeObject<DoWhatPlc.CustomeEvetnArgs>(msgValue);
                    string tagValue;
                    string mesServerCode;
                    string mesServerdllName;
                    string fun;
                    if (!GetMesServerCodeByOpName(out mesServerdllName, out mesServerCode, out fun, e.OpName.ToString(), e.TagTypeCodeID.ToString())) return;
                    if (mesServerdllName == null) return;
                    if (mesServerdllName.Length < 1) return;
                    //if (!GetMesServerCodeByOpName(out mesServerdllName, out mesServerCode, out fun, e.OpName.ToString(), e.TagType.ToString())) return;
                    switch (e.TagValue.ToString().ToLower())
                    {
                        case "false":
                            tagValue = "0";
                            break;
                        case "true":
                            tagValue = "1";
                            break;
                        default:
                            tagValue = e.TagValue.ToString();
                            break;
                    }

                    string ResultOK = "";
                    object[] Paramsters = new object[1];
                    Paramsters[0] = e;

                    System.Reflection.Assembly assembly = null;

                    //自动工位不调用function
                    //if (mesServerCode == "00")
                    //{
                    //    return;
                    //}

                    IsOK = Hashtable_assembly.ContainsKey(mesServerdllName);
                    if (IsOK)
                    {
                        assembly = (System.Reflection.Assembly)Hashtable_assembly[mesServerdllName];
                    }
                    else
                    {
                        assembly = System.Reflection.Assembly.Load(mesServerdllName);
                        if (assembly == null)
                        {
                            return;
                        }
                        Hashtable_assembly.Add(mesServerdllName, assembly);
                    }
                    if (mesServerCode == "00")
                    {
                        fun = "FunWork";
                        ResultOK = Reflection.ReflectionMethod(assembly, "MesServerFunctions", "FunctionMES", fun, Paramsters);

                    }
                    else
                    {
                        ResultOK = Reflection.ReflectionMethod(assembly, "MesServerFunctions", "Functions_BASE_" + mesServerCode, fun, Paramsters);

                    }
                    if (ResultOK != "ok")
                    {
                        // //ApplicationLog.WriteLog("ReflectionMethod is Error:" + OpName + fun + ResultOK);
                    }
                }

            }
            catch (Exception err)
            {
                ////ApplicationLog.WriteLog(err, "MesServerFunction");
            }
        }



    }
}