using DataLinkMesWork;
using MesServerFunctions;
using MesServerWork;
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

namespace MisDataSaveDateMesRead
{
    public partial class Funtion
    {
        public static string MqttTargetTopic = ConfigurationManager.AppSettings["MqttTargetTopic"];

        private static void WorkStart(DoWhatPlc.CustomeEvetnArgs e)
        {
            //var ro = rv.ReturnValueToObject();

            string opName = e.OpName.ToString();
            DateTime arrivedTime = Convert.ToDateTime(e.TimeStampBefore);
            DateTime leavedTime = Convert.ToDateTime(e.TimeStamp);
            int TimeDiff = e.TagKeepTime;
            //int targetTime = 0;
            string isPass = "";

            int QualityMark = 0;
            string tagValue = "0";
            switch (e.TagValue.ToString().ToLower())
            {
                case "1":
                case "true":
                    tagValue = "1";
                    break;
                default:
                    tagValue = "0";
                    break;
            }



            int EngineTypeID = -1;//机型代码
            int PartPalletCode = -1;//托盘编号
            string EngineType = "-1";//机型
            string EngineID = "-1";//工件编号
            //if (hashtable_EngineGetOver_EngineTypeID.Contains(opName))
            //{
            //    EngineTypeID = Convert.ToInt32( hashtable_EngineGetOver_EngineTypeID[opName]);
            //}
            //if (hashtable_EngineGetOver_EngineType.Contains(opName))
            //{
            //    EngineType = hashtable_EngineGetOver_EngineType[opName].ToString();
            //}
            //if (hashtable_EngineGetOver_EngineID.Contains(opName))
            //{
            //    EngineID = hashtable_EngineGetOver_EngineID[opName].ToString();
            //}
            //if (hashtable_PartPalletCode.Contains(opName))
            //{
            //    PartPalletCode = Convert.ToInt32(hashtable_PartPalletCode[opName]);
            //}
            //int EngineTypeID;//机型代码
            //int PartPallet_Code;//托盘编号
            //string EngineType;//机型
            //string EngineID;//工件编号
            string OrderForm;
            MIS_BASE.GetEngineTypeFromPLC(opName, out EngineTypeID, out EngineType, out EngineID, out int PartPallet_Code, out OrderForm);

            //MIS_BASE.GetEngineTypeFromPLC(opName, out EngineTypeID, out EngineType, out EngineID, out PartPallet_Code);

            //EngineData Enginedata = (EngineData)EngineData_Hash_Read(opName);
            //WorkStartInsert_AutoWorkStation(opName, EngineID, PartPalletCode, arrivedTime, leavedTime, TimeDiff, targetTime, null, isPass, 0);

            if (tagValue == "0")
            {
                QualityMark = Convert.ToInt32(MIS_BASE.ReadPLC(21, opName));

                if (QualityMark == 1)
                {
                    isPass = "1";
                }
                else
                {
                    isPass = "0";
                }

                WorkStartInsert_V2022(opName, arrivedTime, leavedTime, TimeDiff, isPass);

                MIS_BASE.Write_TagTypeID(5, opName, false);//MES传递机型
                MIS_BASE.Write_TagTypeID(66, opName, false);//允许工作
                MIS_BASE.Write_TagTypeID(51, opName, false);//合格放行
                MIS_BASE.Write_TagTypeID(8, opName, false);//不合格放行
                MIS_BASE.Write_TagTypeID(7, opName, false);//工作完成
                MIS_BASE.Write_TagTypeID(20, opName, false);//保存完成

            }

            //通知WEB更新产量
            SendMessage.SendActuallyCount(opName, 0);
        }

