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

namespace MesServerFunctions
{
    partial class FunctionMES
    {
        /// <summary>
        /// 允许保存(针对半自动和手动压装工位)
        /// </summary>
        /// <param name="OpName"></param>
        /// <param name="tagValue"></param>
        private void MesRead(object e)
        {
            {
                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.Material_Insert(OpName, qualityMask);

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

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

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

                            //A10 工位，完工后判断是母线测还是非母线测 是母线测就将数据转到40 非 转 50
                            //A40 工位，完工清除数据 视为下线
                            //A50 工位，完工判断是否为线路侧 是则转到 60 不是就下线清除
                            //A60 工位，完工视为下线 清除数据

                            bool isClear = false; // 是否下线
                            string toOpName = "";
                            if(OpName == "A50" && EngineTypeID_MES == 2)
                            {
                                toOpName = "A60";
                            }
                            else
                            {
                                isClear = true;
                            }

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

                            if(toOpName != "")
                            {
                                BaseDataSystemMES.OpName_MIS_MOBY_Insert(toOpName, MobyTagType.EngineID_MES, EngineID_MES);
                                MIS_BASE.WritePLC(34, toOpName, false);
                                // 写入机型代码 （大机型） 机型 发动机号 订单号
                                MIS_BASE.Write_EngineTypeID_MES(toOpName, EngineTypeID_MES);
                                MIS_BASE.Write_EngineType_MES(toOpName, EngineType_MES);
                                MIS_BASE.Write_EngineID_MES(toOpName, EngineID_MES);
                                MIS_BASE.Write_OrderForm_MES(toOpName, OrderForm_MES);
                            }

                            //对应手动工位的
                            //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)
                {
                    ////ApplicationLog.WriteLog(err, "Function08:MesReadOver");
                }
            }

        }

    }
}
