using System;
using MesServerWork;
using MisDataFunDll;
//using SystemFramework;

namespace MesServerFunctions
{
    partial class FunctionMES
    {
        /// <summary>
        /// 初始化处理工序工步执行过程
        /// </summary>
        private void InitWorkStep(string OpName, int engineTypeID, string engineType, string engineID)
        {
            try
            {
                NextWorkStep(OpName, engineTypeID, engineType, engineID);
            }
            catch (Exception err)
            {
                ////ApplicationLog.WriteLog(err, err.Message);
            }
        }

        /// <summary>
        /// 处理工序工步执行过程下一步信息
        /// </summary>
        private void NextWorkStep(string OpName, int engineTypeID, string engineType, string engineID)
        {
            int workStepProperty = 512;


            if (MIS_BASE.hashtable_workStepProperty == null)
            {
                MIS_BASE.InitHashtable_MesServerCode();
            }
            //获取工步属性
            if (MIS_BASE.hashtable_workStepProperty.ContainsKey(OpName))
            {
                workStepProperty = Convert.ToInt32(MIS_BASE.hashtable_workStepProperty[OpName]);
            }

            //16和64都表示分步保存质量数据（B6中并不是表示正常拧紧与返修拧紧的关系）
            if (workStepProperty == 16 || workStepProperty == 64)
            {
                int BoltCode = 0;
                int ProcessCode = 512;
                bool isHaveNg = false;

                bool sn_WorkingStepIsOver = GetShaftWoringStepIsWorked_WorkGroup(OpName, engineTypeID, engineType, out ProcessCode, out BoltCode, out workStepProperty, out isHaveNg);

                if (!sn_WorkingStepIsOver)
                {
                    // 显示弹窗与图片 不知道为啥没有 后加读
                    SendInfomation("ShaftPosition", "1", OpName);
                    //发动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所有拧紧任务完成！");
                    }
                }
            }
        }

        /// <summary>
        /// 判断是否还有需要拧紧的组号 ProcessCode=拧紧序号 BoltCode=套筒号
        /// </summary>
        /// <param name="opName"></param>
        /// <param name="engineTypeID"></param>
        /// <param name="engineType"></param>
        /// <param name="ProcessCode">拧紧序号</param>
        /// <param name="BoltCode">套筒号</param>
        /// <returns></returns>
        private bool GetShaftWoringStepIsWorked_WorkGroup(string opName, int engineTypeID, string engineType, out int ProcessCode, out int BoltCode, out int workStepProperty, out bool isHaveNg)
        {
            ProcessCode = 0;
            BoltCode = 0;
            workStepProperty = 0;
            bool sn_WorkingStepIsOver = true;//是否完成拧紧
            isHaveNg = false;//是否有拧紧不合格

            //电子看板_工位机型_质量数据测量位置_临时表_查询_完成情况
            MisDataFun.MesServer_ShaftPosition_Select_FinishResult(opName, out ProcessCode, out BoltCode, out workStepProperty, out sn_WorkingStepIsOver, out isHaveNg);
            if (MIS_BASE.hashtable_WorkGroupIndex == null)
            {
                MIS_BASE.InitHashtable_MesServerCode();
            }

            //记录当前拧紧序号
            if (MIS_BASE.hashtable_WorkGroupIndex.ContainsKey(opName))
            {
                MIS_BASE.hashtable_WorkGroupIndex[opName] = ProcessCode;
            }
            return sn_WorkingStepIsOver;
        }
    }
}
