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

import com.sap.conn.jco.JCoDestination;
import com.sap.conn.jco.JCoFunction;
import com.weaver.common.i18n.tool.util.I18nContextUtil;
import java.lang.*;
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.general.BaseBean;
import com.weaver.verupgrade.interfaces.workflow.action.Action;

@org.springframework.stereotype.Service("workflow_action_ZOA_FI_CONUNIT_REDUNCHECK")
public class ZOA_FI_CONUNIT_REDUNCHECK extends BaseBean implements Action, EsbServerlessRpcRemoteInterface {

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

    private static final Logger logger_d28aa0c1 = LoggerFactory.getLogger(ZOA_FI_CONUNIT_REDUNCHECK.class);

    private JCoDestination destination;

    private String[][] retstr;

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

    public String[][] Getexecute(String[] paramString) {
        this.retstr = new String[2][39];
        BaseBean baseBean = new BaseBean();
        try {
            RecordSetTrans localRecordSetTrans = I18nContextUtil.getBean(RecordSetTrans.class);
            localRecordSetTrans.setAutoCommit(false);
            JCoFunction localJCoFunction = this.destination.getRepository().getFunction("ZOA_FI_CONUNIT_REDUNCHECK");
            if (localJCoFunction == null) {
                System.out.println("ZOA_FI_CONUNIT_REDUNCHECK not found in SAP.");
                this.retstr[0][1] = "ZOA_FI_CONUNIT_REDUNCHECK not found in SAP.";
            } else {
                // JCoStructure localJCoStructure = localJCoFunction.getImportParameterList().getStructure("IN");
                // 公司代码
                localJCoFunction.getImportParameterList().setValue("BUKRS", paramString[0]);
                // 供应商或债权人的账号
                localJCoFunction.getImportParameterList().setValue("LIFNR", paramString[1]);
                // 客户编号
                localJCoFunction.getImportParameterList().setValue("KUNNR", paramString[2]);
                // 纳税登记证代码
                localJCoFunction.getImportParameterList().setValue("STCD5", paramString[3]);
                // 名称
                localJCoFunction.getImportParameterList().setValue("NAME1", paramString[4]);
                // 往来单位类别 1=供应商 2=客户
                localJCoFunction.getImportParameterList().setValue("CUTYP", paramString[5]);
                logger_d28aa0c1.error(String.valueOf("BUKRS=" + paramString[0]));
                logger_d28aa0c1.error(String.valueOf("LIFNR=" + paramString[1]));
                logger_d28aa0c1.error(String.valueOf("KUNNR=" + paramString[2]));
                logger_d28aa0c1.error(String.valueOf("STCD5=" + paramString[3]));
                logger_d28aa0c1.error(String.valueOf("NAME1=" + paramString[4]));
                logger_d28aa0c1.error(String.valueOf("CUTYP=" + paramString[5]));
                try {
                    localJCoFunction.execute(this.destination);
                    logger_d28aa0c1.error(String.valueOf("ZOA_FI_CONUNIT_REDUNCHECK execute success"));
                    this.retstr[0][0] = localJCoFunction.getExportParameterList().getValue("E_STATUS").toString();
                    this.retstr[0][1] = localJCoFunction.getExportParameterList().getValue("E_MESSAGE").toString();
                    logger_d28aa0c1.error(String.valueOf("ZOA_FI_CONUNIT_REDUNCHECK 返回值 E_STATUS(" + this.retstr[0][0] + ") E_MESSAGE(" + this.retstr[0][1] + ")"));
                } catch (Exception localException2) {
                    logger_d28aa0c1.error(String.valueOf("localException2：" + localException2.getMessage()));
                    this.retstr[0][1] = localException2.toString();
                }
            }
            return this.retstr;
        } catch (Exception localException1) {
            logger_d28aa0c1.error(String.valueOf("localException1：" + localException1.getMessage()));
            this.retstr[0][1] = 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;
    }
}
