From 39e071022d213a0ea8bf6da519761e81722780e8 Mon Sep 17 00:00:00 2001 From: Yangwl <1726150332@qq.com> Date: Thu, 13 Jul 2023 09:56:19 +0800 Subject: [PATCH] =?UTF-8?q?SAP=E6=A8=A1=E5=9D=97=E5=90=8C=E6=AD=A5?= =?UTF-8?q?=E7=94=9F=E4=BA=A7=E8=AE=A2=E5=8D=95=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- op-modules/op-sap/pom.xml | 100 ++++++++++++++ .../main/java/com/op/mes/SapApplication.java | 23 ++++ .../java/com/op/mes/util/SAPConnUtils.java | 80 +++++++++++ .../main/java/com/op/mes/util/SapConn.java | 125 ++++++++++++++++++ .../java/com/op/mes/util/SapConnConfig.java | 106 +++++++++++++++ .../op-sap/src/main/resources/banner.txt | 10 ++ .../op-sap/src/main/resources/bootstrap.yml | 31 +++++ .../op-sap/src/main/resources/logback.xml | 74 +++++++++++ op-modules/pom.xml | 1 + 9 files changed, 550 insertions(+) create mode 100644 op-modules/op-sap/pom.xml create mode 100644 op-modules/op-sap/src/main/java/com/op/mes/SapApplication.java create mode 100644 op-modules/op-sap/src/main/java/com/op/mes/util/SAPConnUtils.java create mode 100644 op-modules/op-sap/src/main/java/com/op/mes/util/SapConn.java create mode 100644 op-modules/op-sap/src/main/java/com/op/mes/util/SapConnConfig.java create mode 100644 op-modules/op-sap/src/main/resources/banner.txt create mode 100644 op-modules/op-sap/src/main/resources/bootstrap.yml create mode 100644 op-modules/op-sap/src/main/resources/logback.xml diff --git a/op-modules/op-sap/pom.xml b/op-modules/op-sap/pom.xml new file mode 100644 index 00000000..22ccc618 --- /dev/null +++ b/op-modules/op-sap/pom.xml @@ -0,0 +1,100 @@ + + + + com.op + op-modules + 0.0.1 + + 4.0.0 + + op-modules-sap + + + op-modules-sap系统模块 + + + + + + + com.alibaba.cloud + spring-cloud-starter-alibaba-nacos-discovery + + + + + com.alibaba.cloud + spring-cloud-starter-alibaba-nacos-config + + + + + com.alibaba.cloud + spring-cloud-starter-alibaba-sentinel + + + + + org.springframework.boot + spring-boot-starter-actuator + + + + + io.springfox + springfox-swagger-ui + ${swagger.fox.version} + + + + + mysql + mysql-connector-java + + + + + com.op + op-common-datasource + + + + + com.op + op-common-datascope + + + + + com.op + op-common-log + + + + + com.op + op-common-swagger + + + + + + ${project.artifactId} + + + org.springframework.boot + spring-boot-maven-plugin + + + + repackage + + + + + + + + diff --git a/op-modules/op-sap/src/main/java/com/op/mes/SapApplication.java b/op-modules/op-sap/src/main/java/com/op/mes/SapApplication.java new file mode 100644 index 00000000..21ae6143 --- /dev/null +++ b/op-modules/op-sap/src/main/java/com/op/mes/SapApplication.java @@ -0,0 +1,23 @@ +package com.op.mes; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import com.op.common.security.annotation.EnableCustomConfig; +import com.op.common.security.annotation.EnableRyFeignClients; +import com.op.common.swagger.annotation.EnableCustomSwagger2; + +/** + * 系统模块 + * + * @author OP + */ +@EnableCustomConfig +@EnableCustomSwagger2 +@EnableRyFeignClients +@SpringBootApplication +public class SapApplication { + public static void main(String[] args) { + SpringApplication.run(SapApplication.class, args); + System.err.println("Sap服务启动成功"); + } +} diff --git a/op-modules/op-sap/src/main/java/com/op/mes/util/SAPConnUtils.java b/op-modules/op-sap/src/main/java/com/op/mes/util/SAPConnUtils.java new file mode 100644 index 00000000..8a03efad --- /dev/null +++ b/op-modules/op-sap/src/main/java/com/op/mes/util/SAPConnUtils.java @@ -0,0 +1,80 @@ +package com.op.mes.util; + +import com.sap.conn.jco.JCoDestination; +import com.sap.conn.jco.JCoDestinationManager; +import com.sap.conn.jco.JCoException; +import com.sap.conn.jco.ext.DestinationDataProvider; + +import java.io.File; +import java.io.FileOutputStream; +import java.util.Properties; + +public class SAPConnUtils { + private static final String ABAP_AS_POOLED = "ABAP_AS_WITH_POOL"; + + /** + * 创建SAP接口属性文件。 + * @param name ABAP管道名称 + * @param suffix 属性文件后缀 + * @param properties 属性文件内容 + */ + private static void createDataFile(String name, String suffix, Properties properties){ + File cfg = new File(name+"."+suffix); + if(cfg.exists()){ + cfg.deleteOnExit(); + } + try{ + FileOutputStream fos = new FileOutputStream(cfg, false); + properties.store(fos, "for tests only !"); + fos.close(); + }catch (Exception e){ + System.out.println("Create Data file fault, error msg: " + e.toString()); + throw new RuntimeException("Unable to create the destination file " + cfg.getName(), e); + } + } + + /** + * 初始化SAP连接 + */ + private static void initProperties(SapConn sapConn) { + Properties connectProperties = new Properties(); + // SAP服务器 + connectProperties.setProperty(DestinationDataProvider.JCO_ASHOST, sapConn.getJCO_ASHOST()); + // SAP系统编号 + connectProperties.setProperty(DestinationDataProvider.JCO_SYSNR, sapConn.getJCO_SYSNR()); + // SAP集团 + connectProperties.setProperty(DestinationDataProvider.JCO_CLIENT, sapConn.getJCO_CLIENT()); + // SAP用户名 + connectProperties.setProperty(DestinationDataProvider.JCO_USER, sapConn.getJCO_USER()); + // SAP密码 + connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD, sapConn.getJCO_PASSWD()); + // SAP登录语言 + connectProperties.setProperty(DestinationDataProvider.JCO_LANG, sapConn.getJCO_LANG()); + // 最大连接数 + connectProperties.setProperty(DestinationDataProvider.JCO_POOL_CAPACITY, sapConn.getJCO_POOL_CAPACITY()); + // 最大连接线程 + connectProperties.setProperty(DestinationDataProvider.JCO_PEAK_LIMIT, sapConn.getJCO_PEAK_LIMIT()); + // SAP ROUTER + connectProperties.setProperty(DestinationDataProvider.JCO_SAPROUTER, sapConn.getJCO_SAPROUTER()); + + createDataFile(ABAP_AS_POOLED, "jcoDestination", connectProperties); + } + + /** + * 获取SAP连接 + * @return SAP连接对象 + */ + public static JCoDestination connect(SapConn sapConn){ + System.out.println("正在连接至SAP..."); + JCoDestination destination = null; + initProperties(sapConn); + try { + destination = JCoDestinationManager.getDestination(ABAP_AS_POOLED); + destination.ping(); + System.out.println("已成功建立sap的连接"); + } catch (JCoException e) { + System.out.println("Connect SAP fault, error msg: " + e.toString()); + } + return destination; + } +} diff --git a/op-modules/op-sap/src/main/java/com/op/mes/util/SapConn.java b/op-modules/op-sap/src/main/java/com/op/mes/util/SapConn.java new file mode 100644 index 00000000..ef13634b --- /dev/null +++ b/op-modules/op-sap/src/main/java/com/op/mes/util/SapConn.java @@ -0,0 +1,125 @@ +package com.op.mes.util; + +public class SapConn { + // SAP服务器 + private String JCO_ASHOST; + // SAP系统编号 + private String JCO_SYSNR; + // SAP集团 + private String JCO_CLIENT; + // SAP用户名 + private String JCO_USER; + // SAP密码 + private String JCO_PASSWD; + // SAP登录语言 + private String JCO_LANG; + // 最大连接数 + private String JCO_POOL_CAPACITY; + // 最大连接线程 + private String JCO_PEAK_LIMIT; + // SAP ROUTER + private String JCO_SAPROUTER; + + public SapConn(String JCO_ASHOST, String JCO_SYSNR, String JCO_CLIENT, String JCO_USER, + String JCO_PASSWD, String JCO_LANG, String JCO_POOL_CAPACITY, String JCO_PEAK_LIMIT, + String JCO_SAPROUTER) { + this.JCO_ASHOST = JCO_ASHOST; + this.JCO_SYSNR = JCO_SYSNR; + this.JCO_CLIENT = JCO_CLIENT; + this.JCO_USER = JCO_USER; + this.JCO_PASSWD = JCO_PASSWD; + this.JCO_LANG = JCO_LANG; + this.JCO_POOL_CAPACITY = JCO_POOL_CAPACITY; + this.JCO_PEAK_LIMIT = JCO_PEAK_LIMIT; + this.JCO_SAPROUTER = JCO_SAPROUTER; + } + + public SapConn() {} + + public String getJCO_ASHOST() { + return JCO_ASHOST; + } + + public void setJCO_ASHOST(String JCO_ASHOST) { + this.JCO_ASHOST = JCO_ASHOST; + } + + public String getJCO_SYSNR() { + return JCO_SYSNR; + } + + public void setJCO_SYSNR(String JCO_SYSNR) { + this.JCO_SYSNR = JCO_SYSNR; + } + + public String getJCO_CLIENT() { + return JCO_CLIENT; + } + + public void setJCO_CLIENT(String JCO_CLIENT) { + this.JCO_CLIENT = JCO_CLIENT; + } + + public String getJCO_USER() { + return JCO_USER; + } + + public void setJCO_USER(String JCO_USER) { + this.JCO_USER = JCO_USER; + } + + public String getJCO_PASSWD() { + return JCO_PASSWD; + } + + public void setJCO_PASSWD(String JCO_PASSWD) { + this.JCO_PASSWD = JCO_PASSWD; + } + + public String getJCO_LANG() { + return JCO_LANG; + } + + public void setJCO_LANG(String JCO_LANG) { + this.JCO_LANG = JCO_LANG; + } + + public String getJCO_POOL_CAPACITY() { + return JCO_POOL_CAPACITY; + } + + public void setJCO_POOL_CAPACITY(String JCO_POOL_CAPACITY) { + this.JCO_POOL_CAPACITY = JCO_POOL_CAPACITY; + } + + public String getJCO_PEAK_LIMIT() { + return JCO_PEAK_LIMIT; + } + + public void setJCO_PEAK_LIMIT(String JCO_PEAK_LIMIT) { + this.JCO_PEAK_LIMIT = JCO_PEAK_LIMIT; + } + + public String getJCO_SAPROUTER() { + return JCO_SAPROUTER; + } + + public void setJCO_SAPROUTER(String JCO_SAPROUTER) { + this.JCO_SAPROUTER = JCO_SAPROUTER; + } + + @Override + public String toString() { + return "SapConn{" + + "JCO_ASHOST='" + JCO_ASHOST + '\'' + + ", JCO_SYSNR='" + JCO_SYSNR + '\'' + + ", JCO_CLIENT='" + JCO_CLIENT + '\'' + + ", JCO_USER='" + JCO_USER + '\'' + + ", JCO_PASSWD='" + JCO_PASSWD + '\'' + + ", JCO_LANG='" + JCO_LANG + '\'' + + ", JCO_POOL_CAPACITY='" + JCO_POOL_CAPACITY + '\'' + + ", JCO_PEAK_LIMIT='" + JCO_PEAK_LIMIT + '\'' + + ", JCO_SAPROUTER='" + JCO_SAPROUTER + '\'' + + '}'; + } +} diff --git a/op-modules/op-sap/src/main/java/com/op/mes/util/SapConnConfig.java b/op-modules/op-sap/src/main/java/com/op/mes/util/SapConnConfig.java new file mode 100644 index 00000000..fb5ecd7f --- /dev/null +++ b/op-modules/op-sap/src/main/java/com/op/mes/util/SapConnConfig.java @@ -0,0 +1,106 @@ +package com.op.mes.util; + +import com.sap.conn.jco.*; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +@Configuration +public class SapConnConfig { + static List> returnList = new ArrayList>(); + static JCoFunction function; + @Value("${sap.ashost}") + public String ashost; + + @Value("${sap.sysnr}") + public String sysnr; + + @Value("${sap.client}") + public String client; + + @Value("${sap.user}") + public String user; + + @Value("${sap.passwd}") + public String passwd; + + @Value("${sap.lang}") + public String lang; + + @Value("${sap.pool_capacity}") + public String pool_capacity; + + @Value("${sap.peak_limit}") + public String peak_limit; + + @Value("${sap.saprouter}") + public String saprouter; + @Bean + public void SapConn() { + SapConn con = new SapConn( + ashost, + sysnr, + client, + user, + passwd, + lang, + pool_capacity, + peak_limit, + saprouter + ); + // 连接 + JCoDestination jCoDestination = SAPConnUtils.connect(con); + RfcTest(jCoDestination); + + } + public static List> RfcTest(JCoDestination jCoDestination){ + try { + // 获取调用 RFC 函数对象 + JCoFunction func = jCoDestination.getRepository().getFunction("ZPPR_MES_PRO_TO"); + + // 配置传入参数 + JCoParameterList importParameterList = func.getImportParameterList(); + //importParameterList.setValue("S_AUFNR",001002125255 ); + + // 调用并获取返回值 + func.execute(jCoDestination); + // 获取 内表 - ZMES_PRO + JCoTable maraTable = func.getTableParameterList().getTable("ZMES_PRO"); + JCoRecordMetaData metaData = maraTable.getRecordMetaData(); + System.out.println("###"+metaData.toString()); + // 循环输出 Table 数据 + for (int i = 0; i < maraTable.getNumRows(); i++) { + maraTable.setRow(i); + String AUFNR = maraTable.getString("AUFNR"); + String AUART = maraTable.getString("AUART"); + String MAUFNR = maraTable.getString("MAUFNR"); + String PLNBEZ = maraTable.getString("PLNBEZ"); + String MAKTX = maraTable.getString("MAKTX"); + String PWERK = maraTable.getString("PWERK"); + String GAMNG = maraTable.getString("GAMNG"); + String GMEIN = maraTable.getString("GMEIN"); + String PLNNR = maraTable.getString("PLNNR"); + String GSTRP = maraTable.getString("GSTRP"); + String GLTRP = maraTable.getString("GLTRP"); + String ERNAM = maraTable.getString("ERNAM"); + String ERDAT = maraTable.getString("ERDAT"); + String STTXT = maraTable.getString("STTXT"); + +// System.out.println("物料编号:" + matnr + " - 创建日期:" + esdra + " - 创建人:" + ernam + " - 物料组:" + matkl + " - 单位:" + meins); + System.out.println("订单号:" +AUFNR+" - 订单类型:" + AUART+ " - 上级订单编号:" + MAUFNR+ " - 物料号:" + PLNBEZ+ " - 物料描述(短文本):" + MAKTX + + " - 订单的计划工厂:" + PWERK+ " - 订单数量总计:" + GAMNG+ " - 基本计量单位:" + GMEIN+ " - 任务清单组码:" + PLNNR+ " - 基本开始日期:" + GSTRP + + " - 基本完成日期:" + GLTRP+ " - 输入者:" + ERNAM+ " - 创建日期:" + ERDAT+ " - 系统状态:" + STTXT); + } + + + } catch (Exception e) { + e.printStackTrace(); + } + return returnList; + } + +} diff --git a/op-modules/op-sap/src/main/resources/banner.txt b/op-modules/op-sap/src/main/resources/banner.txt new file mode 100644 index 00000000..deffc073 --- /dev/null +++ b/op-modules/op-sap/src/main/resources/banner.txt @@ -0,0 +1,10 @@ +Spring Boot Version: ${spring-boot.version} +Spring Application Name: ${spring.application.name} + + ____ _____ _____ _____ + / __ \| __ \ / ____| /\ | __ \ + | | | | |__) |____| (___ / \ | |__) | + | | | | ___/______\___ \ / /\ \ | ___/ + | |__| | | ____) / ____ \| | + \____/|_| |_____/_/ \_\_| + diff --git a/op-modules/op-sap/src/main/resources/bootstrap.yml b/op-modules/op-sap/src/main/resources/bootstrap.yml new file mode 100644 index 00000000..77a2432a --- /dev/null +++ b/op-modules/op-sap/src/main/resources/bootstrap.yml @@ -0,0 +1,31 @@ +# Tomcat +server: + port: 9205 + +# Spring +spring: + application: + # 应用名称 + name: op-sap + profiles: + # 环境配置 + active: dev + cloud: + nacos: + discovery: + namespace: haiwei-test + group: ywl + # 服务注册地址 + server-addr: 140.249.53.142:8848 + config: + namespace: haiwei-test + group: ywl + #命名空间 + #group: local + # 配置中心地址 + server-addr: 140.249.53.142:8848 + # 配置文件格式 + file-extension: yml + # 共享配置 + shared-configs: + - application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension} diff --git a/op-modules/op-sap/src/main/resources/logback.xml b/op-modules/op-sap/src/main/resources/logback.xml new file mode 100644 index 00000000..1b5726ff --- /dev/null +++ b/op-modules/op-sap/src/main/resources/logback.xml @@ -0,0 +1,74 @@ + + + + + + + + + + + ${log.pattern} + + + + + + ${log.path}/info.log + + + + ${log.path}/info.%d{yyyy-MM-dd}.log + + 60 + + + ${log.pattern} + + + + INFO + + ACCEPT + + DENY + + + + + ${log.path}/error.log + + + + ${log.path}/error.%d{yyyy-MM-dd}.log + + 60 + + + ${log.pattern} + + + + ERROR + + ACCEPT + + DENY + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/op-modules/pom.xml b/op-modules/pom.xml index 2c8c70a8..39c2789c 100644 --- a/op-modules/pom.xml +++ b/op-modules/pom.xml @@ -15,6 +15,7 @@ op-file op-mes op-wms + op-sap op-modules