        //}
        /// <summary>
        /// 基本数据_工作日历_班次产量查询_New
        /// </summary>
        /// <param name="opName"></param>
        /// <param name="opNameCount">在线产量</param>
        /// <param name="opNameCountTime">在线节拍</param>
        static public void OpNameCount_New(string opName, out int opNameCount, out int opNameCountTime)
        {
            opNameCount = 0;
            opNameCountTime = 0;
            string procedureName;
            procedureName = "基本数据_工作日历_班次产量查询_New";
            SqlParameter[] thisParms = new SqlParameter[3];
            thisParms[0] = new System.Data.SqlClient.SqlParameter("@工位号", opName);
            thisParms[1] = new System.Data.SqlClient.SqlParameter("@在线产量", 0);
            thisParms[1].Direction = ParameterDirection.Output;
            thisParms[2] = new System.Data.SqlClient.SqlParameter("@在线节拍", 0);
            thisParms[2].Direction = ParameterDirection.Output;
            if (DataAccess.ExecuteStoredProcedure(procedureName, ref thisParms))
            {
                opNameCount = Convert.ToInt32(thisParms[1].Value);
                opNameCountTime = Convert.ToInt32(thisParms[2].Value);
            }
        }
        /// <summary>
        /// 测量数据发动机在线状态_增加数据_自动工位
        /// </summary>
        /// <param name="opName"></param>
        /// <param name="EngineID"></param>
        /// <param name="Pallet"></param>
        /// <param name="arrivedTime"></param>
        /// <param name="leavedTime"></param>
        /// <param name="TimeDiff"></param>
        /// <param name="Target"></param>
        /// <param name="lineType"></param>
        /// <param name="isPass"></param>
        /// <param name="OpNameUpDownType"></param>
        static public void WorkStartInsert_AutoWorkStation(string opName, string EngineID, int Pallet, DateTime arrivedTime, DateTime leavedTime, int TimeDiff, int Target, string lineType, string isPass, int OpNameUpDownType)
        {
            string procedureName;
            procedureName = "测量数据发动机在线状态_增加数据_自动工位";
            SqlParameter[] thisParms = new SqlParameter[10];
            thisParms[0] = new System.Data.SqlClient.SqlParameter("@工位号", opName);
            thisParms[1] = new System.Data.SqlClient.SqlParameter("@发动机号", EngineID);
            thisParms[2] = new System.Data.SqlClient.SqlParameter("@托盘编号", Pallet);
            thisParms[3] = new System.Data.SqlClient.SqlParameter("@到达时间", arrivedTime);
            thisParms[4] = new System.Data.SqlClient.SqlParameter("@离开时间", leavedTime);
            thisParms[5] = new System.Data.SqlClient.SqlParameter("@在线时间", TimeDiff);
            thisParms[6] = new System.Data.SqlClient.SqlParameter("@目标时间", Target);
            thisParms[7] = new System.Data.SqlClient.SqlParameter("@线体类型", lineType);
            thisParms[8] = new System.Data.SqlClient.SqlParameter("@是否合格", isPass);
            thisParms[9] = new System.Data.SqlClient.SqlParameter("@工位上下线类型", OpNameUpDownType);
            DataAccess.ExecuteStoredProcedure(procedureName, ref thisParms);
        }

        static public void WorkStartInsert_V2022(string opName, DateTime arrivedTime, DateTime leavedTime, int TimeDiff, string isPass)
        {
            string procedureName;
            procedureName = "测量数据发动机在线状态_增加数据_V2022";
            SqlParameter[] thisParms = new SqlParameter[5];
            thisParms[0] = new System.Data.SqlClient.SqlParameter("@工位号", opName);
            thisParms[1] = new System.Data.SqlClient.SqlParameter("@到达时间", arrivedTime);
            thisParms[2] = new System.Data.SqlClient.SqlParameter("@离开时间", leavedTime);
            thisParms[3] = new System.Data.SqlClient.SqlParameter("@在线时间", TimeDiff);
            thisParms[4] = new System.Data.SqlClient.SqlParameter("@是否合格", isPass);
            DataAccess.ExecuteStoredProcedure(procedureName, ref thisParms);
        }

        static public void WorkStartInsert(string opName, DateTime arrivedTime, DateTime leavedTime, int TimeDiff, int Target, string lineType, string isPass, int OpNameUpDownType)
        {
            string procedureName;
            procedureName = "测量数据发动机在线状态_增加数据";
            SqlParameter[] thisParms = new SqlParameter[8];
            thisParms[0] = new System.Data.SqlClient.SqlParameter("@工位号", opName);
            thisParms[1] = new System.Data.SqlClient.SqlParameter("@到达时间", arrivedTime);
            thisParms[2] = new System.Data.SqlClient.SqlParameter("@离开时间", leavedTime);
            thisParms[3] = new System.Data.SqlClient.SqlParameter("@在线时间", TimeDiff);
            thisParms[4] = new System.Data.SqlClient.SqlParameter("@目标时间", Target);
            thisParms[5] = new System.Data.SqlClient.SqlParameter("@线体类型", lineType);
            thisParms[6] = new System.Data.SqlClient.SqlParameter("@是否合格", isPass);
            thisParms[7] = new System.Data.SqlClient.SqlParameter("@工位上下线类型", OpNameUpDownType);
            DataAccess.ExecuteStoredProcedure(procedureName, ref thisParms);
        }
    }
}
