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

namespace MesServerFunctions
{
    partial class FunctionMES
    {
        /// <summary>
        /// 工件到位与离开时，清空MES自身信号
        /// </summary>
        /// <param name="OpName"></param>
        /// <param name="tagValue"></param>
        private void WorkStart(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());
                    //北汽福田工位状态监控MOBY_查询_订单下达用
                    DataTable dt;
                    dt = BaseDataSystemMES.DownOrderToOpNameMobyList(OpName);
                    if (dt != null)
                    {
                        //开始上线前清空交互区数据
                        MIS_BASE.WritePLC(73, OpName, "");
                        MIS_BASE.WritePLC(74, OpName, "");
                        MIS_BASE.WritePLC(75, OpName, "");
                        int workType = 0;
                        if (dt.Rows.Count > 0)
                        {
                            // 正常逻辑 获得机型信息、配方号、 
                            EngineTypeID = Convert.ToInt32(dt.Rows[0][MobyTagType.EngineTypeID_MES]);
                            EngineType = dt.Rows[0][MobyTagType.EngineType_MES].ToString();
                            OrderForm = dt.Rows[0][MobyTagType.OrderForm_MES].ToString();
                            PLCRcipe = Convert.ToInt32(dt.Rows[0][MobyTagType.PLCRcipe]);
                            EngineID = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss").Replace("-", "").Replace(" ", "").Replace(":", "");
                            EngineID = EngineID + "_" + EngineType;  //拼接工件编号 按照唯一码加时间来拼接
                            // 将产品信息写入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.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.WritePLC(4, OpName, "");

                    // 是否返修点
                    MIS_BASE.WritePLC(33, OpName, false);
                    MIS_BASE.WritePLC(34, OpName, false);
                    MIS_BASE.WritePLC(24, OpName, 0);
                }
            }
            catch (Exception err)
            {
                ////ApplicationLog.WriteLog(err, "Function02:FalseGO");
            }
        }

    }
}
