using System;
using System.Collections.Generic;
using System.Collections;
using System.Xml;
using iMESCIOC.Request.WMS_Request;
using iMESCIOC.Response.WMS_Response;
using Microsoft.VisualBasic;
using Newtonsoft.Json;
using tcEAI_C;
using iMESCIOC.Request.EAP;
using Microsoft.VisualBasic.ApplicationServices;
using EAIws;
using System.Data;
using Newtonsoft.Json.Linq;
using iMESCIOC.Response.EAP;
using Microsoft.VisualBasic.CompilerServices;
using API_Base;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using iMESCIOC.Request.WMS_Request;
using iMESCIOC.Response.WMS_Response;
using iMESCIOC.Request.EAP;
using iMESCIOC.Response.EAP;

namespace EAIws_C
{

    static class modEAP
    {

        private static XmlDocument xmlDoc = new XmlDocument();       // 用以讀取Xml字串
        private static DateTime ArriveTime;              // 送達時間
        private static string strIdentity;               // Identity XML字串
        private static string strReturnValue;            // ReturnValue XML字串
        private static string strException;              // Exception XML字串
        private static string strResult;                 // Result XML字串
        private static string strMessage;                // Message XML字串
        private static object strServiceName = "modEAP";
        private static int i = 0;

      

        #region "测试工序进出站"
        private static string funCheckJson(object parameters)
        {
            string result = "";
            string transactionid = "";
            string moduleid = "";
            string functionid = "";
            string computername = "";
            string curuserno = "DS";
            string curusrpwd = "";//新增用户密码 SM2加密
            string sendtime = "";
            string languagemode = "";
            string outXml = "";
            string Json = "";

            string ContentType = "";
            string SourceSystem = "";
            string TargetSystem = "";
            string Authorization = "";
            try
            {
                //转换为数组
                if (parameters is object[] paramArray && paramArray.Length >= 2)
                {
                    if (paramArray[1] is API_Base.ServiceInfo serviceInfo)
                    {
                        Json = serviceInfo.InJson;
                        string headerText = serviceInfo.httpRequest.ToString();

                        // 解析逻辑
                        var lines = headerText.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
                        Dictionary<string, string> headers = new Dictionary<string, string>();
                        foreach (var line in lines)
                        {
                            int index = line.IndexOf(':');
                            if (index > 0)
                            {
                                string key = line.Substring(0, index).Trim();
                                string value = line.Substring(index + 1).Trim();
                                headers[key] = value;
                            }
                        }
                        // 结果
                        //curuserno = headers.TryGetValue("curuserno", out var no) ? no : "";
                        //curusrpwd = headers.TryGetValue("curusrpwd", out var pwd) ? pwd : "";


                        ContentType = headers.TryGetValue("Content-Type", out var contp) ? contp : "";
                        SourceSystem = headers.TryGetValue("Source-System", out var sous) ? sous : "";
                        TargetSystem = headers.TryGetValue("Target-System", out var tars) ? tars : "";
                        Authorization = headers.TryGetValue("Authorization", out var auth) ? auth : "";

                        curusrpwd = Authorization;
                    }
                }

                tcEAI_C.clsERP.funCheckUser(curuserno, curusrpwd);

            }
            catch (Exception ex)
            {
                throw ex;
            }
            return Json;
        }
        public static string funTrackIn_Inquiry_CS(object parameters)
        {
            string funTrackIn_Inquiry_CS = default(string);
            // 0102096: [6.0.5] EAP自動報工架構_SDD210906_05-01/ Execute_CI_LOT
            string strReturnValue = "fail";
            string unitid = "";
            string eqp_id = "";
            string socketid = "";
            string strResponseJson = "";//传出JOSN
            string strRequestJson = ""; //传入JOSN
            var aryMessage = new List<ArrayList>();
            string LanguageMode = "zh-cht";  // 預設繁中
            string strMsg = ""; //返回信息
            var strException = new List<ArrayList>();  // Exception XML字串
            var strMessage = new List<ArrayList>();    // Message XML字串
            string strResult = ""; //返回结果

            try
            {
                //获取进站报文
                strRequestJson = funCheckJson(parameters);

                // ======================
                // 1.反序列化 JSON
                // ======================
                var result = JsonConvert.DeserializeObject<TrackIn_Inquiry_CS_Request>(strRequestJson);

                unitid = result.unitid;
                socketid = result.socketid;
                eqp_id = result.eqp_id;
                if (eqp_id =="")
                {
                    throw new iMESException.MESException("0000-200002", "[%EquipmentNo%] [%NOT FOUND%]");
                }

                var jObj = JObject.Parse(strRequestJson);
                Dictionary<string, string> dicPars = jObj.ToObject<Dictionary<string, string>>();

                var dicPars_Tables = default(Dictionary<string, DataTable>);

                DataTable dt = new DataTable("lotattrib");
                dt.Columns.Add("row_count", typeof(int));
                dt.Columns.Add("attribno", typeof(string));
                dt.Columns.Add("attribvalue", typeof(string));

                DataRow dr = dt.NewRow();
                dr["row_count"] = 1;
                dr["attribno"] = "socketid";
                dr["attribvalue"] = socketid ;
                dt.Rows.Add(dr);

                dicPars_Tables = new Dictionary<string, DataTable>
                {
                    { "lotattrib", dt }
                };

                // 2.執行tcEAI
                var obj = new tcEAI_C.clsEAP();
                strReturnValue = obj.Execute_CI_LOT(dicPars, dicPars_Tables, "DS", LanguageMode, ref aryMessage);
                strResult = "success";
            }

            catch (Exception ex)
            {
                strResult = "fail";
                //翻译
                strMsg = modWS.TranslateMsg(ex.Message);
            }
            finally
            {
                //组装返回JOSN
                TrackIn_Inquiry_CS_Response response = null;

                if (strResult == "success")
                {
                    response = new TrackIn_Inquiry_CS_Response
                    {
                        code = "0000",
                        message = "TrackIn_Inquiry_CS successfully",
                        time = DateTime .Now
                    };
                }
                else
                {
                    // 失败逻辑
                    response = new TrackIn_Inquiry_CS_Response
                    {
                        code = "1111",
                        message = "TrackIn_Inquiry ：异常信息："+ strMsg,
                        time = DateTime.Now
                    };
                }

                //resModel 转成JOSN返回
                strResponseJson = JsonConvert.SerializeObject(response);
                funTrackIn_Inquiry_CS = strResponseJson;

                // 紀錄Transaction Log
                tcEAI_C.clsERP.addTransactionLog_ErrorCode(DateTime.Now.ToString("yyyyMMddHHmmssss"),
                    "EAP", "funTrackIn_Inquiry_CS",
                    "EAP", "EAP", DateTime.Now.ToString(),
                    strResult, "", strMessage, strException, strRequestJson, strResponseJson, "funTrackIn_Inquiry_CS",
                    Conversions.ToInteger(System.Configuration.ConfigurationManager.AppSettings["keepLog"]),
                    Conversions.ToInteger(System.Configuration.ConfigurationManager.AppSettings["keepXML"]),
                    LogClass: "EAP");
            }

            return funTrackIn_Inquiry_CS;
        }

