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

namespace MesServerFunctions
{
    partial class FunctionMES
    {
        /// <summary>
        /// PLC传递机型处理
        /// </summary>
        /// <param name="OpName"></param>
        /// <param name="tagValue"></param>
        private void EngineTypeGetOver_PLC(object e)
        {

            {
                try
                {
                    DoWhatPlc.CustomeEvetnArgs msgEvent = (DoWhatPlc.CustomeEvetnArgs)e;
                    string OpName = Convert.ToString(msgEvent.OpName);
                    int tagValue = Convert.ToInt32(msgEvent.TagValue);

                    if (tagValue == 1)
                    {
                        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;
                        }

                        if (EngineTypeID <= 0 || EngineID.Length == 0 || EngineType.Length == 0 || OrderForm.Length == 0)
                        {
                            SendMessageFun.AsyncSendMessage_Notice(OpName, "PLC传递机型为空");
                            return;
                        }

                        BaseDataSystemMES.OpName_MIS_MOBY_Insert(OpName, MobyTagType.EngineTypeID, EngineTypeID.ToString());
                        BaseDataSystemMES.OpName_MIS_MOBY_Insert(OpName, MobyTagType.EngineType, EngineType);
                        BaseDataSystemMES.OpName_MIS_MOBY_Insert(OpName, MobyTagType.EngineID, EngineID);
                        BaseDataSystemMES.OpName_MIS_MOBY_Insert(OpName, MobyTagType.OrderForm, OrderForm);
                        BaseDataSystemMES.OpName_MIS_MOBY_Insert(OpName, MobyTagType.PartPallet_Code, PartPallet_Code.ToString());
                        //显示当前订单信息
                        SendMessage.ShowMoby(OpName);

                        // 判断是否返修件 是否需要继续工作
                        DataTable backWorkDt = BaseDataSystemMES.Get_IsBackWork(OpName, EngineID);
                        if (backWorkDt.Rows[0]["result"].ToString() == "1")
                        {
                            // 写返修点并且退出方法
                            MIS_BASE.WritePLC(33, OpName, true);
                            string message = "MES|ShowTipBox|" + OpName + "|该工件为返工工件！";
                            SendMessage.AsyncSendMessage(message);
                            return;
                        }

                        //判断该订单数据是否已经工作过，有的话则在WEB跳出弹窗选择 并同时禁止工作
                        DataTable dt2 = BaseDataSystemMES.EngineIsTwoWork(OpName, OrderForm, EngineID);
                        if (dt2.Rows[0]["result"].ToString() == "1") //已完成
                        {
                            //不允许工作
                            MIS_BASE.Write_FixAllow(OpName, false);
                            //通知WEB校验重新工作还是跳过  点重新工作给允许工作，点跳过给工作完成
                            string message = "MES|Engine_Is_TwoWork|" + OpName + "|1";
                            SendMessage.AsyncSendMessage(message);
                        }
                        else
                        {
                            EngineTypeGetOver_PLC_Work(OpName, EngineTypeID, EngineType, EngineID, PartPallet_Code, OrderForm);
                        }
                    }
                }
                catch (Exception err)
                {
                    // 保存出错日志 1.申请上线 2.传递机型 3.允许保存 4.扫码
                    MisDataSaveDate.FuntionMesRead.Alarm_SaveFileLog4Error(2, e, "Fun1057", err);
                    ////ApplicationLog.WriteLog(err, "Function02:EngineTypeGetOver_PLC");
                }
            }


        }
        /// <summary>
        /// 二次工作
        /// </summary>
        /// <param name="OpName"></param>
        private void EngineTypeGetOver_PLC_Work_two(string OpName)
        {
            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);
            EngineTypeGetOver_PLC_Work(OpName, EngineTypeID, EngineType, EngineID, Convert.ToInt32(PartPallet_Code), OrderForm);

        }
        /// <summary>
        /// 跳过二次工作
        /// </summary>
        /// <param name="OpName"></param>
        private void EngineTypeGetOver_PLC_Work_Skip(string OpName)
        {
            MIS_BASE.Write_MesReadOver(OpName, true);
            MIS_BASE.Write_MaterialVerifyOver(OpName, true);
        }

    }
}
