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 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);
                            // 只有50工位有扫码记录
                            if (OpName == "OP050")
                            {
                                string BarCode = MIS_BASE.ReadPLC(40, OpName).ToString();
                                // 保存扫码结果
                                BaseDataSystemMES.HeLi_BarCodeData_Add(OpName, EngineType, EngineID, OrderForm, BarCode);
                            }

                            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);

                            // 记录手动存储的物料数据
                            int byte1 = Convert.ToInt32(MIS_BASE.ReadPLC(211, OpName));
                            int byte2 = Convert.ToInt32(MIS_BASE.ReadPLC(202, OpName));
                            int byte3 = Convert.ToInt32(MIS_BASE.ReadPLC(203, OpName));
                            int byte4 = Convert.ToInt32(MIS_BASE.ReadPLC(204, OpName));
                            int byte5 = Convert.ToInt32(MIS_BASE.ReadPLC(205, OpName));
                            int byte6 = Convert.ToInt32(MIS_BASE.ReadPLC(206, OpName));
                            int byte7 = Convert.ToInt32(MIS_BASE.ReadPLC(207, OpName));
                            int byte8 = Convert.ToInt32(MIS_BASE.ReadPLC(208, OpName));
                            int byte9 = Convert.ToInt32(MIS_BASE.ReadPLC(209, OpName));
                            int byte10 = Convert.ToInt32(MIS_BASE.ReadPLC(210, OpName));
                            BaseDataSystemMES.HeLi_HMIMaterialFactory_Add(OpName, EngineID, OrderForm, byte1, byte2, byte3, byte4, byte5, byte6, byte7, byte8, byte9, byte10);

                            //对应手动工位的
                            //MIS_BASE.Write_ConfirmOkGo(OpName, true);
                            //通知设备，MES的质量数据（和物料数据）保存完成
                            MIS_BASE.Write_MesReadOver(OpName, true);
                            MIS_BASE.Write_MaterialVerifyOver(OpName, true);
                            //统计在线产量，在线节拍
                            SendMessage.SendActuallyCount(OpName, tagValue);
                        }
                        else
                        {
                            MIS_BASE.Write_MesReadOver(OpName, false);
                            MIS_BASE.Write_MaterialVerifyOver(OpName, false);
                        }
                    }

                }
                catch (Exception err)
                {
                    // 保存出错日志 1.申请上线 2.传递机型 3.允许保存 4.扫码
                    MisDataSaveDate.FuntionMesRead.Alarm_SaveFileLog4Error(System.Reflection.MethodBase.GetCurrentMethod().Name, e, "Fun1052", err);
                }
            }

        }

    }
}