        public static string TrackOut_Inquiry_CS(object parameters)
        {
            string Execute_CO_LOTRet = default(string);

            string strReturnValue = "fail";
            string unitid = "";
            string eqp_id = "";
            string result_c = "";
            string strResponseJson = "";//传出JOSN
            string strRequestJson = ""; //传入JOSN
            var aryMessage = new List<ArrayList>();
            string LanguageMode = "zh-cht";  // 預設繁中
            string strMsg = ""; //返回信息
            var strException = new List<ArrayList>();  // Exception XML字串
            var strMessage = new List<ArrayList>();    // Message XML字串
            string strResult = ""; //返回结果

            try
            {
                strReturnValue = "";

                //获取进站报文
                strRequestJson = funCheckJson(parameters);

                // ======================
                // 1.反序列化 JSON
                // ======================
                var result = JsonConvert.DeserializeObject<TrackOut_Inquiry_CS_Request>(strRequestJson);

                unitid = result.unitid;
                result_c = result.result;
                eqp_id = result.eqp_id;
                if (eqp_id == "")
                {
                    throw new iMESException.MESException("0000-200002", "[%EquipmentNo%] [%NOT FOUND%]");
                }

                var jObj = JObject.Parse(strRequestJson);
                Dictionary<string, string> dicPars = jObj.ToObject<Dictionary<string, string>>();

                var dicPars_Tables = new Dictionary<string, DataTable>();

                // 2.執行tcEAI
                var obj = new tcEAI_C.clsEAP();
                strReturnValue = obj.Execute_CO_LOT(dicPars, dicPars_Tables, "DS", LanguageMode, ref aryMessage);
                strResult = "success";
            }

            catch (Exception ex)
            {
                strResult = "fail";
                //翻译
                strMsg = modWS.TranslateMsg(ex.Message);
            }
            finally
            {
                //组装返回JOSN
                TrackIn_Inquiry_CS_Response response = null;

                if (strResult == "success")
                {
                    response = new TrackIn_Inquiry_CS_Response
                    {
                        code = "0000",
                        message = "TrackOut_Inquiry_CS successfully",
                        time = DateTime.Now
                    };
                }
                else
                {
                    // 失败逻辑
                    response = new TrackIn_Inquiry_CS_Response
                    {
                        code = "1111",
                        message = "TrackOut_Inquiry ：异常信息：" + strMsg,
                        time = DateTime.Now
                    };
                }

                //resModel 转成JOSN返回
                strResponseJson = JsonConvert.SerializeObject(response);
                Execute_CO_LOTRet = strResponseJson;

                // 紀錄Transaction Log
                tcEAI_C.clsERP.addTransactionLog_ErrorCode(DateTime.Now.ToString("yyyyMMddHHmmssss"),
                    "EAP", "funTrackOut_Inquiry_CS",
                    "EAP", "EAP", DateTime.Now.ToString(),
                    strResult, "", strMessage, strException, strRequestJson, strResponseJson, "funTrackOut_Inquiry_CS",
                    Conversions.ToInteger(System.Configuration.ConfigurationManager.AppSettings["keepLog"]),
                    Conversions.ToInteger(System.Configuration.ConfigurationManager.AppSettings["keepXML"]),
                    LogClass: "EAP");
            }

            return Execute_CO_LOTRet;
        }
        #endregion


