using bizFacade;
using MesServerWork;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading;
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);

                //+*************DbName*****************
                string DbName = Convert.ToString(msgEvent.OpName);
                int tagValue = Convert.ToInt32(msgEvent.TagValue);
                int workGetOrderType = 0;

                if (tagValue > 0)
                {
                    // 是否为二次压装
                    if(OpName == "J07PA040")
                    {
                        if (Convert.ToBoolean(MIS_BASE.ReadPLC(64, OpName)))
                        {
                            workGetOrderType = 1;
                        }
                    }
                    

                    BaseDataSystemMES.OpName_MIS_MOBY_Insert(OpName, MobyTagType.WorkStartIn, 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.Write_MaterialVerifyOver(OpName, false);

                    //工作类型 0 正常件 1 PA30 件 2 PA40 返回件
                    //int workType = 0;
                    
                    //if (OpName == "J07PA030")
                    //{
                    //    //该工件是PA30工件
                    //    workType = 1;
                    //}

                    ////判断 是否为二次加工件  （64）
                    //if (OpName == "J07PA040" && workGetOrderType == 1)
                    //{
                    //    //该工件是PA40二次加工件
                    //    workType = 2;
                    //}

                    //if (workType == 0)
                    //{
                    // 正常逻辑 获得机型信息、配方号
                    //获取到的5s 6s 工件条码
                    //北汽福田工位状态监控MOBY_查询_订单下达用
                    DataTable dt;
                    dt = BaseDataSystemMES.GetNowWorkOrderData(OpName, workGetOrderType);
                    if(dt.Rows.Count > 0)
                    {
                        if (Convert.ToInt32(dt.Rows[0]["线首是否完成工作"]) == 4)
                        {
                            MIS_BASE.WritePLC(32, OpName, true);
                            SendMessage.AsyncSendMessage_Notice(OpName, "该订单所有总成号已在MA010工作完毕，不允许继续工作，请联系班长进行修改完工状态！");
                            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);
                        }
                    }
                    else
                    {
                        MIS_BASE.WritePLC(32, OpName, true);
                        SendMessage.AsyncSendMessage_Notice(OpName, "该订单已生产完毕或无选中的生产订单，请选择其他订单进行生产！");
                        return;
                    }
                        
                    //}
                    //else
                    //{
                    //    DataTable dt2;
                    //    if (workType == 1)
                    //    {
                    //        // PA30逻辑 从PA20获得机型信息、配方号、 
                    //        dt2 = BaseDataSystemMES.lineOutTowBackWork_GetData("J07PA020", 1);
                    //    }
                    //    else //workType == 2
                    //    {
                    //        // PA40Back逻辑 从PA40获得机型信息、配方号、 
                    //        dt2 = BaseDataSystemMES.lineOutTowBackWork_GetData("J07PA040", 0);
                    //    }
                               
                    //    EngineTypeID = Convert.ToInt32(dt2.Rows[0]["机型代码"]);
                    //    //配方号暂时没保存
                    //    EngineType = dt2.Rows[0]["机型"].ToString();
                    //    OrderForm = dt2.Rows[0]["订单号"].ToString();
                    //    EngineID = dt2.Rows[0]["总成号"].ToString();
                    //}

                    BaseDataSystemMES.OpName_MIS_MOBY_Insert(OpName, MobyTagType.EngineID_MES, EngineID);

                    // 将产品信息写入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.PLCRcipeByOpName(OpName, out byte[] bytes, out string tagStartAdr, PLCRcipe);
                    // 写入配方数据
                    //DtWebApi.ReadWritePLC.Write_TagValueByTagTypeID("94", OpName, System.Text.Encoding.ASCII.GetString(bytes));
                    // 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.Write_MaterialVerifyOver(OpName, false);
                    //清除上线信息
                    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(115, OpName, "");
                    //空挡开关检测
                    MIS_BASE.WritePLC(95, OpName, 0);
                    //倒挡开关检测
                    MIS_BASE.WritePLC(96, OpName, 0);
                    //里程表检测
                    MIS_BASE.WritePLC(97, OpName, 0);
                    //大机型扩展
                    MIS_BASE.WritePLC(98, OpName, 0);
                    //TCU
                    MIS_BASE.WritePLC(122, OpName, 0);
                    //注油量
                    MIS_BASE.WritePLC(123, OpName, 0);
                }
            }
            catch (Exception err)
            {
                // 保存出错日志 1.申请上线 2.传递机型 3.允许保存 4.扫码
                MisDataSaveDate.FuntionMesRead.Alarm_SaveFileLog4Error(1, e, "Fun1053", err);
                ////ApplicationLog.WriteLog(err, "Function02:FalseGO");
            }
        }
    }
}
