using MesToMs;
using MesWork;
using SlMesDbIterface;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DC_A95
{
    /// <summary>
    /// 钩子函数
    /// </summary>
    public class Hanger
    {
       

        /// <summary>
        /// 加载界面显示的信号值的初始值
        /// </summary>
        public static void Load_Station_PageShowSignal()
        {
            foreach (DictionaryEntry item in MesWorkForm.ht_Station_Label)
            {
                var stationName = item.Key.ToString();

                MesWorkForm.ht_Signal__Station_Auto.Add(stationName, false); 
                MesWorkForm.ht_Signal__Station_Running.Add(stationName, false); 
                MesWorkForm.ht_Signal__Station_Manual.Add(stationName, false); 
                MesWorkForm.ht_Signal__Station_Alarm.Add(stationName, false);
                if (!MesWorkForm.ht_Signal__Station_OffLine.ContainsKey(stationName))
                {
                    MesWorkForm.ht_Signal__Station_OffLine.Add(stationName, false);
                }
            }


        }



        public static void Load_Table()
        {
            //查询出“全部原始基本数据列表”
            var drs = MesWork.PlcLinkForm.dt_BasicData.Select($"是否启用 = 1 and 变量类型代码=101 or 变量类型代码 = 102  or 变量类型代码 = 105 and 监控属性代码 = '{DataLinkMesWork.ApplicationConfig.MES_Code.ToString()}'");
            if (drs.Length > 1)
            {
                MesWork_QC.BasicDataTagValue = MesWork.PlcLinkForm.dt_BasicData.Select($"是否启用 = 1 and 变量类型代码=101 or 变量类型代码 = 102  or 变量类型代码 = 105 and 监控属性代码 = '{DataLinkMesWork.ApplicationConfig.MES_Code.ToString()}'").CopyToDataTable<DataRow>();
                if (MesWork_QC.BasicDataTagValue.Columns.Contains("上限值"))
                {
                    MesWork_QC.BasicDataTagValue.Columns["上限值"].ColumnName = "值上限";
                }
                if (MesWork_QC.BasicDataTagValue.Columns.Contains("下限值"))
                {
                    MesWork_QC.BasicDataTagValue.Columns["下限值"].ColumnName = "值下限";
                }
            }

            // BasicDataTagValue = MesWork.PlcLinkForm.dt_BasicData.Select($"是否启用 = 1 and 监控属性代码 = '{DataLinkMesWork.ApplicationConfig.MES_Code.ToString()}'").CopyToDataTable<DataRow>();
            //查询出“MES_基本数据_测量范围”
            MesWork_QC.BasicDataTagValueUpDown = MesWork.PlcLinkForm.dt_BasicData;
            if (MesWork_QC.BasicDataTagValueUpDown.Columns.Contains("上限值"))
            {
                MesWork_QC.BasicDataTagValueUpDown.Columns["上限值"].ColumnName = "值上限";
            }
            if (MesWork_QC.BasicDataTagValueUpDown.Columns.Contains("下限值"))
            {
                MesWork_QC.BasicDataTagValueUpDown.Columns["下限值"].ColumnName = "值下限";
            }
            // 模拟时，写入质量数据
            InitBasicDataTagValue();
         
        }

        /// <summary>
        /// 模拟时，写入质量数据
        /// </summary>
        private static void InitBasicDataTagValue()
        {
            if (MesWorkForm.IsDemoMesServer != 1) return;
            string tagID;
            double tagValue;
            //写工位总合格标志  变量类型代码=21
            for (int i = 0; i < MesWork.PlcLinkForm.dt_BasicData.Rows.Count; i++)
            {
                if (MesWork.PlcLinkForm.dt_BasicData.Rows[i]["变量类型代码"].ToString() == "21")
                {
                    tagID = MesWork.PlcLinkForm.dt_BasicData.Rows[i]["TagID"].ToString();
                    MesWorkForm.WritePLC(tagID, 1);
                }

            }
            if (MesWork_QC.BasicDataTagValue != null)
            {
                for (int i = 0; i < MesWork_QC.BasicDataTagValue.Rows.Count; i++)
                {

                    try
                    {
                        switch (Convert.ToInt32(MesWork_QC.BasicDataTagValue.Rows[i]["变量特点"]))
                        {

                            case 7:
                                tagID = MesWork_QC.BasicDataTagValue.Rows[i]["TagID"].ToString();
                                MesWorkForm.WritePLC(tagID, 1);
                                break;
                            case 6:
                                tagID = MesWork_QC.BasicDataTagValue.Rows[i]["TagID"].ToString();
                                tagValue = Convert.ToDouble(MesWork_QC.BasicDataTagValue.Rows[i]["列位置"]) * 100
                                    + Convert.ToDouble(MesWork_QC.BasicDataTagValue.Rows[i]["变量排序"]) / 100;
                                MesWorkForm.WritePLC(tagID, tagValue);
                                break;
                        }
                    }
                    catch { }

                }
            }

        }













    }
}
