using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DataLinkMesWork;
using System.Collections;

using SystemFramework;
using DoWhatPlc;
//using BasicData;
//using OpcData;

namespace bizFacade
{

    public partial class BaseDataSystemMES
    {

     
        /// <summary>
        /// 对应玉柴项目
        /// 20111101
        /// 质量数据的上限值，下限值，理论值来源于表[MES_基本数据_测量范围]
        /// 通过TagID和engineTypeID。如果engineTypeID=0，上限值，下限值，理论值=""
        /// 质量数据可以读数据 19
        /// 分散控制
        /// tagID_CF质量数据激发变量对应的质量数据列表
        /// </summary>
        /// <param name="valuList">tagID_CF质量数据激发变量对应的质量数据列表（Hashtable）</param>
        /// <param name="opName">质量数据列表</param>
        /// <param name="BasicDataTagValue">质量数据列表</param>
        /// <param name="BasicDataTagValueUpDown"></param>
        /// <param name="dt">质量数据列表</param>
        /// <param name="qualityMask">质量数据总成合格标志 21（ =0 数据合格） （= 1数据不合格）</param>
        /// <param name="engineTypeID">质量数据发动机机型标志 9</param>
        /// <param name="engineID">质量数据发动机号 10</param>
        /// <param name="engineType">质量数据发动机机型  11</param>
        /// <param name="partPallet"></param>
        /// <param name="engineTraceCode"></param>
        /// <param name="engineCap">缸盖号[TagValue2]</param>
        /// <param name="hashtable_EngineTypeID">发动机类型转换表</param>
        /// <param name="mes_MustTest">发动机类型转换表</param>
        /// <param name="onLineState">发动机类型转换表</param>
        /// <param name="partMoveDirection">发动机类型转换表</param>
        /// <param name="pressIsOK">发动机类型转换表</param>
        /// <param name="pressIsNoOK">发动机类型转换表</param>
        static public void GetQualityDataTable_B5A(Hashtable valuList, string opName, DataTable BasicDataTagValue,
            DataTable BasicDataTagValueUpDown,
            out DataTable dt,
            out int qualityMask,
            out int engineTypeID,
            out string engineID,
            out string engineType,out string partPallet, out string engineTraceCode, out string engineCap, Hashtable hashtable_EngineTypeID,
            out int mes_MustTest,out int onLineState,out int partMoveDirection,
            out int pressIsOK, out int pressIsNoOK)
        {
            //78 0：屏蔽MES；1：启用MES(必须MIS)
            //79 通过线体工位PC实现下达工位工作状态：工位有三种状态：在线、离线、屏蔽。在线;0正常状态，实现正常功能。1离线：2屏蔽。(必须MIS)
            //41 机体流向标志（1 BYTE）：0普通工位：1：上线（发动机上线）；2：下线（发动机下线）；(必须PLC)
            //51 返修工位，修改后的数据MES将数据写入PLC ，MES通知PLC数据修改完成。(YN)
            //8 只要返修，必须由mes发送此信号，plc接收此信号后才能放行
            //（暂时没用）用于有扳手拧紧工位。通知设备螺钉拧紧完成。PC已经将数据保存到数据库中。(B5A)
            mes_MustTest = 0;
            onLineState = 0;
            partMoveDirection = 0;
            pressIsOK = 0;
            pressIsNoOK = 0;
            QualityDataType qualityData;
            //是否有数据
            //bool isUsing;

            DataRow dr;
            DataRow[] dr_WWW;
            //MES_基本数据_测量范围
            DataRow[] dr_WWW_UpDown;

            dt = BuildDataTables_WWW();
            qualityMask = 0;
            engineTypeID = 0;
            engineID = "";
            engineType = "";
            engineTraceCode = "";
            engineCap = "";
            partPallet = "";
            //测量位置
            string shaftID;
            //测量项目
            string item;
            //测量单位
            string unit;
            //机体类型
            //原始数据中的机型代码
            int engineTypeID_Source;
            //上限值
            string tagValueUp;
            //下限值
            string tagValueDown;
            //理论值
            string tagValeNormal;
            //TagValue
            // 合格标志
            //string IsGoodBad;


            foreach (DictionaryEntry valueItem in valuList) //ht为一个Hashtable实例 
            {
                try
                {
                    qualityData = (QualityDataType)valueItem.Value;

                    switch (qualityData.tagValueType)
                    {
                        case 21:
                            qualityMask = Convert.ToInt32(qualityData.tagValue);
                            //转变一下
                            //qualityMask = transferIsOK_OpName_2(Convert.ToInt32(qualityData.tagValue));
                            break;
                        case 8:
                            //8 只要返修，必须由mes发送此信号，plc接收此信号后才能放行
                            //（暂时没用）用于有扳手拧紧工位。通知设备螺钉拧紧完成。PC已经将数据保存到数据库中。(B5A)
                            pressIsNoOK = Convert.ToInt32(qualityData.tagValue);
                            break;
                        //质量数据发动机机型标志 22(玉柴) 发动机机型标志 9 （有MOBY）需要换算成2的n次幂的形式，以便生成报表
                        case 9:
                            engineTypeID = Convert.ToInt32(qualityData.tagValue);
                            if (engineTypeID == 0)
                            {
                                engineTypeID = 1;
                            }
                            if (hashtable_EngineTypeID.ContainsKey(engineTypeID))
                            {
                                string engineTypeIDString = hashtable_EngineTypeID[engineTypeID].ToString();
                                engineTypeID = Convert.ToInt32(engineTypeIDString);
                                if (engineTypeID == 0)
                                {
                                    engineTypeID = 1;
                                }
                            }
                            break;
                        //发动机编号 23   10
                        //case 23:
                        case 10:
                            engineID = Convert.ToString(qualityData.tagValue);
                            break;
                        //质量数据发动机机型  24(玉柴) 11（有MOBY）
                        //case 24:
                        case 11:
                            engineType = Convert.ToString(qualityData.tagValue);
                            break;
                        case 41:
                            //41 机体流向标志（1 BYTE）：0普通工位：1：上线（发动机上线）；
                            //2：下线（发动机下线）；(必须PLC)
                            partMoveDirection = Convert.ToInt32(qualityData.tagValue);
                            break;
                        case 51:
                            //51 返修工位，修改后的数据MES将数据写入PLC ，MES通知PLC数据修改完成。(YN)
                            pressIsOK = Convert.ToInt32(qualityData.tagValue);
                            break;
                        //78 0：屏蔽MES；1：启用MES(必须MIS)
                        case 78:
                            mes_MustTest = Convert.ToInt32(qualityData.tagValue);
                            break;
                            //79 通过线体工位PC实现下达工位工作状态：工位有三种状态：在线、离线、屏蔽。在线：
                            //0正常状态，实现正常功能。1离线：2屏蔽。(必须MIS)
                        case 79:
                            onLineState = Convert.ToInt32(qualityData.tagValue);
                            break;
                        //托盘编号(B5A)
                        case 80:
                            partPallet = Convert.ToString(qualityData.tagValue);
                            break;
                    }
                    //if ((qualityData.tagProperty != 2) || (qualityData.tagProperty != 6))
                    //{
                    //    continue ;
                    //}
                    if ((qualityData.tagProperty == 2) || (qualityData.tagProperty == 6))
                    {
                        if (engineTypeID == 0)
                        {
                            engineTypeID = 1;
                        }                        
                        dr_WWW = BasicDataTagValue.Select("TagID = " + qualityData.tagID);
                        try
                        {
                            dr_WWW_UpDown = BasicDataTagValueUpDown.Select("TagID = " + qualityData.tagID);
                        }
                        catch { dr_WWW_UpDown = null; }


                        if (dr_WWW.Length > 0)
                        {
                            {
                                engineTypeID_Source = Convert.ToInt32(dr_WWW[0]["机体类型"]);
                                int temp;
                                temp = engineTypeID_Source & engineTypeID;
                                if (temp == 0)
                                {
                                    continue;
                                }
                                if (qualityData.isGoodBad == -1)
                                {
                                    continue;
                                }
                            }
                        }
                        else
                        {
                            continue;

                        }

                        try
                        {
                            shaftID = dr_WWW[0]["测量位置"].ToString();
                        }
                        catch
                        {
                            shaftID = "";
                        }
                        try
                        {
                            item = dr_WWW[0]["测量项目"].ToString();
                        }
                        catch
                        {
                            item = "";
                        }
                        try
                        {
                            unit = dr_WWW[0]["测量单位"].ToString();
                        }
                        catch
                        {
                            unit = "";
                        }
                        try
                        {
                            tagValueUp = "";
                            if (dr_WWW_UpDown != null&& dr_WWW_UpDown.Length >0)
                            {
                            tagValueUp = dr_WWW_UpDown[0]["值上限"].ToString();
                            }
                            else
                            {
                                tagValueUp = "";
                            }
                        }
                        catch
                        {
                            tagValueUp = "";
                        }
                        try
                        {
                            tagValueDown = "";
                            if (dr_WWW_UpDown != null && dr_WWW_UpDown.Length > 0)
                            {
                            tagValueDown = dr_WWW_UpDown[0]["值下限"].ToString();
                            }
                        }
                        catch
                        {
                            tagValueDown = "";
                        }
                        try
                        {
                            tagValeNormal = "";
                            if (dr_WWW_UpDown != null && dr_WWW_UpDown.Length > 0)
                            {
                                tagValeNormal = dr_WWW_UpDown[0]["理论值"].ToString();
                            }
                               
                        }
                        catch
                        {
                            tagValeNormal = "";
                        }

                        dr = dt.NewRow();

                        dr["TagID"] = qualityData.tagID;
                        dr["测量位置"] = shaftID;
                        dr["测量项目"] = item;
                        dr["测量单位"] = unit;
                        dr["上限值"] = tagValueUp;
                        dr["下限值"] = tagValueDown;
                        dr["理论值"] = tagValeNormal;
                        dr["工位号"] = opName;
                        dr["列位置"] = qualityData.tagOrderBy;
                        dr["变量排序"] = qualityData.tagItemOrderBy;

                        dr["IsGoodBad"] = qualityData.isGoodBad;

                        switch (qualityData.tagTypeID)//
                        {
                            //合格标志数组
                            case 8203:
                                dr["TagValue"] = BaseDataSystemMES.GetQualityData_GoodBad_String((bool[])qualityData.tagValue);
                                break;
                            case 8209:
                                dr["TagValue"] = "";
                                break;
                            //非标准REAL
                            case 8210:
                                dr["TagValue"] = GetNoNormalReal(qualityData.tagValue);
                                dt.Rows.Add(dr);
                                break;
                            default:
                                dr["TagValue"] = Convert.ToString(qualityData.tagValue);
                                dt.Rows.Add(dr);
                                break;
                        }


                    }



                }
                catch (Exception err)
                {
                    ////ApplicationLog.WriteLog(err, err.Message);
                }
            }
        }
       
