using System;
using System.Windows.Forms;
using MesServerWork;
using MisDataFunDll;
//using SystemFramework;

namespace MesServerFunctions
{
    partial class FunctionMES
    {
        /// <summary>
        /// 拧紧结束确认（必须MES）
        /// </summary>
        /// <param name="OpName"></param>
        /// <param name="tagValue"></param>
        private void FixOver(object e)
        {
            try
            {
                var msgEvent = (DeviceDriver_BasicData.CustomeEvetnArgs)e;
                string OpName = Convert.ToString(msgEvent.OpName);
                int tagValue = Convert.ToInt32(msgEvent.TagValue);

                if (tagValue == 0)
                {
                    #region  
                    int EngineTypeID;//机型代码
                    string EngineType;//机型
                    string EngineID;//工件编号
                    int PartPallet_Code;//托盘编号
                    MIS_BASE.GetEngineTypeFromDB(OpName, out EngineTypeID, out EngineType, out EngineID, out PartPallet_Code);///从PLC中获取MOBY信息
                    #endregion

                    int IsOK = 1;
                    if (MIS_BASE.hashtable_QualityMesRead == null)
                    {
                        MIS_BASE.InitHashtable_MesServerCode();
                    }
                    if (MIS_BASE.hashtable_QualityMesRead.ContainsKey(OpName))
                    {
                        IsOK = Convert.ToInt32(MIS_BASE.hashtable_QualityMesRead[OpName]);
                    }

                    int workStepProperty = 0;
                    int BoltCode = 0;
                    int ProcessCode = 0;
                    bool isHaveNg = false;
                    bool sn_WorkingStepIsOver = GetShaftWoringStepIsWorked_WorkGroup(OpName, EngineTypeID, EngineType, out ProcessCode, out BoltCode, out workStepProperty, out isHaveNg);

                    // 判断是否还有需要拧紧的组号
                    if (!sn_WorkingStepIsOver)
                    {
                        //必须拧紧合格，才能给出允许拧紧
                        if (IsOK == 1)
                        {
                            //发动UDP,通知MES工作站拧紧第几组
                            SendInfomation("FixAllowGuid", Convert.ToString(ProcessCode) + "&" + Convert.ToString(BoltCode), OpName);//显示拧紧标志

                            //写入工作顺序
                            MIS_BASE.Write_ProcessCode(OpName, ProcessCode);

                            //写入子程序(套筒号)
                            MIS_BASE.Write_BoltCode(OpName, BoltCode);

                            //通知PLC允许工作
                            MIS_BASE.Write_FixAllow(OpName, true);
                            MisDataFun.LogRecording_OP(OpName, "INFO", "MIS", "MES允许手动拧紧！位置：" + ProcessCode.ToString() + "，套筒：" + BoltCode.ToString());
                        }
                    }
                    else
                    {
                        // 是否存在拧紧不合格的
                        if(!isHaveNg)
                        {
                            MIS_BASE.Write_ConfirmOkGo(OpName, true);
                            MIS_BASE.Write_FalseGO(OpName, false);
                            MisDataFun.LogRecording_OP(OpName, "INFO", "MIS", "MES所有拧紧任务完成！");
                        }
                    }
                }
            }
            catch (Exception err)
            {
                ////ApplicationLog.WriteLog(err, "Function04:FixOver");
            }
        }
        /// <summary>
        /// 重新工作
        /// </summary>
        /// <param name="OpName"></param>
        private void WorkAgain(string OpName)
        {
            try
            {
                // 1：允许拧紧清零
                MIS_BASE.Write_FixAllow(OpName, false);
                // 2：从PLC获取当前拧紧序号
                int BoltCode = 0;
                int ProcessCode = 0;
 
                MIS_BASE.Read_BoltCode(OpName, out BoltCode);
                MIS_BASE.Read_ProcessCode(OpName, out ProcessCode);
                if(BoltCode > 0 && ProcessCode > 0)
                {
                    //记录当前拧紧序号
                    if (MIS_BASE.hashtable_WorkGroupIndex.ContainsKey(OpName))
                    {
                        MIS_BASE.hashtable_WorkGroupIndex[OpName] = ProcessCode;
                    }

                    // 更新当前位置为未拧紧状态
                    MisDataFun.MesServer_ShaftPosition_FixAllow_Again(OpName, ProcessCode);
                    //发动UDP,通知MES工作站拧紧第几组
                    SendInfomation("FixAllowGuid", Convert.ToString(ProcessCode) + "&" + Convert.ToString(BoltCode), OpName);//显示拧紧标志
                    MIS_BASE.Write_FixAllow(OpName, true);
                }
            }
            catch (Exception err)
            {
                ////ApplicationLog.WriteLog(err, "Function04:WorkAgain");
            }
        }

        /// <summary>
        /// 拧紧完成(PLC)，不合格，但是要继续拧紧下一个
        /// </summary>
        /// <param name="OpName"></param>
        /// <param name="tagValue"></param>
        private void FixOK_Ng_Next(string OpName)
        {
            try
            {
                int EngineTypeID;//机型代码
                string EngineType;//机型
                string EngineID;//工件编号
                int PartPallet_Code;//托盘编号
                MIS_BASE.GetEngineTypeFromDB(OpName, out EngineTypeID, out EngineType, out EngineID, out PartPallet_Code);///从PLC中获取MOBY信息


                int workStepProperty = 0;
                int BoltCode = 0;
                int ProcessCode = 0;
                bool isHaveNg = false;

                bool sn_WorkingStepIsOver = GetShaftWoringStepIsWorked_WorkGroup(OpName, EngineTypeID, EngineType, out ProcessCode, out BoltCode, out workStepProperty, out isHaveNg);
                if(!sn_WorkingStepIsOver)
                {
                    //发动UDP,通知MES工作站拧紧第几组
                    SendInfomation("FixAllowGuid", Convert.ToString(ProcessCode) + "&" + Convert.ToString(BoltCode), OpName);//显示拧紧标志

                    //写入工作顺序
                    MIS_BASE.Write_ProcessCode(OpName, ProcessCode);

                    //写入子程序(套筒号)
                    MIS_BASE.Write_BoltCode(OpName, BoltCode);

                    //通知PLC允许工作
                    MIS_BASE.Write_FixAllow(OpName, true);
                    MisDataFun.LogRecording_OP(OpName, "INFO", "MIS", "MES允许手动拧紧！位置：" + ProcessCode.ToString() + "，套筒：" + BoltCode.ToString());

                }

            }
            catch (Exception err)
            {
                ////ApplicationLog.WriteLog(err, "Function04:FixOK");
            }
        }
    }
}