        #region EAP 客制接口
        /// <summary>
        /// 接口断点测试
        /// </summary>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public static string funEAPTEST(object parameters)
        {
            string funEAPTEST = default(string);
            string strResult = ""; //返回结果
            string strMsg = ""; //返回信息
            string strKeyValue = "";
            var strException = new List<ArrayList>();  // Exception XML字串
            var strMessage = new List<ArrayList>();    // Message XML字串
            string strResponseJson = "";//传出JOSN
            string strRequestJson = ""; //传入JOSN


            try
            {



                strResult = "success";



            exitFun:
                ;
            }
            catch (Exception ex)
            {
                strResult = "fail";
                //翻译
                strMsg = modWS.TranslateMsg(ex.Message);

            }
            finally
            {

            }
            return funEAPTEST;
        }


        /// <summary>
        /// 首站-进站接口
        /// </summary>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public static string funTrackIn_Inquiry(object parameters)
        {
            string funTrackIn_Inquiry = default(string);
            string strResult = ""; //返回结果
            string strMsg = ""; //返回信息
            string strKeyValue = "";
            var strException = new List<ArrayList>();  // Exception XML字串
            var strMessage = new List<ArrayList>();    // Message XML字串
            string strResponseJson = "";//传出JOSN
            string strRequestJson = ""; //传入JOSN
            bool bolclsEAP = false;

            try
            {
                //获取进站报文
                strRequestJson = modERP.funCheckJson(parameters);

                // ======================
                // 1.反序列化 JSON
                // ======================
                var result = JsonConvert.DeserializeObject<EAP_funTrackIn_Inquiry_Request>(strRequestJson);

                //检查mono和eqp_id是否都传

                if (result.mono == null || result.eqp_id == null)
                {
                    throw new Exception("[%工单编号和设备编号都必须传入!%]");
                }

                //判空
                if (result.mono == "" || result.eqp_id == "")
                {
                    throw new Exception("[%工单编号或者设备编号不能为空!%]");
                }


                bolclsEAP = clsEAP.funTrackIn_Inquiry(ref strMessage, ref strException, MONO: result.mono, EQPID: result.eqp_id);
                if (bolclsEAP == false)
                {
                    //说明有提示
                    modWS.ChgTranslateMessage(ref strMessage, ref strMsg);
                    throw new Exception(strMsg);
                }

                strResult = "success";



            exitFun:
                ;
            }
            catch (Exception ex)
            {
                strResult = "fail";
                //翻译
                strMsg = modWS.TranslateMsg(ex.Message);

            }
            finally
            {
                //组装返回JOSN
                var resModel = new EAP_funTrackInOut_Inquiry_Response();
                if (strResult == "success")
                {
                    resModel.Code = 0000;
                    resModel.Message = "TrackIn_Inquiry successfully";
                }
                else
                {
                    // 失败逻辑
                    resModel.Code = 1111;
                    resModel.Message = "TrackIn_Inquiry ：" + strMsg;
                }

                //resModel 转成JOSN返回
                strResponseJson = JsonConvert.SerializeObject(resModel);
                funTrackIn_Inquiry = strResponseJson;

                // 紀錄Transaction Log
                tcEAI_C.clsERP.addTransactionLog_ErrorCode(DateTime.Now.ToString("yyyyMMddHHmmssss"),
                    "WIP", "funTrackIn_Inquiry",
                    "EAP", "EAP", DateTime.Now.ToString(),
                    strResult, strKeyValue, strMessage, strException, strRequestJson, strResponseJson, "TrackIn_Inquiry",
                    Conversions.ToInteger(System.Configuration.ConfigurationManager.AppSettings["keepLog"]),
                    Conversions.ToInteger(System.Configuration.ConfigurationManager.AppSettings["keepXML"]),
                    LogClass: "EAP");

            }
            return funTrackIn_Inquiry;
        }

