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

import  com.weaver.seconddev.interfaces.swfa.BillFieldUtil;
import java.util.HashMap;
import java.util.Map;
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 com.weaver.verupgrade.conn.RecordSet;
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_FI_IORD_RFC_Action")
public class ZOA_FI_IORD_RFC_Action extends BaseBean implements Action, EsbServerlessRpcRemoteInterface {

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

    private static final Logger logger_a0e600a3 = LoggerFactory.getLogger(ZOA_FI_IORD_RFC_Action.class);

    @Override
    public WeaResult<Map<String, Object>> execute(Map<String, Object> requestInfoMap) {
        logger_a0e600a3.error("ZOA_FI_IORD_RFC_Action start");
        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 {
            String requestid = requestinfo.getRequestid();
            try {
                logger_a0e600a3.error(String.valueOf("ZOA_FI_IORD_RFC_Action--start--" + requestid));
                String workflowid = requestinfo.getWorkflowid();
                Property[] properties = requestinfo.getMainTableInfo().getProperty();
                String[][] param = new String[1][21];
                Map<String, String> map = new HashMap<String, String>();
                for (int i = 0; i < properties.length; i++) {
                    String name = properties[i].getName().toUpperCase();
                    String value = Util.null2String(properties[i].getValue());
                    map.put(name, value);
                }
                if ("0".equals(map.get("YWLX")) || "1".equals(map.get("YWLX"))) {
                    BillFieldUtil util = new BillFieldUtil();
//                    String tablename = util.getTablename(workflowid);
                    String tablename = requestinfo.getRequestManager().getBillTableName();
                    param[0][0] = "HT01";
                    param[0][1] = (util.getselectNameByCol("DDLX", workflowid, map.get("DDLX"))).substring(0, 4);
                    param[0][2] = map.get("GSDM");
                    param[0][3] = map.get("GCDM");
                    param[0][4] = map.get("LRZX");
                    param[0][5] = map.get("FZDCBZX");
                    param[0][6] = map.get("XMMS");
                    param[0][7] = map.get("SQR");
                    param[0][8] = map.get("SQRDH");
                    param[0][9] = map.get("XMFZR");
                    param[0][10] = map.get("FZRDH");
                    param[0][11] = "";
                    param[0][12] = map.get("HB");
                    param[0][13] = map.get("SQRQ").replaceAll("-", "");
                    param[0][14] = map.get("BM");
                    param[0][15] = map.get("GZKS").replaceAll("-", "");
                    param[0][16] = map.get("GZJS").replaceAll("-", "");
                    String gnfw =  map.get("GNFW");
                    if(gnfw != "" && gnfw != null && gnfw.length() > 0){
                        param[0][17] = gnfw.substring(0, 4);
                    }else {
                        param[0][17] = "" ;
                    }
                    param[0][18] = "OC";
                    param[0][19] = "";
                    if ("0".equals(map.get("YWLX"))) {
                        param[0][19] = "X";
                    }
                    param[0][20] = "";
                    ZOA_FI_IORD_RFC rfc = new ZOA_FI_IORD_RFC();
                    String[][] ret = rfc.Getexecute("", param);
                    logger_a0e600a3.error(String.valueOf("ZOA_FI_IORD_RFC_Action--info--[" + ret[0][0] + "-" + ret[0][1] + "-" + ret[0][2] + "]-" + requestid));
                    RecordSet rs = I18nContextUtil.getBean(RecordSet.class);
                    String poolname = CONN_TYPE.workflow.getType();
                    // TODO E10执行sql方法第二参数必须指定源，默认使用流程源，单体客户无需修改，微服务/组合客户需根据查询表所属服务切换源，或使用外部数据源
                    String sql = "update e10_core_business.dbo." + tablename + " set state='" + ret[0][0] + "',message='" + ret[0][1] + "',nbddh=isnull('" + ret[0][2] + "','') where id=" + requestid;
                    logger_a0e600a3.error(String.valueOf("ZOA_FI_IORD_RFC_Action--sql--" + sql + "-" + requestid));
                    boolean b = rs.executeSql(sql, poolname);
                    logger_a0e600a3.error(String.valueOf("ZOA_FI_IORD_RFC_Action--b--" + b + "-" + requestid));
                    if (!"S".equals(ret[0][0])) {
                        requestinfo.getRequestManager().setMessageid("ERROR");
                        requestinfo.getRequestManager().setMessagecontent("SAP返回错误信息：" + ret[0][1]);
                        return  WeaResult.fail(500, "ZOA_FI_IORD_RFC_Action ret[0][1]"+  ret[0][1], getResultMapForAction(weaverResultMap, "result", ret[0][1], requestinfo.getRequestManager()));
                    }
                }
            } catch (Exception e) {
                requestinfo.getRequestManager().setMessageid("ERROR");
                requestinfo.getRequestManager().setMessagecontent("SAP返回错误信息：" + e.toString());
                logger_a0e600a3.error(String.valueOf("ZOA_FI_IORD_RFC_Action--error--[" + e.toString() + "]-" + requestid));
                return  WeaResult.fail(500, "ZOA_FI_IORD_RFC_Action err"+ e, getResultMapForAction(weaverResultMap, "result",e.getMessage(), requestinfo.getRequestManager()));
            }
            logger_a0e600a3.error(String.valueOf("ZOA_FI_IORD_RFC_Action--end--" + requestid));
            return WeaResult.success(getResultMapForAction(weaverResultMap, "result", Action.SUCCESS, requestinfo.getRequestManager()));
        } catch (Exception e) {
            e.printStackTrace();
            logger_a0e600a3.error("ZOA_FI_IORD_RFC_Action e: {}", e.getMessage());
            return  WeaResult.fail(500, "ZOA_FI_IORD_RFC_Action e {}"+ e, getResultMapForAction(weaverResultMap, "result",e.getMessage(), 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;
    }
}
