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

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.util.HashMap;
import java.util.Map;
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.general.BaseBean;
import com.weaver.verupgrade.general.Util;
import com.weaver.verupgrade.interfaces.workflow.action.Action;
import com.weaver.verupgrade.soa.workflow.request.*;
import com.weaver.seconddev.interfaces.workflow.action.EasTzlbzdApproveResult;
import com.weaver.verupgrade.soa.workflow.request.Property;

/**
 * oa-报账系统
 * 投资类报账单审批结果返回
 */
@org.springframework.stereotype.Service("workflow_action_EasTzlbzdApproveResultAction")
public class EasTzlbzdApproveResultAction extends BaseBean implements Action, EsbServerlessRpcRemoteInterface {

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

    @Autowired
    private EasTzlbzdApproveResult mGetResult;

    private static final Logger logger_a0a49142 = LoggerFactory.getLogger(EasTzlbzdApproveResultAction.class);

    @Override
    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);
        /**
         * 流程号*
         */
        // String requestNum = "";
        try {
            /**
             * 系统标识*
             */
            String systemFlag = "OA";
            /**
             * 请求ID*
             */
            String requestID = "";
            /**
             * 报账单Id*
             */
            String boeHeaderId = "";
            /**
             * 接口ID*
             */
            String ITFID = "";
            /**
             * 操作类型 1：通过 2：驳回*
             */
            String optType = "";
            /**
             * 流程号*
             */
            // String requestNum = "";
            try {
                Property[] properties = requestInfo.getMainTableInfo().getProperty();
                requestID = requestInfo.getRequestid();
                // requestNum = requestInfo.getWorkflowid();
                logger_a0a49142.error(String.valueOf("EasTzlbzdApproveResultAction--start--" + requestID));
                logger_a0a49142.error("requestIdTemp == " +requestIdTemp);
                logger_a0a49142.error("userIdTemp == " +userIdTemp);
                Map<String, String> mMap = 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());
                    mMap.put(name, value);
                    // writeLog("EasTzlbzdApproveResultAction name:" + name+"||value="+value);
                }
                boeHeaderId = mMap.get("BOEHEADERID");
                logger_a0a49142.error(String.valueOf("报账单Id:" + boeHeaderId));
                String state = mMap.get("STATE2");
                if ("0".equals(state)) {
                    optType = "Y";
                } else if ("1".equals(state)) {
                    optType = "N";
                }
                logger_a0a49142.error(String.valueOf("审批结果:" + state));
                // String lcbh = mMap.get("LCBH");
                JSONObject mJson = new JSONObject();
                mJson.put("systemFlag", systemFlag);
                // mJson.put("requestId",requestID);
                mJson.put("ApplyNo", boeHeaderId);
                mJson.put("ITFID", ITFID);
                // 操作类型 Y：通过 N：驳回
                mJson.put("optType", optType);
                // 请求id
                mJson.put("requestID", requestID);
                // mJson.put("OANumber",lcbh);//流程编号
//                EasTzlbzdApproveResult mGetResult = new EasTzlbzdApproveResult();
                logger_a0a49142.error(String.valueOf("mJson：" + mJson));
                String result = mGetResult.getApproveResult(mJson);
                logger_a0a49142.error(String.valueOf("调用报账系统投资类报账单审批接口返回值：" + result));
                if (!"E".equals(result)) {
                    JSONObject jsonObject = JSONObject.parseObject(result);
                    // 接口处理状态 S-成功，E-失败
                    String isTat = jsonObject.getString("istat");
                    // 处理消息
                    String srMessage = jsonObject.getString("srmsage");
                    if (!"S".equals(isTat)) {
                        // 失败
                        logger_a0a49142.error(String.valueOf("报账系统返回错误信息：" + srMessage));
                        requestInfo.getRequestManager().setMessageid("ERROR");
                        requestInfo.getRequestManager().setMessagecontent("报账系统返回错误信息：" + srMessage);
                        return  WeaResult.fail(500, "报账系统返回错误信息：" + srMessage , getResultMapForAction(weaverResultMap, "result", "报账系统返回错误信息：" + srMessage, requestInfo.getRequestManager()));

                    }
                } else {
                    logger_a0a49142.error(String.valueOf("调用报账系统投资类报账单审批接口返回失败，请联系报账系统管理员！"));
                    requestInfo.getRequestManager().setMessageid("ERROR");
                    requestInfo.getRequestManager().setMessagecontent("调用报账系统投资类报账单审批接口返回失败，请联系报账系统管理员！");
                    return  WeaResult.fail(500, "调用报账系统投资类报账单审批接口返回失败，请联系报账系统管理员！" , getResultMapForAction(weaverResultMap, "result", "调用报账系统投资类报账单审批接口返回失败，请联系报账系统管理员！", requestInfo.getRequestManager()));

                }
            } catch (Exception e) {
                logger_a0a49142.error(String.valueOf("调用报账系统投资类报账单审批接口返回失败，请联系报账系统管理员！"));
                requestInfo.getRequestManager().setMessageid("ERROR");
                requestInfo.getRequestManager().setMessagecontent("调用报账系统投资类报账单审批接口返回失败，请联系报账系统管理员！");
                return  WeaResult.fail(500, "调用报账系统投资类报账单审批接口返回失败，请联系报账系统管理员！"+e, getResultMapForAction(weaverResultMap, "result", e.getMessage(), requestInfo.getRequestManager()));

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