//using BasicData;
using bizFacade;
//using DataBaseAccess;
//using bizFacade;
using DataLinkMesWork;
using MesServerWork;
using System;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
//using SystemFramework;

namespace MisDataFunction
{
    partial class Function
    {
        /// <summary>
        /// 工件到位与离开时，清空MES自身信号
        /// </summary>
        /// <param name="OpName"></param>
        /// <param name="tagValue"></param>
        private static void WorkStart(string opName, string tagValue)
        {
            try
            {
                //工位没有工件，或工件刚刚离开  工件离开工位后，清空该工位的发动机号
                if (tagValue == "0")
                {
                    Write_TagTypeID(5, opName, false);//MES传递机型
                    Write_TagTypeID(66, opName, false);//允许工作
                    Write_TagTypeID(51, opName, false);//合格放行
                    Write_TagTypeID(8, opName, false);//不合格放行
                    Write_TagTypeID(7, opName, false);//工作完成
                    Write_TagTypeID(20, opName, false);//保存完成
          
                    //string isHaveComputer = hashtableOpName_IsHaveComputer[opName].ToString();
                    //if (Convert.ToInt32(isHaveComputer) == 0)//自动工位 读合格标志
                    {
                        if (IsEngTypeGetOver_Read(opName))
                        {
                            //PLC未传递机型 不保存过站信息  
                            SaveProductLeaveData_AutoWorkStation(opName, Convert.ToInt32(ReadPLC(21, opName)));
                            //SaveProductLeaveData_AutoWorkStation(opName, Convert.ToInt32(MesDataFunction.OpcOperation.ReadPLC(21, opName)));
                        }
                       
                    }
                 
                    //清空信号
                    //m_WorkStart_OpName[opName] = 0;//清空工件到位标志
                    //通知WEB更新产量
                    SendActuallyCount(opName,0);
                }
                else
                {
                    //保存工件到达数据
                    //m_WorkStart_OpName_Time[opName] = DateTime.Now;
                    //m_WorkStart_OpName[opName] = 1;
                }

            }
            catch (Exception err)
            {
                ////ApplicationLog.WriteLog(err, err.Message);

            }
        }
        /// <summary>
        /// 保存过站信息 （数据库读机型信息）
        /// </summary>
        /// <param name="opName"></param>
        /// <param name="QualityMark"></param>
        static void SaveProductLeaveData(string opName, int QualityMark)
        {
            try
            {
                if (!IsEngTypeGetOver_Read(opName))
                {
                    //PLC未传递机型 不保存过站信息
                    return;
                }
                IsEngTypeGetOver_Write(opName, false);//清空传递机型标志

                DateTime arrivedTime;
                DateTime leavedTime;
                int TimeDiff;
                int targetTime = 0;
                string lineType = "";
                string isPass;
                if (QualityMark == 1)
                {
                    isPass = "PASS";
                }
                else
                {
                    isPass = "FAIL";
                }
                //工位上下线类型 
                //（0普通工位）（1正常上线）（2正常下线）（3返修上线）（4返修下线）
                int OpNameUpDownType;
                OpNameUpDownType = Convert.ToInt32(hashtableOpName_OpType[opName]);
                arrivedTime = (DateTime)m_WorkStart_OpName_Time[opName];
                leavedTime = DateTime.Now;
                TimeDiff = Convert.ToInt32(leavedTime.Subtract(arrivedTime).TotalSeconds);
                lineType = hashtableOpName_LineType[opName].ToString();

                //1->0 工件离开 工件刚刚离开
                //if (Convert.ToInt32(m_WorkStart_OpName[opName]) == 1)
                //{
                //    WorkStartInsert(opName, arrivedTime, leavedTime, TimeDiff, targetTime, lineType, isPass, OpNameUpDownType);
                //}
                //通知WEB更新产量
                SendActuallyCount(opName, 0);
            }
            catch (Exception err)
            {
                ////ApplicationLog.WriteLog(err, err.Message);

            }


        }
        /// <summary>
        /// 保存过站信息 （数据库读机型信息）
        /// </summary>
        /// <param name="opName"></param>
        /// <param name="QualityMark"></param>
        static void SaveProductLeaveData_AutoWorkStation(string opName, int QualityMark)
        {
            try
            {
                if (!IsEngTypeGetOver_Read(opName))
                {
                    //PLC未传递机型 不保存过站信息
                    return;
                }
                IsEngTypeGetOver_Write(opName, false);//清空传递机型标志

                DateTime arrivedTime;
                DateTime leavedTime;
                int TimeDiff;
                int targetTime = 0;
                string lineType = "";
                string isPass;
                if (QualityMark == 1)
                {
                    isPass = "PASS";
                }
                else
                {
                    isPass = "FAIL";
                }
                //工位上下线类型 
                //（0普通工位）（1正常上线）（2正常下线）（3返修上线）（4返修下线）
                int OpNameUpDownType;
                //OpNameUpDownType = Convert.ToInt32(hashtableOpName_OpType[opName]);
               // arrivedTime = (DateTime)m_WorkStart_OpName_Time[opName];
                leavedTime = DateTime.Now;
                //TimeDiff = Convert.ToInt32(leavedTime.Subtract(arrivedTime).TotalSeconds);
                //lineType = hashtableOpName_LineType[opName].ToString();

                //1->0 工件离开 工件刚刚离开
                //if (Convert.ToInt32(m_WorkStart_OpName[opName]) == 1)
                //{
                //    //查询工件编号、托盘号
                //EngineData Enginedata = (EngineData)EngineData_Hash_Read(opName);
                //WorkStartInsert_AutoWorkStation(opName, Enginedata.EngineID, Enginedata.Pallet, arrivedTime, leavedTime, TimeDiff, targetTime, lineType, isPass, OpNameUpDownType);
                //}

            }
            catch (Exception err)
            {
                ////ApplicationLog.WriteLog(err, err.Message);

            }


        }

        /// <summary>
        /// 测量数据发动机在线状态_增加数据
        /// </summary>
        /// <param name="opName"></param>
        /// <param name="arrivedTime"></param>
        /// <param name="leavedTime"></param>
        /// <param name="TimeDiff"></param>
        /// <param name="Target"></param>
        /// <param name="isPass"></param>
        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);
        }



        /// <summary>
        /// 发送在线产量、在线节拍
        /// </summary>
        /// <param name="OpName"></param>
        /// <param name="tagValue"></param>
        static  void SendActuallyCount(string OpName, int tagValue)
        {
            //当工件离开时，更新在线产量、在线节拍
            if (tagValue == 0)
            {
                try
                {
                    int opNameCount = 0;//在线产量
                    int opNameCountTime = 0;//在线节拍
                                            //基本数据_工作日历_班次产量查询_New
                    BaseDataSystemMES.OpNameCount_New(OpName, out opNameCount, out opNameCountTime);

                    string SendMessage;
                    SendMessage = "MES|ActuallyCount|" + OpName + "|" + opNameCount.ToString() + "&" + opNameCountTime.ToString();
                 
                    MIS_BASE.asyncSendMessage(SendMessage);
                }
                catch (Exception err)
                {
                    ////ApplicationLog.WriteLog(err, "Function01:SendActuallyCount");
                }
            }
        }

    }
}
