package com.weaver.seconddev.interfaces.workflow.action;

import com.sap.conn.jco.JCoDestination;
import com.sap.conn.jco.JCoFunction;
import com.sap.conn.jco.JCoParameterList;
import com.sap.conn.jco.JCoRepository;
import com.sap.conn.jco.JCoTable;
import java.io.PrintStream;
import com.weaver.common.i18n.tool.util.I18nContextUtil;
import java.util.*;
import com.weaver.verupgrade.workflow.request.RequestManager;
import com.weaver.esb.api.rpc.EsbServerlessRpcRemoteInterface;
import com.weaver.common.base.entity.result.WeaResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import com.weaver.verupgrade.soa.workflow.request.RequestInfo;
import java.util.Map;
import java.util.HashMap;
import com.weaver.verupgrade.conn.RecordSetTrans;
import com.weaver.verupgrade.interfaces.workflow.action.Action;

@org.springframework.stereotype.Service("workflow_action_ZOA_MM_MA_VALIDITY")
public class ZOA_MM_MA_VALIDITY implements Action, EsbServerlessRpcRemoteInterface {

    // 对应E9的请求信息对象
    @Autowired
    private RequestInfo requestInfoTemp;

    public static final org.slf4j.Logger logger_e2f735b0 = LoggerFactory.getLogger(ZOA_MM_MA_VALIDITY.class);

    private JCoDestination destination;

    private int cursorindex;

    private int dataconts;

    private JCoTable datas;

    private String[][] retstr;

    public ZOA_MM_MA_VALIDITY() {
        new SAPConn();
        this.destination = SAPConn.getJCoDestination();
        this.cursorindex = -1;
        this.dataconts = 0;
        this.datas = null;
        this.retstr = null;
    }

    public WeaResult<Map<String, Object>> execute(Map<String, Object> requestInfoMap) {
        Map<String, Object> weaverResultMap = new HashMap<>();
        Long requestIdTemp = Long.parseLong(String.valueOf(requestInfoMap.getOrDefault("requestid", -1)));
        Long userIdTemp = Long.parseLong(String.valueOf(requestInfoMap.getOrDefault("userid", -1)));
        RequestInfo paramRequestInfo = requestInfoTemp.getRequestInfo(requestIdTemp, userIdTemp);
        try {
            return WeaResult.success(getResultMapForAction(weaverResultMap, "result", "1", paramRequestInfo.getRequestManager()));
        } catch (Exception e) {
            e.printStackTrace();
            logger_e2f735b0.error("ZOA_MM_MA_VALIDITY e: {}", e.getMessage());
            return  WeaResult.fail(500, "ZOA_MM_MA_VALIDITY e {}"+ e, getResultMapForAction(weaverResultMap, "result",e.getMessage(), paramRequestInfo.getRequestManager()));
        }
    }

    public String[][] Getexecute(String[][] paramString) {
        for (int i = 1; i < paramString.length; i++) {
            for (int j = 0; j < paramString[i].length; j++) {
                logger_e2f735b0.error("ZOA_MM_MA_VALIDITY paramString["+i+"]["+j+"]--" + paramString[i][j]);
            }
        }
        this.retstr = paramString;
        try {
            RecordSetTrans localRecordSetTrans = I18nContextUtil.getBean(RecordSetTrans.class);
            localRecordSetTrans.setAutoCommit(false);
            JCoFunction localJCoFunction = this.destination.getRepository().getFunction("ZOA_MM_MA_VALIDITY");
            if (localJCoFunction == null) {
                System.out.println("ZOA_MM_MA_VALIDITY not found in SAP.");
                this.retstr[0][1] = "ZOA_MM_MA_VALIDITY not found in SAP.";
            } else {
                JCoTable localJCoTable = localJCoFunction.getTableParameterList().getTable("T_INOUT");
                for (int i = 1; i < paramString.length; i++) {
                    if (paramString[i][0].equals("aaaa")) {
                        continue;
                    } else {
                        localJCoTable.appendRow();
                        localJCoTable.setValue("INDEX", paramString[i][0]);
                        localJCoTable.setValue("MATKL", paramString[i][1]);
                        localJCoTable.setValue("WGBEZ", paramString[i][2]);
                        localJCoTable.setValue("MATNR", paramString[i][3]);
                        localJCoTable.setValue("MAKTX", paramString[i][4]);
                        logger_e2f735b0.error("ZOA_MM_MA_VALIDITY paramString["+i+"][5]--" + paramString[i][5]);
                        localJCoTable.setValue("T01", paramString[i][5]);
                        localJCoTable.setValue("T02", paramString[i][6]);
                        localJCoTable.setValue("T03", paramString[i][7]);
                        localJCoTable.setValue("T04", paramString[i][8]);
                        localJCoTable.setValue("T05", paramString[i][9]);
                        localJCoTable.setValue("T06", paramString[i][10]);
                        localJCoTable.setValue("T07", paramString[i][11]);
                        localJCoTable.setValue("T08", paramString[i][12]);
                        localJCoTable.setValue("T09", paramString[i][13]);
                        localJCoTable.setValue("T10", paramString[i][14]);
                        localJCoTable.setValue("T11", paramString[i][15]);
                        localJCoTable.setValue("T12", paramString[i][16]);
                        localJCoTable.setValue("T13", paramString[i][17]);
                        localJCoTable.setValue("T14", paramString[i][18]);
                        localJCoTable.setValue("T15", paramString[i][19]);
                    }
                }
                try {
                    localJCoFunction.execute(this.destination);
                    this.retstr[0][0] = localJCoFunction.getExportParameterList().getValue("E_STATUS").toString();
                    this.retstr[0][1] = localJCoFunction.getExportParameterList().getValue("E_MESSAGE").toString();
                    JCoTable localJCoTable1 = localJCoFunction.getTableParameterList().getTable("T_INOUT");
                    int z = -1;
                    for (int j = 1; j < retstr.length; j++) {
                        if (retstr[j][0].equals("aaaa")) {
                            continue;
                        } else {
                            z = z + 1;
                            localJCoTable1.setRow(z);
                            this.retstr[j][3] = localJCoTable1.getString("MATNR").trim();
                            this.retstr[j][4] = localJCoTable1.getString("MAKTX").trim();
                            this.retstr[j][20] = localJCoTable1.getString("STATUS").trim();
                            this.retstr[j][21] = localJCoTable1.getString("MESSAGE").trim();
                        }
                    }
                } catch (Exception localException2) {
                    this.retstr[0][1] = localException2.toString();
                    logger_e2f735b0.error("Exception--", localException2);
                }
            }
            return this.retstr;
        } catch (Exception localException1) {
            this.retstr[0][1] = localException1.getMessage();
            logger_e2f735b0.error("Exception--", localException1);
            return this.retstr;
        }
    }

    Map<String, Object> getResultMapForAction(Map<String, Object> map, String key, Object value, RequestManager requestManager) {
        if (key != null && !key.isEmpty()) {
            map.put(key, value);
        }
        String msgContent = requestManager.getMessagecontent();
        if (msgContent != null && !msgContent.isEmpty()) {
            map.put("msgContent", msgContent);
        }
        String msgId = requestManager.getMessageid();
        if (msgId != null && !msgId.isEmpty()) {
            map.put("msgId", msgId);
        }
        return map;
    }
}
