using System;
using System.Data;
using System.Windows.Forms;
using bizFacade;
//using SystemFramework;
using MesServerWork;

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
                {
                    DoWhatPlc.CustomeEvetnArgs msgEvent = (DoWhatPlc.CustomeEvetnArgs)e;
                    string OpName = Convert.ToString(msgEvent.OpName);
                    string barcode = Convert.ToString(msgEvent.TagValue);
                    SendMessageFun.SendInfomation("BarCodeData", barcode, OpName);  // 将扫码值发送给前端
                    string AlarmStr = "";
                    bool isShowBarNGTipBox;

                    int IsAllowWork;
                    bool IsSuccess = false;

                    int EngineTypeID;//机型代码
                    int PartPallet_Code;//托盘编号

                    string EngineType;//机型
                    string EngineID;//工件编号
                    string OrderForm;
                    MIS_BASE.GetEngineTypeFromPLC(OpName, out EngineTypeID, out EngineType, out EngineID, out PartPallet_Code, out OrderForm);

                    // 判断是否存在异常导致不允许工作 输出msg到前端
                    DataTable OpNameIsWorkDt = BaseDataSystemMES.OpNameIsAllowWork(OpName);
                    if (Convert.ToInt32(OpNameIsWorkDt.Rows[0]["提示类型"]) == 3)
                    {
                        SendMessageFun.SendInfomation("ShowTipBox", OpNameIsWorkDt.Rows[0]["提示文本"].ToString(), OpName);
                        return;
                    }

                    bool EngineGetOver_PLC_NextWorkStep;
                    //传递机型是否到位
                    MIS_BASE.Read_EngineGetOver_PLC(OpName, out EngineGetOver_PLC_NextWorkStep);
                    if (!EngineGetOver_PLC_NextWorkStep)
                    {
                        SendMessageFun.AsyncSendMessage_Notice(OpName, "PLC未传递机型");
                        isShowBarNGTipBox = BaseDataSystemMES.Check_BarCodeNGTemp(OpName, 1);
                        if (isShowBarNGTipBox) SendMessageFun.SendInfomation("ShowTipBox", "多次扫描条码失败，请检查扫码位置是否正确！", OpName);
                        return;
                    }

                    // MA080 只扫描铭牌信息记录  记录完就退出
                    int SaveBarCodeStatusType = 0;
                    if (OpName == "J07MA080")
                    {
                        bool isMaterialVerifyOver;
                        MIS_BASE.Read_MaterialVerifyOver(OpName,out isMaterialVerifyOver);
                        if (isMaterialVerifyOver) {
                            SendMessageFun.AsyncSendMessage_Notice(OpName, "工作已完成，不再校验铭牌内容");
                            isShowBarNGTipBox = BaseDataSystemMES.Check_BarCodeNGTemp(OpName, 1);
                            if (isShowBarNGTipBox) SendMessageFun.SendInfomation("ShowTipBox", "多次扫描条码失败，请检查扫码位置是否正确！", OpName);
                            return;
                        }
                        // 1.查看铭牌信息是否存在铭牌内容
                        // 2.校验铭牌信息 成功则保存标志位(168)
                        string barCodeData = MIS_BASE.ReadPLC(40, OpName).ToString();
                        if(barCodeData == null || barCodeData == "")
                        {
                            // 先校验有没有记录  如果没有则继续正常提示
                            DataTable barCodeCheckDt = BaseDataSystemMES.Check_MA080BarCode(EngineID,barcode);
                            if(barCodeCheckDt.Rows.Count > 0) {
                                MIS_BASE.WritePLC(168, OpName, 4);
                                SaveBarCodeStatusType = 2;
                                SendMessageFun.AsyncSendMessage_Notice(OpName, "存在打印铭牌记录，校验成功！");
                            }
                            else
                            {
                                MIS_BASE.WritePLC(168, OpName, 1);
                                SaveBarCodeStatusType = 1;
                                SendMessageFun.AsyncSendMessage_Notice(OpName, "请先打印铭牌后进行校验信息");
                                isShowBarNGTipBox = BaseDataSystemMES.Check_BarCodeNGTemp(OpName, 1);
                                if (isShowBarNGTipBox) SendMessageFun.SendInfomation("ShowTipBox", "多次扫描条码失败，请检查扫码位置是否正确！", OpName);
                            }
                        }
                        else
                        {
                            if(barCodeData == barcode)
                            {
                                // 校验条码中是否包含序列号
                                if (barcode.Contains(EngineID.Split('*')[2]))
                                {
                                    MIS_BASE.WritePLC(168, OpName, 2);
                                    SaveBarCodeStatusType = 2;
                                    SendMessageFun.AsyncSendMessage_Notice(OpName, "铭牌二维码信息校验成功");
                                    BaseDataSystemMES.Check_BarCodeNGTemp(OpName, 2);
                                }
                                else
                                {
                                    MIS_BASE.WritePLC(168, OpName, 3);
                                    SaveBarCodeStatusType = 3;
                                    SendMessageFun.AsyncSendMessage_Notice(OpName, "所扫铭牌二维码中未包含条码序列号，请确认条码是否打印正确！");
                                    isShowBarNGTipBox = BaseDataSystemMES.Check_BarCodeNGTemp(OpName, 1);
                                    if (isShowBarNGTipBox) SendMessageFun.SendInfomation("ShowTipBox", "多次扫铭牌二维码失败，请检查扫码位置是否正确！", OpName);
                                }
                            }
                            else
                            {
                                MIS_BASE.WritePLC(168, OpName, 3);
                                SaveBarCodeStatusType = 3;
                                SendMessageFun.AsyncSendMessage_Notice(OpName, "扫码信息于打印信息不一致！校验失败");
                                isShowBarNGTipBox = BaseDataSystemMES.Check_BarCodeNGTemp(OpName, 1);
                                if (isShowBarNGTipBox) SendMessageFun.SendInfomation("ShowTipBox", "多次扫描铭牌二维码失败，请检查扫码位置是否正确！", OpName);
                            }
                            SendMessageFun.SendInfomation("J07MA080BarCodeCheckBrushWriting", barcode, OpName);
                            if(SaveBarCodeStatusType == 2)
                            {
                                bool isOk = BaseDataSystemMES.BarCodeCheck_Add(OpName, EngineID, EngineType, EngineTypeID, barcode, SaveBarCodeStatusType);
                                if (isOk)
                                {
                                    // 通知前端刷新结果
                                    SendMessageFun.SendInfomation("J07MA080BarCodeCheckBrushWriting", barcode, OpName);
                                }
                                else
                                {
                                    SendMessageFun.AsyncSendMessage_Notice(OpName, "铭牌二维码信息保存失败");
                                    isShowBarNGTipBox = BaseDataSystemMES.Check_BarCodeNGTemp(OpName, 1);
                                    if (isShowBarNGTipBox) SendMessageFun.SendInfomation("ShowTipBox", "多次扫描铭牌二维码失败，请检查扫码位置是否正确！", OpName);
                                }
                            }
                        }

                        return;
                    }


                    //物料验证
                    BaseDataSystemMES.PartMaterial_DM_Update(OpName, barcode, out IsSuccess);
                    if (!IsSuccess)
                    {
                        SendMessageFun.AsyncSendMessage_Notice(OpName, "条码无效,请确认");
                        isShowBarNGTipBox = BaseDataSystemMES.Check_BarCodeNGTemp(OpName, 1);
                        if (isShowBarNGTipBox) SendMessageFun.SendInfomation("ShowTipBox", "多次扫描物料码失败，请检查扫码位置是否正确！", OpName);
                        return;
                    }
                    else
                    {
                        SendMessageFun.SendInfomation("MaterialList", "1", OpName);//通知界面更新零件信息
                    }




                    //获取是否允许工作
                    BaseDataSystemMES.MesServer_Judge_IsCanWork(OpName, out IsAllowWork);

                    //此处逻辑有问题   需要考虑到批次扫描  提示PLC未传递机型的情况
                    if (IsAllowWork == 1)
                    {
                        //BaseDataSystemMES.Material_Insert(OpName, 1);

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

                        //通知PLC允许工作,允许工作在保存完成时清空
                        MIS_BASE.Write_FixAllow(OpName, true);

                        // 在物料验证完成后110  工位将该条码的总分先关系保存 并且信息拉取过来并且给允许工作 将两个工位该总成绑定的物料都给拉过来
                        if(OpName == "J07MA110")
                        {
                            BaseDataSystemMES.BusPartLine_Add(EngineID, barcode, OpName, OrderForm, EngineType);
                            BaseDataSystemMES.Update_DataBaind(EngineID,OpName,"J07PA050");
                            BaseDataSystemMES.Update_DataBaind(EngineID,OpName,"J07PA060");
                        }
                    }
                    else
                    {
                        SendMessageFun.AsyncSendMessage_Notice(OpName, "请扫描物料！");
                    }


                }
                catch (Exception err)
                {
                    // 保存出错日志 1.申请上线 2.传递机型 3.允许保存 4.扫码
                    MisDataSaveDate.FuntionMesRead.Alarm_SaveFileLog4Error(4, e, "Fun1052", err);
                    ////ApplicationLog.WriteLog(err, "Function02:Barcode");
                }

            }

        }


    }
}
