using System;
using System.Linq;
using MesDataFunction;


//using SystemFramework;
using MesServerWork;
using MisDataFunDll;
using MisDataSaveDate;

namespace MesServerFunctions
{
    partial class FunctionMES
    {
        /// <summary>
        ///1.扫描配送单号，用于接收物料，扫描单号的时候，将接收的物料信息显示在“物料拉动”Tab区域
        ///2.扫描料箱标签/看板卡，用于追溯物料批次信息
        ///3.扫描零件条码，根据零件图号进行防错
        ///第一步，判断条码类型，根据条码类型，执行对应的存储过程
        ///第二步，条码类型(将扫描的条码保存到对应的表中)
        ///第三步，扫描完成之后给出允许工作信号
        /// 处理页面扫描条码
        /// </summary>
        /// <param name="OpName"></param>
        /// <param name="barcode"></param>
        public void Barcode(object e)
        {

            {
                try
                {
                    var msgEvent = (DeviceDriver_BasicData.CustomeEvetnArgs)e;
                    string OpName = Convert.ToString(msgEvent.OpName);
                    string barcode = Convert.ToString(msgEvent.TagValue);
                    string AlarmStr = "";

                    int IsAllowWork;
                    string msg = "";

                    int EngineTypeID;//机型代码
                    string EngineType;//机型
                    string EngineID;//工件编号
                    string OrderForm; // 工单号
                    int PartPallet_Code;//托盘编号

                    // 如果有申请上线扫码就是为了获取产品信息
                    MIS_BASE.Read_PartLoad(OpName, out bool isPartLoad);
                    MIS_BASE.Read_EngineTypeGetOver_MES(OpName, out bool isEngineTypeGetOverMes);
                    if (isPartLoad && !isEngineTypeGetOverMes)
                    {
                        if (MisDataBaseFun.WriteEngineDataByEngineID(OpName, barcode)) return; // 获取产品信息成功
                    }

                    //传递机型是否到位
                    MIS_BASE.Read_EngineGetOver_PLC(OpName, out bool EngineGetOver_PLC_NextWorkStep);
                    if (!EngineGetOver_PLC_NextWorkStep)
                    {
                        string msgErr = "【" + OpName + "】" + "无法扫码，PLC未传递机型";
                        SendMessageFun.AsyncSendMessage_Notice_Error(OpName, msgErr, "条码扫描");
                        MisDataFun.LogRecording_OP(OpName, "ERROR", "barcode", msgErr);
                        return;
                    }

                    // 读取PLC中的信息
                    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 int EngineTypeID2);

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

                    // 验证托盘码
                    int ScanPalletCodeStatu = MisDataBaseFun.ScanPalletCode(OpName, barcode, EngineID);
                    if (ScanPalletCodeStatu == 2) return;
                    if (ScanPalletCodeStatu == 3) // 本次扫码完走传递机型
                    {
                        MisDataFun.OpName_MIS_MOBY_Insert(OpName, EngineTypeID, EngineType, EngineID, PartPallet_Code, OrderForm, 0, 0, "");
                        EngineTypeGetOver_PLC_Work(OpName, EngineTypeID, EngineType, EngineID, PartPallet_Code, OrderForm);
                        return;
                    }

                    // 校验涂胶时间与记录 方法里已经记日志了 但是不能不给装配
                    if (MisDataFun.GetSpecialOpExist(OpName, "涂胶时间差校验") && barcode.IndexOf("P000") != -1 && barcode.Length > 21)
                    {
                        int TJResult = MisDataFun.CheckTJTimeAndSave(OpName, EngineID, barcode, 0);
                        //if (TJResult != 1) return;
                    }
                    if (MisDataFun.GetSpecialOpExist(OpName, "涂胶时间差校验2") && barcode.IndexOf("P000") != -1 && barcode.Length > 21)
                    {
                        int TJResult = MisDataFun.CheckTJTimeAndSave(OpName, EngineID, barcode, 0, 2);
                        //if (TJResult != 1) return;
                    }
                    if (MisDataFun.GetSpecialOpExist(OpName, "上盖固定架扫码校验") && barcode.IndexOf("P000") != -1 && barcode.Length > 21 && (EngineTypeID == 1 || EngineTypeID == 5))
                    {
                        int TJResult = MisDataFun.CheckLineOutDataIsAllOk(OpName, barcode);
                        if (TJResult != 1) return;
                    }

                    //物料验证 先本地校验，成功之后在向工厂MES校验
                    MisDataFun.PartMaterial_DM_Check(OpName, barcode, EngineID, 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)
                    {
                        if (MisDataFun.GetSpecialOpExist(OpName, "手动入箱工位")) //模组码存入Pack中 同时给 模组记录Pack信息
                        {
                            MisDataFun.WorkPlan_TurnLine(EngineID, barcode, 1);
                        }

                        //更新订单完工数量及物料
                        SendMessageFun.SendMessage("2", "11", OpName);

                        if (!EngineGetOver_PLC_NextWorkStep) return; // 避免没有传递机型时再次扫码触发允许工作
                        //通知PLC允许工作,允许工作在保存完成时清空
                        MIS_BASE.Write_FixAllow(OpName, true);
                    }
                    else
                    {
                        SendMessageFun.AsyncSendMessage_Notice(OpName, "请扫描物料！");
                    }
                }
                catch (Exception err)
                {
                    ////ApplicationLog.WriteLog(err, "Function02:Barcode");
                }
            }
        }
    }
}
