
using ABB.Robotics.Controllers.IOSystemDomain;
using BasicData;
using DeviceDriver_BasicData;
using Newtonsoft.Json;
using OpcBasic;
using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Web;

/// <summary>
/// 
/// </summary>
namespace MesWork
{
    /// <summary>
    /// 
    /// </summary>
    public partial class MesWorkForm
    {
        /// <summary>
        /// 处理MIS逻辑
        /// </summary>
        /// <param name="e"></param>
        private void MIS_Funtion(object e)
        {
            try
            {
                var msgEvent = (CustomeEvetnArgs)e;
                if (msgEvent.TagValue == null) return;

                var tagID = msgEvent.TagID.ToString();
                var opName = msgEvent.OpName.ToString();
                var tagTypeCodeID = (int)msgEvent.TagTypeCodeID;
                var tagTypeID = (EnumTagTypeID)msgEvent.TagTypeID;
                var TagTypeCodeID = (EnumTagTypeCodeID)tagTypeCodeID;
                var alarmMsg = msgEvent.TagName.ToString();
                var tagValue = "";
                switch (tagTypeID)
                {
                    case EnumTagTypeID.BOOL:
                        tagValue = (Convert.ToInt32(msgEvent.TagValue)).ToString();
                        break;
                    default:
                        tagValue = msgEvent.TagValue.ToString();
                        break;
                }

                switch (tagTypeCodeID)
                {
                    case 14:
                        MesWorkForm.WritePLC(15, opName, tagValue);
                        break;
                    case 19:
                        if (tagValue == "1")
                        {
                            DataTable dt_WWW = BuildDataTables_WWW();
                            SaveQualityData(opName, tagValue, tagID, out dt_WWW);
                            string qty = JsonConvert.SerializeObject(dt_WWW);
                            string SendMessage = "MES|ShowTimelyQuality_Resources|" + opName + "|" + qty;
                            SendMsg(opName, MesServer_SendProtocol_Plc, SendMessage);
                        }
                        else
                        {
                            MesWorkForm.WritePLC(20, opName, "0");
                        }
                        break;
                    // 扫码完成，比对结果
                    case 65:
                        if (tagValue == "1")
                        {
                            GetQualityDataCount(opName);
                        }
                        else
                        {
                            MesWorkForm.WritePLC(66, opName, 0);
                        }
                        break;
                    default:
                        break;
                }
            }
            catch (Exception err)
            { }



        }



        private static void SaveQualityData(string opName, string tagValue, string tagID, out DataTable dt_WWW, bool isCheck = false)
        {
            int id_tagCF;
            int qualityMask = 0;
            int engineTypeID = 0;
            string engineID = "";
            string engineType = "";
            string partPallet = "";
            string orderFormPlc = "";
            DataTable BasicDataTagValueUpDown = new DataTable();
            DataTable BasicDataTagValue = new DataTable();
             dt_WWW = BuildDataTables_WWW();
            Hashtable valueList;
            try
            {

                // 加注结果
                //dt_WWW.Rows.Add(GetQualityData(opName, 63, isOk).ItemArray);
                string isOk = MesWorkForm.ReadPLC(63, opName).ToString();
                engineID = MesWorkForm.ReadPLC(64, opName).ToString();
                engineID = engineID.Replace(" ", "");
                // 真空值
                dt_WWW.Rows.Add(GetQualityData(opName, 60, isOk).ItemArray);
                // 加注量
                dt_WWW.Rows.Add(GetQualityData(opName, 61, isOk).ItemArray);
                // 节拍时间
                dt_WWW.Rows.Add(GetQualityData(opName, 62, isOk).ItemArray);
     

                DateTime lastupdatetime = DateTime.Now;
                //保存质量数
                B_DB_Opera.BaseData_tagTable_QualityData_B5A_IndexInsert(engineID, tagID, engineType, engineTypeID, opName, qualityMask, partPallet, out id_tagCF);
                B_DB_Opera.BaseData_tagTable_QualityData_B5A_Insert(engineID, id_tagCF, opName, dt_WWW, isCheck);
                //回写保存完成数据
                WritePLC(20, opName, true);
            }
            catch (Exception err)
            {
                WritePLC(20, opName, true);
            }
        }

        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;
        }
        public static DataRow GetQualityData(string opName, int tagTypeCode, string isOk)
        {
            string TagValueUp = "";
            string TagValueDown = "";
            DataRow dr = BuildDataTables_WWW().NewRow();
            var qualityData = (TagFormat)MesWorkForm.ReadPLC_TF(tagTypeCode, opName);
            if(tagTypeCode == 61)
            {
                TagValueUp = MesWorkForm.ReadPLC(67, opName).ToString();
                TagValueDown = MesWorkForm.ReadPLC(68, opName).ToString();
            }
            if (tagTypeCode == 60)
            {
                TagValueUp = MesWorkForm.ReadPLC(69, opName).ToString();
            }
            dr["TagID"] = qualityData.TagID;
            dr["测量位置"] = qualityData.ShaftID;
            dr["测量项目"] = qualityData.ItemName;
            dr["测量单位"] = qualityData.Unit;
            dr["上限值"] = TagValueUp;
            dr["下限值"] = TagValueDown;
            dr["理论值"] = qualityData.TagValeNormal;
            dr["工位号"] = opName;
            dr["列位置"] = qualityData.TagOrderBy;
            dr["变量排序"] = qualityData.TagItemOrderBy;
            dr["IsGoodBad"] = isOk;
            dr["TagValue"] = Convert.ToString(qualityData.TagValue);
            return dr;
        }

        public static void GetQualityDataCount(string opName)
        {
            string engineID = "";
            engineID = MesWorkForm.ReadPLC(64, opName).ToString();
            if(engineID == null || engineID == "")
            {
                WritePLC(66, opName, 2);
                return;
            }
            try
            {
                DataTable dt = null;
                B_DB_Opera.GetQualityDataCount(engineID, opName, out dt);
                if (dt != null && dt.Rows.Count > 0)
                {
                    WritePLC(66, opName, 2);
                }
                else
                {
                    WritePLC(66, opName, 1);
                }
            }
            catch (Exception err)
            {
                WritePLC(66, opName, 2);
            }
         
        }
    }
}

