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<Map<String, Object>> returnList = new ArrayList<Map<String, Object>>();
    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<Map<String, Object>> 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;
    }

}
