using MesDataFunction;
using MesServerWork;
using MisDataFunDll;
using MisDataSaveDate;
using System;
using System.Threading;


namespace MesServerFunctions
{
    partial class FunctionMES
    {
        object lockBarcode = new object();

        ///第一步，判断条码类型，根据条码类型，执行对应的存储过程
        ///第二步，条码类型(将扫描的条码保存到对应的表中)
        ///第三步，扫描完成之后给出允许工作信号
        /// 处理页面扫描条码
        /// </summary>
        /// <param name="OpName"></param>
        /// <param name="barcode"></param>
        public void Barcode(object e)
        {
            lock(lockBarcode)
            {
                try
                {
                    var msgEvent = (DeviceDriver_BasicData.CustomeEvetnArgs)e;
                    string OpName = Convert.ToString(msgEvent.OpName);
                    string barcode = Convert.ToString(msgEvent.TagValue);
                   
                    int IsAllowWork;
                    MIS_BASE.Read_EngineGetOver_PLC(OpName, out bool Read_EngineGetOver_PLC_Bool); // PLC传递机型
                    MIS_BASE.GetEngineTypeFromPLC_MES(OpName, out int EngineTypeID_MES, out string EngineType_MES, out string EngineID_MES, out string OrderForm_MES);
                    MIS_BASE.GetEngineTypeFromPLC(OpName, out int EngineTypeID, out string EngineType, out string EngineID, out int PartPallet_Code, out string OrderForm);

                    if (!Read_EngineGetOver_PLC_Bool)
                    {
                        string msg1 = "无法扫码，PLC未传递机型!";
                        MisDataFun.LogRecording_OP(OpName, "ERROR", "Barcode", msg1);
                        SendMessageFun.AsyncSendMessage_Notice_Error(OpName, msg1, "条码扫描");
                        return;
                    }

                    if (!MisDataBaseFun.CheckMesApiIsOk(OpName, EngineID, "miFindCustomAndSfcData")) return; // 验证入站接口是否成功！

                    // 上线工位的机型校验
                    if (!MisDataBaseFun.EngineTypeVerifyOnLine(OpName, EngineID, EngineTypeID, EngineID_MES, EngineTypeID_MES)) return;

                    //物料验证
                    string msg;
                    //物料验证 先本地校验，成功之后在向工厂MES校验
                    MisDataFun.PartMaterial_DM_Check(OpName, barcode, EngineID_MES, out int isOk, out bool MesCheck, out bool isAllBatchCheck, out bool ShowBox, out string DMmsg, out string MaterialsCode, out string batchCode, out int ID, out bool isBatch);
                    MisDataFun.MesServer_Judge_IsCanWork(OpName, out IsAllowWork);  //获取是否允许工作

                    //此处逻辑有问题   需要考虑到批次扫描  提示PLC未传递机型的情况
                    if (IsAllowWork == 1)
                    {
                        //string msg1 = "物料验证成功，进入拧紧流程！";
                        //MisDataFun.LogRecording_OP(OpName, "INFO", "Barcode", msg1);
                        //SendMessageFun.AsyncSendMessage_Notice(OpName, msg1);
                        //SendMessageFun.SendMessage("2", "11", OpName);
                        ////拧紧逻辑
                        //InitWorkStep(OpName, EngineTypeID, EngineType, EngineID);
                        //更新订单完工数量及物料
                        SendMessageFun.SendMessage("2", "11", OpName);
                        if(EngineTypeID == 2 || EngineTypeID == 3) PrinterCommandCenter.PrintToPrinter(OpName, EngineID); // 打印PACK条码
                        MIS_BASE.Write_FixAllow(OpName, true); // 允许工作
                        MisDataFun.LogRecording_OP(OpName, "INFO", "Barcode", "物料验证完成 允许工作！");
                    }
                    else
                    {
                        SendMessageFun.AsyncSendMessage_Notice(OpName, "请扫描物料！");
                    }
                }
                catch (Exception err)
                {
                    ////ApplicationLog.WriteLog(err, "Function08:Barcode");
                }
            }
        }

    }
}
