using System;
using System.Data;
using bizFacade;
using MesServerWork;
//using SystemFramework;


namespace MesServerFunctions
{
    partial class Functions_BASE_WebSubmit
    {
        /// <summary>
        /// 分线上总线的数量
        /// </summary>
        public static int SubLineLoadPartCount = 2;
        /// <summary>
        /// 分线上总线已经匹配的数量
        /// </summary>
        public static int SubLineLoadPartCountMatch = 0;
        /// <summary>
        /// 正常逻辑工作
        /// </summary>
        /// <param name="OpName"></param>
        /// <param name="EngineTypeID"></param>
        /// <param name="EngineType"></param>
        /// <param name="EngineID"></param>
        /// <param name="PartPallet_Code"></param>
        private static void EngineTypeGetOver_PLC_Work(string OpName, int EngineTypeID, string EngineType, string EngineID, int PartPallet_Code,string OrderForm)
        {
            int IsAllowWork;
            int MES_QualityMask = 1;
            int repairPart;//读取返修标志
            int IsrepairBarcode = 0;//拆解返修标志

            //string OrderForm = "";//订单号
            string repairBarcode = "";//拆解返修条码

            DataTable dt;//读取订单信息

            MIS_BASE.Read_OnlineRepair(OpName, out repairPart);
            BaseDataSystemMES.GetEngineID_Select(OpName, EngineID, out dt);

            if (dt != null && dt.Rows.Count > 0)
            {
                //OrderForm = Convert.ToString(dt.Rows[0]["订单号"]);
                IsrepairBarcode = Convert.ToInt32(dt.Rows[0]["拆解标志"]);
                repairBarcode = Convert.ToString(dt.Rows[0]["拆解返修条码"]);
            }
            //记录MOBY信息（只限总装工位）
            BaseDataSystemMES.OpName_MIS_MOBY_Insert(OpName, OrderForm,EngineTypeID, EngineType, EngineID, PartPallet_Code, repairPart, IsrepairBarcode, repairBarcode);
            SaveMobyMessage(OpName, EngineTypeID, EngineType, EngineID, OrderForm);
            BaseDataSystemMES.MaterialPush_EngineIDStatus_Insert(OpName);
            BaseDataSystemMES.MesServer_Process_EngineTypeOver(OpName);
            Sendmessage("5123", "1111", OpName);

            MIS_BASE.Read_PalletInfo(OpName, out MES_QualityMask);
            SendInfomation("QualityMask", Convert.ToString(MES_QualityMask), OpName);
            if (MES_QualityMask != 1)
            {
                //工作完成
                MIS_BASE.Write_MaterialVerifyOver(OpName, true);
                SendInfomation("NgGO", "1", OpName);//显示不合格标志
                return;
            }

            BaseDataSystemMES.MesServer_Judge_IsCanWork(OpName, out IsAllowWork);
            if (IsAllowWork == 1)
            {
                Match(OpName, EngineID, EngineTypeID, EngineType, PartPallet_Code);//匹配
            }
        }


        /// <summary>
        /// 保存MOBY信息
        /// </summary>
        /// <param name="OpName"></param>
        /// <param name="engineTypeID"></param>
        /// <param name="engineType"></param>
        /// <param name="engineID"></param>
        private static void SaveMobyMessage(string OpName, int engineTypeID, string engineType, string engineID, string orderForm)
        {

            if (MIS_BASE.hashtable_EngineGetOver_EngineTypeID == null)
            {
                MIS_BASE.InitHashtable_MesServerCode();
            }
            if (MIS_BASE.hashtable_EngineGetOver_EngineTypeID.ContainsKey(OpName))
            {
                MIS_BASE.hashtable_EngineGetOver_EngineTypeID[OpName] = engineTypeID;
            }
            if (MIS_BASE.hashtable_EngineGetOver_EngineType.ContainsKey(OpName))
            {
                MIS_BASE.hashtable_EngineGetOver_EngineType[OpName] = engineType;
            }
            if (MIS_BASE.hashtable_EngineGetOver_EngineID.ContainsKey(OpName))
            {
                MIS_BASE.hashtable_EngineGetOver_EngineID[OpName] = engineID;
            }
            if (MIS_BASE.hashtable_EngineGetOver_OrderForm.ContainsKey(OpName))
            {
                MIS_BASE.hashtable_EngineGetOver_OrderForm[OpName] = orderForm;
            }
        }

