using bizFacade;
using MesServerWork;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SyUpLoad
{
    class SyUpLoadFun2
    {
        public static void MesServerFunction(string msgValue)
        {
            try
            {
                DoWhatPlc.ReturnValueString rv = Newtonsoft.Json.JsonConvert.DeserializeObject<DoWhatPlc.ReturnValueString>(msgValue);

                string OpName;
                string tagValue;
                string fun;
                string mesServerCode;
                string mesServerdllName;

                OpName = rv.OpName;
                switch (rv.TagValue.ToLower())
                {
                    case "false":
                        tagValue = "0";
                        break;
                    case "true":
                        tagValue = "1";
                        break;
                    default:
                        tagValue = rv.TagValue;
                        break;
                }
                switch (rv.TagTypeCodeID)
                {
                    ////PLC 申请发动机编号 PartLoad
                    //case "43":
                    //    PartLoad(OpName, tagValue);
                    //    break;
                    ////PLC 传递机型 EngineTypeGetOverPLC
                    //case "44":
                    //    EngineTypeGetOverPLC(OpName, tagValue);
                    //    break;
                    //MES 申请下线  PalletDown
                    case "50":
                        PalletDown(OpName, tagValue);
                        break;
                }

            }
            catch (Exception err)
            {
                ////ApplicationLog.WriteLog(err, "MesServerFunction");
            }
        }
        public static void PalletDown(string opName, string tagValue)
        {
            switch (tagValue)
            {
                case "0":
                    DtWebApi.ReadWritePLC.WritePLC("35", opName, false);

                    break;
                case "1":
                    //保存数据
                    //string opName;
                    string orderForm = "";
                    int PartPalletCode;
                    string engineType;
                    int engineTypeID;
                    string engineID;
                    string barCodeType = "1";

                    // BaseDataSystemMES.MesServer_Process_EngineTypeOver(OpName);

                    ////将产品信息从PLC读取
                    MIS_BASE.Read_EngineTypeID(opName, out engineTypeID);
                    MIS_BASE.Read_EngineType(opName, out engineType);
                    MIS_BASE.Read_EngineID(opName, out engineID);
                    MIS_BASE.Read_PalletCode(opName, out PartPalletCode);
                    string kuweiNum = DtWebApi.ReadWritePLC.ReadPLC("112", opName).ToString();

                    //第一步，查询订单号，保存机型、机型代码、总成号、托盘号、订单号到数据库
                    BaseDataSystemMES.GetEngineID_Select(opName, engineID, out DataTable dt);
                    if (dt != null && dt.Rows.Count > 0)
                    {
                        orderForm = Convert.ToString(dt.Rows[0]["订单号"]);
                    }
                    //BaseDataSystemMES.GetUpLoadEngineIDOrderForm(opName, kuweiNum, out DataTable dt);
                    //if (dt.Rows.Count > 0)
                    //{
                    //    orderForm = dt.Rows[0]["订单号"].ToString();
                    //}

                    BaseDataSystemMES.OpName_MIS_MOBY_Insert(opName, orderForm, engineTypeID, engineType, engineID, PartPalletCode, 0, 0, "");


                    DtWebApi.ReadWritePLC.WritePLC("35", opName, true);
                    break;
            }
        }
        /// <summary>
        /// PartLoad（DBX500.3=1）[43]
        /// </summary>
        public static void PartLoad(string OpName,string  tagValue)
        {
            //获得 盆子码（发动机号）、小车号、机型代码、机型
            try
            {
                int engineTypeID;
                string engineID;
                string engineType;
                string PartCode_engineType;
                byte kuweiNum;
                if (tagValue == "1")
                {
                    ////将产品信息读PLC
                    MIS_BASE.Read_EngineTypeID(OpName,out engineTypeID);
                    MIS_BASE.Read_EngineType(OpName,out engineType);
                    MIS_BASE.Read_EngineID(OpName,out engineID);
                    string PartCode1;
                    string[] PartCodeList = new string[6];
                    string PartCode2;
                    string PartCode3;
                    string PartCode4;
                    string PartCode5;
                    string PartCode6;
                    PartCodeList[0] = DtWebApi.ReadWritePLC.ReadPLC("2001", OpName).ToString();
                    PartCodeList[1] = DtWebApi.ReadWritePLC.ReadPLC("2002", OpName).ToString();
                    PartCodeList[2] = DtWebApi.ReadWritePLC.ReadPLC("2003", OpName).ToString();
                    PartCodeList[3] = DtWebApi.ReadWritePLC.ReadPLC("2004", OpName).ToString();
                    PartCodeList[4] = DtWebApi.ReadWritePLC.ReadPLC("2005", OpName).ToString();
                    PartCodeList[5] = DtWebApi.ReadWritePLC.ReadPLC("2006", OpName).ToString();
                    int codeID=2007;//2007 2008 2009 2010 2011 2012
                    bool IsMath=false;
                    for (int i = 0; i < PartCodeList.Length; i++)
                    {
                        codeID = codeID + i;
                        //处理字符串
                        PartCode_engineType = GetEngineType(PartCodeList[i]);
                        if (PartCode_engineType != null)
                        {
                            if (PartCode_engineType == engineType)
                            {
                                DtWebApi.ReadWritePLC.WritePLC(codeID.ToString(), OpName, 1);
                                IsMath = true;
                            }
                            else
                            {
                                DtWebApi.ReadWritePLC.WritePLC(codeID.ToString(), OpName, 2);
                            }

                        }
                    }

                    if(IsMath)
                    {
                        DtWebApi.ReadWritePLC.WritePLC("2014", OpName, 1);

                    }
                    else
                    {
                        DtWebApi.ReadWritePLC.WritePLC("2014", OpName, 2);

                    }
                }

            }
            catch (Exception err)
            {
                ////ApplicationLog.WriteLog(err, "Function03:PartLoad");
            }
        }

        private static string GetEngineType(string PartCode)
        {
            return PartCode;
        }
        /// <summary>
        /// 44
        /// 1 : 保存数据
        /// 0 ：复位
        /// </summary>
        /// <param name="tagValue"></param>
        public static void EngineTypeGetOverPLC(string opName,string tagValue)
        {
            switch (tagValue)
            {
                case "0":
                    DtWebApi.ReadWritePLC.WritePLC("2014", opName, 0);
                    MIS_BASE.Write_MaterialVerifyOver(opName, false);
                    break;
                case "1":
                    string engineType;
                    int engineTypeID;
                    string engineID;

                    string engineType_MES;
                    int engineTypeID_MES;
                    string engineID_MES;
                    // BaseDataSystemMES.MesServer_Process_EngineTypeOver(OpName);

                    ////将产品信息从PLC读取
                    MIS_BASE.Read_EngineTypeID(opName,out engineTypeID);
                    MIS_BASE.Read_EngineType(opName, out engineType);
                    MIS_BASE.Read_EngineID(opName, out engineID);
                    //将分线数据，汇总到总线
                    ////将产品信息从PLC读取
                    MIS_BASE.Read_EngineTypeID_MES(opName, out engineTypeID_MES);
                    MIS_BASE.Read_EngineType_MES(opName, out engineType_MES);
                    MIS_BASE.Read_EngineID_MES(opName, out engineID_MES);

                    //1转移数据到总线
                    BaseDataSystemMES.MailLineAndSubLineData_Insert(engineID, engineTypeID, engineType, engineID_MES);//分线上总线数据转移


                    MIS_BASE.Write_MaterialVerifyOver(opName, true);

                    break;
            }
        }
    }
}