        /// <summary>
        /// 首站-出站接口
        /// </summary>
        /// <param name="parameters"></param>
        /// <returns></returns>
        /// <summary>
        /// 首站-出站接口
        /// </summary>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public static string funTrackOut_Inquiry(object parameters)
        {
            string funTrackOut_Inquiry = default(string);
            string strResult = ""; //返回结果
            string strMsg = ""; //返回信息
            string strKeyValue = "";
            var strException = new List<ArrayList>();  // Exception XML字串
            var strMessage = new List<ArrayList>();    // Message XML字串
            string strResponseJson = "";//传出JOSN
            string strRequestJson = ""; //传入JOSN
            bool bolclsEAP = false;


            try
            {
                //获取进站报文
                strRequestJson = modERP.funCheckJson(parameters);

                // ======================
                // 1.反序列化 JSON
                // ======================
                var result = JsonConvert.DeserializeObject<EAP_funTrackOut_Inquiry_Request>(strRequestJson);

                //检查mono和eqp_id是否都传

                if (result.mono == null || result.eqp_id == null || result.panleid == null || result.qty == null || result.column == null || result.line == null)
                {
                    throw new Exception("[%工单编号,设备编号,panleid,数量,行数，列数都必须传入!%]");
                }



                //判空
                if (result.mono == "" || result.eqp_id == "" || result.panleid == "" || result.qty == "" || result.column == "" || result.line == "")
                {
                    throw new Exception("[%工单编号,设备编号,panleid,数量,行数，列数都必须传入!%]");
                }

                //数量不能为0或者负数
                if (Convert.ToDecimal(result.qty) <= 0)
                {
                    throw new Exception("[%数量不能为0或者负数!%]");
                }


                bolclsEAP = clsEAP.funTrackOut_Inquiry(ref strMessage, ref strException, MONO: result.mono, EQPID: result.eqp_id, PanleId: result.panleid, Column: result.column, Line: result.line, carrierId: result.carrier_id, Qty: Convert.ToDecimal(result.qty), MapData: result.mapdata);
                if (bolclsEAP == false)
                {
                    //说明有提示
                    modWS.ChgTranslateMessage(ref strMessage, ref strMsg);
                    throw new Exception(strMsg);
                }

                strResult = "success";



            exitFun:
                ;
            }
            catch (Exception ex)
            {
                strResult = "fail";
                //翻译
                strMsg = modWS.TranslateMsg(ex.Message);

            }
            finally
            {
                //组装返回JOSN
                var resModel = new EAP_funTrackInOut_Inquiry_Response();
                if (strResult == "success")
                {
                    resModel.Code = 0;
                    resModel.Message = "TrackOut_Inquiry successfully";
                }
                else
                {
                    // 失败逻辑
                    resModel.Code = 1111;
                    resModel.Message = "TrackOut_Inquiry ：" + strMsg;
                }

                //resModel 转成JOSN返回
                strResponseJson = JsonConvert.SerializeObject(resModel);
                funTrackOut_Inquiry = strResponseJson;

                // 紀錄Transaction Log
                tcEAI_C.clsERP.addTransactionLog_ErrorCode(DateTime.Now.ToString("yyyyMMddHHmmssss"),
                    "WIP", "funTrackOut_Inquiry",
                    "EAP", "EAP", DateTime.Now.ToString(),
                    strResult, strKeyValue, strMessage, strException, strRequestJson, strResponseJson, "TrackOut_Inquiry",
                    Conversions.ToInteger(System.Configuration.ConfigurationManager.AppSettings["keepLog"]),
                    Conversions.ToInteger(System.Configuration.ConfigurationManager.AppSettings["keepXML"]),
                    LogClass: "EAP");

            }
            return funTrackOut_Inquiry;
        }



