using bizFacade;
using Function1052.SaveCode;
using MesServerWork;
using MesWork;
using System;
using System.Data;
//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);
                        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);
                        if (tagValue == 1)
                        {
                            int qualityMask;

                            //先去判断是否需要弹出弹窗，若弹出，点确认后给PLC发一个点表示点过了
                            DataTable WordOver = BaseDataSystemMES.WorkOver_IsShowWindow(OpName, EngineTypeID);
                            if (Convert.ToInt32(WordOver.Rows[0]["result"]) == 1)
                            {
                                string message = "MES|WorkOver_Is_ShowWindow|" + OpName + "|" + WordOver.Rows[0]["msg"];
                                SendMessage.AsyncSendMessage(message);
                                //MIS_BASE.WritePLC(35, OpName, 1);
                            }

                            // MA080 需要保存铭牌二维码信息
                            if (OpName == "J07MA080")
                            {
                                SaveCode_J07MA080.SaveCode(OpName);
                            }

                            // 保存质量数据
                            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);

                            // ET030 需要保存试验台信息
                            if (OpName == "J07ET030")
                            {
                                BaseDataSystemMES.TestStandDataSave();
                                // 判断如果合格标志是不合格那就添加到返工记录里去                            }
                                if(qualityMask == 2)
                                {
                                    BaseDataSystemMES.TestStandBackWorkData(EngineID, EngineType, OrderForm);
                                }
                            }

                            //对应手动工位的   
                            //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);
                        }
                    }

                }
                catch (Exception err)
                {
                    // 保存出错日志 1.申请上线 2.传递机型 3.允许保存 4.扫码
                    MisDataSaveDate.FuntionMesRead.Alarm_SaveFileLog4Error(3, e, "Fun1052", err);
                    ////ApplicationLog.WriteLog(err, "Function08:MesReadOver");
                }
            }

        }

    }
}
