using System;
using System.Data;
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);

                int EngineTypeID;//机型代码
                string EngineType;//机型
                string EngineID;//工件编号

                string OrderForm;
                //线首显示订单信息

                if (tagValue == 1)
                {
                    MIS_BASE.Write_TagTypeID(115, OpName, 0);
                    MIS_BASE.Write_TagTypeID(95, OpName, 0);
                    MIS_BASE.Write_TagTypeID(96, OpName, 0);
                    MIS_BASE.Write_TagTypeID(97, OpName, 0);
                    MIS_BASE.Write_TagTypeID(98, OpName, "");

                    //清空MOBY的产品流水号
                    int CountNumber = Convert.ToInt32(MIS_BASE.ReadPLC(38, OpName));

                    DataTable dt;
                    dt = BaseDataSystemMES.GetNowWorkOrderData(OpName, CountNumber);
                    if (dt.Rows.Count > 0)
                    {
                        if (Convert.ToInt32(dt.Rows[0]["线首是否完成工作"]) == 4)
                        {
                            MIS_BASE.WritePLC(32, OpName, true);
                            SendMessage.AsyncSendMessage_Notice(OpName, "该订单所有总成号已在MA040工作完毕，不允许继续工作，请联系班长进行修改完工状态！");
                            return;
                        }
                        else
                        {
                            EngineTypeID = Convert.ToInt32(dt.Rows[0]["机型代码"]);
                            EngineType = dt.Rows[0]["零件号"].ToString();
                            EngineID = dt.Rows[0]["变速箱总成号"].ToString();
                            OrderForm = dt.Rows[0]["订单号"].ToString();
                            MIS_BASE.WritePLC(32, OpName, false);

                            // PA070 的6s 件需要压五个杆   70  只有70 6s 会走这个方法
                            if (OpName == "J07PA070" && (EngineTypeID == 4 || EngineTypeID == 8))
                            {
                                // 不需要扫码直接给传递机型
                                // 将产品信息写入PLC
                                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);

                                BaseDataSystemMES.OpName_MIS_MOBY_Insert(OpName, MobyTagType.EngineID_MES, EngineID);
                                BaseDataSystemMES.OpName_MIS_MOBY_Insert(OpName, MobyTagType.EngineType_MES, EngineType);
                                //SendMessageFun.SendMessage("1235", "1111", OpName);
                                MIS_BASE.Write_EngineTypeGetOver_MES(OpName, true);
                            }
                        }
                    }
                    else
                    {
                        MIS_BASE.WritePLC(32, OpName, true);
                        SendMessage.AsyncSendMessage_Notice(OpName, "该订单已生产完毕或无选中的生产订单，请选择其他订单进行生产！");
                        return;
                    }
                }
                else
                {
                    BaseDataSystemMES.OpName_MIS_MOBY_Insert(OpName, MobyTagType.WorkStartOut, DateTime.Now.ToString());
                    MIS_BASE.Write_MesReadOver(OpName, false);
                    MIS_BASE.Write_ConfirmOkGo(OpName, false);
                    MIS_BASE.Write_FixAllow(OpName, false);
                    MIS_BASE.Write_EngineTypeGetOver_MES(OpName, false);
                    //工作完成
                    MIS_BASE.Write_MaterialVerifyOver(OpName, false);
                }
            }
            catch (Exception err)
            {
                // 保存出错日志 1.申请上线 2.传递机型 3.允许保存 4.扫码
                MisDataSaveDate.FuntionMesRead.Alarm_SaveFileLog4Error(1, e, "Fun1059", err);
                ////ApplicationLog.WriteLog(err, "Function08:WorkStart");
            }
        }
    }
}
