using System;
using System.Data;
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;
                    {
                        DoWhatPlc.CustomeEvetnArgs msgEvent = (DoWhatPlc.CustomeEvetnArgs)e;
                        string OpName = Convert.ToString(msgEvent.OpName);
                        int tagValue = Convert.ToInt32(msgEvent.TagValue);

                        if (tagValue == 1)
                        {
                            int CountNumber = Convert.ToInt32(MIS_BASE.ReadPLC(38, OpName));

                            

                            MIS_BASE.GetEngineTypeFromPLC(OpName, out EngineTypeID, out EngineType, out EngineID, out PartPallet_Code, out OrderForm);
                            //保存数据到线首表 先保存到  北汽福田变速器序列号总线上线  用于之前判断是否重号

                            if (Db.UpLoadOpNameInsert(EngineID, EngineTypeID, EngineType, OpName))
                            {
                                // 保存质量数据
                                int qualityMask = MisDataSaveDateMesRead.Funtion.MesRead(OpName, tagValue.ToString(), msgEvent.TagID.ToString());
                                // 推送质量数据到MES的页面
                                MIS_BASE.SendInfomation("ShowTimelyQuality", "1&" + qualityMask.ToString(), OpName);
                                BaseDataSystemMES.OpName_MIS_MOBY_Insert(OpName, MobyTagType.WorkStartOut, DateTime.Now.ToString());
                                // 保存物料数据
                                BaseDataSystemMES.Material_Insert(OpName, tagValue);
                                // 测量数据发动机在线状态
                                BaseDataSystemMES.WorkStartInsert(OpName, qualityMask);
                                SendMessage.SendActuallyCount(OpName, tagValue);//统计在线产量，在线节拍

                                //通知设备，MES的质量数据（和物料数据）保存完成
                                MIS_BASE.Write_MesReadOver(OpName, true);
                                // MES工作完成
                                MIS_BASE.Write_MaterialVerifyOver(OpName, true);

                                // 根据二次压装来判断计数编号是哪个
                                // 6.该订单计数加一
                                // 计数  从总分线对应表中读取出条码来进行计数
                                if(OpName == "J07PA080" && (EngineTypeID == 2 || EngineTypeID == 4))
                                {
                                    DataTable dt = BaseDataSystemMES.newSelectWorkOrder(OpName, EngineID);
                                    BaseDataSystemMES.newAddOrderCount(OpName, dt.Rows[0]["主线总成号"].ToString(), OrderForm, CountNumber);
                                }
                                else
                                {
                                    BaseDataSystemMES.newAddOrderCount(OpName, EngineID, OrderForm, CountNumber);
                                }
                                SendMessageFun.SendInfomation("FlushOverWorkList", "1", OpName);
                            }
                            else
                            {
                                SendMessage.AsyncSendMessage_Notice(OpName, "保存上线数据失败！");
                            }
                        }
                        else
                        {
                            MIS_BASE.Write_MesReadOver(OpName, false);
                        }

                    }

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

            }
           
        }
    }
}
