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

namespace MesServerFunctions
{
    partial class FunctionMES
    {
        object lockMesRead = new object();
        /// <summary>
        /// 保存完成(针对半自动和手动压装工位)
        /// </summary>
        /// <param name="OpName"></param>
        /// <param name="tagValue"></param>
        private void MesRead(object e)
        {
            try
            {
                var msgEvent = (DeviceDriver_BasicData.CustomeEvetnArgs)e;
                string OpName = Convert.ToString(msgEvent.OpName);
                int tagValue = Convert.ToInt32(msgEvent.TagValue);

                int EngineTypeID;
                string EngineType;
                string EngineID;
                int PartPallet_Code;
                string OrderForm;

                if (tagValue == 1)
                {
                    MisDataFun.LogRecording_OP(OpName, "INFO", "MIS", "PLC允许保存数据！");
                    int qualityMask;
                    MIS_BASE.GetEngineTypeFromPLC(OpName, out EngineTypeID, out EngineType, out EngineID, out PartPallet_Code, out OrderForm);

                    // PDC产线plc只写了发动机号和机型代码，其余要从数据库中读取
                    MisDataFun.Moby_Select_PDC(EngineID, out OrderForm, out EngineType, out EngineTypeID);

                    // 保存质量数据
                    qualityMask = MisDataSaveDateMesRead.Funtion.MesRead(OpName, tagValue.ToString(), msgEvent.TagID.ToString());

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

                    // 保存过站信息
                    MisDataFun.WorkStartInsert(OpName, qualityMask);

                    // 写入加工合格还是不合格 根据工位合格标志写
                    MIS_BASE.Write_OpNameIsOk(OpName, qualityMask);
                    MisDataBaseFun.WriteMesReadOver(OpName, true); // 保存完成工作完成
                }
                else
                {
                    // 写入加工合格还是不合格 根据工位合格标志写
                    MIS_BASE.Write_OpNameIsOk(OpName, -99);
                    MisDataBaseFun.WriteMesReadOver(OpName, false); // 保存完成工作完成
                    MisDataFun.LogRecording_OP(OpName, "INFO", "MIS", "PLC允许保存数据取消！");
                }


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

        }
    }
}
