using System;
using System.Collections.Concurrent;
using System.Linq;
using bizFacade;
using DoWhatPlc;
//using SystemFramework;
using MesServerWork;
using Newtonsoft.Json;

namespace MesServerFunctions
{
    partial class FunctionMES
    {
        /// <summary>
        /// 参数刷入完成
        /// </summary>
        /// <param name="OpName"></param>
        /// <param name="tagValue"></param>
        private void ParamsWriteOver(object e)
        {
            try
            {
                DoWhatPlc.CustomeEvetnArgs msgEvent = (DoWhatPlc.CustomeEvetnArgs)e;
                string OpName = Convert.ToString(msgEvent.OpName);
                string OpName_mes = Convert.ToString(msgEvent.OpName) + "_mes";
                int tagValue = Convert.ToInt32(msgEvent.TagValue);
                int EngineTypeID;
                if (tagValue == 1)
                {
                    ConcurrentDictionary<string, object> rtList = MIS_BASE.ReadPLC_OpName(OpName);
                    var pp = JsonConvert.SerializeObject(rtList);
                    var tagValues = JsonConvert.DeserializeObject<ConcurrentDictionary<string, TagFormat>>(pp);
                    // 读取配方号 
                    MIS_BASE.Read_EngineTypeID(OpName, out EngineTypeID);
                    var keyValues = tagValues.Where(x => x.Value.TagTypeCodeID == 58).Where(x => x.Value.TagOrderBy == EngineTypeID.ToString()).OrderBy(x => x.Key).ToDictionary(k => k.Key, v => v.Value);
                    var qc_value_list = keyValues.Values.ToList();
                    var ee = new DoWhatPlc.TagFormat();
                    for (int i = 0; i < qc_value_list.Count; i++)
                    {
                        string tagValue1 = qc_value_list[i].TagValue.ToString();
                        switch (tagValue1.ToLower())
                        {
                            case "true":
                            case "1":
                                tagValue1 = "1";
                                break;
                            default:
                                tagValue1 = "0";
                                break;
                        }

                        if (tagValue1 == "1")
                        {
                            ee = qc_value_list[i];
                            ee.TagValue = tagValue1;
                            break;
                        }
                    }
                    SendMessageFun.SendInfomation("PlcShowWorkPositionUpdate", JsonHelper.ObjectToJson(ee), OpName);
                }
                else
                {

                }
            }
            catch (Exception err)
            {
                ////ApplicationLog.WriteLog(err, "Function08:MesReadOver");
            }
        }
    }
}