        /// <summary>
        /// 返修件需要工作
        /// </summary>
        /// <param name="OpName">工位号</param>
        private static void IsNeedRepaire_OK(string OpName)
        {

            int isRepair;
            int engineTypeID;

            string engineType = "";
            string engineID = "";
            string palletCode = "";
            string orderForm = "";
            string repairCode = "";
            //从监控系统中查询出本工位信息
            BaseDataSystemMES.OpName_MIS_Moby_Select(OpName,
            out  orderForm, out  engineTypeID, out  engineType,
            out  engineID, out palletCode, out isRepair, out repairCode);

            EngineTypeGetOver_PLC_Work(OpName, engineTypeID, engineType, engineID, Convert.ToInt32(palletCode),orderForm);
        }
        /// <summary>
        /// 正常匹配分线和总线
        /// </summary>
        /// <param name="opName"></param>
        /// <param name="engineID">总线工件号</param>
        /// <param name="engineTypeID">总线工件机型代码</param>
        /// <param name="engineType">总线工件机型</param>
        private static void Match(string opName, string engineID, int engineTypeID, string engineType, int PartPallet_Code)
        {
            try
            {
                bool m_EngineGetOver_PLC_SubLine;
                int engineTypeID_SubLine;
                int partPallet_Code_SubLine;
                int PalletInfo = 1;//总线托盘状态

                string opName_SubLine = "";//分线工件信息
                string engineID_SubLine;
                string engineType_SubLine;
                string AlarmStr;
                if (MIS_BASE.hashtable_OpNameRepair == null)
                {
                    MIS_BASE.InitHashtable_MesServerCode();
                }
                //第一步：获取分线中间交互工位号
                if (MIS_BASE.hashtable_OpNameRepair.ContainsKey(opName))
                {
                    opName_SubLine = MIS_BASE.hashtable_OpNameRepair[opName].ToString();
                }
                if (opName_SubLine == "")
                {
                    return;
                }

                //第二步：获取分线传递机型信号
                MIS_BASE.Read_EngineGetOver_PLC(opName_SubLine, out m_EngineGetOver_PLC_SubLine);
                if (!m_EngineGetOver_PLC_SubLine) return;//必须工件同时到位才能进行匹配

                //第三步：判断合格标志
                MIS_BASE.Read_PalletInfo(opName_SubLine, out  PalletInfo);
                if (PalletInfo == 2) return; //若不合格则返回不进行匹配
                string OrderForm;
                //第四步：获取分线的信息，匹配验证
                MIS_BASE.GetEngineTypeFromPLC(opName_SubLine, out engineTypeID_SubLine, out engineType_SubLine, out engineID_SubLine, out partPallet_Code_SubLine,out OrderForm);
                //判断分线数据是否合格
                //if (!BaseDataSystemMES.QulityDataVerify(opName, engineID_SubLine, out AlarmStr) && !BaseDataSystemMES.QualityVerifyStatus_Hash_Read(opName))
                //{
                //    SendInfomation("LineoutAlert", AlarmStr, opName);
                //    return;
                //}

                if (engineTypeID_SubLine == engineTypeID)
                {
                    BaseDataSystemMES.SubLineLoadPartCountMatch_Select(opName, out SubLineLoadPartCountMatch);
                    SubLineLoadPartCountMatch = SubLineLoadPartCountMatch + 1;
                    BaseDataSystemMES.SubLineLoadPartCountMatch_Update(opName, SubLineLoadPartCountMatch);


                    BaseDataSystemMES.MailLineAndSubLineData_Insert(engineID, engineTypeID, engineType, engineID_SubLine);//分线上总线数据转移
                    MIS_BASE.Write_Match(opName, true);//匹配结果
                    MIS_BASE.Write_MatchingCompleted(opName, true);//匹配完成
                    if(SubLineLoadPartCountMatch== SubLineLoadPartCount)
                    {
                        MIS_BASE.Write_FixAllow(opName, true);
                        SubLineLoadPartCountMatch = 0;
                        BaseDataSystemMES.SubLineLoadPartCountMatch_Update(opName, SubLineLoadPartCountMatch);

                    }

                    SendInfomation("MatchSuccess", "1", opName);
                    sendmessage(opName, 1, "Match");//匹配结果
                }
                else
                {
                    MIS_BASE.Write_Match(opName, false);//匹配结果
                    MIS_BASE.Write_MatchingCompleted(opName, true);//匹配完成//68:1、取消返修；2、匹配成功  自己监控发送 MES|MatchingCompleted

                    //发送匹配完成和匹配结果信息
                    AsyncSendMessage_Notice(opName,"匹配失败，工件型号不匹配！");
                    SendInfomation("MatchSuccess", "0", opName);
                    sendmessage(opName, 0, "Match");//匹配结果
                }

            }
            catch (Exception err)
            {
                ////ApplicationLog.WriteLog("Match:" + err.ToString());

            }
        }