        /// <summary>
        /// 进站接口
        /// </summary>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public static string funTrackIn_Inquiry_LQ(object parameters)
        {
            string funTrackIn_Inquiry = default(string);
            string strResult = ""; //返回结果
            string strMsg = ""; //返回信息
            string strKeyValue = "";
            var strException = new List<ArrayList>();  // Exception XML字串
            var strMessage = new List<ArrayList>();    // Message XML字串
            string strResponseJson = "";//传出JOSN
            string strRequestJson = ""; //传入JOSN
            bool bolclsEAP = false;

            try
            {
                //获取进站报文
                strRequestJson = modERP.funCheckJson(parameters);

                // ======================
                // 1.反序列化 JSON
                // ======================
                var result = JsonConvert.DeserializeObject<EAP_funTrackIn_Inquiry_LQ_Request>(strRequestJson);

                //检查mono和eqp_id是否都传

                if ( result.eqp_id == null)
                {
                    throw new Exception("[%设备编号必须传入!%]");
                }

                //判空
                if ( result.eqp_id == "")
                {
                    throw new Exception("[%type或者设备编号不能为空!%]");
                }

                if (result.ID == null)
                {
                    throw new Exception("[%ID必须传入!%]");
                }

                //判空
                if (result.ID == "")
                {
                    throw new Exception("[%ID编号不能为空!%]");
                }

                bolclsEAP = clsEAP.funTrackIn_Inquiry_LQ(ref strMessage, ref strException, EQPID: result.eqp_id, result.ID,result.remark);
                if (bolclsEAP == false)
                {
                    //说明有提示
                    modWS.ChgTranslateMessage(ref strMessage, ref strMsg);
                    throw new Exception(strMsg);
                }

                strResult = "success";



            exitFun:
                ;
            }
            catch (Exception ex)
            {
                strResult = "fail";
                //翻译
                strMsg = modWS.TranslateMsg(ex.Message);

            }
            finally
            {
                //组装返回JOSN
                var resModel = new EAP_funTrackInOut_Inquiry_Response();
                if (strResult == "success")
                {
                    resModel.Code = 0000;
                    resModel.Message = "TrackIn_Inquiry_LQ successfully";
                }
                else
                {
                    // 失败逻辑
                    resModel.Code = 1111;
                    resModel.Message = "TrackIn_Inquiry_LQ ：" + strMsg;
                }

                //resModel 转成JOSN返回
                strResponseJson = JsonConvert.SerializeObject(resModel);
                funTrackIn_Inquiry = strResponseJson;

                // 紀錄Transaction Log
                tcEAI_C.clsERP.addTransactionLog_ErrorCode(DateTime.Now.ToString("yyyyMMddHHmmssss"),
                    "WIP", "funTrackIn_Inquiry_LQ",
                    "EAP", "EAP", DateTime.Now.ToString(),
                    strResult, strKeyValue, strMessage, strException, strRequestJson, strResponseJson, "TrackIn_Inquiry_LQ",
                    Conversions.ToInteger(System.Configuration.ConfigurationManager.AppSettings["keepLog"]),
                    Conversions.ToInteger(System.Configuration.ConfigurationManager.AppSettings["keepXML"]),
                    LogClass: "EAP");

            }
            return funTrackIn_Inquiry;
        }

