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

import com.alibaba.fastjson.JSONObject;
import  com.weaver.seconddev.interfaces.scfoa.GetApproveResult;
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.soa.workflow.request.Property;
import com.weaver.verupgrade.interfaces.workflow.action.Action;

/**
 * 开票申请审批接口
 */
@org.springframework.stereotype.Service("workflow_action_ScfDraftApplyResultAction")
public class ScfDraftApplyResultAction extends BaseBean implements Action, EsbServerlessRpcRemoteInterface {

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

    @Autowired
    private GetApproveResult mGetResult;

    private static final Logger logger_d39bef7 = LoggerFactory.getLogger(ScfDraftApplyResultAction.class);

    @Override
    public WeaResult<Map<String, Object>> execute(Map<String, Object> requestInfoMap) {
        logger_d39bef7.error("ScfDraftApplyResultAction 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 systemFlag = "OA";
            String applyNo = "";
            String requestID = requestInfo.getRequestid();
            // 操作类型，1-通过，2-驳回
            String optType = "";
            // 接口ID
            String ITFID = "";
            // 流程id
            String flowid = requestInfo.getWorkflowid();
            try {
                logger_d39bef7.error(String.valueOf("ScfDraftApplyResultAction--start--" + requestID));
                Property[] properties = requestInfo.getMainTableInfo().getProperty();
                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);
                }
                if ("100003460000004205".equals(flowid)) {
                    // 开票申请
                    applyNo = mMap.get("APPLYNO");
                    ITFID = "HT01OA003";
                } else if ("100003460000004206".equals(flowid)) {
                    // 直贷业务申请
                    applyNo = mMap.get("DISCOUNTNO");
                    ITFID = "HT01OA004";
                }
                logger_d39bef7.error(String.valueOf("申请单号:" + applyNo));
                // state 1批准，2驳回
                optType = mMap.get("STATE");
                logger_d39bef7.error(String.valueOf("流程结果:" + optType));
                JSONObject mJson = new JSONObject();
                mJson.put("systemFlag", systemFlag);
                mJson.put("applyNo", applyNo);
                mJson.put("requestId", requestID);
                mJson.put("optType", optType);
                mJson.put("itfid", ITFID);
                mJson.put("rate", mMap.get("RATE"));
//                GetApproveResult mGetResult = new GetApproveResult();
                String result = mGetResult.getApproveResult(mJson);
                logger_d39bef7.error(mJson.toString());
                logger_d39bef7.error(String.valueOf(requestID + "调用供应链金融系统返回审批结果接口返回值：" + 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_d39bef7.error(String.valueOf(requestID + "供应链金融系统返回错误信息：" + srMessage));
                        // requestInfo.getRequestManager().setMessageid("ERROR");
                        requestInfo.getRequestManager().setMessagecontent("供应链金融系统返回错误信息：" + srMessage);
                        return WeaResult.fail(500, "执行失败:"+srMessage, getResultMapForAction(weaverResultMap, "result", FAILURE_AND_CONTINUE, requestInfo.getRequestManager()));
                    }
                } else {
                    logger_d39bef7.error(String.valueOf("调用供应链金融系统开票申请审批接口返回失败，请联系供应链金融系统管理员！"));
                    // requestInfo.getRequestManager().setMessageid("ERROR");
                    requestInfo.getRequestManager().setMessagecontent("调用供应链金融系统开票申请审批接口返回失败，请联系供应链金融系统管理员！");
                    return WeaResult.fail(500, "调用供应链金融系统开票申请审批接口返回失败，请联系供应链金融系统管理员！"+mJson.toString(), getResultMapForAction(weaverResultMap, "result", FAILURE_AND_CONTINUE, requestInfo.getRequestManager()));
                }
                logger_d39bef7.error(String.valueOf("ScfDraftApplyResultAction--end--" + requestID));
            } catch (Exception e) {
                logger_d39bef7.error(String.valueOf(requestID + "调用供应链金融系统返回审批结果接口出错：" ), e);
                // requestInfo.getRequestManager().setMessageid("ERROR");
                requestInfo.getRequestManager().setMessagecontent("调用供应链金融系统开票申请审批接口返回失败，请联系供应链金融系统管理员！");
                return  WeaResult.fail(500, "ScfDraftApplyResultAction e rr"+ e, getResultMapForAction(weaverResultMap, "result",e.getMessage(), requestInfo.getRequestManager()));
            }
            return WeaResult.success(getResultMapForAction(weaverResultMap, "result", Action.SUCCESS, requestInfo.getRequestManager()));
        } catch (Exception e) {
            e.printStackTrace();
            logger_d39bef7.error("ScfDraftApplyResultAction e: {}", e);
            return  WeaResult.fail(500, "ScfDraftApplyResultAction 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;
    }
}
