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 SyUpLoadFun1
    {
        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;


                }

            }
            catch (Exception err)
            {
                ////ApplicationLog.WriteLog(err, "MesServerFunction");
            }
        }
       
        /// <summary>
        /// MES将盆子码(DB24-63)[74]、小车号(DBB20)[112]、机型代码(DBB22)[73]、机型(DBB64-103)[75]信息写到交互DB块中
        /// (DBX0.1=1)[5] MES传递机型
        /// </summary>
        public static void PartLoad(string OpName,string  tagValue)
        {
            //获得 盆子码（发动机号）、小车号、机型代码、机型
            try
            {



                int engineTypeID;
                string engineID;
                string engineType;
                byte kuweiNum;
                if (tagValue == "1")
                {
                    // BaseDataSystemMES.MesServer_Process_EngineTypeOver(OpName);
                    DataTable dt;
                    BaseDataSystemMES.GetUpLoadBarCodeEngineID(OpName, out dt);
                    if (dt.Rows.Count < 1)
                    {

                        //button_ManualPartLoad.Invoke(new Action(() => {
                        //    button_ManualPartLoad.Enabled = true;
                        //}));
                        MessageBox.Show("虚拟库存无件，请扫描上线工位小车上的工件条码");
                        return;
                    }
                    engineTypeID = Convert.ToInt32(dt.Rows[0]["机型代码"]);
                    engineID = dt.Rows[0]["发动机号"].ToString();
                    engineType = dt.Rows[0]["发动机型号"].ToString();
                    kuweiNum = Convert.ToByte(dt.Rows[0]["库位号"]);
                    string OrderForm = dt.Rows[0]["订单号"].ToString();
                    MIS_BASE.Read_PalletCode(OpName, out int PartPallet_Code);

                    //BaseDataSystemMES.OpName_MIS_MOBY_Insert(OpName, OrderForm, engineTypeID, engineType, engineID, PartPallet_Code, 0, 0, "");


                    ////将产品信息写入PLC
                    MIS_BASE.Write_EngineTypeID_MES(OpName, engineTypeID);
                    MIS_BASE.Write_EngineType_MES(OpName, engineType);
                    MIS_BASE.Write_EngineID_MES(OpName, engineID);
                    DtWebApi.ReadWritePLC.WritePLC("112", OpName, kuweiNum);

                    MIS_BASE.Write_EngineTypeGetOver_MES(OpName, true);
                }

            }
            catch (Exception err)
            {
                ////ApplicationLog.WriteLog(err, "Function03:PartLoad");
            }
        }
        /// <summary>
        /// 44
        /// 1 : 保存数据
        /// 0 ：复位
        /// </summary>
        /// <param name="tagValue"></param>
        public static void EngineTypeGetOverPLC(string opName,string tagValue)
        {
            switch (tagValue)
            {
                case "0":
                    MIS_BASE.Write_EngineTypeGetOver_MES(opName, false);
                    MIS_BASE.Write_MaterialVerifyOver(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.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, "");

                    //MES保存数据 减掉盆子库存
                    /// <summary>
                    /// 电子看板_发动机号_线首工位_增加
                    /// </summary>
                    /// <param name="opName">工位号</param>
                    /// <param name="orderForm">订单号</param>
                    /// <param name="PartPalletCode">托盘号</param>
                    /// <param name="engineType">工件机型</param>
                    /// <param name="dt">工件机型,机型代码,工件编号, 壳体编号型号,工件流水号,QR码,基本流水号</param>
                    BaseDataSystemMES.GetEngineID_Save_Outline(opName, orderForm, PartPalletCode.ToString(), engineType, engineTypeID, engineID, barCodeType);
                    BaseDataSystemMES.GetUpLoadBarCode_0utUpdate(opName, kuweiNum);
                    MIS_BASE.Write_MaterialVerifyOver(opName, true);

                    break;
            }
        }
    }
}
