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

import  com.weaver.seconddev.interfaces.k3.ApprovalResultService;
import  com.weaver.seconddev.interfaces.k3.SI_HTOAPurreqAPPROSync_OUTServiceStub.OAPRResultItemIn;
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.general.Util;
import com.weaver.verupgrade.soa.workflow.request.Property;
import com.weaver.verupgrade.interfaces.workflow.action.Action;

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

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

    @Autowired
    private ApprovalResultService service ;
    private static final Logger logger_c84d53c8 = LoggerFactory.getLogger(ReturnApprovalResultToK3Action.class);

    public ReturnApprovalResultToK3Action() {
    }

    public WeaResult<Map<String, Object>> execute(Map<String, Object> requestInfoMap) {
        logger_c84d53c8.error("ReturnApprovalResultToK3Action 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 request = requestInfoTemp.getRequestInfo(requestIdTemp, userIdTemp);
        try {
            String requestId = request.getRequestid();
            String workflowId = request.getWorkflowid();
            String[] str = new String[2];
            try {
                String FBillNo = "";
                String FResult = "";
                String syncSRM = "";
                Property[] properties = request.getMainTableInfo().getProperty();
                for (int i = 0; i < properties.length; i++) {
                    String name = properties[i].getName().toUpperCase();
                    String value = Util.null2String(properties[i].getValue());
                    if ("CGSQDH".equals(name)) {
                        FBillNo = value;
                        logger_c84d53c8.error(String.valueOf("FBillNo--" + FBillNo));
                    }
                    if ("SPJG".equals(name)) {
                        if ("0".equals(value)) {
                            FResult = "1";
                        } else {
                            FResult = "0";
                        }
                        logger_c84d53c8.error(String.valueOf("FResult--" + FResult));
                    }
                    if ("SFTB".equals(name)) {
                        if ("0".equals(value)) {
                            syncSRM = "1";
                        } else {
                            syncSRM = "0";
                        }
                        logger_c84d53c8.error(String.valueOf("syncSRM--" + syncSRM));
                    }
                }
                OAPRResultItemIn in = new OAPRResultItemIn();
                in.setFBillNo(FBillNo);
                in.setFMessage("");
                in.setFResult(FResult);
                in.setITFID("");
                in.setRequestId(requestId);
                in.setSyncSRM(syncSRM);
                in.setSYSID("");
                in.setWorkflowId(workflowId);
//                ApprovalResultService service = new ApprovalResultService();
                str = service.returnResult(in);
                logger_c84d53c8.error(String.valueOf("返回结果：" + str[0] + str[1]));
                if (!"1".equals(str[0])) {
                    logger_c84d53c8.error(String.valueOf(requestId + "调用K3返回审批结果接口出错：" + str[1]));
                    request.getRequestManager().setMessageid("ERROR");
                    request.getRequestManager().setMessagecontent("K3返回错误信息：" + str[1]);
                    return  WeaResult.fail(500, "ReturnApprovalResultToK3Action str[1]"+ str[1], getResultMapForAction(weaverResultMap, "result",str[1], request.getRequestManager()));
                }
            } catch (Exception e) {
                logger_c84d53c8.error(String.valueOf(requestId + "调用K3返回审批结果接口出错：" + e.toString()));
                request.getRequestManager().setMessageid("ERROR");
                request.getRequestManager().setMessagecontent("K3返回错误信息：" + e.toString());
                return  WeaResult.fail(500, "ReturnApprovalResultToK3Action err"+ e, getResultMapForAction(weaverResultMap, "result",e.getMessage(), request.getRequestManager()));
            }
            return WeaResult.success(getResultMapForAction(weaverResultMap, "result", Action.SUCCESS, request.getRequestManager()));
        } catch (Exception e) {
            e.printStackTrace();
            logger_c84d53c8.error("ReturnApprovalResultToK3Action e: {}", e.getMessage());
            return  WeaResult.fail(500, "ReturnApprovalResultToK3Action e {}"+ e, getResultMapForAction(weaverResultMap, "result",e.getMessage(), request.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;
    }
}
