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 = Convert.ToInt32(MIS_BASE.Read_TagValueByTagTypeID(21, OpName));
                    // MIS_BASE.GetEngineTypeFromPLC(OpName, out EngineTypeID, out EngineType, out EngineID, out PartPallet_Code, out OrderForm);

                    string part1;
                    string part2;
                    MIS_BASE.Read_TagValueByTagTypeID(203, OpName, out part1);
                    MIS_BASE.Read_TagValueByTagTypeID(204, OpName, out part2);
                    // PDC产线plc只写了发动机号和机型代码，其余要从数据库中读取
                    MisDataFun.Moby_Select_PDC(part1, out OrderForm, out EngineType, out EngineTypeID);
                    // 保存质量数据
                    //qualityMask = MisDataSaveDateMesRead.Funtion.MesRead(OpName, tagValue.ToString(), msgEvent.TagID.ToString());
                    // 1.读取所有的电芯码，总合格标志
                    var cell_code = new string[2];
                    cell_code[0] = part1;
                    cell_code[1] = part2;
                    // 2.保存质量数据
                    MisDataSaveDateMesRead.Funtion.MesRead_Cell(cell_code, OpName, tagValue.ToString(), msgEvent.TagID.ToString(), qualityMask);

                    // 判断是否存在首件标志
                    if (MIS_BASE.Read_TagValueByTagTypeID(62, OpName) == "1") MisDataFun.Save_FristWorkData(OpName, cell_code[0]); // 第一个产品 存首件数据

                    if (part1 != null && part1 != "" && part1.Length > 5)
                    {
                        // 保存过站信息
                        MisDataFun.WorkStartInsert_JZ(OpName, part1, Convert.ToInt32(qualityMask));
                    }
                    if (part2 != null && part2 != "" && part2.Length > 5)
                    {
                        // 保存过站信息
                        MisDataFun.WorkStartInsert_JZ(OpName, part2, Convert.ToInt32(qualityMask));
                    }
                    // 写入加工合格还是不合格 根据工位合格标志写
                    MIS_BASE.Write_OpNameIsOk(OpName, qualityMask);

                    //通知PLC，MES工作完成
                    MIS_BASE.Write_MesWorkFinish(OpName, true);
                    //通知PLC，MES保存完成
                    MIS_BASE.Write_MesReadOver(OpName, true);
                    MisDataFun.LogRecording_OP(OpName, "INFO", "MIS", "MES保存数据完成！");
                    MisDataFun.LogRecording_OP(OpName, "INFO", "MIS", "MES工作完成！");

                }
                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");
            }

        }
    }
}
