using bizFacade;
using DataLinkMesWork;
using DoWhatPlc;
using MesServerFunctions;
using MesServerWork;
using MesWork;
using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace MisDataSaveDateMesRead
{
    public partial class Funtion
    {


        /// <summary>
        /// 允许保存(针对半自动和手动压装工位)
        /// </summary>
        /// <param name="OpName"></param>
        /// <param name="tagValue"></param>
        public static int MesRead(string opName, string tagValue, string tagid, bool isCheck = false)
        {
            int qualityMask = 0;

            qualityMask = SaveQualityData(opName, Convert.ToInt32(tagValue), tagid, isCheck);


            //if (tagValue == "0")
            //{
            //    MIS_BASE.Write_TagTypeID(20, opName, false);
            //}
            //else
            //{
            //    qualityMask = SaveQualityData(opName, Convert.ToInt32(tagValue), tagid, isCheck);
            //    MIS_BASE.Write_TagTypeID(20, opName, true);
            //}
            return qualityMask;
        }
        public static string GetRealTimeQualityData(string opName)
        {
            string message = "";
            DataTable dt1 = new DataTable();
            DataTable dt_WWW;
            DataTable dt_WWW_Copy1;
            DataTable dt_WWW_Copy2;
            Hashtable valueList;
            try
            {
                //读取工位全部数据
                var rtList = MIS_BASE.ReadPLC_OpName(opName); //GetDbAndMaxLengthByOpNameByBytes(opName);
                var pp = JsonConvert.SerializeObject(rtList);
                var tagValues = JsonConvert.DeserializeObject<ConcurrentDictionary<string, TagFormat>>(pp);
                //过滤出来质量数据 //获得带合格标志的质量数据
                valueList = ReadPLC_OpName_Qd(tagValues);

                //获得带基本信息的质量数据表
                GetQualityDataTable(valueList, opName, MIS_BASE.BasicDataTagValue, MIS_BASE.BasicDataTagValueUpDown, out dt_WWW);

                dt_WWW_Copy1 = dt_WWW.Copy().Select("变量排序 = 1").CopyToDataTable();
                DataView dv1 = dt_WWW_Copy1.DefaultView;
                dv1.Sort = "列位置 DESC";
                dt_WWW_Copy1 = dv1.ToTable();

                dt_WWW_Copy2 = dt_WWW.Copy().Select("变量排序 = 2").CopyToDataTable();
                DataView dv2 = dt_WWW_Copy2.DefaultView;
                dv2.Sort = "列位置 DESC";
                dt_WWW_Copy2 = dv2.ToTable();

                dt1.Columns.Add("列位置", System.Type.GetType("System.String"));
                dt1.Columns.Add("测量项目1", System.Type.GetType("System.String"));
                dt1.Columns.Add("测量项目2", System.Type.GetType("System.String"));
                dt1.Columns.Add("合格标志", System.Type.GetType("System.String"));
                dt1.Columns.Add("名称", System.Type.GetType("System.String"));

                for (int i = 0; i < dt_WWW_Copy1.Rows.Count; i++)
                {
                    if (dt_WWW_Copy1.Rows[i]["TagValue"].ToString() != "0" || dt_WWW_Copy2.Rows[i]["TagValue"].ToString() != "0")
                    {
                        var dr = dt1.NewRow();
                        dr["列位置"] = dt_WWW_Copy1.Rows[i]["列位置"].ToString();
                        dr["测量项目1"] = dt_WWW_Copy1.Rows[i]["TagValue"].ToString();
                        dr["测量项目2"] = dt_WWW_Copy2.Rows[i]["TagValue"].ToString();
                        dr["合格标志"] = dt_WWW_Copy1.Rows[i]["IsGoodBad"].ToString();
                        dr["名称"] = dt_WWW_Copy1.Rows[i]["测量项目"].ToString();
                        dt1.Rows.Add(dr);
                    }
                }
            }
            catch (Exception err)
            {
            }
            message = JsonHelper.CreateJsonParameters(dt1);

            return message;
        }
        /// <summary>
        /// 返回合格标志
        /// </summary>
        /// <param name="opName"></param>
        /// <param name="tagValue"></param>
        /// <param name="tagID"></param>
        /// <returns></returns>
        private static int SaveQualityData(string opName, int tagValue, string tagID, bool isCheck = false)
        {
            int id_tagCF;
            int qualityMask = 0;
            int engineTypeID = 0;
            string engineID = "";
            string engineType = "";
            string partPallet = "";
            string orderFormPlc = "";
            DataTable dt_WWW;
            Hashtable valueList;
            try
            {

                //读取工位全部数据
                var rtList = MIS_BASE.ReadPLC_OpName(opName); //GetDbAndMaxLengthByOpNameByBytes(opName);
                var pp = JsonConvert.SerializeObject(rtList);
                var tagValues = JsonConvert.DeserializeObject<ConcurrentDictionary<string, TagFormat>>(pp);
                //过滤出来质量数据 //获得带合格标志的质量数据
                valueList = ReadPLC_OpName_Qd(tagValues);


                engineTypeID = Convert.ToInt32(GetValueByTagTypeCodeID(tagValues, Convert.ToInt32(EnumTagTypeCodeID.EngineTypeID)));
                engineID = GetValueByTagTypeCodeID(tagValues, Convert.ToInt32(EnumTagTypeCodeID.EngineID)).ToString();
                engineType = GetValueByTagTypeCodeID(tagValues, Convert.ToInt32(EnumTagTypeCodeID.EngineType)).ToString();
                orderFormPlc = GetValueByTagTypeCodeID(tagValues, Convert.ToInt32(EnumTagTypeCodeID.OrderFormPLC)).ToString();
                qualityMask = Convert.ToInt32(GetValueByTagTypeCodeID(tagValues, Convert.ToInt32(EnumTagTypeCodeID.QualityMask)));



                //获得带基本信息的质量数据表
                GetQualityDataTable(valueList, opName, MIS_BASE.BasicDataTagValue,
                                    MIS_BASE.BasicDataTagValueUpDown, out dt_WWW);

                DateTime lastupdatetime = DateTime.Now;
                //保存质量数
                BaseData_tagTable_QualityData_B5A_IndexInsert(engineID, tagID, engineType, engineTypeID, opName, qualityMask, partPallet, out id_tagCF);
                BaseData_tagTable_QualityData_B5A_Insert(id_tagCF, engineTypeID, engineID, opName, dt_WWW, isCheck);

                // }
                BaseDataSystemMES.Material_Insert(opName, 1);

                //更新订单完工数量及物料
                SendMessageFun.SendMessage("24", "111", opName);
                //回写保存完成数据
                //MIS_BASE.Write_TagTypeID(20, opName, true);
                #region 
                //string orderForm = "";
                //string operatorCode = "";
                //int shiftCode = 1;
                //DateTime arrivedTime;
                //DateTime leavedTime;
                //int TimeDiff;
                //int targetTime = 0;
                //string lineType = "";
                //string isPass;
                //int machineWorkOn = 0;
                //int arrivedOrLeaved = 2;
                //int cartColorOpName = 0;
                //int cartColorConfirm = 0;
                //// int partMoveDirection;
                //int partGoOver = 0;
                //if (qualityMask == 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();

                //string isHaveComputer = hashtableOpName_IsHaveComputer[opName].ToString();
                ////isHaveComputer = MIS_BASE.hashtable_tagTable_OpName_SaveData[opName].ToString();
                ////有计算机的工位，通过工位计算机保存[测量数据发动机在线状态]。
                //if (Convert.ToInt32(isHaveComputer) == 0)
                //{
                //    //1->0 工件离开 工件刚刚离开
                //    if (Convert.ToInt32(m_WorkStart_OpName[opName]) == 1)
                //    {

                //        //[测量数据发动机在线状态]
                //        QualityDataSystem.WorkStartInsert
                //            (opName, orderForm, engineTypeID, engineType, engineID, partPallet.ToString(), operatorCode, shiftCode,
                //            arrivedTime, leavedTime, TimeDiff, targetTime, lineType, isPass, machineWorkOn,
                //            arrivedOrLeaved, cartColorOpName, cartColorConfirm, partMoveDirection, partGoOver, mes_MustTest, onLineState, OpNameUpDownType);
                //    }
                //}
                #endregion
            }
            catch (Exception err)
            {
                //MIS_BASE.Write_TagTypeID(20, opName, true);
                ////ApplicationLog.WriteLog(err, err.Message);
            }
            return qualityMask;
        }
        static public void GetQualityDataTable(Hashtable valuList, string opName, DataTable BasicDataTagValue, DataTable BasicDataTagValueUpDown, out DataTable dt)
        {
            //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)

            QualityDataType qualityData;
            //是否有数据
            //bool isUsing;

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

            dt = BaseDataSystemMES.BuildDataTables_WWW();

            //测量位置
            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;


                    //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"] = BaseDataSystemMES.GetNoNormalReal(qualityData.tagValue);
                                dt.Rows.Add(dr);
                                break;
                            default:
                                dr["TagValue"] = Convert.ToString(qualityData.tagValue);
                                dt.Rows.Add(dr);
                                break;
                        }

                        //福田对质量数据特殊处理，MA20保留4位小数点，其余保留两位
                        if (opName == "J07MA020")
                        {
                            dr["TagValue"] = Convert.ToDouble(dr["TagValue"]).ToString("0.####");
                        }
                        else
                        {
                            dr["TagValue"] = Convert.ToDouble(dr["TagValue"]).ToString("0.##");
                        }

                        if (opName == "J07MA150")
                        {
                            dr["变量排序"] = 1;
                            dr["IsGoodBad"] = 1;
                        }


                    }



                }
                catch (Exception err)
                {
                    //ApplicationLog.WriteLog(err, err.Message);
                }
            }
        }
        /// <summary>
        /// 数据表
        /// </summary>
        /// <returns></returns>
        private static object GetValueByTagTypeCodeID(ConcurrentDictionary<string, TagFormat> opTagList, int TagTypeCodeID)
        {
            try
            {
                return opTagList.Where(x => x.Value.TagTypeCodeID == TagTypeCodeID).ToList()[0].Value.TagValue;

            }
            catch (Exception err) { return "-3"; }
        }
        /// <summary>
        /// 根据工位名称，读取工位对应的质量数据，带合格标志
        /// </summary>
        /// <param name="opName"></param>
        /// <returns></returns>
        public static Hashtable ReadPLC_OpName_Qd(ConcurrentDictionary<string, TagFormat> tagValues)
        {
            //lock (l)
            {

                //var tagValues = DtWebApi.ReadWritePLC.ReadPLC_OpName(opName); //GetDbAndMaxLengthByOpNameByBytes(opName);

                var keyValues = tagValues.OrderBy(x => x.Key).ToDictionary(k => k.Key, v => v.Value);

                TagFormat tagFormatTemp;
                int tagIndex = 1;
                int isOK = 0;

                Hashtable ht = new Hashtable();


                foreach (KeyValuePair<string, TagFormat> _cd in keyValues)
                {
                    if (_cd.Key.ToString()[0].ToString() == "7")
                    {
                        tagFormatTemp = _cd.Value;
                        if (tagIndex % 3 == 1)
                        {
                            isOK = Convert.ToInt32(tagFormatTemp.TagValue);
                        }
                        else if (tagIndex % 3 == 2)
                        {
                            var TF = _cd.Value;

                            QualityDataType qualityDataType;
                            qualityDataType = GetQualityDataType(TF);
                            qualityDataType.isGoodBad = isOK;
                            ht.Add(qualityDataType.tagID, qualityDataType);
                        }
                        else if (tagIndex % 3 == 0)
                        {
                            var TF = (TagFormat)_cd.Value;

                            QualityDataType qualityDataType;
                            qualityDataType = GetQualityDataType(TF);
                            qualityDataType.isGoodBad = isOK;
                            ht.Add(qualityDataType.tagID, qualityDataType);
                        }

                        tagIndex++;
                    }
                    //Console.WriteLine($"key：{kv.Key} value：{kv.Value}");
                }
                return ht;
            }
        }

        //public static Hashtable ReadPLC_OpName_Entite(string opName)
        //{
        //    //lock (l)
        //    {

        //        var tagValues = DtWebApi.ReadWritePLC.ReadPLC_OpName(opName); //GetDbAndMaxLengthByOpNameByBytes(opName);

        //        var keyValues = tagValues.OrderBy(x => x.Key).ToDictionary(k => k.Key, v => v.Value);

        //        TagFormat tagFormatTemp;
        //        int tagIndex = 1;
        //        int isOK = 0;

        //        Hashtable ht = new Hashtable();


        //        foreach (KeyValuePair<string, TagFormat> _cd in keyValues)
        //        {
        //            if (_cd.Key.ToString()[0].ToString() == "7")
        //            {
        //                tagFormatTemp = _cd.Value;
        //                if (tagIndex % 3 == 1)
        //                {
        //                    isOK = Convert.ToInt32(tagFormatTemp.TagValue);
        //                }
        //                else if (tagIndex % 3 == 2)
        //                {
        //                    var TF = _cd.Value;

        //                    QualityDataType qualityDataType;
        //                    qualityDataType = GetQualityDataType(TF);
        //                    qualityDataType.isGoodBad = isOK;
        //                    ht.Add(qualityDataType.tagID, qualityDataType);
        //                }
        //                else if (tagIndex % 3 == 0)
        //                {
        //                    var TF = (TagFormat)_cd.Value;

        //                    QualityDataType qualityDataType;
        //                    qualityDataType = GetQualityDataType(TF);
        //                    qualityDataType.isGoodBad = isOK;
        //                    ht.Add(qualityDataType.tagID, qualityDataType);
        //                }

        //                tagIndex++;
        //            }
        //            //Console.WriteLine($"key：{kv.Key} value：{kv.Value}");
        //        }
        //        return ht;
        //    }
        //}
        public static QualityDataType GetQualityDataType(TagFormat TF)
        {
            QualityDataType qualityDataType;

            qualityDataType = new QualityDataType();

            qualityDataType.tagID = TF.TagID;
            qualityDataType.tagTypeID = TF.TagTypeID;//变量类型
            qualityDataType.opName = TF.OpName;
            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;//变量值

            return qualityDataType;
        }

        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 = "测量数据合格索引_增加";
            Id_tagCF = 0;
            SqlParameter[] thisParms = new SqlParameter[8];
            thisParms[0] = new System.Data.SqlClient.SqlParameter("@EngineID", EngineID);
            thisParms[1] = new System.Data.SqlClient.SqlParameter("@TagID_CF", tagID);
            thisParms[2] = new System.Data.SqlClient.SqlParameter("@EngineType", EngineType);
            thisParms[3] = new System.Data.SqlClient.SqlParameter("@EngineTypeID", EngineTypeID);
            thisParms[4] = new System.Data.SqlClient.SqlParameter("@工位号", opName);
            thisParms[5] = new System.Data.SqlClient.SqlParameter("@托盘编号", PartPalletCode);
            thisParms[6] = new System.Data.SqlClient.SqlParameter("@合格标志", isGoodBad);
            thisParms[7] = new System.Data.SqlClient.SqlParameter("@Id_tagCF", Id_tagCF);
            thisParms[7].Direction = ParameterDirection.Output;

            if (DataAccess.ExecuteStoredProcedure(procedureName, ref thisParms))
            {
                Id_tagCF = Convert.ToInt32(thisParms[7].Value.ToString());
                return true;
            }
            return false;
        }
        public static bool BaseData_tagTable_QualityData_B5A_Insert(int Id_tagCF, int engineTypeID, string engineID, string opName, DataTable dt, bool IsCheck = false)
        {
            string procedureName;
            procedureName = "测量数据合格_增加";
            SqlParameter[] thisParms = new SqlParameter[7];
            thisParms[0] = new System.Data.SqlClient.SqlParameter("@Id_tagCF", Id_tagCF);
            thisParms[1] = new System.Data.SqlClient.SqlParameter("@EngineTypeID", engineTypeID);
            thisParms[2] = new System.Data.SqlClient.SqlParameter("@EngineID", engineID);
            thisParms[3] = new System.Data.SqlClient.SqlParameter("@TagID", SqlDbType.Int, 4, "TagID");
            thisParms[4] = new System.Data.SqlClient.SqlParameter("@TagValue", SqlDbType.NVarChar, 100, "TagValue");
            thisParms[5] = new System.Data.SqlClient.SqlParameter("@IsGoodBad", SqlDbType.Int, 4, "IsGoodBad");
            //是否保存无合格标志数据数据
            thisParms[6] = new System.Data.SqlClient.SqlParameter("@IsCheck", IsCheck);
            //thisParms[6] = new System.Data.SqlClient.SqlParameter("@测量位置", SqlDbType.NVarChar, 50, "测量位置");
            //thisParms[7] = new System.Data.SqlClient.SqlParameter("@测量项目", SqlDbType.NVarChar, 50, "测量项目");
            //thisParms[8] = new System.Data.SqlClient.SqlParameter("@工位号", opName);  //为了给J07MA020 保留四位精度而设置
            //thisParms[9] = new System.Data.SqlClient.SqlParameter("@理论值", SqlDbType.NVarChar, 50, "理论值");
            //thisParms[10] = new System.Data.SqlClient.SqlParameter("@上限值", SqlDbType.NVarChar, 50, "上限值");
            //thisParms[11] = new System.Data.SqlClient.SqlParameter("@下限值", SqlDbType.NVarChar, 50, "下限值");
            //thisParms[12] = new System.Data.SqlClient.SqlParameter("@测量单位", SqlDbType.NVarChar, 50, "测量单位");
            //thisParms[13] = new System.Data.SqlClient.SqlParameter("@列位置", SqlDbType.Int, 4, "列位置");
            //thisParms[14] = new System.Data.SqlClient.SqlParameter("@变量排序", SqlDbType.Int, 4, "变量排序");
            //table.Columns.Add("TagID", typeof(System.Int32));
            //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 DataAccess.ExecuteStoredProcedure(procedureName, ref thisParms, dt);
        }


        public static int MesRead_J07MA150(string opName, string tagValue, string tagid)
        {
            int qualityMask = 0;

            if (tagValue == "0")
            {
                MIS_BASE.Write_TagTypeID(20, opName, false);
            }
            else
            {
                qualityMask = SaveQualityData_J07MA150(opName, Convert.ToInt32(tagValue), tagid);
                MIS_BASE.Write_TagTypeID(20, opName, true);
            }
            return qualityMask;
        }
        /// <summary>
        /// 返回合格标志, 注油工位 质量数据为字符串，读取备用点 变量类型代码 = 41
        /// </summary>
        /// <param name="opName"></param>
        /// <param name="tagValue"></param>
        /// <param name="tagID"></param>
        /// <returns></returns>
        private static int SaveQualityData_J07MA150(string opName, int tagValue, string tagID)
        {
            int id_tagCF;
            int qualityMask = 0;
            int engineTypeID = 0;
            string engineID = "";
            string engineType = "";
            string partPallet = "";
            string orderFormPlc = "";
            DataTable dt_WWW;
            Hashtable valueList;
            try
            {

                //过滤出来质量数据 //获得带合格标志的质量数据
                var rtList = MIS_BASE.ReadPLC_OpName(opName); //GetDbAndMaxLengthByOpNameByBytes(opName);
                var pp = JsonConvert.SerializeObject(rtList);
                var tagValues = JsonConvert.DeserializeObject<ConcurrentDictionary<string, TagFormat>>(pp);
                valueList = ReadPLC_OpName_Qd_J07MA150(tagValues);
                //valueList = ReadPLC_OpName_Qd(tagValues);

                engineTypeID = Convert.ToInt32(GetValueByTagTypeCodeID(tagValues, Convert.ToInt32(EnumTagTypeCodeID.EngineTypeID)));
                engineID = GetValueByTagTypeCodeID(tagValues, Convert.ToInt32(EnumTagTypeCodeID.EngineID)).ToString();
                engineType = GetValueByTagTypeCodeID(tagValues, Convert.ToInt32(EnumTagTypeCodeID.EngineType)).ToString();
                orderFormPlc = GetValueByTagTypeCodeID(tagValues, Convert.ToInt32(EnumTagTypeCodeID.OrderFormPLC)).ToString();
                engineID = GetValueByTagTypeCodeID(tagValues, Convert.ToInt32(EnumTagTypeCodeID.EngineID)).ToString();
                qualityMask = Convert.ToInt32(GetValueByTagTypeCodeID(tagValues, Convert.ToInt32(EnumTagTypeCodeID.QualityMask)));





                //获得带基本信息的质量数据表
                GetQualityDataTable(valueList, opName, MIS_BASE.BasicDataTagValue,
                                    MIS_BASE.BasicDataTagValueUpDown, out dt_WWW);


                DateTime lastupdatetime = DateTime.Now;
                //保存质量数
                BaseData_tagTable_QualityData_B5A_IndexInsert(engineID, tagID, engineType, engineTypeID, opName, qualityMask, partPallet, out id_tagCF);
                BaseData_tagTable_QualityData_B5A_Insert(id_tagCF, engineTypeID, engineID, opName, dt_WWW);

                // }
                BaseDataSystemMES.Material_Insert(opName, 1);

                //更新订单完工数量及物料
                SendMessageFun.SendMessage("24c", "111", opName);

                //回写保存完成数据
                MIS_BASE.Write_TagTypeID(20, opName, true);
            }
            catch (Exception err)
            {
                MIS_BASE.Write_TagTypeID(20, opName, true);
                ////ApplicationLog.WriteLog(err, err.Message);
            }
            return qualityMask;
        }
        public static Hashtable ReadPLC_OpName_Qd_J07MA150(ConcurrentDictionary<string, TagFormat> tagValues)
        {
            //lock (l)
            {

                //var tagValues = DtWebApi.ReadWritePLC.ReadPLC_OpName(opName); //GetDbAndMaxLengthByOpNameByBytes(opName);

                var keyValues = tagValues.OrderBy(x => x.Key).ToDictionary(k => k.Key, v => v.Value);

                TagFormat tagFormatTemp;
                int tagIndex = 1;
                int isOK = 1;

                Hashtable ht = new Hashtable();


                //var TF = _cd.Value;
                ////var TF = Read_TagTypeID(39, opName, out OrderForm);

                //QualityDataType qualityDataType;
                //qualityDataType = GetQualityDataType(TF);
                //qualityDataType.isGoodBad = isOK;
                //ht.Add(qualityDataType.tagID, qualityDataType);


                foreach (KeyValuePair<string, TagFormat> _cd in keyValues)
                {

                    if (_cd.Value.TagTypeCodeID.ToString() == "41")
                    {
                        tagFormatTemp = _cd.Value;

                        var TF = _cd.Value;

                        QualityDataType qualityDataType;
                        qualityDataType = GetQualityDataType(TF);
                        qualityDataType.isGoodBad = isOK;
                        qualityDataType.tagItemOrderBy = 1;
                        qualityDataType.tagOrderBy = 1;
                        ht.Add(qualityDataType.tagID, qualityDataType);
                    }
                    //Console.WriteLine($"key：{kv.Key} value：{kv.Value}");
                }
                return ht;
            }
        }

    }
}