using System;
using System.Data;
using System.Collections;
//using BasicData;
//using bizFacade;
//using SystemFramework;
using System.Configuration;
using bizFacade;
using DoWhatPlc;
using MesWork;
using System.Collections.Generic;

namespace MesServerWork
{
    public partial class MIS_BASE
    {

        /// <summary>
        /// 读取质量数据
        /// </summary>
        /// <param name="valueList"></param>
        public static void ReadPLC_Sync_ValueList(string opName, out Hashtable valueList)
        {
            valueList = null;
            ReadPLCData(opName, out valueList);
        }
        /// <summary>
        /// 根据工位号 同步读取数据
        /// </summary>
        /// <param name="opName">工位号</param>
        /// <param name="valueList">PLC仅仅值列表</param>
        private static void ReadPLCData(string opName, out Hashtable valueList)
        {
            valueList = new Hashtable();
            QualityDataType qualityDataType;
            qualityDataType = new QualityDataType();
            System.Data.DataTable dt;
            dt = BaseDataSystemMES.BaseData_tagTable_QualityData_Item_B5A();

            object tagValueStr = "0";
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (opName == dt.Rows[i]["工位号"].ToString())
                {
                    qualityDataType.tagID = dt.Rows[i]["TagID"].ToString ();
                    qualityDataType.tagTypeID = Convert.ToInt32(dt.Rows[i]["TagTypeID"]);//变量类型
                    qualityDataType.opName = opName;//
                    qualityDataType.isGoodBad = 192;//
                    qualityDataType.tagQuality = 192;//
                    qualityDataType.tagValueType = Convert.ToInt32(dt.Rows[i]["变量类型代码"]);
                    qualityDataType.tagOrderBy = Convert.ToInt32(dt.Rows[i]["列位置"]);
                    qualityDataType.tagItemOrderBy = Convert.ToInt32(dt.Rows[i]["变量排序"]);
                    qualityDataType.tagProperty = Convert.ToInt32(dt.Rows[i]["变量特点"]); ;//变量特点

                    MIS_BASE.Read_TagID(qualityDataType.tagID, out tagValueStr);

                    if (qualityDataType.tagTypeID == 18)
                    {
                        qualityDataType.tagValue = Convert.ToInt32(tagValueStr);//变量值
                    }
                    else
                    {
                        qualityDataType.tagValue = tagValueStr;//变量值
                    }
                    valueList.Add(qualityDataType.tagID, qualityDataType);
                }
            }
        }
        public static QualityDataSaveList ReadPLC_Sync_DataList_MesRead(string opName)
        {

            QualityDataSaveList qualityDataSaveList = new QualityDataSaveList();
            qualityDataSaveList.BasicDataTagValue = MIS_BASE.BasicDataTagValue;
            qualityDataSaveList.BasicDataTagValueUpDown = MIS_BASE.BasicDataTagValueUpDown;
            qualityDataSaveList.hashtable_EngineTypeID = MIS_BASE.hashtable_EngineTypeID;
            qualityDataSaveList.hashtable_tagTable_OpName_SaveData = MIS_BASE.hashtable_tagTable_OpName_SaveData;
            //List<TagIDQualityDataType> TagIDQualityDataTypeList = new List<TagIDQualityDataType>();

            Hashtable ht = new Hashtable();

            var cd = PlcLinkForm.ReadPLC_GroupData(opName);
            foreach (KeyValuePair<string, object> _cd in cd)
            {
                var tagID = Convert.ToInt32(_cd.Key);
                var TF = (TagFormat)_cd.Value;

                QualityDataType qualityDataType;
                qualityDataType = new QualityDataType();

                qualityDataType.tagID = TF.TagID;
                qualityDataType.tagTypeID = TF.TagTypeID;//变量类型
                qualityDataType.opName = opName;
                qualityDataType.isGoodBad = 192;
                int.TryParse(TF.TagQuality, out int TagQuality);
                qualityDataType.tagQuality = TagQuality;
                qualityDataType.tagValueType = TF.TagTypeCodeID;
                int.TryParse(TF.TagOrderBy, out int TagOrderBy);
                qualityDataType.tagOrderBy = TagOrderBy;
                int.TryParse(TF.TagItemOrderBy, out int TagItemOrderBy);
                qualityDataType.tagItemOrderBy = TagItemOrderBy;
                qualityDataType.tagProperty = TF.TagProperty;//变量特点
                qualityDataType.tagValue = TF.TagValue;//变量值

                ht.Add(tagID, qualityDataType);
            }
            qualityDataSaveList.valuList = ht;
            return qualityDataSaveList;

            //string api = "";
            //string type = "GET";
            //string controlStr = "PLC1";
            //string actionStr = "ReadPLC_Sync_DataList_MesRead";
            //Dictionary<string, string> dic = new Dictionary<string, string>();
            //dic.Add("opName", opName);
            //var rc = new CallWebApi().InvokeWebapiQualityDataSaveList(controlStr, actionStr, api, type, dic);
            //return rc;
        }
    }
}
