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

import com.sap.conn.jco.JCoDestination;
import com.sap.conn.jco.JCoFunction;
import com.sap.conn.jco.JCoTable;
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_SD_CREDIT_APPROVAL_HANDLE")
public class ZOA_SD_CREDIT_APPROVAL_HANDLE implements Action, EsbServerlessRpcRemoteInterface {

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

    private static final Logger logger_2f1b447a = LoggerFactory.getLogger(ZOA_SD_CREDIT_APPROVAL_HANDLE.class);

    private JCoDestination destination;

    private String[] retstr;

    public ZOA_SD_CREDIT_APPROVAL_HANDLE() {
        new SAPConn();
        this.destination = SAPConn.getJCoDestination();
        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_2f1b447a.error("ZOA_SD_CREDIT_APPROVAL_HANDLE e: {}", e.getMessage());
            return  WeaResult.fail(500, "ZOA_SD_CREDIT_APPROVAL_HANDLE e: {}"+ e, getResultMapForAction(weaverResultMap, "result", e.getMessage(), paramRequestInfo.getRequestManager()));
        }
    }

    public String[] Getexecute(String paramString1, String[] paramString2) {
        this.retstr = new String[7];
        try {
            RecordSetTrans localRecordSetTrans = I18nContextUtil.getBean(RecordSetTrans.class);
            localRecordSetTrans.setAutoCommit(false);
            JCoFunction localJCoFunction = this.destination.getRepository().getFunction("ZOA_SD_CREDIT_APPROVAL_HANDLE");
            if (localJCoFunction == null) {
                System.out.println("ZOA_SD_CREDIT_APPROVAL_HANDLE not found in SAP.");
                this.retstr[5] = "ZOA_SD_CREDIT_APPROVAL_HANDLE not found in SAP.";
            } else {
                localJCoFunction.getImportParameterList().setValue("P_ACTION", paramString1);
                JCoTable localJCoTable = localJCoFunction.getTableParameterList().getTable("T_DATA");
                for (int i = 0; i < 1; i++) {
                    localJCoTable.appendRow();
                    localJCoTable.setValue("VBELN_VA", paramString2[0]);
                    localJCoTable.setValue("VBELN_VL", paramString2[1]);
                    localJCoTable.setValue("VKORG", paramString2[2]);
                    localJCoTable.setValue("EXISTANCE", paramString2[3]);
                    localJCoTable.setValue("APPROVAL", paramString2[4]);
                    localJCoTable.setValue("MESSAGE", paramString2[5]);
                    localJCoTable.setValue("KUNNR", paramString2[6]);
                }
                try {
                    localJCoFunction.execute(this.destination);
                    JCoTable localJCoTable1 = localJCoFunction.getTableParameterList().getTable("T_DATA");
                    if (localJCoTable1.getNumRows() > 0) {
                        localJCoTable1.setRow(1);
                        // 销售单
                        this.retstr[0] = localJCoTable1.getString("VBELN_VA").trim();
                        // 交货单
                        this.retstr[1] = localJCoTable1.getString("VBELN_VL").trim();
                        // 销售组织
                        this.retstr[2] = localJCoTable1.getString("VKORG").trim();
                        // QUERY查询返回状态S成功E失败
                        this.retstr[3] = localJCoTable1.getString("EXISTANCE").trim();
                        // APPROVAL审核返回状态S成功E失败
                        this.retstr[4] = localJCoTable1.getString("APPROVAL").trim();
                        this.retstr[5] = localJCoTable1.getString("MESSAGE").trim();
                        this.retstr[6] = localJCoTable1.getString("KUNNR").trim();
                    }
                } catch (Exception localException2) {
                    this.retstr[5] = localException2.toString();
                }
            }
            return this.retstr;
        } catch (Exception localException1) {
            this.retstr[5] = 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;
    }
}
