using System;
using System.Data;
using bizFacade;
using MesServerWork;

namespace MesServerFunctions
{
    partial class FunctionMES
    {
        /// <summary>
        /// 正常逻辑工作(传递机型)
        /// </summary>
        /// <param name="OpName"></param>
        /// <param name="EngineTypeID"></param>
        /// <param name="EngineType"></param>
        /// <param name="EngineID"></param>
        /// <param name="PartPallet_Code"></param>
        private void EngineTypeGetOver_PLC_Work(string OpName, int EngineTypeID, string EngineType, string EngineID, int PartPallet_Code,string OrderForm)
        {
            int IsAllowWork;
            int RepairPartStatusPLC = 0;
            int MES_QualityMask = 1;

            DataTable dt;
            dt = BaseDataSystemMES.EngineTypeChange(OpName, EngineType);
            //提示机型发生变化
            if (dt.Rows.Count > 0)
            {
                string mesValue = Newtonsoft.Json.JsonConvert.SerializeObject(dt);//<DoWhatPlc.CustomeEvetnArgs>(recData);
                string message = "MES|MaterialList_Dif_Resources|" + OpName + "|" + mesValue;
                SendMessage.AsyncSendMessage(message);

                // 6S 判断 档位刻度 反馈给前端具体刻度值
                if(EngineTypeID == 4 || EngineTypeID == 8)
                {
                    DataTable dt2 = BaseDataSystemMES.EngineTypeChange_MA020_KeDu(OpName, EngineType);
                    if(Convert.ToInt32(dt2.Rows[0]["result"]) != 1)
                    {
                        MIS_BASE.WritePLC(70, OpName, true);
                        string message2 = "MES|ShowTipBox|" + OpName + "|" + dt2.Rows[0]["msg"].ToString();
                        SendMessage.AsyncSendMessage(message2);
                    }
                }
            }
            // 先判断差异，后将当前工件信息进行记录，不然永远查不出来
            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());

            //电子看板_装配零件_临时表_增加_北汽
            //第二步，将零件信息转移到零件_临时表中
            BaseDataSystemMES.MesServer_Process_EngineTypeOverBQ(OpName);

            //通知web显示
            SendMessageFun.SendMessage("123", "111", OpName);
            MIS_BASE.Read_PalletInfo(OpName, out MES_QualityMask);//合格标志
            MIS_BASE.Read_RepairStatus_PLC(OpName, out RepairPartStatusPLC);//返修标志
            SendMessageFun.SendInfomation("QualityMask", Convert.ToString(MES_QualityMask), OpName);//显示合格标志
            SendMessageFun.SendInfomation("RepairPartStatusPLC", Convert.ToString(RepairPartStatusPLC), OpName);//显示返修标志

            if (MES_QualityMask == 2)
            {
                //工作完成
                // MIS_BASE.Write_MaterialVerifyOver(OpName, true);
                //SendMessageFun.SendInfomation("NgGO", "1", OpName);//显示不合格标志
                //return;
            }
            SendMessageFun.AsyncSendMessage_Notice(OpName, "工件到位，请扫描前壳码进行工作！");
            //第三步，用于工件离开后工数量统计及工件编号追溯
            //BaseDataSystemMES.MaterialPush_EngineIDStatus_Insert(OpName);//更新订单产量

            //第四步，获取是否允许工作
            //BaseDataSystemMES.MesServer_Judge_IsCanWork(OpName, out IsAllowWork);
            //MIS_BASE.Write_FixAllow(OpName, true);
            //暂时先定义为扫描完成才能进行压装工作，因为考虑到工作完成后，操作工未必会去扫描物料的情况
            //if (IsAllowWork == 1)
            //{
            //通知PLC允许工作,允许工作在保存完成时清空

            //}
            //else
            //{
            //    SendMessageFun.AsyncSendMessage_Notice(OpName, "请扫描物料！");
            //}

        }


        /// <summary>
        /// 返修件需要工作
        /// </summary>
        /// <param name="OpName">工位号</param>
        private void IsNeedRepaire_OK(string OpName)
        {
            string orderForm = "";
            int engineTypeID;
            string engineType = "";
            string engineID = "";
            string palletCode = "";
            int isRepair;
            string repairCode = "";
            //从监控系统中查询出本工位信息
            BaseDataSystemMES.OpName_MIS_Moby_Select(OpName,
            out  orderForm, out  engineTypeID, out  engineType,
            out  engineID, out palletCode, out isRepair, out repairCode);

            EngineTypeGetOver_PLC_Work(OpName, engineTypeID, engineType, engineID, Convert.ToInt32(palletCode),orderForm);
        }




    }
}
