using System;
using MesServerWork;
using System.Collections;
using System.Data;
using MisDataFunDll;

namespace MesServerFunctions
{
    partial class FunctionMES
    {
        /// <summary>
        /// 根据组号获得拧紧数据
        /// </summary>
        /// <param name="workGroup"></param>
        private int GetTagValue(string opName, int engineTypeID, string engineType, string engineID, int ProcessCode, int PartPallet_Code)
        {
            //合格标志
            int groupValueIsOK;
            //组号
            int workGroup_Index;
            //序号
            int sn_ShaftStep;
            //工作顺序(拧紧顺序)
            int sn_WorkingStep;

            DataTable dt_ShaftPosition_WorkingStep;

            groupValueIsOK = 2;
            ShaftPosition.ShaftPosition_Select_WorkingStep(opName, engineTypeID, out dt_ShaftPosition_WorkingStep);

            if (dt_ShaftPosition_WorkingStep != null)
            {
                //如果是workGroup=1,则第一次需要禁用索引
                if (ProcessCode <= 1)
                {
                    //不能delete 必须用update 
                    MisDataFun.BaseData_tagTable_QualityData_B5A_SubTable_Delete(engineID, opName);
                }

                for (int i = 0; i < dt_ShaftPosition_WorkingStep.Rows.Count; i++)
                {
                    workGroup_Index = Convert.ToInt32(dt_ShaftPosition_WorkingStep.Rows[i]["组号"]);
                    if (workGroup_Index == ProcessCode)
                    {
                        sn_ShaftStep = Convert.ToInt32(dt_ShaftPosition_WorkingStep.Rows[i]["序号"]);
                        sn_WorkingStep = Convert.ToInt32(dt_ShaftPosition_WorkingStep.Rows[i]["工作顺序"]);   //没有其他使用
                                                                                                          //保存拧紧数据
                        if (GetTagValue_DCT(opName, engineTypeID, engineType, engineID, ProcessCode, PartPallet_Code) == 1)
                        {
                            groupValueIsOK = 1;
                            //显示绿色拧紧轴
                        }
                        else
                        {
                            groupValueIsOK = 2;
                            //显示红色拧紧轴
                        }
                    }
                }
            }
            return groupValueIsOK;
        }

        /// <summary>
        /// 根据序号,工作顺序获得测量值;将数据保存到数据库的临时表中。
        /// </summary>
        /// <param name="opName">工位号</param>
        /// <param name="engineTypeID">发动机机型代码</param>
        /// <param name="engineTypeID">发动机机型</param>
        /// <param name="engineID">当前发动机号</param>
        /// <param name="sn_ShaftStep">拧紧序号</param>
        private int GetTagValue_DCT(string opName, int engineTypeID, string engineType, string engineID, int sn_ShaftStep, int PartPallet_Code)
        {
            int groupValueIsOK;
            string tagValue1;
            string tagValue2;
            int isOK = 1;
            //合格标志的TagID
            int tagID_isOK1;
            int tagID_isOK2;

            //TagID
            int tagID;
            int tagID1;
            int tagID2;

            string MaxValue1;
            string MaxValue2;
            string MinValue1;
            string MinValue2;

            //根据工位号获取哈西表
            Hashtable valueList;


            MIS_BASE.ReadPLC_Sync_ValueList(opName, out valueList);


            GetTagIDBytagOrder(valueList, sn_ShaftStep, out tagID, out tagID1, out tagID2,
                out tagValue1, out tagValue2, out tagID_isOK1, out tagID_isOK2, out MaxValue1, out MaxValue2, out MinValue1, out MinValue2);

            if (tagID == -1 || tagID1 == -1)
            {
                return 0;
            }

            GetPressData_Save(opName, engineTypeID, engineType, engineID,
                 sn_ShaftStep, tagID, tagID1, tagID2, tagValue1, tagValue2, tagID_isOK1, tagID_isOK2, PartPallet_Code, MaxValue1, MaxValue2, MinValue1, MinValue2, sn_ShaftStep);

            if (tagID_isOK1 != 1) isOK = tagID_isOK1;
            if (tagID_isOK2 != 1) isOK = tagID_isOK2;
            if (isOK == -1)
            {
                isOK = 0;
            }
            groupValueIsOK = isOK;
            return groupValueIsOK;
        }