        public static string funTrackOut_Inquiry_LQ(object parameters)
        {
            string funTrackOut_Inquiry = default(string);
            string strResult = ""; //返回结果
            string strMsg = ""; //返回信息
            string strKeyValue = "";
            var strException = new List<ArrayList>();  // Exception XML字串
            var strMessage = new List<ArrayList>();    // Message XML字串
            string strResponseJson = "";//传出JOSN
            string strRequestJson = ""; //传入JOSN
            bool bolclsEAP = false;


            try
            {
                //获取进站报文
                strRequestJson = modERP.funCheckJson(parameters);

                // ======================
                // 1.反序列化 JSON
                // ======================
                var result = JsonConvert.DeserializeObject<EAP_funTrackIn_Inquiry_LQ_Request>(strRequestJson);

                //检查mono和eqp_id是否都传

                if (result.eqp_id == null)
                {
                    throw new Exception("[%设备编号必须传入!%]");
                }

                //判空
                if (result.eqp_id == "")
                {
                    throw new Exception("[%type或者设备编号不能为空!%]");
                }

                if (result.ID == null)
                {
                    throw new Exception("[%ID必须传入!%]");
                }

                //判空
                if (result.ID == "")
                {
                    throw new Exception("[%ID编号不能为空!%]");
                }


                bolclsEAP = clsEAP.funTrackOut_Inquiry_LQ(ref strMessage, ref strException, result.eqp_id, result.ID, result.remark);
                if (bolclsEAP == false)
                {
                    //说明有提示
                    modWS.ChgTranslateMessage(ref strMessage, ref strMsg);
                    throw new Exception(strMsg);
                }

                strResult = "success";



            exitFun:
                ;
            }
            catch (Exception ex)
            {
                strResult = "fail";
                //翻译
                strMsg = modWS.TranslateMsg(ex.Message);

            }
            finally
            {
                //组装返回JOSN
                var resModel = new EAP_funTrackInOut_Inquiry_Response();
                if (strResult == "success")
                {
                    resModel.Code = 0;
                    resModel.Message = "TrackOut_Inquiry_LQ successfully";
                }
                else
                {
                    // 失败逻辑
                    resModel.Code = 1111;
                    resModel.Message = "TrackOut_Inquiry_LQ ：" + strMsg;
                }

                //resModel 转成JOSN返回
                strResponseJson = JsonConvert.SerializeObject(resModel);
                funTrackOut_Inquiry = strResponseJson;

                // 紀錄Transaction Log
                tcEAI_C.clsERP.addTransactionLog_ErrorCode(DateTime.Now.ToString("yyyyMMddHHmmssss"),
                    "WIP", "funTrackOut_Inquiry_LQ",
                    "EAP", "EAP", DateTime.Now.ToString(),
                    strResult, strKeyValue, strMessage, strException, strRequestJson, strResponseJson, "TrackOut_Inquiry_LQ",
                    Conversions.ToInteger(System.Configuration.ConfigurationManager.AppSettings["keepLog"]),
                    Conversions.ToInteger(System.Configuration.ConfigurationManager.AppSettings["keepXML"]),
                    LogClass: "EAP");

            }
            return funTrackOut_Inquiry;
        }
        #endregion
        #region"Mapping反转方向接口"
        /// <summary>
        /// 计量
        /// </summary>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public static string PrdMapping(object parameters)
        {
            string GetJLSPCData = default(string);
            string strResult = ""; //返回结果
            string strMsg = ""; //返回信息
            string strKeyValue = "";
            var strException = new List<ArrayList>();  // Exception XML字串
            var strMessage = new List<ArrayList>();    // Message XML字串
            string strResponseJson = "";//传出JOSN
            string strRequestJson = ""; //传入JOSN

            //调 clsERP 传入参数
            string Productid = "";
            string Eqp_id = "";

            var listTest = new List<decimal>();
            int REVERSALMETHOD = -1;
            try
            {

                strRequestJson = funCheckJson(parameters);

                // ======================
                // 1.反序列化 JSON
                // ======================
                var result = JsonConvert.DeserializeObject<MappingRequest>(strRequestJson);

                //要因 没有值，则给 * 
                Productid = GetSafeString(result.Productid);
                Eqp_id = GetSafeString(result.Eqp_id);

                bool bolclsERP = clsERP.funGetPrdMapping(ref strMessage, ref strException, Productid, Eqp_id, ref REVERSALMETHOD);

                if (bolclsERP == true)
                {
                    strResult = "success";
                }
                else
                {
                    strResult = "fail";
                }
            }
            catch (Exception ex)
            {
                strResult = "fail";
                //翻译
                strMsg = modWS.TranslateMsg(ex.Message);

            }
            finally
            {
                //组装返回JOSN


                MappingResponse response = null;

                if (strResult == "success")
                {
                    ResponseData data = new ResponseData();
                    if (REVERSALMETHOD == 0)
                    {
                        data.REVERSALMETHOD = "上下反转";
                    }
                    else if (REVERSALMETHOD == 1)
                    {
                        data.REVERSALMETHOD = "左右反转";
                    }
                    else if (REVERSALMETHOD == 2)
                    {
                        data.REVERSALMETHOD = "上下左右反转";
                    }
                    else
                    {
                        data.REVERSALMETHOD = "无反转";
                    }
                    response = new MappingResponse
                    {
                        code = "0000",
                        message = "成功",
                        time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                        data = data,
                        extras = null,
                        logId = "",
                        requestId = DateTime.Now.ToString("yyyyMMddHHmmssss")
                    };
                }
                else
                {
                    // 失败逻辑
                    response = new MappingResponse
                    {
                        code = "1111",
                        message = strMsg,
                        time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                        data = null,
                        extras = null,
                        logId = "",
                        requestId = DateTime.Now.ToString("yyyyMMddHHmmssss")
                    };
                }

                //resModel 转成JOSN返回
                strResponseJson = JsonConvert.SerializeObject(response);
                GetJLSPCData = strResponseJson;

                // 紀錄Transaction Log
                tcEAI_C.clsERP.addTransactionLog_ErrorCode(DateTime.Now.ToString("yyyyMMddHHmmssss"),
                    "WIP", "PrdMapping",
                    "ERP", "ERP", DateTime.Now.ToString(),
                    strResult, strKeyValue, strMessage, strException, strRequestJson, strResponseJson, "PrdMapping",
                    Conversions.ToInteger(System.Configuration.ConfigurationManager.AppSettings["keepLog"]),
                    Conversions.ToInteger(System.Configuration.ConfigurationManager.AppSettings["keepXML"]),
                    LogClass: "ERP");
            }

            return GetJLSPCData;
        }
        public static string GetSafeString(string value, string defaultValue = "*")
        {
            return string.IsNullOrEmpty(value) ? defaultValue : value;
        }
        #endregion

