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

import java.rmi.RemoteException;
import org.apache.axis2.AxisFault;
import  com.weaver.seconddev.interfaces.oames.CallMesChuMenZhengService;
import  com.weaver.seconddev.interfaces.oames.OA_CMZ_SERVICEStub;
import  com.weaver.seconddev.interfaces.oames.OA_CMZ_SERVICEStub.ArrayOfOA_CMZ_RETURN_TYPE;
import  com.weaver.seconddev.interfaces.oames.OA_CMZ_SERVICEStub.OA_CMZ_INFO;
import  com.weaver.seconddev.interfaces.oames.OA_CMZ_SERVICEStub.OA_CMZ_RETURN_TYPE;
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.general.BaseBean;
import com.weaver.verupgrade.general.Util;
import com.weaver.verupgrade.soa.workflow.request.Property;
import com.weaver.verupgrade.hrm.User;
import com.weaver.verupgrade.interfaces.workflow.action.Action;

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

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

    @Autowired
    private CallMesChuMenZhengService mes;
    private static final Logger logger_44f7fe13 = LoggerFactory.getLogger(OA_CMZ_Action.class);

    public OA_CMZ_Action() {
    }

    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 {
            try {
                logger_44f7fe13.error(String.valueOf("开始获取取流程信息"));
                String requestid = requestinfo.getRequestid();
                String requestname = requestinfo.getRequestManager().getRequestname();
                Property[] properties = requestinfo.getMainTableInfo().getProperty();
                User user = requestinfo.getRequestManager().getUser();
                String spr = user.getLastname();
                String CMZDJH = "";
                String GCID = "";
                String FXZT = "";
                String QZYJ = "";
                String BMFZR = "";
                for (int i = 0; i < properties.length; i++) {
                    String name = properties[i].getName().toUpperCase();
                    String value = Util.null2String(properties[i].getValue());
                    if (name.equals("CMZDJH")) {
                        CMZDJH = value;
                    }
                    if (name.equals("GCID")) {
                        GCID = value;
                    }
                    if (name.equals("FXZT")) {
                        FXZT = value;
                    }
                    if (name.equals("QZYJ")) {
                        QZYJ = value;
                    }
                    if (name.equals("BMFZR")) {
                        BMFZR = value;
                    }
                }
                logger_44f7fe13.error(String.valueOf(requestid + " " + requestname + " " + CMZDJH + " " + GCID + " " + BMFZR + " " + FXZT + " " + QZYJ + " " + spr));
                OA_CMZ_SERVICEStub.OA_CMZ_INFO info = new OA_CMZ_SERVICEStub.OA_CMZ_INFO();
                OA_CMZ_SERVICEStub.ArrayOfOA_CMZ_INFO array = new OA_CMZ_SERVICEStub.ArrayOfOA_CMZ_INFO();
                OA_CMZ_SERVICEStub.OA_CMZ_MSG resultIn = new OA_CMZ_SERVICEStub.OA_CMZ_MSG();
                try {
                    info.setCMZDJH(CMZDJH);
                    info.setFXZT(FXZT);
                    info.setGCID(GCID);
                    info.setQZYJ(QZYJ);
                    info.setRequestId(requestid);
                    info.setRequestName(requestname);
                    info.setSPR(spr);
                    info.setBMFZR(BMFZR);
                } catch (Exception e) {
                    logger_44f7fe13.error(String.valueOf("当前流程信息赋值报错：" + e.toString()));
                }
                try {
                    logger_44f7fe13.error(String.valueOf("开始封装流程信息 "));
                    array.addOA_CMZ_INFO(info);
                    resultIn.setDATAS(array);
                } catch (NumberFormatException e) {
                    System.err.println(e.toString());
                    logger_44f7fe13.error(String.valueOf("封装报错 " + e.toString()));
                }
                logger_44f7fe13.error(String.valueOf("调用接口RequestId--" + requestid));
                String sta = "";
                String meg = "";
                // 调用接口
//                CallMesChuMenZhengService mes = new CallMesChuMenZhengService();
                mes.setOainput(resultIn);
                String[] rtInfo = mes.call();
                logger_44f7fe13.error(String.valueOf("调用接口完成--Istat--" + rtInfo[0] + "--Message--" + rtInfo[1]));
                // 存储流程是否调用成功
                sta = rtInfo[0];
                meg = rtInfo[1].replace("'", "’");
                String tablename = "";
                String wfid = requestinfo.getWorkflowid();
                // 流程信息类
                BillFieldUtil unit = new BillFieldUtil();
//                tablename = unit.getTablename(wfid);
                tablename = requestinfo.getRequestManager().getBillTableName();
                String logsql = "update e10_core_business.dbo." + tablename + " set status ='" + sta + "', message = '" + meg + "' where id = " + requestid;
                logger_44f7fe13.error(String.valueOf("logsql:" + logsql));
                RecordSet rs = I18nContextUtil.getBean(RecordSet.class);
                String poolname = CONN_TYPE.workflow.getType();
                // TODO E10执行sql方法第二参数必须指定源，默认使用流程源，单体客户无需修改，微服务/组合客户需根据查询表所属服务切换源，或使用外部数据源
                boolean b = rs.executeSql(logsql, poolname);
                logger_44f7fe13.error("b--"+b);
                logger_44f7fe13.error(String.valueOf("出门证接口调用: " + requestid + "完成"));
            } catch (Exception e) {
                System.err.println(e.toString());
                logger_44f7fe13.error(String.valueOf("调用接口报错 " + e.toString()));
                return  WeaResult.fail(500, "OA_CMZ_Action err"+ e, getResultMapForAction(weaverResultMap, "result",e.getMessage(), requestinfo.getRequestManager()));

            }
            return WeaResult.success(getResultMapForAction(weaverResultMap, "result", Action.SUCCESS, requestinfo.getRequestManager()));
        } catch (Exception e) {
            e.printStackTrace();
            logger_44f7fe13.error("OA_CMZ_Action e: {}", e.getMessage());
            return  WeaResult.fail(500, "OA_CMZ_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;
    }
}
