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(OpName, tagValue.ToString(), msgEvent.TagID.ToString());
                            //保存物料数据
                            BaseDataSystemMES.Material_Insert(OpName, qualityMask);

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

                            //推送质量数据到MES的页面
                            SendMessageFun.SendInfomation("ShowTimelyQuality", "1&" + qualityMask.ToString(), OpName);


                            //对应手动工位的
                            //MIS_BASE.Write_ConfirmOkGo(OpName, true);


                            //MIS_BASE.Write_FixAllow(OpName, false);

                            //MIS_BASE.Write_ConfirmOkGo(OpName, false);


                            //MIS_BASE.Write_FixOver(OpName, false);
                            //MIS_BASE.Write_MaterialVerifyOver(OpName, false);


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

                            // 判断050 工位是否有传递机型 有的话 允许050工作 同时删除记录表中轮毂号
                            MIS_BASE.Read_EngineGetOver_PLC("OP050", out bool EngineGetOver_PLC_NextWorkStep);
                            if (EngineGetOver_PLC_NextWorkStep)
                            {
                                MIS_BASE.Write_FixAllow("OP050", true);
                                // 查询050 工位当前到位的工件编号
                                MIS_BASE.Read_EngineID("OP050", out string OP050_EngineID);
                                // 绑定关系 同时 删除记录
                                Db.OPA92_DeleteLineData(OP050_EngineID);
                            }

                            //通知设备，MES的质量数据（和物料数据）保存完成
                            MIS_BASE.Write_MesReadOver(OpName, true);
                        }
                        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");
                }
            }

        }
    }
}
