
using System;
using System.Data;
using System.Data.SqlClient;

namespace MesWork
{
    /// <summary>
    /// 
    /// </summary>
    public class B_DB_Opera
    {

        public static void Event_WorkStart(string opName, int tagTypeCodeID, string partType, string tagID)
        {
            var ChangeTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
            MesWorkForm.taskSql.Enqueue(new NJ_ZC.ParamT
            {
                procedureName = "Event_WorkStart",
                sqlParameter = new SqlParameter[] {
                 new SqlParameter("@OpName", opName),
                 new SqlParameter("@TagTypeCodeID", tagTypeCodeID),
                 new SqlParameter("@PartType", partType),
                 new SqlParameter("@ChangeTime",ChangeTime),
                 new SqlParameter("@TagID", tagID)
                }
            });

        }

        public static void Event_WorkEnd(string opName, int tagTypeCodeID, string partType
            , string barCode, int qualityMask, int outCount, int duration
            , string tagID)
        {
            /*
@OpName nvarchar(50) = null,
@TagTypeCodeID int = 0,
@PartType int = 0,
@BarCode nvarchar(2048) = null,
@QualityMask int = 0,
@OutCount int = 0,
@Duration int = 0,
@ChangeTime datetime = null,
@TagID nvarchar(200) = null
             */

            var ChangeTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
            MesWorkForm.taskSql.Enqueue(new NJ_ZC.ParamT
            {
                procedureName = "Event_WorkEnd",
                sqlParameter = new SqlParameter[] {
                 new SqlParameter("@OpName", opName),
                 new SqlParameter("@TagTypeCodeID", tagTypeCodeID),
                 new SqlParameter("@PartType", partType),
                 new SqlParameter("@BarCode", barCode),
                 new SqlParameter("@QualityMask", qualityMask),
                 new SqlParameter("@OutCount", outCount),
                 new SqlParameter("@Duration", duration),
                 new SqlParameter("@ChangeTime",ChangeTime),
                 new SqlParameter("@TagID", tagID)
                }
            });
        }


       


        public static void Event_Signal_Log(string opName, int tagTypeCodeID, string value, string tagID)
        {
            var ChangeTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
            MesWorkForm.taskSql.Enqueue(new NJ_ZC.ParamT
            {
                procedureName = "Event_Signal_Log",
                sqlParameter = new SqlParameter[] {
                 new SqlParameter("@OpName", opName),
                 new SqlParameter("@TagTypeCodeID", tagTypeCodeID),
                 new SqlParameter("@Value", value),
                 new SqlParameter("@ChangeTime",ChangeTime),
                 new SqlParameter("@TagID", tagID)
                }
            });
        }


        public static void Event_Alarm_Start(string opName, int tagTypeCodeID, string alarmText, string tagID)
        {
            var ChangeTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
            MesWorkForm.taskSql.Enqueue(new NJ_ZC.ParamT
            {
                procedureName = "Event_Alarm_Start",
                sqlParameter = new SqlParameter[] {
                 new SqlParameter("@OpName", opName),
                 new SqlParameter("@TagTypeCodeID", tagTypeCodeID),
                 new SqlParameter("@AlarmText", alarmText),
                 new SqlParameter("@ChangeTime",ChangeTime),
                 new SqlParameter("@TagID", tagID)
                }
            });

        }


        public static void Event_Alarm_End(string opName, int tagTypeCodeID, string alarmText, string tagID)
        {
            var ChangeTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
            MesWorkForm.taskSql.Enqueue(new NJ_ZC.ParamT
            {
                procedureName = "Event_Alarm_End",
                sqlParameter = new SqlParameter[] {
                 new SqlParameter("@OpName", opName),
                 new SqlParameter("@TagTypeCodeID", tagTypeCodeID),
                 new SqlParameter("@AlarmText", alarmText),
                 new SqlParameter("@ChangeTime",ChangeTime),
                 new SqlParameter("@TagID", tagID)
                }
            });

        }

