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

import com.sap.conn.jco.JCoDestination;
import com.sap.conn.jco.JCoFunction;
import  com.weaver.seconddev.interfaces.swfa.BillFieldUtil;
import com.weaver.common.i18n.tool.util.I18nContextUtil;
import com.weaver.verupgrade.conn.CONN_TYPE;
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.RecordSet;
import com.weaver.verupgrade.conn.RecordSetTrans;
import com.weaver.verupgrade.general.BaseBean;
import com.weaver.verupgrade.general.Util;
import com.weaver.verupgrade.soa.workflow.request.Property;
import com.weaver.verupgrade.interfaces.workflow.action.Action;



//不启用
@org.springframework.stereotype.Service("workflow_action_ZOA_SIGN_RFCAction")
public class ZOA_SIGN_RFCAction extends BaseBean implements Action, EsbServerlessRpcRemoteInterface {

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

    private static final Logger logger_43f5aff5 = LoggerFactory.getLogger(ZOA_SIGN_RFCAction.class);

    private JCoDestination destination;

    private String[] retstr;

    public ZOA_SIGN_RFCAction() {
        new SAPConn();
        destination = SAPConn.getJCoDestination();
        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 requestinfo = requestInfoTemp.getRequestInfo(requestIdTemp, userIdTemp);
        try {
            BillFieldUtil unit = new BillFieldUtil();
            String wfid = requestinfo.getWorkflowid();
//            String tablename = unit.getTablename(wfid);
            String tablename = requestinfo.getRequestManager().getBillTableName();
            String requestid = requestinfo.getRequestid();
            RecordSet rs = I18nContextUtil.getBean(RecordSet.class);
            retstr = new String[2];
            //TODO custom 不启用
//            try {
//                String sapId = "";
//                Property[] aproperty = requestinfo.getMainTableInfo().getProperty();
//                for (int i = 0; i < aproperty.length; i++) {
//                    String name = aproperty[i].getName().toUpperCase();
//                    String value = Util.null2String(aproperty[i].getValue());
//                    if (name.equals("BH")) {
//                        sapId = value;
//                    }
//                }
//                RecordSetTrans recordsettrans = I18nContextUtil.getBean(RecordSetTrans.class);
//                recordsettrans.setAutoCommit(false);
//                JCoFunction jcofunction = destination.getRepository().getFunction("ZOA_SIGN_RFC");
//                if (jcofunction == null) {
//                    logger_43f5aff5.error(String.valueOf("ZOA_SIGN_RFC not found in SAP."));
//                    retstr[0] = "E";
//                    retstr[1] = "没找到SAP的ZOA_SIGN_RFC函数";
//                } else {
//                    jcofunction.getImportParameterList().setValue("ID", sapId);
//                    logger_43f5aff5.error(String.valueOf("开始调用ZOA_SIGN_RFC函数"));
//                    jcofunction.execute(destination);
//                    retstr[0] = jcofunction.getExportParameterList().getValue("E_STATUS").toString();
//                    retstr[1] = jcofunction.getExportParameterList().getValue("E_MESSAGE").toString();
//                    logger_43f5aff5.error(String.valueOf("调用ZOA_SIGN_RFC函数成功：" + retstr[0] + "," + retstr[1]));
//                }
//                String sql = "update " + tablename + " set status= '" + retstr[0] + "',message= cast(message as varchar(8000)) +'," + retstr[1] + "' where requestId =" + requestid;
//                String poolname_pq = CONN_TYPE.workflow.getType();
//                // TODO E10执行sql方法第二参数必须指定源，默认使用流程源，单体客户无需修改，微服务/组合客户需根据查询表所属服务切换源，或使用外部数据源
//                rs.executeSql(sql, poolname_pq);
//                if (!"S".equals(retstr[0])) {
//                    requestinfo.getRequestManager().setMessageid("ERROR");
//                    requestinfo.getRequestManager().setMessagecontent("SAP返回错误信息：" + retstr[1]);
//                    return WeaResult.success(getResultMapForAction(weaverResultMap, "result", Action.SUCCESS, requestinfo.getRequestManager()));
//                }
//            } catch (Exception e) {
//                logger_43f5aff5.error(String.valueOf("调用ZOA_SIGN_RFC解冻出错：" + e.toString()));
//                requestinfo.getRequestManager().setMessageid("ERROR");
//                requestinfo.getRequestManager().setMessagecontent("调用SAP接口出错：" + e.getMessage());
//                String sql = "update " + tablename + " set status= '" + retstr[0] + "',message= cast(message as varchar(8000)) +'," + retstr[1] + "' where requestId =" + requestid;
//                String poolname = CONN_TYPE.workflow.getType();
//                // TODO E10执行sql方法第二参数必须指定源，默认使用流程源，单体客户无需修改，微服务/组合客户需根据查询表所属服务切换源，或使用外部数据源
//                rs.executeSql(sql, poolname);
//                return WeaResult.success(getResultMapForAction(weaverResultMap, "result", Action.SUCCESS, requestinfo.getRequestManager()));
//            }
            return WeaResult.success(getResultMapForAction(weaverResultMap, "result", Action.SUCCESS, requestinfo.getRequestManager()));
        } catch (Exception e) {
            e.printStackTrace();
            logger_43f5aff5.error("ZOA_SIGN_RFCAction e: {}", e.getMessage());
            return  WeaResult.fail(500, "ZOA_SIGN_RFCAction e: {}" + e, getResultMapForAction(weaverResultMap, "result", "", requestinfo.getRequestManager()));
        }
    }

    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;
    }
}
