using bizFacade;
using MesServerWork;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MesServerFunctions
{
    partial class FunctionMES
    {
        /// <summary>
        /// 申请上线 43
        /// </summary>
        /// <param name="OpName"></param>
        /// <param name="tagValue"></param>
        private void PartLoad(object e)
        {
            try
            {
                int EngineTypeID;//机型代码
                string EngineType;//机型
                string EngineID;//工件编号

                string OrderForm;

                int PLCRcipe;

                DoWhatPlc.CustomeEvetnArgs msgEvent = (DoWhatPlc.CustomeEvetnArgs)e;
                string OpName = Convert.ToString(msgEvent.OpName);

                int tagValue = Convert.ToInt32(msgEvent.TagValue);
                if (tagValue > 0)
                {
                    BaseDataSystemMES.OpName_MIS_MOBY_Insert(OpName, MobyTagType.WorkStartIn, DateTime.Now.ToString());

                    int CountNumber = Convert.ToInt32(MIS_BASE.ReadPLC(38, OpName));
                    DataTable isOutLineWorkDt;
                    BaseDataSystemMES.Get_TempData(OpName, "非本线工作订单", out isOutLineWorkDt);
                    if (isOutLineWorkDt.Rows.Count > 0)
                    {
                        // 判断是否值等于1 等于1 则直接给日期总成号  反之从生产订单中获取
                        if(isOutLineWorkDt.Rows[0]["数值"].ToString() == "1")
                        {
                            System.DateTime currentTime = new System.DateTime();
                            currentTime = System.DateTime.Now;
                            EngineTypeID = 2;
                            EngineType = "1384 033 002";
                            EngineID = currentTime.ToString("g").Replace("/","").Replace(" ", "-").Replace(":", "");
                            OrderForm = currentTime.ToString("g").Replace("/", "").Replace(" ", "-").Replace(":", "");
                            MIS_BASE.WritePLC(32, OpName, false);
                        }
                        else
                        {
                            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, "该订单所有总成号已在MA110工作完毕，不允许继续工作，请联系班长进行修改完工状态！");
                                    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);

                                    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);
                                    // MES传递机型
                                    MIS_BASE.Write_EngineTypeGetOver_MES(OpName, true);
                                }
                            }
                            else
                            {
                                MIS_BASE.WritePLC(32, OpName, true);
                                SendMessage.AsyncSendMessage_Notice(OpName, "该订单已生产完毕或无选中的生产订单，请选择其他订单进行生产！");
                                return;
                            }
                        }
                    }
                    else
                    {
                        DataTable dt;
                        dt = BaseDataSystemMES.GetNowWorkOrderData(OpName, 0);
                        if (dt.Rows.Count > 0)
                        {
                            if (Convert.ToInt32(dt.Rows[0]["线首是否完成工作"]) == 4)
                            {
                                MIS_BASE.WritePLC(32, OpName, true);
                                SendMessage.AsyncSendMessage_Notice(OpName, "该订单所有总成号已在MA110工作完毕，不允许继续工作，请联系班长进行修改完工状态！");
                                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);

                                // 获取左置右置  35 （DBX 11.2）
                                DataTable LeftRightDt = BaseDataSystemMES.GetNowWorkLeftRight(EngineType);
                                if(Convert.ToInt32(LeftRightDt.Rows[0]["result"]) == 1)
                                {
                                    MIS_BASE.WritePLC(35, OpName, true);
                                }

                                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);
                                // MES传递机型
                                MIS_BASE.Write_EngineTypeGetOver_MES(OpName, true);
                            }
                        }
                        else
                        {
                            MIS_BASE.WritePLC(32, OpName, true);
                            SendMessage.AsyncSendMessage_Notice(OpName, "该订单已生产完毕或无选中的生产订单，请选择其他订单进行生产！");
                            return;
                        }
                    }
                    // 将产品信息写入PLC
                    // 如果是 J07PA050 AMT 工件则直接不给传递机型了在扫码完成后传 反之继续传
                    //if(!(OpName == "J07PA050" && (EngineTypeID == 1 || EngineTypeID == 4)))
                    //{
                    //    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);
                    //    // MES传递机型
                    //    MIS_BASE.Write_EngineTypeGetOver_MES(OpName, true);
                    //}
                }
                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_ConfirmOkGo(OpName, false);
                    MIS_BASE.Write_EngineTypeGetOver_MES(OpName, false);
                    MIS_BASE.WritePLC(35, OpName, false);
                    //工作完成
                    MIS_BASE.Write_MaterialVerifyOver(OpName, false);
                }
            }
            catch (Exception err)
            {
                // 保存出错日志 1.申请上线 2.传递机型 3.允许保存 4.扫码
                MisDataSaveDate.FuntionMesRead.Alarm_SaveFileLog4Error(1, e, "Fun1060", err);
                ////ApplicationLog.WriteLog(err, "Function02:FalseGO");
            }
        }
    }
}