        /// <summary>
        /// 数据表
        /// </summary>
        /// <returns></returns>
        static public DataTable BuildDataTables_WWW()
        {
            DataTable table;
            DataColumnCollection columns;
            table = new DataTable();
            columns = table.Columns;
            table.Columns.Add("TagID", typeof(System.String));
            table.Columns.Add("TagValue", typeof(System.String));
            table.Columns.Add("IsGoodBad", typeof(System.Int32));
            table.Columns.Add("测量位置", typeof(System.String));
            table.Columns.Add("测量项目", typeof(System.String));
            table.Columns.Add("工位号", typeof(System.String));
            table.Columns.Add("理论值", typeof(System.String));
            table.Columns.Add("上限值", typeof(System.String));
            table.Columns.Add("下限值", typeof(System.String));
            table.Columns.Add("测量单位", typeof(System.String));
            table.Columns.Add("列位置", typeof(System.Int32));
            table.Columns.Add("变量排序", typeof(System.Int32));

            return table;
        }
        /// <summary>
        /// 返回非标准real
        /// </summary>
        /// <param name="vDataValue"></param>
        /// <returns></returns>
        static public 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
            {
                ////ApplicationLog.WriteLog(err, "GetNoNormalReal");
                return "0.0";

            }
        }

        /// <summary>
        /// 29 工位具体零件合格标志
        /// </summary>
        /// <param name="QualityData_GoodBad"></param>
        static public string GetQualityData_GoodBad_String(bool[] QualityData_GoodBad)
        {
            string str;
            str = "";
            if (QualityData_GoodBad != null)
            {

                for (int i = 0; i < QualityData_GoodBad.Length; i++)
                {
                    str = str + (Convert.ToInt32(QualityData_GoodBad[i])).ToString() + "|";
                }

            }
            return str;

        }
        /// <summary>
        /// 返回非标准real
        /// </summary>
        /// <param name="vDataValue"></param>
        /// <returns></returns>
  

    }
}