        #region "MakingCode明码查询接口"
        /// <summary>
        /// 明码查询接口
        /// </summary>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public static string MakingCode(object parameters)
        {
            string GetMakingCode = default(string);
            string strResult = ""; //返回结果
            string strMsg = ""; //返回信息
            string strKeyValue = "";
            var strException = new List<ArrayList>();  // Exception XML字串
            var strMessage = new List<ArrayList>();    // Message XML字串
            string strResponseJson = "";//传出JSON
            string strRequestJson = ""; //传入JSON

            //返回数据
            string strMAKINGCODE = "";
            string strUNITSN = "";

            try
            {
                strRequestJson = funCheckJson(parameters);

                // ======================
                // 1.反序列化 JSON
                // ======================
                var result = JsonConvert.DeserializeObject<MakingCodeRequest>(strRequestJson);

                string PanelSN = GetSafeString(result.PanelSN, "");
                string EQPID = GetSafeString(result.EQPID, "");

                if (string.IsNullOrEmpty(PanelSN))
                {
                    throw new Exception("[%PanelSN不能为空!%]");
                }
                if (string.IsNullOrEmpty(EQPID))
                {
                    throw new Exception("[%EQPID不能为空!%]");
                }

                bool bolclsEAP = clsEAP.funMakingCode(ref strMessage, ref strException, PanelSN, EQPID, ref strMAKINGCODE, ref strUNITSN);

                if (bolclsEAP == true)
                {
                    strResult = "success";
                }
                else
                {
                    strResult = "fail";
                    modWS.ChgTranslateMessage(ref strMessage, ref strMsg);
                }
            }
            catch (Exception ex)
            {
                strResult = "fail";
                //翻译
                strMsg = modWS.TranslateMsg(ex.Message);
            }
            finally
            {
                //组装返回JSON
                MakingCodeResponse response = null;

                if (strResult == "success")
                {
                    var data = new MakingCodeResponseData
                    {
                        MAKINGCODE = strMAKINGCODE,
                        UNITSN = strUNITSN
                    };
                    response = new MakingCodeResponse
                    {
                        code = "0000",
                        message = "MakingCode successfully",
                        time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                        data = data,
                        extras = new Dictionary<string, object>(),
                        logId = "",
                        requestId = DateTime.Now.ToString("yyyyMMddHHmmssss")
                    };
                }
                else
                {
                    response = new MakingCodeResponse
                    {
                        code = "1111",
                        message = "MakingCode ：" + strMsg,
                        time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                        data = new MakingCodeResponseData(),
                        extras = new Dictionary<string, object>(),
                        logId = "",
                        requestId = DateTime.Now.ToString("yyyyMMddHHmmssss")
                    };
                }

                //resModel 转成JSON返回
                strResponseJson = JsonConvert.SerializeObject(response);
                GetMakingCode = strResponseJson;

                // 紀錄Transaction Log
                tcEAI_C.clsERP.addTransactionLog_ErrorCode(DateTime.Now.ToString("yyyyMMddHHmmssss"),
                    "WIP", "MakingCode",
                    "EAP", "EAP", DateTime.Now.ToString(),
                    strResult, strKeyValue, strMessage, strException, strRequestJson, strResponseJson, "MakingCode",
                    Conversions.ToInteger(System.Configuration.ConfigurationManager.AppSettings["keepLog"]),
                    Conversions.ToInteger(System.Configuration.ConfigurationManager.AppSettings["keepXML"]),
                    LogClass: "EAP");
            }

            return GetMakingCode;
        }
        #endregion

