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);
            }

            if(OpName == "J07MA050")
            {
                // 搜索是否为换型工件，是的话进抽检模式
                dt = BaseDataSystemMES.EngineTypeChange_MA050(EngineType);
                //提示机型发生变化
                if (Convert.ToInt32(dt.Rows[0]["result"]) == 1)
                {
                    string message = "MES|MaterialList_Dif_Resources_MA050|" + OpName + "|" + 1;
                    SendMessage.AsyncSendMessage(message);
                }
            }

            if(OpName == "J07MA040")
            {
                // 判断是否存在细一轴 若存在则去反给PLC（11.2）35
                DataTable OneAxleDt = BaseDataSystemMES.Get_EngineOneAxle(EngineType);
                if (Convert.ToInt32(OneAxleDt.Rows[0]["result"]) == 1)
                {
                    MIS_BASE.WritePLC(35, OpName, true);
                    SendMessageFun.AsyncSendMessage_Notice(OpName, "该工件存在细一轴物料，请注意！");
                }
            }
            

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

            //通知web显示
            SendMessageFun.SendMessage("1235", "1111", 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;
            }

            //第三步，用于工件离开后完工数量统计及工件编号追溯
            //BaseDataSystemMES.MaterialPush_EngineIDStatus_Insert(OpName);//更新订单产量

            // 读取当前工件螺丝数量
            DataTable dt3 = BaseDataSystemMES.Get_WorkPositionSizeByPBOM(OpName, EngineType);
            // 变量类型代码数组
            // 128 ~ 167
            //遍历 将所需要写入地址的值读取出来写入到相应地址中
            for (int i = 0; i <= dt3.Rows.Count - 1; i++)
            {
                // 读取当前行的数据 判断该放到第几行 将对于的数据放到对应数据区中
                // 获取OP操作号 用于判断放哪个地址
                int OPNumber = Convert.ToInt32(dt3.Rows[i]["OP本工位操作顺序号"]);
                // 128 ~ 137 操作类型
                MIS_BASE.WritePLC(127 + OPNumber, OpName, dt3.Rows[i]["主线设备操作类型"]);
                // 138 ~ 147 拧紧程序号
                MIS_BASE.WritePLC(137 + OPNumber, OpName, dt3.Rows[i]["拧紧程序号"]);
                // 148 ~ 157 螺栓数量
                MIS_BASE.WritePLC(147 + OPNumber, OpName, dt3.Rows[i]["传递数量"]);
                // 158 ~ 167 OP操作号(操作顺序 预留) 目前不用写入这个而是根据这个判断数据放对应区第几行
            }


            //读取当前法兰类型，发送给PLC(124)
            if(OpName == "J07MA070")
            {
                DataTable dt4 = BaseDataSystemMES.Get_EngineFlangeType(EngineType);
                int flange = Convert.ToInt32(dt4.Rows[0]["flange"]);
                if (dt4.Rows.Count > 0) MIS_BASE.WritePLC(124, OpName, flange);
                // 发送有无pkB 若有，就发1
                if (BaseDataSystemMES.Get_IsExistPKB(EngineType))
                {
                    MIS_BASE.WritePLC(112, OpName, 1);
                }
                else
                {
                    MIS_BASE.WritePLC(112, OpName, 0);
                }
            }

            //判断当前工件是否为售后机型 0.7(53)
            int Aftermarket;
            if (OpName == "J07MA080")
            {
                DataTable dt4 = BaseDataSystemMES.Get_EngineAftermarketType(EngineType);
                Aftermarket = Convert.ToInt32(dt4.Rows[0]["是否售后机型"]);
                MIS_BASE.WritePLC(53, OpName, Aftermarket);
                if(Aftermarket == 1)
                {
                    string message = "MES|ShowTipBox|" + OpName + "|该工件为售后机型,需手动打印铭牌！";
                    SendMessage.AsyncSendMessage(message);
                }
            }

            //读取当前脉冲轮类型，发送给PLC(99)
            if (OpName == "J07ET030")
            {
                DataTable dt5 = BaseDataSystemMES.Get_ImpulseWheelMeter(EngineType);
                int impulseWheelMeter = Convert.ToInt32(dt5.Rows[0]["result"]);
                if (dt5.Rows.Count > 0) MIS_BASE.WritePLC(99, OpName, impulseWheelMeter);
                if (EngineType == "1384033006") MIS_BASE.WritePLC(53, OpName, true);
                DataTable dt6 = BaseDataSystemMES.Get_EmptySwitch_ONOFF(EngineType);
                MIS_BASE.WritePLC(114, OpName, dt6.Rows[0]["result"]);

                DataTable dt7 = BaseDataSystemMES.Get_Test_FrockType(EngineType);
                MIS_BASE.WritePLC(17, OpName, dt7.Rows[0]["result"]);

            }

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

        }

        /// <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);
        }

        /// <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);
        }




    }
}
