/// <summary>
        /// 获取数据
        /// </summary>
        /// <returns></returns>
        public ETLSapTableResultCollection GetData(string sapTableName)
        {
            using (var srv = new Saplocalhost.WebServiceConnectSAPSoapClient())
            {
                // 新的取得SAP连接信息
                string sapInstanceId = this.hidSAPInstanceId.Value;
                sapInstanceId.CheckStringIsNullOrEmpty("sapInstanceId");
                SAPClient Params = SAPClientAdapter.Instance.LoadByID(sapInstanceId);

                Saplocalhost.SAPPara param = new Saplocalhost.SAPPara()
                {
                    ApplicationServer = Params.ApplicationServer,
                    Client            = Params.Client,
                    Language          = Params.Language,
                    Password          = Params.Password,
                    SystemNumber      = int.Parse(Params.SystemNumber),
                    User                 = Params.User,
                    AppServerService     = Params.AppServerService,
                    LogonGroup           = Params.LogonGroup,
                    MessageServerHost    = Params.MessageServerHost,
                    MessageServerService = Params.MessageServerService,
                    SystemID             = Params.SystemID
                };

                DataTable table = srv.SAP_TableFiled_Get(sapTableName, param);

                return(GetDatas(table));
            }
        }
        public void TestMethod1()
        {
            //XMLHelper helper = new XMLHelper();
            //var model = helper.Do();

            //Console.WriteLine(model.ReqeustData.RequestInfo.Content.Items.Count);

            string test = "<?xml version=\"1.0\" encoding=\"utf - 8\"?><SERVICE><CONTROL><SERVICEID>INT_MM000200035</SERVICEID><SRVGUID>005056B5CE271EEA89BEB9DF4D36F5BA</SRVGUID><SRVTIMESTAMP>20191224023434</SRVTIMESTAMP></CONTROL><DATA><REQUEST><REQID>005056B5CE271EEA89BEB9DF4D3755BA</REQID><CONTENT><ITMS><WMASN>ASN191223500717</WMASN><EBELN>4500122312</EBELN><EBELP>00010</EBELP><BSART>NB</BSART><LIFNR>0020000150</LIFNR><NAME1>南通海益包装有限公司</NAME1><MATNR>000000640301001881</MATNR><GRQTY>500.000</GRQTY><DHDAT>20191224</DHDAT><WERKS>1101</WERKS><LGORT>Z192</LGORT><MES_INS_RESULT>Y1</MES_INS_RESULT><MBLNR>5000815635</MBLNR><MJAHR>2019</MJAHR><ZEILE>0001</ZEILE><BWART>105</BWART></ITMS><ITMS><WMASN>ASN191223500717</WMASN><EBELN>4500122312</EBELN><EBELP>00020</EBELP><BSART>NB</BSART><LIFNR>0020000150</LIFNR><NAME1>南通海益包装有限公司</NAME1><MATNR>000000640301001882</MATNR><GRQTY>200.000</GRQTY><DHDAT>20191224</DHDAT><WERKS>1101</WERKS><LGORT>Z192</LGORT><MES_INS_RESULT>Y1</MES_INS_RESULT><MBLNR>5000815635</MBLNR><MJAHR>2019</MJAHR><ZEILE>0002</ZEILE><BWART>105</BWART></ITMS><ITMS><WMASN>ASN191223500717</WMASN><EBELN>4500122312</EBELN><EBELP>00030</EBELP><BSART>NB</BSART><LIFNR>0020000150</LIFNR><NAME1>南通海益包装有限公司</NAME1><MATNR>000000640301002137</MATNR><GRQTY>120.000</GRQTY><DHDAT>20191224</DHDAT><WERKS>1101</WERKS><LGORT>Z192</LGORT><MES_INS_RESULT>Y1</MES_INS_RESULT><MBLNR>5000815635</MBLNR><MJAHR>2019</MJAHR><ZEILE>0003</ZEILE><BWART>105</BWART></ITMS><ITMS><WMASN>ASN191223500717</WMASN><EBELN>4500122312</EBELN><EBELP>00040</EBELP><BSART>NB</BSART><LIFNR>0020000150</LIFNR><NAME1>南通海益包装有限公司</NAME1><MATNR>000000640301002202</MATNR><GRQTY>80.000</GRQTY><DHDAT>20191224</DHDAT><WERKS>1101</WERKS><LGORT>Z192</LGORT><MES_INS_RESULT>Y1</MES_INS_RESULT><MBLNR>5000815635</MBLNR><MJAHR>2019</MJAHR><ZEILE>0004</ZEILE><BWART>105</BWART></ITMS><ITMS><WMASN>ASN191223500717</WMASN><EBELN>4500122312</EBELN><EBELP>00050</EBELP><BSART>NB</BSART><LIFNR>0020000150</LIFNR><NAME1>南通海益包装有限公司</NAME1><MATNR>000000640501000067</MATNR><GRQTY>378.000</GRQTY><DHDAT>20191224</DHDAT><WERKS>1101</WERKS><LGORT>Z192</LGORT><MES_INS_RESULT>Y1</MES_INS_RESULT><MBLNR>5000815635</MBLNR><MJAHR>2019</MJAHR><ZEILE>0005</ZEILE><BWART>105</BWART></ITMS><ITMS><WMASN>ASN191223500717</WMASN><EBELN>4500122312</EBELN><EBELP>00060</EBELP><BSART>NB</BSART><LIFNR>0020000150</LIFNR><NAME1>南通海益包装有限公司</NAME1><MATNR>000000640501000067</MATNR><GRQTY>225.000</GRQTY><DHDAT>20191224</DHDAT><WERKS>1101</WERKS><LGORT>Z192</LGORT><MES_INS_RESULT>Y1</MES_INS_RESULT><MBLNR>5000815635</MBLNR><MJAHR>2019</MJAHR><ZEILE>0006</ZEILE><BWART>105</BWART></ITMS><ITMS><WMASN>ASN191223500717</WMASN><EBELN>4500122312</EBELN><EBELP>00070</EBELP><BSART>NB</BSART><LIFNR>0020000150</LIFNR><NAME1>南通海益包装有限公司</NAME1><MATNR>000000640501000119</MATNR><GRQTY>500.000</GRQTY><DHDAT>20191224</DHDAT><WERKS>1101</WERKS><LGORT>Z192</LGORT><MES_INS_RESULT>Y1</MES_INS_RESULT><MBLNR>5000815635</MBLNR><MJAHR>2019</MJAHR><ZEILE>0007</ZEILE><BWART>105</BWART></ITMS><ITMS><WMASN>ASN191223500717</WMASN><EBELN>4500122312</EBELN><EBELP>00080</EBELP><BSART>NB</BSART><LIFNR>0020000150</LIFNR><NAME1>南通海益包装有限公司</NAME1><MATNR>000000640501000119</MATNR><GRQTY>120.000</GRQTY><DHDAT>20191224</DHDAT><WERKS>1101</WERKS><LGORT>Z192</LGORT><MES_INS_RESULT>Y1</MES_INS_RESULT><MBLNR>5000815635</MBLNR><MJAHR>2019</MJAHR><ZEILE>0008</ZEILE><BWART>105</BWART></ITMS><ITMS><WMASN>ASN191223500717</WMASN><EBELN>4500122312</EBELN><EBELP>00090</EBELP><BSART>NB</BSART><LIFNR>0020000150</LIFNR><NAME1>南通海益包装有限公司</NAME1><MATNR>000000640501000140</MATNR><GRQTY>200.000</GRQTY><DHDAT>20191224</DHDAT><WERKS>1101</WERKS><LGORT>Z192</LGORT><MES_INS_RESULT>Y1</MES_INS_RESULT><MBLNR>5000815635</MBLNR><MJAHR>2019</MJAHR><ZEILE>0009</ZEILE><BWART>105</BWART></ITMS><ITMS><WMASN>ASN191223500717</WMASN><EBELN>4500122312</EBELN><EBELP>00100</EBELP><BSART>NB</BSART><LIFNR>0020000150</LIFNR><NAME1>南通海益包装有限公司</NAME1><MATNR>000000640501000140</MATNR><GRQTY>80.000</GRQTY><DHDAT>20191224</DHDAT><WERKS>1101</WERKS><LGORT>Z192</LGORT><MES_INS_RESULT>Y1</MES_INS_RESULT><MBLNR>5000815635</MBLNR><MJAHR>2019</MJAHR><ZEILE>0010</ZEILE><BWART>105</BWART></ITMS><ITMS><WMASN>ASN191223500717</WMASN><EBELN>4500122312</EBELN><EBELP>00110</EBELP><BSART>NB</BSART><LIFNR>0020000150</LIFNR><NAME1>南通海益包装有限公司</NAME1><MATNR>000000640501000176</MATNR><GRQTY>112.000</GRQTY><DHDAT>20191224</DHDAT><WERKS>1101</WERKS><LGORT>Z192</LGORT><MES_INS_RESULT>Y1</MES_INS_RESULT><MBLNR>5000815635</MBLNR><MJAHR>2019</MJAHR><ZEILE>0011</ZEILE><BWART>105</BWART></ITMS></CONTENT></REQUEST></DATA></SERVICE>";

            IClient client = new SAPClient();

            Debug.WriteLine(client.Excute(test));

            Assert.IsTrue(true);
        }
        private SAPPara buildSapLogonParam(SAPClient sapParamLoad)
        {
            var sapParam = new SAPPara()
            {
                ApplicationServer = sapParamLoad.ApplicationServer,
                Client            = sapParamLoad.Client,
                Language          = sapParamLoad.Language,
                Password          = sapParamLoad.Password,
                SystemNumber      = int.Parse(sapParamLoad.SystemNumber),
                User = sapParamLoad.User,
                MessageServerHost    = sapParamLoad.MessageServerHost,
                MessageServerService = sapParamLoad.MessageServerService,
                LogonGroup           = sapParamLoad.LogonGroup,
                AppServerService     = sapParamLoad.AppServerService,
                SystemID             = sapParamLoad.SystemID
            };

            return(sapParam);
        }
        public ServerResult ExecuteSAPRFC(string rfcName, string jsonData, string SAPInstanceId)
        {
            ServerResult result = new ServerResult();

            try
            {
                //反序列化
                DEEntityInstanceBase resultInstance = JSONSerializerExecute.Deserialize <DEEntityInstanceBase>(jsonData);

                //调用sap服务
                #region
                if (string.IsNullOrEmpty(SAPInstanceId))
                {
                    throw new Exception("请检查Config文件中是否配置了SAPInstanceId");
                }

                //todo:目前实体跟sap结构是1对1 所以只取第一个外部实体,将来会改成一对多,需要调用方传入外部实体名
                OuterEntity outerEntity = resultInstance.EntityDefine.OuterEntities.FirstOrDefault();
                outerEntity.NullCheck(string.Format("找不到实体定义【{0}】的外部实体!", resultInstance.EntityDefine.CodeName));

                string          tCode  = outerEntity.Name;
                List <SapValue> values = resultInstance.ToParams(tCode);

                //sap服务定义
                //Saplocalhost.WebServiceConnectSAP sapService = new Saplocalhost.WebServiceConnectSAP();
                var sapService = new Saplocalhost.WebServiceConnectSAPSoapClient();
                Saplocalhost.InstanceParam sappar = new Saplocalhost.InstanceParam();

                #region 给sap参数赋值

                //todo:这的转换不太好,以后WebService改为WCF后会解决此问题
                sappar.SapType = outerEntity.CustomType == InType.CustomInterface ? Saplocalhost.InType.CustomInterface : Saplocalhost.InType.StandardInterface;
                sappar.Values  = ChangeToWSValueType(values);

                //获取Sap用户信息
                string errorMessage = string.Empty;

                // 获取SAP用户连接信息
                SAPClient sapParamLoad = SAPClientAdapter.Instance.LoadByID(SAPInstanceId);

                if (sapParamLoad == null)
                {
                    errorMessage = "请检查Config文件的SAPInstanceId的配置项";
                }
                //if (string.IsNullOrEmpty(sapParamLoad.ApplicationServer) || string.IsNullOrEmpty(sapParamLoad.SystemNumber)
                //     || string.IsNullOrEmpty(sapParamLoad.Client) || string.IsNullOrEmpty(sapParamLoad.User)
                //     || string.IsNullOrEmpty(sapParamLoad.Password) || string.IsNullOrEmpty(sapParamLoad.Language))
                //{
                //    errorMessage = "请检查SAP连接数据";
                //}
                if (!string.IsNullOrEmpty(errorMessage))
                {
                    throw new Exception(errorMessage);
                }

                #endregion

                //构造SAP登录参数
                var sapParam = buildSapLogonParam(sapParamLoad);

                //返回值
                string resultSap = string.Empty;
                Saplocalhost.InstanceParam returnValue = sapService.ExecuteSAPRFC(rfcName, sappar, sapParam);

                resultInstance.FromParams(ChangeToSapValue(returnValue.Values.ToArray()));
                //将返回结果序列化之后return给上一层(解决WS传输问题)
                result.ObjResult = JSONSerializerExecute.Serialize(resultInstance);
                #endregion
            }
            catch (Exception e)
            {
                result.IsSuccess = false;
                result.Result    = string.Format("{{'ErrorMsg':'{0}'}}", e.Message);
            }

            return(result);
        }
        public ServerResult ExecuteSAPBDC(string jsonData, string clientID)
        {
            ServerResult result    = new ServerResult();
            string       resultSap = string.Empty;

            try
            {
                //反序列化
                DEEntityInstanceBase resultInstance = JSONSerializerExecute.Deserialize <DEEntityInstanceBase>(jsonData);

                DEInstanceAdapter.Instance.Update(resultInstance);

                //调用sap服务
                #region

                //todo:目前实体跟sap结构是1对1 所以只取第一个外部实体,将来会改成一对多,需要调用方传入外部实体名
                OuterEntity outerEntity = resultInstance.EntityDefine.OuterEntities.FirstOrDefault();
                outerEntity.NullCheck(string.Format("找不到实体定义【{0}】的外部实体!", resultInstance.EntityDefine.CodeName));

                string          tCode  = outerEntity.Name;
                List <SapValue> values = resultInstance.ToParams(tCode);

                //sap服务定义
                Saplocalhost.WebServiceConnectSAPSoapClient sapService = new Saplocalhost.WebServiceConnectSAPSoapClient();
                Saplocalhost.InstanceParam sappar = new Saplocalhost.InstanceParam();

                #region 给sap参数赋值
                //todo:这的转换不太好,以后WebService改为WCF后会解决此问题
                sappar.SapType = outerEntity.CustomType == InType.CustomInterface ? Saplocalhost.InType.CustomInterface : Saplocalhost.InType.StandardInterface;
                sappar.Values  = GetRealSapValues(values);

                //SAPLoginParams Params = SAPLoginParams.GetConfig();

                string errorMessage = string.Empty;

                // 获取SAP用户连接信息
                SAPClient sapParamLoad = SAPClientAdapter.Instance.LoadByID(clientID);

                if (sapParamLoad == null)
                {
                    errorMessage = "请检查Config文件的SapAppServer和SapClient配置项";
                }
                if (string.IsNullOrEmpty(sapParamLoad.ApplicationServer) || string.IsNullOrEmpty(sapParamLoad.Client))
                {
                    errorMessage = "请检查Config文件的SapAppServer和SapClient配置项";
                }
                if (!string.IsNullOrEmpty(errorMessage))
                {
                    throw new Exception(errorMessage);
                }

                #endregion

                var sapParam = buildSapLogonParam(sapParamLoad);

                //返回值
                //(问海军)
                DataTable returnResult = sapService.ExecuteSAP(out resultSap, tCode, sappar, sapParam);
                returnResult.TableName = "sapResult";
                result.ObjResult       = SerializeDataTableXml(returnResult);

                #endregion
            }
            catch (Exception e)
            {
                result.IsSuccess = false;
                result.Result    = e.Message;
            }

            return(result);
        }