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

import java.util.Map;
import com.sap.conn.jco.JCoDestination;
import com.sap.conn.jco.JCoFunction;
import com.sap.conn.jco.JCoStructure;
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.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_FICO_COC_CREATION")
public class ZOA_FICO_COC_CREATION extends BaseBean implements Action, EsbServerlessRpcRemoteInterface {

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

    private static final Logger logger_d3a2e505 = LoggerFactory.getLogger(ZOA_FICO_COC_CREATION.class);

    private JCoDestination destination;

    public ZOA_FICO_COC_CREATION() {
        new SAPConn();
        this.destination = SAPConn.getJCoDestination();
    }

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

    public String[] Getexecute(Map<String, String> map) {
        BaseBean baseBean = new BaseBean();
        String[] retstr = new String[3];
        retstr[0] = "E";
        try {
            RecordSetTrans localRecordSetTrans = I18nContextUtil.getBean(RecordSetTrans.class);
            localRecordSetTrans.setAutoCommit(false);
            JCoFunction localJCoFunction = this.destination.getRepository().getFunction("ZOA_FICO_COC_CREATION");
            if (localJCoFunction == null) {
                logger_d3a2e505.error(String.valueOf("ZOA_FICO_COC_CREATION not found in SAP."));
                retstr[1] = "ZOA_FICO_COC_CREATION not found in SAP.";
            } else {
                JCoStructure localJCoStructure = localJCoFunction.getImportParameterList().getStructure("I_INPUT");
                // 成本中心编码
                localJCoStructure.setValue("KOSTL", map.get("CBZXDM"));
                // 起始日期
                localJCoStructure.setValue("DATAB", map.get("YXKSRQGH"));
                // 名称
                localJCoStructure.setValue("KTEXT", map.get("CBZXMCGH"));
                // 描述
                localJCoStructure.setValue("LTEXT", map.get("CBZXMSGH"));
                // 负责人
                localJCoStructure.setValue("VERAK", map.get("FZRGH"));
                // 成本中心类型
                localJCoStructure.setValue("KOSAR", map.get("CBZXLXDMGH"));
                // 层次结构范围
                localJCoStructure.setValue("KHINR", map.get("CCJGDMGH"));
                // 公司代码
                localJCoStructure.setValue("BUKRS", map.get("GSDMGH"));
                // 功能范围
                localJCoStructure.setValue("FKBER", map.get("GNFWDMGH"));
                // localJCoStructure.setValue("WAERS", map.get("HBDMGH")); //货币
                // 利润中心
                localJCoStructure.setValue("PRCTR", map.get("LRZXDMGH"));
                // 工厂
                localJCoStructure.setValue("ZZWERKS", map.get("GCDMGH"));
                localJCoFunction.execute(this.destination);
                retstr[0] = localJCoFunction.getExportParameterList().getValue("E_ISTAT").toString();
                retstr[1] = localJCoFunction.getExportParameterList().getValue("E_MESSAGE").toString();
                // 成本中心
                retstr[2] = localJCoFunction.getExportParameterList().getValue("E_KOSTL").toString();
            }
        } catch (Exception e1) {
            logger_d3a2e505.error(String.valueOf("e1：" + e1.getMessage()));
            retstr[1] = e1.getMessage();
        }
        return 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;
    }
}
