using System;
using System.Collections.Generic;
using System.Threading;
using bizFacade;
//using SystemFramework;
using MesServerWork;

namespace MesServerFunctions
{
    partial class FunctionMES
    {
        object lockMesRead = new object ();
        /// <summary>
        /// 保存完成(针对半自动和手动压装工位)
        /// </summary>
        /// <param name="OpName"></param>
        /// <param name="tagValue"></param>
        private void MesRead(object e)
        {
            lock(lockMesRead)
            {
                try
                {
                    int EngineTypeID;
                    string EngineType;
                    string EngineID;
                    int PartPallet_Code;
                    string OrderForm;

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

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

                        if (tagValue == 1)
                        {
                            int qualityMask;
                            //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);
                            //保存数据到线首表 先保存到  北汽福田变速器序列号总线上线  用于之前判断是否重号
                            // 保存质量数据
                            qualityMask = MisDataSaveDateMesRead.Funtion.MesRead(OpName, tagValue.ToString(), msgEvent.TagID.ToString());

                            // 存追溯表数据
                            BaseDataSystemMES.WorkOrderOpWorkOver(OpName, EngineID_MES, OrderForm_MES);

                            // 保存物料数据
                            BaseDataSystemMES.Material_Insert(OpName, qualityMask);

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

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

                            MIS_BASE.GetEngineTypeFromPLC_MES(OpName, out EngineTypeID_MES, out EngineType_MES, out EngineID_MES, out OrderForm_MES);

                            // 更新 当前工件数据 更新为已下线
                            BaseDataSystemMES.WorkOrderOpWorkOutLine(EngineID_MES, OrderForm_MES);

                            //通知设备，MES的质量数据（和物料数据）保存完成
                            MIS_BASE.Write_MesReadOver(OpName, true);
                            MIS_BASE.Write_MaterialVerifyOver(OpName, true);
                            //统计在线产量，在线节拍
                            SendMessage.SendActuallyCount(OpName, tagValue);
                            Thread.Sleep(1000);
                            MIS_BASE.Write_MesRead_PLC(OpName, false);
                        }
                        else
                        {
                            // 清楚保存完成 清除PLC的所有信号
                            MIS_BASE.Write_MesReadOver(OpName, false);
                            MIS_BASE.Write_MaterialVerifyOver(OpName, false);
                            MIS_BASE.Write_WorkStart(OpName, false);
                            MIS_BASE.Write_EngineTypeGetOver_PLC(OpName, false);
                        }

                    }

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

            }
           
        }
    }
}
