using bizFacade;
using MesServerWork;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using MisDataSaveDateMesRead;
using Newtonsoft.Json;
using System.Collections.Concurrent;
using DoWhatPlc;
//using SystemFramework;

namespace MesServerFunctions
{
    partial class FunctionMES
    {
        /// <summary>
        /// 允许保存(针对半自动和手动压装工位)
        /// </summary>
        /// <param name="OpName"></param>
        /// <param name="tagValue"></param>
        private void MesRead(object e)
        {
            {
                try
                {
                    int EngineTypeID;
                    string EngineType;
                    string EngineID;
                    int PartPallet_Code;
                    string OrderForm;
                    {
                        DoWhatPlc.CustomeEvetnArgs msgEvent = (DoWhatPlc.CustomeEvetnArgs)e;
                        string OpName = Convert.ToString(msgEvent.OpName);
                        int tagValue = Convert.ToInt32(msgEvent.TagValue);
                        if (tagValue == 1)
                        {
                            MIS_BASE.GetEngineTypeFromPLC(OpName, out EngineTypeID, out EngineType, out EngineID, out PartPallet_Code, out OrderForm);

                            // 如果是PA060 工位在允许保存的时候来找待生产数据 进行判断  若有就直接存往下走逻辑
                            //if (OpName == "J07PA060")
                            //{
                            //    DataTable dt;
                            //    dt = BaseDataSystemMES.GetNowWorkOrderData(OpName, 0);
                            //    if (dt.Rows.Count > 0)
                            //    {
                            //        EngineTypeID = Convert.ToInt32(dt.Rows[0]["机型代码"]);
                            //        EngineType = dt.Rows[0]["零件号"].ToString();
                            //        EngineID = dt.Rows[0]["变速箱总成号"].ToString();
                            //        OrderForm = dt.Rows[0]["订单号"].ToString();
                            //        MIS_BASE.WritePLC(32, OpName, false);

                            //        BaseDataSystemMES.OpName_MIS_MOBY_Insert(OpName, MobyTagType.EngineTypeID, EngineTypeID.ToString());
                            //        BaseDataSystemMES.OpName_MIS_MOBY_Insert(OpName, MobyTagType.EngineType, EngineType);
                            //        BaseDataSystemMES.OpName_MIS_MOBY_Insert(OpName, MobyTagType.EngineID, EngineID);
                            //        BaseDataSystemMES.OpName_MIS_MOBY_Insert(OpName, MobyTagType.OrderForm, OrderForm);
                            //    }
                            //    else
                            //    {
                            //        MIS_BASE.WritePLC(32, OpName, true);
                            //        SendMessage.AsyncSendMessage_Notice(OpName, "该订单已生产完毕或无选中的生产订单，请选择其他订单进行生产！");
                            //        return;
                            //    }
                            //}

                            //保存数据到线首表 先保存到  北汽福田变速器序列号总线上线  用于之前判断是否重号
                            if (Db.UpLoadOpNameInsert(EngineID, EngineTypeID, EngineType, OpName))
                            {
                                int qualityMask = 1;
                                //保存质量数据
                                qualityMask = Funtion.MesRead(OpName, tagValue.ToString(), msgEvent.TagID.ToString());
                                //if (OpName == "J07PA060")
                                //{
                                //    DataTable dt_WWW;
                                //    Hashtable valueList;
                                //    //读取工位全部数据
                                //    var rtList = MIS_BASE.ReadPLC_OpName(OpName); //GetDbAndMaxLengthByOpNameByBytes(opName);
                                //    var pp = JsonConvert.SerializeObject(rtList);
                                //    var tagValues = JsonConvert.DeserializeObject<ConcurrentDictionary<string, TagFormat>>(pp);
                                //    //过滤出来质量数据 //获得带合格标志的质量数据
                                //    valueList = Funtion.ReadPLC_OpName_Qd(tagValues);
                                //    List<List<string>> screwUpStrList = new List<List<string>>();
                                //    //获得带基本信息的质量数据表
                                //    Funtion.GetQualityDataTable(valueList, OpName, MIS_BASE.BasicDataTagValue,
                                //    MIS_BASE.BasicDataTagValueUpDown, out dt_WWW, screwUpStrList);
                                //    string tagID = msgEvent.TagID.ToString();
                                //    int id_tagCF;
                                //    Funtion.BaseData_tagTable_QualityData_B5A_IndexInsert(EngineID, tagID, EngineType, EngineTypeID, OpName, qualityMask, "0", out id_tagCF);
                                //    Funtion.BaseData_tagTable_QualityData_B5A_Insert(id_tagCF, EngineTypeID, EngineID, OpName, dt_WWW);
                                //}
                                //else
                                //{
                                //    qualityMask = Funtion.MesRead(OpName, tagValue.ToString(), msgEvent.TagID.ToString());
                                //}
                                //保存物料数据
                                BaseDataSystemMES.Material_Insert(OpName, qualityMask);
                                //测量数据发动机在线状态
                                BaseDataSystemMES.WorkStartInsert(OpName, qualityMask);
                                //推送质量数据到MES的页面
                                SendMessageFun.SendInfomation("ShowTimelyQuality", "1&" + qualityMask.ToString(), OpName);
                                //统计在线产量，在线节拍
                                SendMessage.SendActuallyCount(OpName, tagValue);

                                // 根据AMT 与 MT 来判断计数编号是哪个
                                // 6.该订单计数加一
                                // 计数  从总分线对应表中读取出条码来进行计数
                                //string MainLineEngine = EngineID;
                                

                                //if (EngineTypeID == 1 || EngineTypeID == 4)
                                //{
                                //    DataTable dt = BaseDataSystemMES.newSelectWorkOrder(OpName, EngineID);
                                //    MainLineEngine = dt.Rows[0]["主线总成号"].ToString();
                                //}
                                // 6.该订单计数加一
                                int CountNumber = Convert.ToInt32(MIS_BASE.ReadPLC(38, OpName));
                                // pa050 = 3 表示两个都需要存
                                if (CountNumber == 2)
                                {
                                    //BaseDataSystemMES.newAddOrderCount(OpName, MainLineEngine, OrderForm, 0);
                                    //BaseDataSystemMES.newAddOrderCount(OpName, MainLineEngine, OrderForm, 1);
                                    BaseDataSystemMES.newAddOrderCount(OpName, EngineID, OrderForm, 0);
                                    BaseDataSystemMES.newAddOrderCount(OpName, EngineID, OrderForm, 1);
                                }
                                else
                                {
                                    //BaseDataSystemMES.newAddOrderCount(OpName, MainLineEngine, OrderForm, CountNumber);
                                    BaseDataSystemMES.newAddOrderCount(OpName, EngineID, OrderForm, CountNumber);
                                }

                                SendMessageFun.SendInfomation("FlushOverWorkList", "1", OpName);

                                //通知设备，MES的质量数据（和物料数据）保存完成
                                MIS_BASE.Write_MesReadOver(OpName, true);
                                //MES工作完成
                                MIS_BASE.Write_MaterialVerifyOver(OpName, true);

                                SendMessageFun.SendMessage("1235", "1111", OpName);
                            }
                            else
                            {
                                SendMessage.AsyncSendMessage_Notice(OpName, "保存上线数据失败！");
                            }
                        }
                        else
                        {
                            MIS_BASE.Write_MesReadOver(OpName, false);
                        }
                    }

                }
                catch (Exception err)
                {
                    // 保存出错日志 1.申请上线 2.传递机型 3.允许保存 4.扫码
                    MisDataSaveDate.FuntionMesRead.Alarm_SaveFileLog4Error(3, e, "Fun1060", err);
                    ////ApplicationLog.WriteLog(err, "Function08:MesReadOver");
                }
            }

        }
    }
}
