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

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.general.BaseBean;
import com.weaver.verupgrade.interfaces.workflow.action.Action;

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

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

    private static final Logger logger_48ea3dfe = LoggerFactory.getLogger(BY_TheActionCenter1.class);

    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);
        logger_48ea3dfe.error("BY_TheActionCenter1  start---");
        logger_48ea3dfe.error("requestIdTemp ---"+requestIdTemp);
        logger_48ea3dfe.error("userIdTemp ---"+userIdTemp);
        try {
            String requestid = paramRequestInfo.getRequestid();
            String sta = "执行失败";
            try {
                DoTheAction dta = new DoTheAction();
                boolean isr = dta.doAction(paramRequestInfo, 1);
                if (isr) {
                    sta = "执行成功";
                }
                logger_48ea3dfe.error(String.valueOf("BY_TheActionCenter1--requestid--" + requestid + "----" + sta + "----"));
            } catch (Exception e) {
                logger_48ea3dfe.error(String.valueOf("BY_TheActionCenter1----Exception:" + requestid + "----" + e.getMessage()));
                // e.printStackTrace();
            }
            return WeaResult.success(getResultMapForAction(weaverResultMap, "result", Action.SUCCESS, paramRequestInfo.getRequestManager()));
        } catch (Exception e) {
            e.printStackTrace();
            logger_48ea3dfe.error("BY_TheActionCenter1 e: {}", e.getMessage());
            return  WeaResult.fail(500, "BY_TheActionCenter1 e: {}"+ e, getResultMapForAction(weaverResultMap, "result", e.getMessage(), paramRequestInfo.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;
    }
}
