using System;
using System.Threading;
using bizFacade;
//using SystemFramework;
using MesServerWork;

namespace MesServerFunctions
{
    partial class FunctionMES
    {
        /// <summary>
        /// 申请上线(针对上线工位)
        /// </summary>
        /// <param name="OpName"></param>
        /// <param name="tagValue"></param>
        private void PartLoad(object e)
        {
            try
            {
                DoWhatPlc.CustomeEvetnArgs msgEvent = (DoWhatPlc.CustomeEvetnArgs)e;
                string OpName = Convert.ToString(msgEvent.OpName);
                int tagValue = Convert.ToInt32(msgEvent.TagValue);
                

                if (tagValue == 1)
                {
                    // 读取当前选择的计划中的机型，写入PLC
                    // 获取订单的机型
                    string Program_No = "";
                    Db.GetEngineType_Order(OpName, Program_No, out int EngineTypeID, out string EngineType, out string EngineID, out string OrderForm, out int isCheckOk, out int planCount, out int haveUplaodCount, out int remainingQuantity);
                    if (isCheckOk == 2)
                    {
                        MIS_BASE.Write_EngineTypeID_MES(OpName, 0);
                        MIS_BASE.Write_EngineType_MES(OpName, "");
                        MIS_BASE.Write_EngineID_MES(OpName, "");
                        MIS_BASE.Write_OrderForm_MES(OpName, "");

                        MIS_BASE.WritePLC(95, OpName, planCount);
                        MIS_BASE.WritePLC(96, OpName, haveUplaodCount);
                        MIS_BASE.WritePLC(97, OpName, remainingQuantity);

                        MIS_BASE.WritePLC(34, OpName, true);
                        string msg = "【" + OpName + "】" + "无上线订单，请下达新订单！";
                        SendMessage.AsyncSendMessage_Notice(OpName, msg);
                        MyLog4Net.MyLogHelper.Error("PartLoad", msg);
                        return;
                    }
                    else
                    {
                        BaseDataSystemMES.OpName_MIS_MOBY_Insert(OpName, MobyTagType.EngineID_MES, EngineID);

                        MIS_BASE.WritePLC(34, OpName, false);

                        // 写入机型代码 （大机型） 机型 发动机号 订单号
                        MIS_BASE.Write_EngineTypeID_MES(OpName, EngineTypeID);
                        MIS_BASE.Write_EngineType_MES(OpName, EngineType);
                        MIS_BASE.Write_EngineID_MES(OpName, EngineID);
                        MIS_BASE.Write_OrderForm_MES(OpName, OrderForm);
                        MIS_BASE.WritePLC(95, OpName, planCount);
                        MIS_BASE.WritePLC(96, OpName, haveUplaodCount);
                        MIS_BASE.WritePLC(97, OpName, remainingQuantity);

                        // 写入配方号
                        string tagStartAdr;
                        BaseDataSystemMES.OpName_MIS_MOBY_Insert(OpName, MobyTagType.EngineTypeID_MES, EngineTypeID.ToString());
                        BaseDataSystemMES.PLCRcipeCurrent(OpName, out byte[] bytes, out tagStartAdr);
                        MIS_BASE.Write_TagValueByTagTypeID(94, OpName, System.Text.Encoding.ASCII.GetString(bytes));

                        Thread.Sleep(1000);
                        // MES传递机型
                        MIS_BASE.Write_EngineTypeGetOver_MES(OpName, true);
                        // 通知web显示
                        SendMessage.ShowMoby(OpName);
                    }
                }
                else
                {
                    //上线工位清MES传递机型信号
                    MIS_BASE.Write_EngineTypeGetOver_MES(OpName, false);
                }
            }
            catch (Exception err)
            {
                ////ApplicationLog.WriteLog(err, "Function08:MesReadOver");
            }
        }
    }
}