        /// <summary>
        /// 强制匹配分线和总线
        /// </summary>
        /// <param name="opName"></param>
        private static void ForceMatch(string opName)
        {
            try
            {
                bool m_EngineGetOver_PLC_SubLine;
                bool m_EngineGetOver_PLC;

                int engineTypeID_SubLine;
                int engineTypeID;
                int partPallet_Code_SubLine;
                int partPallet_Code;

                string opName_SubLine = "";//分线工件信息
                string engineID_SubLine;
                string engineType_SubLine;
                string engineID;
                string engineType;
                if (MIS_BASE.hashtable_OpNameRepair == null)
                {
                    MIS_BASE.InitHashtable_MesServerCode();
                }
                //获取分线中间交互工位号
                if (MIS_BASE.hashtable_OpNameRepair.ContainsKey(opName))
                {
                    opName_SubLine = MIS_BASE.hashtable_OpNameRepair[opName].ToString();
                }
                if (opName_SubLine == "")
                {
                    return;
                }

                MIS_BASE.Read_EngineGetOver_PLC(opName_SubLine, out m_EngineGetOver_PLC_SubLine);//1:读取分线工件到位的传递机型信号。

                if (!m_EngineGetOver_PLC_SubLine) return;//必须工件同时到位才能进行匹配

                MIS_BASE.Read_EngineGetOver_PLC(opName, out m_EngineGetOver_PLC);//2:读取总线工件到位的传递机型信号。

                if (!m_EngineGetOver_PLC) return;//必须工件同时到位才能进行匹配
                string OrderForm;
                MIS_BASE.GetEngineTypeFromPLC(opName_SubLine, out engineTypeID_SubLine, out engineType_SubLine, out engineID_SubLine, out partPallet_Code_SubLine,out  OrderForm);//获取分线的信息
                MIS_BASE.GetEngineTypeFromPLC(opName, out engineTypeID, out engineType, out engineID, out partPallet_Code, out OrderForm);

                BaseDataSystemMES.MailLineAndSubLineData_Insert(engineID, engineTypeID, engineType, engineID_SubLine);//分线上总线数据转移

                MIS_BASE.Write_Match(opName, true);//匹配结果
                MIS_BASE.Write_MatchingCompleted(opName, true);//匹配完成

                SendInfomation("MatchSuccess", "1", opName);//界面显示匹配成功

                MIS_BASE.Write_FixAllow(opName, true);//允许PLC工作

                //发送匹配完成和匹配结果信息
                sendmessage(opName, 1, "MatchingComplete");//匹配完成
                sendmessage(opName, 1, "Match");//匹配结果
            }
            catch
            {

            }
        }
    }
}
