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

namespace MesServerFunctions
{
    partial class FunctionMES
    {
        object lockBarcodeEngineTypeGetOver = new object();


        /// <summary>
        /// PLC传递机型处理
        /// </summary>
        /// <param name="OpName"></param>
        /// <param name="tagValue"></param>
        private void EngineTypeGetOver_PLC(object e)
        {
            lock(lockBarcodeEngineTypeGetOver) 
            {

                try
                {
                    DoWhatPlc.CustomeEvetnArgs msgEvent = (DoWhatPlc.CustomeEvetnArgs)e;
                    string OpName = Convert.ToString(msgEvent.OpName);
                    int tagValue = Convert.ToInt32(msgEvent.TagValue);
                    //判断PLC机型与MES机型是否一致
                    if (tagValue == 1)
                    {
                        bool MesRead = false;

                        int EngineTypeID;//机型代码
                        int PartPallet_Code;//托盘编号

                        string EngineType;//机型
                        string EngineID;//工件编号

                        int EngineTypeID_MES;//机型代码
                        string EngineType_MES;//机型
                        string EngineID_MES;//工件编号

                        //上线工位清MES传递机型信号
                        MIS_BASE.Write_EngineTypeGetOver_MES(OpName, false);

                        // 判断是否存在异常导致不允许工作 输出msg到前端
                        DataTable OpNameIsWorkDt = BaseDataSystemMES.OpNameIsAllowWork(OpName);
                        if (Convert.ToInt32(OpNameIsWorkDt.Rows[0]["提示类型"]) == 3)
                        {
                            SendMessageFun.SendInfomation("ShowTipBox", OpNameIsWorkDt.Rows[0]["提示文本"].ToString(), OpName);
                            return;
                        }

                        MIS_BASE.Read_QualityData_Read_CF(OpName, out MesRead);
                        //第一步，判断允许保存和保存完成信号是否全部被清空，若未清空，则通知复位MES&PLC
                        if (MesRead)
                        {
                            //未能清空信号，请求复位
                            SendMessageFun.SendInfomation("AskReset", "1", OpName);
                            return;
                        }
                        string OrderForm;
                        string OrderForm_MES;
                        MIS_BASE.GetEngineTypeFromPLC(OpName, out EngineTypeID, out EngineType, out EngineID, out PartPallet_Code, out OrderForm);
                        MIS_BASE.GetEngineTypeFromPLC_MES(OpName, out EngineTypeID_MES, out EngineType_MES, out EngineID_MES, out OrderForm_MES);

                        // 测试的时候用的  plc读取订单号有问题
                        // OrderForm = BaseDataSystemMES.GetOrderDataBySn(EngineID);

                        if (EngineTypeID_MES == EngineTypeID & EngineID_MES == EngineID & EngineType_MES == EngineType & OrderForm_MES == OrderForm)
                        {
                            //
                            DataTable dt = BaseDataSystemMES.EngineTypeChange(OpName, EngineType);
                            // 提示机型发生变化
                            if (dt.Rows.Count > 0)
                            {
                                string mesValue = Newtonsoft.Json.JsonConvert.SerializeObject(dt);//<DoWhatPlc.CustomeEvetnArgs>(recData);
                                string message = "MES|MaterialList_Dif_Resources|" + OpName + "|" + mesValue;
                                SendMessage.AsyncSendMessage(message);
                            }


                            //写入到数据库的[北汽福田工位状态监控MOBY]表中
                            BaseDataSystemMES.OpName_MIS_MOBY_Insert(OpName, MobyTagType.EngineTypeID, EngineTypeID.ToString());
                            BaseDataSystemMES.OpName_MIS_MOBY_Insert(OpName, MobyTagType.EngineType, EngineType);
                            BaseDataSystemMES.OpName_MIS_MOBY_Insert(OpName, MobyTagType.EngineID, EngineID);
                            BaseDataSystemMES.OpName_MIS_MOBY_Insert(OpName, MobyTagType.OrderForm, OrderForm);
                            BaseDataSystemMES.OpName_MIS_MOBY_Insert(OpName, MobyTagType.PartPallet_Code, PartPallet_Code.ToString());

                            //电子看板_装配零件_临时表_增加_北汽
                            //第二步，将零件信息转移到零件_临时表中
                            BaseDataSystemMES.MesServer_Process_EngineTypeOverBQ(OpName);

                            //显示当前订单信息
                            //SendMessage.ShowMoby(OpName);
                            SendMessageFun.SendMessage("1235", "1111", OpName);

                            EngineTypeGetOver_PLC_Work(OpName, EngineTypeID, EngineType, EngineID, PartPallet_Code, OrderForm);
                            return;
                        }
                        else
                        {
                            SendMessage.AsyncSendMessage_Notice(OpName, "码块RFID机型与MES机型不匹配！");
                        }

                    }
                }
                catch (Exception err)
                {
                    // 保存出错日志 1.申请上线 2.传递机型 3.允许保存 4.扫码
                    MisDataSaveDate.FuntionMesRead.Alarm_SaveFileLog4Error(2, e, "Fun1059", err);
                    ////ApplicationLog.WriteLog(err, "Function08:EngineTypeGetOver_PLC");
                }
            }
           
        }
        /// <summary>
        /// 二次工作
        /// </summary>
        /// <param name="OpName"></param>
        private void EngineTypeGetOver_PLC_Work_two(string OpName)
        {
            int EngineTypeID;//机型代码
            int PartPallet_Code;//托盘编号

            string EngineType;//机型
            string EngineID;//工件编号
            string OrderForm;
            MIS_BASE.GetEngineTypeFromPLC(OpName, out EngineTypeID, out EngineType, out EngineID, out PartPallet_Code, out OrderForm);
            EngineTypeGetOver_PLC_Work(OpName, EngineTypeID, EngineType, EngineID, Convert.ToInt32(PartPallet_Code), OrderForm);

        }
        /// <summary>
        /// 跳过二次工作
        /// </summary>
        /// <param name="OpName"></param>
        private void EngineTypeGetOver_PLC_Work_Skip(string OpName)
        {
            MIS_BASE.Write_MesReadOver(OpName, true);
            MIS_BASE.Write_MaterialVerifyOver(OpName, true);
        }


    }
}