        /// <summary>
        /// 根据列位置获取三个TagID(合格标志，扭矩，转角),以及对应的值
        /// </summary>
        /// <param name="valueList"></param>
        /// <param name="sn_ShaftStep"></param>
        /// <param name="tagID"></param>
        /// <param name="tagID1"></param>
        /// <param name="tagID2"></param>
        /// <param name="tavIDValue1"></param>
        /// <param name="tavIDValue2"></param>
        /// <param name="isGoodBad"></param>
        private void GetTagIDBytagOrder(Hashtable valueList, int sn_ShaftStep,
             out int tagID, out int tagID1, out int tagID2,
             out string tavIDValue1, out string tavIDValue2, out int isGoodBad1, out int isGoodBad2, out string MaxValue1, out string MaxValue2, out string MinValue1, out string MinValue2)
        {
            int[] boolTemp = new int[32];//int数组存储轴类合格标志
            bool IsGetgoodOrBad = false;//是否根据8203或者8209获取到合格标志
                                        //初始化赋值
            tagID = -1;
            tagID1 = -1;
            tagID2 = -1;

            tavIDValue1 = "";
            tavIDValue2 = "";
            MaxValue1 = "";
            MaxValue2 = "";
            MinValue1 = "";
            MinValue2 = "";
            //合格标志的TagID
            isGoodBad1 = 0;
            isGoodBad2 = 0;

            DeviceDriver_BasicData.CustomeEvetnArgs qualityData;

            int tagid;//TagID
            int tagOrderBy;//列位置
            int tagItemOrderBy;//变量排序
            int tagTypeID;//TagID特点
            int tagProperty;//变量特点（6,7）
            object tagValue;//值
            string tagValue_1 = "0";//值

            //根据当前组号获取对应的质量数据
            foreach (DictionaryEntry valueItem in valueList) //ht为一个Hashtable实例 
            {
                try
                {
                    qualityData = (DeviceDriver_BasicData.CustomeEvetnArgs)valueItem.Value;
                    tagProperty = Convert.ToInt32(qualityData.TagProperty);
                    tagid = Convert.ToInt32(qualityData.TagID);
                    tagTypeID = Convert.ToInt32(qualityData.TagTypeID);
                    tagValue = qualityData.TagValue;
                    tagOrderBy = Convert.ToInt32(qualityData.TagOrderBy);
                    tagItemOrderBy = Convert.ToInt32(qualityData.TagItemOrderBy);

                    //若为空，则转变为0
                    if (tagValue == null)
                    {
                        tagValue = 0;
                    }
                    //读取数据
                    switch (tagTypeID)
                    {
                        //轴合格标志
                        case 8209:

                            break;
                        //轴合格标志
                        case 8203:
                            IsGetgoodOrBad = true;
                            try
                            {
                                boolTemp = (int[])tagValue;
                                tagID = tagid;
                                isGoodBad1 = Convert.ToInt32(boolTemp[sn_ShaftStep - 1]);
                                //转换一下
                                if (isGoodBad1 == -1)
                                {
                                    isGoodBad1 = 0;
                                }
                            }
                            catch
                            {
                                tagID = -1;
                                isGoodBad1 = 0;
                            }
                            break;
                        //非标准REAL
                        case 8210:
                            tagValue_1 = GetNoNormalReal(tagValue);
                            break;
                        default:
                            tagValue_1 = Convert.ToString(tagValue);
                            break;
                    }
                    //单个数据进行读取
                    if (tagOrderBy == 1)
                    {
                        //获取到了当前的列位置（1,2,3,4）
                        //依次再获取合格标志，扭矩，转角
                        switch (tagItemOrderBy)
                        {
                            case 0://合格标志
                                if (!IsGetgoodOrBad)
                                {
                                    tagID = tagid;
                                    try
                                    {
                                        isGoodBad1 = Convert.ToInt32(tagValue_1);
                                        //转换一下
                                        if (isGoodBad1 == -1)
                                        {
                                            isGoodBad1 = 0;
                                        }
                                    }
                                    catch
                                    {
                                        isGoodBad1 = 0;
                                    }
                                }
                                break;
                            case 1://扭矩值
                                tagID1 = tagid;
                                tavIDValue1 = tagValue_1;
                                break;
                            case 2://上限值1
                                MaxValue1 = tagValue_1;
                                break;
                            case 3://下限值1
                                MinValue1 = tagValue_1;
                                break;
                        }
                    }
                    if (tagOrderBy == 2)
                    {
                        //获取到了当前的列位置（1,2,3,4）
                        //依次再获取合格标志，扭矩，转角
                        switch (tagItemOrderBy)
                        {
                            case 0://合格标志
                                if (!IsGetgoodOrBad)
                                {
                                    tagID = tagid + (sn_ShaftStep * 4);
                                    try
                                    {
                                        isGoodBad2 = Convert.ToInt32(tagValue_1);
                                        //转换一下
                                        if (isGoodBad2 == -1)
                                        {
                                            isGoodBad2 = 0;
                                        }
                                    }
                                    catch
                                    {
                                        isGoodBad2 = 0;
                                    }
                                }
                                break;
                            case 1://角度值
                                tagID2 = tagid;
                                tavIDValue2 = tagValue_1;
                                break;
                            case 2://上限值2
                                MaxValue2 = tagValue_1;
                                break;
                            case 3://下限值2
                                MinValue2 = tagValue_1;
                                break;
                        }
                    }
                }
                catch
                {
                    tagID = -1;
                    tagID1 = -1;
                    tagID2 = -1;

                    tavIDValue1 = "";
                    tavIDValue2 = "";
                    MaxValue1 = "";
                    MaxValue2 = "";
                    MinValue1 = "";
                    MinValue2 = "";
                    //合格标志的TagID
                    isGoodBad1 = 0;
                    isGoodBad2 = 0;

                }
            }
        }


