using bizFacade;
using MesServerWork;
using System;
//using SystemFramework;

namespace MesServerFunctions
{
    partial class FunctionMES
    {
        /// <summary>
        /// 允许保存(针对半自动和手动压装工位)
        /// </summary>
        /// <param name="OpName"></param>
        /// <param name="tagValue"></param>
        private void MesRead(object e)
        {
            {
                try
                {

                    {
                        DoWhatPlc.CustomeEvetnArgs msgEvent = (DoWhatPlc.CustomeEvetnArgs)e;
                        string OpName = Convert.ToString(msgEvent.OpName);
                        int tagValue = Convert.ToInt32(msgEvent.TagValue);

                        if (tagValue == 1)
                        {
                            int qualityMask;

                            //保存质量数据
                            qualityMask = MisDataSaveDateMesRead.Funtion.MesRead_J07MA150(OpName, tagValue.ToString(), msgEvent.TagID.ToString());
                            //保存物料数据
                            BaseDataSystemMES.Material_Insert(OpName, qualityMask);

                            //测量数据发动机在线状态
                            BaseDataSystemMES.WorkStartInsert(OpName, qualityMask);

                            //推送质量数据到MES的页面d
                            SendMessageFun.SendInfomation("ShowTimelyQuality", "1&" + qualityMask.ToString(), 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);
                            string SLResult = MIS_BASE.ReadPLC(41, OpName).ToString();  //试漏结果
                            int TurnLineWork = Convert.ToInt32(MIS_BASE.ReadPLC(64, OpName)); //是否转线
                            // 保存150工位试漏结果
                            BaseDataSystemMES.SLResult_Add( SLResult, EngineID,  EngineType,  EngineTypeID,  TurnLineWork);

                            //通知设备，MES的质量数据（和物料数据）保存完成
                            MIS_BASE.Write_MesReadOver(OpName, true);
                            MIS_BASE.Write_MaterialVerifyOver(OpName, true);

                            //统计在线产量，在线节拍
                            SendMessage.SendActuallyCount(OpName, tagValue);

                            string awaitOpName = "OPA90";
                            // 读取A90 是否在等待
                            int isAwait = Convert.ToInt32(MIS_BASE.ReadPLC(29, awaitOpName));
                            // 如果是OPA60 的话去再次判断工件匹配状态
                            if (isAwait == 1)
                            {
                                MIS_BASE.Read_TagTypeID(201, awaitOpName, out string EngineTypeID_QiaoKe);

                                string await_EngineID = MIS_BASE.ReadPLC(10, awaitOpName).ToString(); // 读取A90等待工件的工件编号
                                int await_EngineTypeID = Convert.ToInt32(MIS_BASE.ReadPLC(9, awaitOpName)); // 读取A90等待工件的机型编号
                                string await_OrderForm = MIS_BASE.ReadPLC(39, awaitOpName).ToString(); // 读取A90等待工件的订单号
                                Db.OP50_OPA90_MatchResult(await_EngineTypeID, Convert.ToInt32(EngineTypeID_QiaoKe), await_EngineID, await_OrderForm, out int MatchResult, out string MatchResultStr);
                                if (MatchResult == 1)
                                {
                                    // 轮毂线
                                    MIS_BASE.Write_Match(awaitOpName, true);// 匹配结果
                                    MIS_BASE.Write_MatchingCompleted(awaitOpName, true);// 匹配完成
                                    MIS_BASE.Write_FixAllow(awaitOpName, true); // 允许工作
                                    MIS_BASE.WritePLC(29, awaitOpName, false); // 取消等待
                                    return;
                                }
                            }

                            string awaitOpName2 = "OP051";
                            // 读取A90 是否在等待
                            int isAwait2 = Convert.ToInt32(MIS_BASE.ReadPLC(29, awaitOpName2));
                            if (isAwait2 == 1 && OpName == "OPA60")
                            {
                                string await2_EngineID = MIS_BASE.ReadPLC(10, awaitOpName2).ToString(); // 读取051等待工件的工件编号
                                string await2_OrderForm = MIS_BASE.ReadPLC(39, awaitOpName2).ToString(); // 读取051等待工件的订单号


                                Db.OP051_OP050_NeedWork(await2_EngineID, awaitOpName2, await2_OrderForm, out int MatchResult);
                              
                                if (MatchResult == 1) //匹配 有需要装配的轮毂
                                {
                                    MIS_BASE.Write_Match(awaitOpName2, true);// 匹配结果
                                    MIS_BASE.Write_MatchingCompleted(awaitOpName2, true);// 匹配完成
                                    MIS_BASE.WritePLC(77, awaitOpName2, "1");// 托盘是否需要工作 1：需要工作 2：不需要工作
                                    MIS_BASE.Write_FixAllow(awaitOpName2, true); // 允许工作
                                    MIS_BASE.WritePLC(29, awaitOpName2, false); // 取消等待
                                }
                            }
                        }
                        else
                        {
                            MIS_BASE.Write_MesReadOver(OpName, false);
                            MIS_BASE.Write_ConfirmOkGo(OpName, false);
                            MIS_BASE.Write_FixAllow(OpName, false);
                            MIS_BASE.Write_ConfirmOkGo(OpName, false);
                            //工作完成
                            MIS_BASE.Write_MaterialVerifyOver(OpName, false);
                        }
                    }

                }
                catch (Exception err)
                {
                    ////ApplicationLog.WriteLog(err, "Function08:MesReadOver");
                }
            }

        }
    }
}