        public static void Event_AlarmAndTip_Start(string opName, int tagTypeCodeID, string alarmText, string tagID,int Alarm1_Tip2)
        {
            var ChangeTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
            MesWorkForm.taskSql.Enqueue(new NJ_ZC.ParamT
            {
                procedureName = "Event_AlarmAndTip_Start",
                sqlParameter = new SqlParameter[] {
                 new SqlParameter("@OpName", opName),
                 new SqlParameter("@TagTypeCodeID", tagTypeCodeID),
                 new SqlParameter("@AlarmText", alarmText),
                 new SqlParameter("@ChangeTime",ChangeTime),
                 new SqlParameter("@TagID", tagID),
                 new SqlParameter("@Alarm1_Tip2", Alarm1_Tip2),
                }
            });

        }


        public static void MES_StatusChange(string opName, int StatusCode, string TriggerTime)
        {
            SqlParameter[] thisParms = new SqlParameter[3];
            thisParms[0] = new SqlParameter("@opName", opName);
            thisParms[1] = new SqlParameter("@StatusCode", StatusCode);
            thisParms[2] = new SqlParameter("@TriggerTime", TriggerTime);

            MesWorkForm.taskSql.Enqueue(new NJ_ZC.ParamT
            {
                procedureName = "设备管理_状态变化新增",
                sqlParameter = thisParms
            });
        }

        public static void ClearLog()
        {
            MesWorkForm.taskSql.Enqueue(new NJ_ZC.ParamT
            {
                procedureName = "Event_Delete_Station_记录_日志_运行日志",
                sqlParameter = new SqlParameter[] {
                 new SqlParameter("@工位号", "DDV")
                }
            });
        }

        public static void Exec_DispatchSql()
        {
            MesWorkForm.taskSql.Enqueue(new NJ_ZC.ParamT
            {
                procedureName = "Event_Delete_部分历史数据",
                sqlParameter = new SqlParameter[] {
                }
            });
        }



        public static void SelectData_WritePLC()
        {
            var procedureName = "TV_PLC写入_查询";
            var sqlParameter = new SqlParameter[] { };

            DataLinkMesWork.SQLCommon.ExecuteStoredProcedure(procedureName, MesWorkForm.ConnectionString, ref sqlParameter,out DataTable dt, out string errorMessage);
            if (dt.Rows.Count > 0)
            {
                //select isnull(@前悬基准时间,0) as 前悬基准时间,isnull(@后悬基准时间,0) as 后悬基准时间,
                //isnull(@前悬应到人数, 0) as 前悬应到人数,isnull(@后悬应到人数, 0) as 后悬应到人数
                var 前悬基准时间_79 = dt.Rows[0]["前悬基准时间"].ToString();
                var 前悬应到人数_77 = dt.Rows[0]["前悬应到人数"].ToString();
                var 前悬抽检频率_83 = dt.Rows[0]["前悬抽检频率"].ToString();

                var 后悬基准时间_80 = dt.Rows[0]["后悬基准时间"].ToString();
                var 后悬应到人数_78 = dt.Rows[0]["后悬应到人数"].ToString();
                var 后悬抽检频率_84 = dt.Rows[0]["后悬抽检频率"].ToString();

                var opName_QX = "OP1010";
                MesWorkForm.WritePLC(79, opName_QX, 前悬基准时间_79);
                MesWorkForm.WritePLC(77, opName_QX, 前悬应到人数_77);
                MesWorkForm.WritePLC(83, opName_QX, 前悬抽检频率_83);
                var opName_HX = "OP1020";
                MesWorkForm.WritePLC(80, opName_HX, 后悬基准时间_80);
                MesWorkForm.WritePLC(78, opName_HX, 后悬应到人数_78);
                MesWorkForm.WritePLC(84, opName_HX, 后悬抽检频率_84);

            }
        }


