diff --git a/admin.net.pro/Admin.NET/Vistar.Application/SapService/Dto/SapBomInput.cs b/admin.net.pro/Admin.NET/Vistar.Application/SapService/Dto/SapBomInput.cs new file mode 100644 index 0000000..e6c405e --- /dev/null +++ b/admin.net.pro/Admin.NET/Vistar.Application/SapService/Dto/SapBomInput.cs @@ -0,0 +1,47 @@ +// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。 +// +// 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。 +// +// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任! + +using OnceMi.AspNetCore.OSS; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Vistar.Application.SapService.Dto; +public class IS_REQ +{ + public string ReqKeyId { get; set; } + public string BusinessId { get; set; } + public string MessageId { get; set; } + public string SndPrn { get; set; } + public string RcvPrn { get; set; } + public string ReqUser { get; set; } + public string Note1 { get; set; } + public string Note2 { get; set; } + public string Note3 { get; set; } + public string Zwbid { get; set; } + public string Matnr { get; set; } + public string Werks { get; set; } + public string Bmeng { get; set; } + public string Aennr { get; set; } + public string Datuv { get; set; } + public List ItemList { get; set; } +} + +public class ItemData +{ + public string POSNR { get; set; } + public string IDNRK { get; set; } + public string MENGE { get; set; } + public string SORTF { get; set; } + public string ITISOB { get; set; } + public string ALPGR { get; set; } + public string ALPRF { get; set; } + public string ZDELETE { get; set; } + public string POSTP { get; set; } + public string ZYFMK { get; set; } +} diff --git a/admin.net.pro/Admin.NET/Vistar.Application/SapService/Dto/SapMaterialInput.cs b/admin.net.pro/Admin.NET/Vistar.Application/SapService/Dto/SapMaterialInput.cs new file mode 100644 index 0000000..b96b3b3 --- /dev/null +++ b/admin.net.pro/Admin.NET/Vistar.Application/SapService/Dto/SapMaterialInput.cs @@ -0,0 +1,48 @@ +// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。 +// +// 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。 +// +// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任! + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Vistar.Application.SapService.Dto; +public class SapMaterialInput +{ + public string Reqkeyid { get; set; } + public string Businessid { get; set; } + public string Messageid { get; set; } + public string Sndprn { get; set; } + public string Rcvprn { get; set; } + public string Requser { get; set; } + public string Note1 { get; set; } + public string Note2 { get; set; } + public string Note3 { get; set; } + + public string Zwbid { get; set; } + public string Matnr { get; set; } + public string Mbrsh { get; set; } + public string Mtart { get; set; } + public string Maktx { get; set; } + public string Meins { get; set; } + public string Matkl { get; set; } + public string Bismt { get; set; } + public string Groes { get; set; } + public string Normt { get; set; } + public string Ferth { get; set; } + public string Zeinr { get; set; } + public string Mstae { get; set; } + public string Raube { get; set; } + public string Mhdrz { get; set; } + public string Mhdhb { get; set; } + public string Werks { get; set; } + public string Beskz { get; set; } + public string Sobsl { get; set; } + public string Schgt { get; set; } + public string Rgekz { get; set; } + public string Zbom { get; set; } +} diff --git a/admin.net.pro/Admin.NET/Vistar.Application/SapService/SapService.cs b/admin.net.pro/Admin.NET/Vistar.Application/SapService/SapService.cs new file mode 100644 index 0000000..b2a9a00 --- /dev/null +++ b/admin.net.pro/Admin.NET/Vistar.Application/SapService/SapService.cs @@ -0,0 +1,303 @@ +// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。 +// +// 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。 +// +// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任! + +using Admin.NET.Core; +using Admin.NET.Core.Service; +using DocumentFormat.OpenXml.ExtendedProperties; +using Furion.DependencyInjection; +using Furion.DynamicApiController; +using MailKit; +using OnceMi.AspNetCore.OSS; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net.Http.Headers; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; +using Vistar.Application.SapService.Dto; +using Vistar.Application.Util; + +namespace Vistar.Application.SapService; + +public class SapService : IDynamicApiController, ITransient +{ + private readonly SysConfigService _sysConfigService; + public SapService(SysConfigService sysConfigService) + { + _sysConfigService = sysConfigService; + } + /// + /// SAP同步物料 + /// + /// + /// + public async Task SapMaterialApi(SapMaterialInput input) + { + // 创建SOAP XML请求 + var soapEnvelope = new XDocument( + new XDeclaration("1.0", "utf-8", "yes"), + new XElement(XName.Get("Envelope", "http://schemas.xmlsoap.org/soap/envelope/"), + new XElement(XName.Get("Header", "http://schemas.xmlsoap.org/soap/envelope/")), + new XElement(XName.Get("Body", "http://schemas.xmlsoap.org/soap/envelope/"), + new XElement(XName.Get("Zmmfm012", "urn:sap-com:document:sap:soap:functions:mc-style"), + new XElement("IsReq", + new XElement("Reqkeyid", input.Reqkeyid), + new XElement("Businessid", input.Businessid), + new XElement("Messageid", input.Messageid), + new XElement("Sndprn", input.Sndprn), + new XElement("Rcvprn", input.Rcvprn), + new XElement("Requser", input.Requser), + new XElement("Note1", input.Note1), + new XElement("Note2", input.Note2), + new XElement("Note3", input.Note3) + ), + new XElement("ItData", + new XElement("item", + new XElement("Zwbid", input.Zwbid), + new XElement("Matnr", input.Matnr), + new XElement("Mbrsh", input.Mbrsh), + new XElement("Mtart", input.Mtart), + new XElement("Maktx", input.Maktx), + new XElement("Meins", input.Meins), + new XElement("Matkl", input.Matkl), + new XElement("Bismt", input.Bismt), + new XElement("Groes", input.Groes), + new XElement("Normt", input.Normt), + new XElement("Ferth", input.Ferth), + new XElement("Zeinr", input.Zeinr), + new XElement("Mstae", input.Mstae), + new XElement("Raube", input.Raube), + new XElement("Mhdrz", input.Mhdrz), + new XElement("Mhdhb", input.Mhdhb), + new XElement("PlantView", + new XElement("item", + new XElement("Werks", input.Werks), + new XElement("Beskz", input.Beskz), + new XElement("Sobsl", input.Sobsl), + new XElement("Schgt", input.Schgt), + new XElement("Rgekz", input.Rgekz), + new XElement("Zbom", input.Zbom) + ) + ) + ) + ) + ) + ) + ) + ); + var httpClientHandler = new HttpClientHandler + { + ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => true, + ClientCertificateOptions = ClientCertificateOption.Manual + }; + try + { + using (var httpClient = new HttpClient(httpClientHandler)) + { + // 设置基本身份验证信息 + var username = await _sysConfigService.GetConfigValueByCode(ConfigConst.SapUserName); + var password = await _sysConfigService.GetConfigValueByCode(ConfigConst.SapPassword); + var credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes($"{username}:{password}")); + httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", credentials); + // 将 XML 内容转换为字符串并设置请求内容类型为 text/xml + var content = new StringContent(soapEnvelope.ToString(), Encoding.UTF8, "text/xml"); + content.Headers.ContentType = new MediaTypeHeaderValue("text/xml"); + // 设置 SAP Web 服务的 URL + var url = "https://vhjqeds4ci.sap.vistar-eq.com:44300/sap/bc/srt/rfc/sap/zmmfm012/120/zmmfm012/zmmfm012"; + + // 发起 POST 请求到 SAP Web 服务 + var response = await httpClient.PostAsync(url, content); + if (response.IsSuccessStatusCode) + { + var result = await response.Content.ReadAsStringAsync(); + XDocument doc = XDocument.Parse(result); + var item = doc.Descendants("item").FirstOrDefault(); + var zwbid = item?.Element("Zwbid")?.Value; + var matnr = item?.Element("Matnr")?.Value; + var code = item?.Element("Code")?.Value; + var msg = item?.Element("Msg")?.Value; + var output = new SapOutput() + { + code = code, + msg = msg, + result = result + }; + return output; + } + else { + var result = await response.Content.ReadAsStringAsync(); + var output = new SapOutput() + { + code = "失败", + msg = "调用失败,状态码:"+response.StatusCode, + result = result + }; + return output; + } + } + } + catch (Exception ex) + { + var output = new SapOutput() + { + code = "失败", + msg = "发生错误" + ex.Message, + result = "发生错误" + ex + }; + return output; + } + + } + + /// + /// SAP同步BOM + /// + /// + /// + public async Task SapBomApi(IS_REQ input) + { + // 创建SOAP XML请求 + var soapEnvelope = new XDocument( + new XDeclaration("1.0", "utf-8", "yes"), + new XElement(XName.Get("Envelope", "http://schemas.xmlsoap.org/soap/envelope/"), + new XElement(XName.Get("Header", "http://schemas.xmlsoap.org/soap/envelope/")), + new XElement(XName.Get("Body", "http://schemas.xmlsoap.org/soap/envelope/"), + new XElement(XName.Get("ZPPFM001", "urn:sap-com:document:sap:rfc:functions"), + new XElement("IS_REQ", + new XElement("REQKEYID", input.ReqKeyId), + new XElement("BUSINESSID", input.BusinessId), + new XElement("MESSAGEID", input.MessageId), + new XElement("SNDPRN", input.SndPrn), + new XElement("RCVPRN", input.RcvPrn), + new XElement("REQUSER", input.ReqUser), + new XElement("NOTE1", input.Note1), + new XElement("NOTE2", input.Note2), + new XElement("NOTE3", input.Note3) + ), + new XElement("IT_DATA", + new XElement("item", + new XElement("ZWBID", input.Zwbid), + new XElement("MATNR", input.Matnr), + new XElement("WERKS", input.Werks), + new XElement("BMENG", input.Bmeng), + new XElement("AENNR", input.Aennr), + new XElement("DATUV", input.Datuv), + new XElement("ITEM", + // 使用循环生成 元素 + GenerateItemElements(input.ItemList) + ) + ) + ) + ) + ) + ) + ); + var httpClientHandler = new HttpClientHandler + { + ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => true, + ClientCertificateOptions = ClientCertificateOption.Manual + }; + try + { + using (var httpClient = new HttpClient(httpClientHandler)) + { + // 设置基本身份验证信息 + var username = await _sysConfigService.GetConfigValueByCode(ConfigConst.SapUserName); + var password = await _sysConfigService.GetConfigValueByCode(ConfigConst.SapPassword); + var credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes($"{username}:{password}")); + httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", credentials); + // 将 XML 内容转换为字符串并设置请求内容类型为 text/xml + var content = new StringContent(soapEnvelope.ToString(), Encoding.UTF8, "text/xml"); + content.Headers.ContentType = new MediaTypeHeaderValue("text/xml"); + // 设置 SAP Web 服务的 URL + var url = "https://vhjqeds4ci.sap.vistar-eq.com:44300/sap/bc/srt/rfc/sap/zppfm001/120/zppfm001/zppfm001"; + + // 发起 POST 请求到 SAP Web 服务 + var response = await httpClient.PostAsync(url, content); + + if (response.IsSuccessStatusCode) + { + var result = await response.Content.ReadAsStringAsync(); + XDocument doc = XDocument.Parse(result); + var item = doc.Descendants("item").FirstOrDefault(); + var code = item?.Element("CODE")?.Value; + var msg = item?.Element("MSG")?.Value; + if (item == null) + { + var ES_RETItem = doc.Descendants("ES_RET").FirstOrDefault(); + var ES_RETcode = ES_RETItem?.Element("CODE")?.Value; + var ES_RETmsg = ES_RETItem?.Element("MSG")?.Value; + var outputES_RET = new SapOutput() + { + code = ES_RETcode, + msg = ES_RETmsg, + result = result + }; + return outputES_RET; + } + var output = new SapOutput() + { + code = code, + msg = msg, + result = result + }; + return output; + } + else + { + var result = await response.Content.ReadAsStringAsync(); + var output = new SapOutput() + { + code = "失败", + msg = "调用失败,状态码: " + response.StatusCode, + result = result + }; + return output; + } + } + } + catch (Exception ex) + { + // 记录错误日志 + Console.WriteLine("发生错误: " + ex.Message); + var output = new SapOutput() + { + code = "失败", + msg = "发生错误" + ex.Message, + result = "发生错误"+ex + }; + return output; + } + } + + public List GenerateItemElements(List items) + { + var itemElements = new List(); + + foreach (var item in items) + { + var element = new XElement("item", + new XElement("POSNR", item.POSNR), + new XElement("IDNRK", item.IDNRK), + new XElement("MENGE", item.MENGE), + new XElement("SORTF", item.SORTF), + new XElement("ITSOB", item.ITISOB), + new XElement("ALPGR", item.ALPGR), + new XElement("ALPRF", item.ALPRF), + new XElement("ZDELETE", item.ZDELETE), + new XElement("POSTP", item.POSTP), + new XElement("ZYFMK", item.ZYFMK) + ); + + itemElements.Add(element); + } + + return itemElements; + } +} + +