        #region "LotMessage批次信息查询接口"
        /// <summary>
        /// 批次信息查询接口
        /// </summary>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public static string LotMessage(object parameters)
        {
            string GetLotMessage = default(string);
            string strResult = ""; //返回结果
            string strMsg = ""; //返回信息
            string strKeyValue = "";
            var strException = new List<ArrayList>();  // Exception XML字串
            var strMessage = new List<ArrayList>();    // Message XML字串
            string strResponseJson = "";//传出JSON
            string strRequestJson = ""; //传入JSON

            //返回数据
            string strLOTNO = "";
            string strPROCESSNO = "";
            string strOPNO = "";
            string strPRODUCTNO = "";
            string strCUSTOMERNO = "";

            try
            {
                strRequestJson = funCheckJson(parameters);

                // ======================
                // 1.反序列化 JSON
                // ======================
                var result = JsonConvert.DeserializeObject<LotMessageRequest>(strRequestJson);

                string PanelSN = GetSafeString(result.PanelSN, "");
                string EQPID = GetSafeString(result.EQPID, "");

                if (string.IsNullOrEmpty(PanelSN))
                {
                    throw new Exception("[%PanelSN不能为空!%]");
                }
                if (string.IsNullOrEmpty(EQPID))
                {
                    throw new Exception("[%EQPID不能为空!%]");
                }

                bool bolclsEAP = clsEAP.funLotMessage(ref strMessage, ref strException, PanelSN, EQPID,
                    ref strLOTNO, ref strPROCESSNO, ref strOPNO, ref strPRODUCTNO, ref strCUSTOMERNO);

                if (bolclsEAP == true)
                {
                    strResult = "success";
                }
                else
                {
                    strResult = "fail";
                    modWS.ChgTranslateMessage(ref strMessage, ref strMsg);
                }
            }
            catch (Exception ex)
            {
                strResult = "fail";
                //翻译
                strMsg = modWS.TranslateMsg(ex.Message);
            }
            finally
            {
                //组装返回JSON
                LotMessageResponse response = null;

                if (strResult == "success")
                {
                    var data = new LotMessageResponseData
                    {
                        LOTNO = strLOTNO,
                        PROCESSNO = strPROCESSNO,
                        OPNO = strOPNO,
                        PRODUCTNO = strPRODUCTNO,
                        CUSTOMERNO = strCUSTOMERNO
                    };
                    response = new LotMessageResponse
                    {
                        code = "0000",
                        message = "LotMessage successfully",
                        time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                        data = data,
                        extras = new Dictionary<string, object>(),
                        logId = "",
                        requestId = DateTime.Now.ToString("yyyyMMddHHmmssss")
                    };
                }
                else
                {
                    response = new LotMessageResponse
                    {
                        code = "1111",
                        message = "LotMessage ：" + strMsg,
                        time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                        data = new LotMessageResponseData(),
                        extras = new Dictionary<string, object>(),
                        logId = "",
                        requestId = DateTime.Now.ToString("yyyyMMddHHmmssss")
                    };
                }

                //resModel 转成JSON返回
                strResponseJson = JsonConvert.SerializeObject(response);
                GetLotMessage = strResponseJson;

                // 紀錄Transaction Log
                tcEAI_C.clsERP.addTransactionLog_ErrorCode(DateTime.Now.ToString("yyyyMMddHHmmssss"),
                    "WIP", "LotMessage",
                    "EAP", "EAP", DateTime.Now.ToString(),
                    strResult, strKeyValue, strMessage, strException, strRequestJson, strResponseJson, "LotMessage",
                    Conversions.ToInteger(System.Configuration.ConfigurationManager.AppSettings["keepLog"]),
                    Conversions.ToInteger(System.Configuration.ConfigurationManager.AppSettings["keepXML"]),
                    LogClass: "EAP");
            }

            return GetLotMessage;
        }
        #endregion

        #region "MACBLUE MAC&蓝牙地址生成接口"
        /// <summary>
        /// MAC&蓝牙地址生成接口
        /// </summary>
        public static string MACBLUE(object parameters)
        {
            string GetMACBLUE = default(string);
            string strResult = "";
            string strMsg = "";
            string strKeyValue = "";
            var strException = new List<ArrayList>();
            var strMessage = new List<ArrayList>();
            string strResponseJson = "";
            string strRequestJson = "";

            try
            {
                strRequestJson = funCheckJson(parameters);
                string responseStr = clsEAP.funMACBLUE(strRequestJson);

                var responseJson = JsonConvert.DeserializeObject<MACBLUEResponse>(responseStr);
                if (responseJson.code == "0000")
                {
                    strResult = "success";
                    strResponseJson = responseStr;
                }
                else
                {
                    strResult = "fail";
                    strMsg = responseJson.message;
                    strResponseJson = responseStr;
                }
            }
            catch (Exception ex)
            {
                strResult = "fail";
                strMsg = modWS.TranslateMsg(ex.Message);
            }
            finally
            {
                MACBLUEResponse response = null;
                if (strResult == "success")
                {
                    response = JsonConvert.DeserializeObject<MACBLUEResponse>(strResponseJson);
                }
                else
                {
                    response = new MACBLUEResponse
                    {
                        code = "1111",
                        message = "MACBLUE ：" + strMsg,
                        time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                        data = new MACBLUEResponseData(),
                        extras = new Dictionary<string, object>(),
                        logId = "",
                        requestId = DateTime.Now.ToString("yyyyMMddHHmmssss")
                    };
                    strResponseJson = JsonConvert.SerializeObject(response);
                }

                GetMACBLUE = strResponseJson;

                tcEAI_C.clsERP.addTransactionLog_ErrorCode(DateTime.Now.ToString("yyyyMMddHHmmssss"),
                    "WIP", "MACBLUE",
                    "EAP", "EAP", DateTime.Now.ToString(),
                    strResult, strKeyValue, strMessage, strException, strRequestJson, strResponseJson, "MACBLUE",
                    Conversions.ToInteger(System.Configuration.ConfigurationManager.AppSettings["keepLog"]),
                    Conversions.ToInteger(System.Configuration.ConfigurationManager.AppSettings["keepXML"]),
                    LogClass: "EAP");
            }

            return GetMACBLUE;
        }
        #endregion


    }
}