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_PR_CREATE_ZJGC")
public class ZOA_MM_PR_CREATE_ZJGC implements Action, EsbServerlessRpcRemoteInterface {

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

    private static final Logger logger_e1892d9f = LoggerFactory.getLogger(ZOA_MM_PR_CREATE_ZJGC.class);

    private JCoDestination destination;

    private int cursorindex;

    private int dataconts;

    private JCoTable datas;

    private String[] retstr;

    public ZOA_MM_PR_CREATE_ZJGC() {
        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_e1892d9f.error("ZOA_MM_PR_CREATE_ZJGC e: {}", e.getMessage());
            return  WeaResult.fail(500, "ZOA_ZOA_MM_PR_CREATE_ZJGCMM_PR_CREATE_YFFY e: {}"+ e, getResultMapForAction(weaverResultMap, "result", e.getMessage(), paramRequestInfo.getRequestManager()));
        }
    }

    public String[] Getexecute(String paramString1, String[][] paramString2) {
        this.retstr = new String[3];
        try {
            RecordSetTrans localRecordSetTrans = I18nContextUtil.getBean(RecordSetTrans.class);
            localRecordSetTrans.setAutoCommit(false);
            JCoFunction localJCoFunction = this.destination.getRepository().getFunction("ZOA_MM_PR_CREATE");
            if (localJCoFunction == null) {
                System.out.println("ZOA_MM_PR_CREATE not found in SAP.");
                this.retstr[2] = "ZOA_MM_PR_CREATE not found in SAP.";
            } else {
                localJCoFunction.getImportParameterList().setValue("I_TYPE", paramString1);
                JCoTable localJCoTable = localJCoFunction.getTableParameterList().getTable("T_PRITEM");
                JCoTable localJCoTable1 = localJCoFunction.getTableParameterList().getTable("T_PRITEMX");
                JCoTable localJCoTable2 = localJCoFunction.getTableParameterList().getTable("T_PRACCOUNT");
                JCoTable localJCoTable3 = localJCoFunction.getTableParameterList().getTable("T_PRACCOUNTX");
                JCoTable localJCoTable4 = localJCoFunction.getTableParameterList().getTable("T_PRITEMTEXT");
                for (int i = 0; i < paramString2.length; i++) {
                    localJCoTable.appendRow();
                    localJCoTable1.appendRow();
                    localJCoTable2.appendRow();
                    localJCoTable3.appendRow();
                    localJCoTable4.appendRow();
                    localJCoTable.setValue("PREQ_ITEM", paramString2[i][0]);
                    localJCoTable.setValue("ACCTASSCAT", paramString2[i][1]);
                    localJCoTable.setValue("SHORT_TEXT", paramString2[i][2]);
                    localJCoTable.setValue("QUANTITY", paramString2[i][3]);
                    localJCoTable.setValue("UNIT", paramString2[i][4]);
                    localJCoTable.setValue("DELIV_DATE", paramString2[i][5]);
                    localJCoTable.setValue("MATL_GROUP", paramString2[i][6]);
                    localJCoTable.setValue("PLANT", paramString2[i][7]);
                    localJCoTable.setValue("PUR_GROUP", paramString2[i][8]);
                    localJCoTable.setValue("PREQ_NAME", paramString2[i][9]);
                    if (paramString2[i].length == 13) {
                        if (paramString2[i][12] != null && !"".equals(paramString2[i][12])) {
                            localJCoTable.setValue("STORE_LOC", paramString2[i][12]);
                            localJCoTable1.setValue("STORE_LOC", "X");
                        }
                    }
                    localJCoTable1.setValue("PREQ_ITEM", paramString2[i][0]);
                    localJCoTable1.setValue("ACCTASSCAT", "X");
                    localJCoTable1.setValue("SHORT_TEXT", "X");
                    localJCoTable1.setValue("QUANTITY", "X");
                    localJCoTable1.setValue("UNIT", "X");
                    localJCoTable1.setValue("DELIV_DATE", "X");
                    localJCoTable1.setValue("MATL_GROUP", "X");
                    localJCoTable1.setValue("PLANT", "X");
                    localJCoTable1.setValue("PUR_GROUP", "X");
                    localJCoTable1.setValue("PREQ_NAME", "X");
                    localJCoTable2.setValue("PREQ_ITEM", paramString2[i][0]);
                    localJCoTable2.setValue("SERIAL_NO", "01");
                    localJCoTable2.setValue("ORDERID", paramString2[i][10]);
                    localJCoTable3.setValue("PREQ_ITEM", paramString2[i][0]);
                    localJCoTable3.setValue("SERIAL_NO", "01");
                    localJCoTable3.setValue("ORDERID", "X");
                    localJCoTable4.setValue("PREQ_NO", "*");
                    localJCoTable4.setValue("PREQ_ITEM", paramString2[i][0]);
                    localJCoTable4.setValue("TEXT_ID", "B01");
                    localJCoTable4.setValue("TEXT_FORM", "*");
                    localJCoTable4.setValue("TEXT_LINE", paramString2[i][11]);
                }
                try {
                    localJCoFunction.execute(this.destination);
                    this.retstr[0] = localJCoFunction.getExportParameterList().getValue("E_PRNO").toString();
                    this.retstr[1] = localJCoFunction.getExportParameterList().getValue("E_STATUS").toString();
                    this.retstr[2] = localJCoFunction.getExportParameterList().getValue("E_MESSAGE").toString();
                } catch (Exception localException2) {
                    this.retstr[2] = localException2.toString();
                }
            }
            return this.retstr;
        } catch (Exception localException1) {
            this.retstr[2] = localException1.getMessage();
            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;
    }
}