        /// <summary>
        /// 列位置（序号）对应的数据
        /// 保存数据到数据库
        /// </summary>
        /// <param name="opName"></param>
        /// <param name="engineTypeID"></param>
        /// <param name="engineType"></param>
        /// <param name="engineID"></param>
        /// <param name="tagOrderBy"></param>
        /// <param name="tagID"></param>
        /// <param name="tagID1"></param>
        /// <param name="tagID2"></param>
        /// <param name="tavIDValue1"></param>
        /// <param name="tavIDValue2"></param>
        /// <param name="isGoodBad"></param>
        private void GetPressData_Save(string opName, int engineTypeID, string engineType, string engineID,
            int tagOrderBy, int tagID, int tagID1, int tagID2, string tavIDValue1, string tavIDValue2, int isGoodBad1, int isGoodBad2, int PartPallet_Code, string MaxValue1, string MaxValue2, string MinValue1, string MinValue2, int sn_ShaftStep)
        {
            //保存数据到数据库
            //测量数据合格_手动拧紧_增加
            MisDataFun.BaseData_tagTable_QualityData_B5A_Insert
                                (engineTypeID, engineType, engineID,
                                tavIDValue1, isGoodBad1,
                                opName, PartPallet_Code.ToString(), tagID1, tagID1, MaxValue1, MinValue1, sn_ShaftStep, 1);
            //保存数据到数据库
            //测量数据合格_手动拧紧_增加
            MisDataFun.BaseData_tagTable_QualityData_B5A_Insert
                                (engineTypeID, engineType, engineID,
                                tavIDValue2, isGoodBad2,
                                opName, PartPallet_Code.ToString(), tagID2, tagID2, MaxValue2, MinValue2, sn_ShaftStep, 2);
        }

        /// <summary>
        /// 返回非标准real
        /// </summary>
        /// <param name="vDataValue"></param>
        /// <returns></returns>
        static private string GetNoNormalReal(object vDataValue)
        {
            try
            {
                string real_a;
                string real_b;
                string groupValueString;
                int length;

                //int real_b_Int;

                length = ((System.Array)(vDataValue)).Length;
                groupValueString = "";
                if (length != 2)
                    return "0.0";

                real_a = Convert.ToUInt16((((System.Array)(vDataValue)).GetValue(0))).ToString();
                real_b = Convert.ToUInt16((((System.Array)(vDataValue)).GetValue(1))).ToString("00");
                groupValueString = real_a + "." + real_b;
                return groupValueString;
            }
            catch
            {
                return "0.0";
            }
        }

    }
}
