泛微E8和SAP对接工具类

package com.customcode.sap.util;

import com.customcode.util.NewXmActionLog;
import com.sap.mw.jco.JCO;
import weaver.interfaces.sap.SAPConn;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by IntelliJ IDEA
 *
 * @author
 * @createDate 2018-10-20 22:49
 * @version:v1.0
 */
public class SapUtils {
    private NewXmActionLog log = new NewXmActionLog();
    private String logName = "SapUtils";

    /**
     * 执行SAP
     *
     * @param functionName RFC函数名称
     * @param importParams 输入参数
     * @param importTable  输入表
     * @param exportParams 输出参数
     * @param exportTable  输出表
     * @param myConnection sap连接
     * @return Map 含 exportParameter 输出参数 exportTable 输出表
     */
    public Map<String, Object> sapExecute(String functionName, Map<String, Object> importParams, Map<String, List<Map<String, Object>>> importTable, Map<String, Object> exportParams, Map<String, List<Map<String, Object>>> exportTable, JCO.Client myConnection) {
        /*存储最终结果:含 exportParameter 输出参数 exportTable 输出表*/
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            //1.0、开启SAP连接
            myConnection.connect();
            SAPConn sapConn = new SAPConn();
            JCO.Function function = sapConn.excuteBapi(functionName, myConnection);
            if (null == function) {
                log.error(logName, "SAP_RFC中没有此函数:" + functionName);
            }
            /*1.1、输入参数 */
            try {
                if (null != importParams && !importParams.isEmpty()) {
                    JCO.ParameterList importParameterList = function.getImportParameterList();
                    Set<String> keySet = importParams.keySet();
                    for (String key : keySet) {
                        importParameterList.setValue(importParams.get(key), key);
                    }
                }
            } catch (Exception e) {
                log.error(logName, "SAP输入参数异常:" + e);
            }
            /*1.1.2、输入表 */
            try {
                if (null != importTable && !importTable.isEmpty()) {
                    JCO.ParameterList tableParameterList = function.getTableParameterList();
                    // 表名集合
                    Set<String> keySet = importTable.keySet();
                    for (String key : keySet) {
                        JCO.Table table = tableParameterList.getTable(key);
                        List<Map<String, Object>> paramsList = importTable.get(key);
                        for (int li = 0; li < paramsList.size(); li++) {
                            // 要先追加行才能写入
                            table.appendRow();
                            Map<String, Object> paramsMap = paramsList.get(li);
                            Set<String> paramsKeySet = paramsMap.keySet();
                            for (String paramKey : paramsKeySet) {
                                table.setValue(paramsMap.get(paramKey), paramKey);
                            }
                        }
                    }
                }
            } catch (Exception e) {
                log.error(logName, "SAP输入表异常:" + e);
            }
            /*1.2、执行配置好的function */
            myConnection.execute(function);
            /*1.3、输出参数 */
            try {
                if (exportParams != null && !exportParams.isEmpty()) {
                    JCO.ParameterList exportParameterList = function.getExportParameterList();
                    int fieldCount = exportParameterList.getFieldCount();
                    for (int i = 0; i < fieldCount; i++) {
                        JCO.Field field = exportParameterList.getField(i);
                        exportParams.put(field.getName(), field.getValue());
                    }
                    result.put("exportParameter", exportParams);
                }
            } catch (Exception e) {
                log.error(logName, "SAP输出参数异常:" + e);
            }
            /*1.4、输出表 :一个表用一个list存多行,一个行用一个map存储*/
            try {
                if (null != exportTable && !exportTable.isEmpty()) {
                    JCO.ParameterList tableParameterList = function.getTableParameterList();
                    Set<String> keySet = exportTable.keySet();
                    for (String key : keySet) {
                        /*表*/
                        JCO.Table table = tableParameterList.getTable(key);
                        JCO.Field field = null;
                        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
                        /*行*/
                        int numRows = table.getNumRows();
                        for (int i = 0; i < numRows; i++) {
                            Map<String, Object> map = new HashMap<String, Object>();
                            /*列*/
                            int numColumns = table.getNumColumns();
                            for (int j = 0; j < numColumns; j++) {
                                field = table.getField(j);
                                map.put(field.getName(), field.getValue());
                            }
                            list.add(map);
                            /*下一行*/
                            table.nextRow();
                        }
                        exportTable.put(key, list);
                    }
                    result.put("exportTable", exportTable);
                }
            } catch (Exception e) {
                log.error(logName, "SAP输出表异常:" + e);
            }
            /*1.5、结果*/
            result.put("status", 200);
            result.put("message", "end");
        } catch (Exception ex) {
            result.put("status", 500);
            result.put("message", "SAP Exception:" + ex);
        } finally {
            if (null != myConnection) {
                myConnection.disconnect();
            }
        }
        return result;
    }
}

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值