        public static bool BaseData_tagTable_QualityData_B5A_IndexInsert(string EngineID, string tagID, string EngineType, int EngineTypeID, string opName, int isGoodBad, string PartPalletCode, out int Id_tagCF)
        {
            string procedureName;
            procedureName = "CATL_测量数据合格索引_增加_加注设备";
            Id_tagCF = 0;
            SqlParameter[] thisParms = new SqlParameter[4];
            thisParms[0] = new System.Data.SqlClient.SqlParameter("@TagID_CF", tagID);
            thisParms[1] = new System.Data.SqlClient.SqlParameter("@工位号", opName);
            thisParms[2] = new System.Data.SqlClient.SqlParameter("@EngineID", EngineID);
            thisParms[3] = new System.Data.SqlClient.SqlParameter("@Id_tagCF", Id_tagCF);
            thisParms[3].Direction = ParameterDirection.Output;

            if (DataLinkMesWork.SQLCommon.ExecuteStoredProcedure(procedureName, MesWorkForm.ConnectionString, ref thisParms, out string errorMessage))
            {
                Id_tagCF = Convert.ToInt32(thisParms[3].Value.ToString());
                return true;
            }
            return false;
        }
        public static bool BaseData_tagTable_QualityData_B5A_Insert(string EngineID, int Id_tagCF, string opName, DataTable dt, bool IsCheck = false)
        {
            string procedureName;
            procedureName = "CATL_测量数据合格_增加_加注设备";
            SqlParameter[] thisParms = new SqlParameter[7];
            thisParms[0] = new System.Data.SqlClient.SqlParameter("@Id_tagCF", Id_tagCF);
            thisParms[1] = new System.Data.SqlClient.SqlParameter("@TagID", SqlDbType.NVarChar, 100, "TagID");
            thisParms[2] = new System.Data.SqlClient.SqlParameter("@TagValue", SqlDbType.NVarChar, 100, "TagValue");
            thisParms[3] = new System.Data.SqlClient.SqlParameter("@IsGoodBad", SqlDbType.Int, 4, "IsGoodBad");
            thisParms[4] = new System.Data.SqlClient.SqlParameter("@EngineID", EngineID);
            thisParms[5] = new System.Data.SqlClient.SqlParameter("@上限值", SqlDbType.NVarChar, 100, "上限值");
            thisParms[6] = new System.Data.SqlClient.SqlParameter("@下限值", SqlDbType.NVarChar, 100, "下限值");

            return DataLinkMesWork.SQLCommon.ExecuteStoredProcedure(procedureName, MesWorkForm.ConnectionString, ref thisParms, dt, out string errorMessage);
        }

        public static bool BaseData_tagTable_CharData_Insert(string vin, string testCode, string testStatus, string testBeginTime, string testEndTime, string faultInfo, string syncTime, out string errorMessage)
        {
            string procedureName;
            procedureName = "CATL_充电数据_增加";
            SqlParameter[] thisParms = new SqlParameter[7];
            thisParms[0] = new System.Data.SqlClient.SqlParameter("@vin", vin);
            thisParms[1] = new System.Data.SqlClient.SqlParameter("@testCode", testCode);
            thisParms[2] = new System.Data.SqlClient.SqlParameter("@testStatus", testStatus);
            thisParms[3] = new System.Data.SqlClient.SqlParameter("@testBeginTime", testBeginTime);
            thisParms[4] = new System.Data.SqlClient.SqlParameter("@testEndTime", testEndTime);
            thisParms[5] = new System.Data.SqlClient.SqlParameter("@faultInfo", faultInfo);
            thisParms[6] = new System.Data.SqlClient.SqlParameter("@同步时间", syncTime);

            return DataLinkMesWork.SQLCommon.ExecuteStoredProcedure(procedureName, MesWorkForm.ConnectionString, ref thisParms, out errorMessage);
        }

        public static bool GetQualityDataCount(string EngineID, string opName, out DataTable dt)
        {
            dt = new DataTable();
            string procedureName;
            procedureName = "CATL_测量数据合格_查询测试次数";
            SqlParameter[] thisParms = new SqlParameter[2];
            thisParms[0] = new System.Data.SqlClient.SqlParameter("@EngineID", EngineID);
            thisParms[1] = new System.Data.SqlClient.SqlParameter("@工位号", opName);

            return DataLinkMesWork.SQLCommon.ExecuteStoredProcedure(procedureName, MesWorkForm.ConnectionString, ref thisParms, out dt, out string errorMessage);

        }
        
    }




}
