using System;
using System.Collections.Generic;
using System.Threading;
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)
        {
            string opName = "";
            string engineID = "";
            string orderForm = "";
            try
            {
                int EngineTypeID;
                string EngineType;
                string EngineID;
                int PartPallet_Code;
                string OrderForm;

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

                {
                    DeviceDriver_BasicData.CustomeEvetnArgs msgEvent = (DeviceDriver_BasicData.CustomeEvetnArgs)e;
                    string OpName = Convert.ToString(msgEvent.OpName);
                    opName = 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);
                        engineID = EngineID;
                        orderForm = OrderForm;

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

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

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

                        // 存追溯表数据  同时质量数据存入 接口表
                        MisDataFun.WorkOrderOpWorkOver(OpName, EngineID, OrderForm);
                 
                        // 推送质量数据到MES的页面
                        SendMessageFun.SendInfomation("ShowTimelyQuality", "1&" + qualityMask.ToString(), OpName);

                        //对应手动工位的
                        //MIS_BASE.Write_ConfirmOkGo(OpName, true);
                        //通知设备，MES的质量数据（和物料数据）保存完成
                        MIS_BASE.Write_MesReadOver(OpName, true);
                        //MIS_BASE.Write_MaterialVerifyOver(OpName, true);
                        //统计在线产量，在线节拍
                        SendMessage.SendActuallyCount(OpName, tagValue);
                        //调用工厂MES接口报工
                        WebApi.CALL_Inerface_DataHandle.CALL_Inerface_JobFinished(OpName, EngineID, OrderForm);
                    }
                    else
                    {
                        MIS_BASE.Write_MesReadOver(OpName, false);
                        MIS_BASE.Write_MaterialVerifyOver(OpName, false);
                    }

                }

            }
            catch (Exception err)
            {
                string logOpName = string.IsNullOrWhiteSpace(opName) ? "Function1051_MesRead" : opName;
                string logText = $"MesRead异常：订单号={orderForm}, 工件编号={engineID}, 异常={err.Message}";
                MisDataSaveDate.Helper.ApiLogHelper.SaveMesLog(logOpName, logText, MisDataSaveDate.Helper.MesLogType.ERROR);
                MyLog4Net.MyLogHelper.Error("Function1051_MesRead", $"{logText}{Environment.NewLine}{err}");
            }

        }
    }
}
