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

import com.alibaba.fastjson.JSONObject;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import com.weaver.seconddev.interfaces.prop.ConfigProp;
import  com.weaver.seconddev.interfaces.swfa.BillFieldUtil;
import com.weaver.common.i18n.tool.util.I18nContextUtil;
import org.springframework.core.env.Environment;
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.hrm.resource.ResourceComInfo;
import com.weaver.verupgrade.soa.workflow.request.MainTableInfo;
import com.weaver.verupgrade.soa.workflow.request.Property;
import com.weaver.verupgrade.interfaces.workflow.action.Action;

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

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

    private static final Logger logger_c3039127 = LoggerFactory.getLogger(OaSrmVendorPunishCommitAction.class);

    @Autowired
    private SendPostRequest_PO mGetResult ;
    private String SENDID;

    private String ITFID;

    private String SYSID;

    @Autowired
    private ConfigProp configProp;
    public OaSrmVendorPunishCommitAction() {
        this.SENDID = "";
        this.ITFID = "HT01BDS001";
        this.SYSID = "SYS004";
    }

    public WeaResult<Map<String, Object>> execute(Map<String, Object> requestInfoMap) {
        logger_c3039127.error("OaSrmVendorPunishCommitAction 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 requestID = "";
            try {
                ResourceComInfo hr = I18nContextUtil.getBean(ResourceComInfo.class);
                BillFieldUtil unit = new BillFieldUtil();
                String workFlowId = requestInfo.getWorkflowid();
                Property[] properties = requestInfo.getMainTableInfo().getProperty();
                requestID = requestInfo.getRequestid();
                logger_c3039127.error(String.valueOf("OaSrmVendorPunishCommitAction--start--" + requestID));
                Map mMap = new HashMap();
                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);
                }
                String sfqzzx = (String) mMap.get("SFQZZX");
                String uuid = UUID.randomUUID().toString();
                JSONObject mReqJson = new JSONObject();
                mReqJson.put("SENDID", uuid);
                mReqJson.put("ITFID", this.ITFID);
                mReqJson.put("SYSID", this.SYSID);
                JSONObject mJson = new JSONObject();
                mJson.put("workflowId", workFlowId);
                mJson.put("requestId", requestID);
                mJson.put("lcbh", mMap.get("LCBH"));
                String sqdw = (String) mMap.get("SQDW");
                String sqdwname = unit.getSubcompanyname(sqdw);
                mJson.put("companyCode", mMap.get("GSBM"));
                mJson.put("companyName", mMap.get("GSMC"));
                String sqbm = (String) mMap.get("SQBM");
                String sqbmname = unit.getDepartmentname(sqbm);
                mJson.put("departmentName", sqbmname);
                String sqr = (String) mMap.get("SQR");
                String sqrcode = (String) mMap.get("GH");
                String sqrname = "";
                if (!(sqr.equals(""))) {
                    sqrname = hr.getResourcename(sqr);
                }
                mJson.put("applyName", sqrname);
                mJson.put("applyEmpNo", sqrcode);
                mJson.put("vendorCode", mMap.get("GYSBM"));
                mJson.put("vendorName", mMap.get("GYSMC"));
                mJson.put("materialDesc", mMap.get("WLMS"));
                if (sfqzzx.equals("0")) {
                    mJson.put("requestType", "4");
                } else {
                    mJson.put("requestType", "3");
                }
                String placecode = (String) mMap.get("FSDD");
                String placename = BillFieldUtil.getselectNameByCol("fsdd", workFlowId, placecode);
                mJson.put("happenPlace", placename);
                mJson.put("happenDate", mMap.get("FSRQ"));
                mJson.put("qcDesc", mMap.get("ZLWTMS"));
                mJson.put("materialLoss", mMap.get("CLSS"));
                mJson.put("achievementsFines", mMap.get("JXFK"));
                mJson.put("otherLossesAndFines", mMap.get("QTFK"));
                mJson.put("totalAmount", mMap.get("SPHJ"));
                mReqJson.put("DATAS", mJson);
                logger_c3039127.error("调用SRM供应商绩效处罚信息同步接口请求数据：=====" + mReqJson.toString());
//                String url = Util.null2String(I18nContextUtilcus.getBean(Environment.class).getProperty("htsrmoasync.OaSrmVendorPunish")).trim();
                String url = configProp.getOaSrmVendorPunish() ;
                logger_c3039127.error("调用SRM供应商绩效处罚信息同步接口地址：" + url);
//                SendPostRequest_PO mGetResult = new SendPostRequest_PO();
                String result = mGetResult.getResult(mReqJson, url);
                logger_c3039127.error(String.valueOf("调用SRM供应商绩效处罚信息同步接口请求数据：" + result));
                if (!("E".equals(result))) {
                    JSONObject jsonObject = JSONObject.parseObject(result);
                    JSONObject returnJson = jsonObject.getJSONObject("RETURN");
                    String isTat = returnJson.getString("istat");
                    String srMessage = returnJson.getString("message");
                    logger_c3039127.error("SRM系统返回数据：" + jsonObject.toString());
                    if (!("S".equals(isTat))) {
                        logger_c3039127.error(String.valueOf("SRM返回错误信息：" + srMessage));
                        requestInfo.getRequestManager().setMessagecontent("SRM返回错误信息：" + srMessage);
                        return  WeaResult.fail(500, "OaSrmVendorPunishCommitAction srMessage"+ srMessage, getResultMapForAction(weaverResultMap, "result",srMessage, requestInfo.getRequestManager()));
                    }
                } else {
                    logger_c3039127.error(String.valueOf("调用SRM系统接口返回失败，请联系SRM系统管理员！"));
                    requestInfo.getRequestManager().setMessagecontent("调用SRM接口返回失败，请联系SRM系统管理员！");
                    return  WeaResult.fail(500, "调用SRM接口返回失败，请联系SRM系统管理员！", getResultMapForAction(weaverResultMap, "result","调用SRM接口返回失败，请联系SRM系统管理员！", requestInfo.getRequestManager()));
                }
            } catch (Exception e) {
                logger_c3039127.error(String.valueOf("提交失败！"));
                logger_c3039127.error(String.valueOf("Error======" + e.getMessage()));
                requestInfo.getRequestManager().setMessagecontent("提交失败！");
                return  WeaResult.fail(500, "OaSrmVendorPunishCommitAction err}"+ e, getResultMapForAction(weaverResultMap, "result",e.getMessage(), requestInfo.getRequestManager()));
            }
            return WeaResult.success(getResultMapForAction(weaverResultMap, "result", "1", requestInfo.getRequestManager()));
        } catch (Exception e) {
            e.printStackTrace();
            logger_c3039127.error("OaSrmVendorPunishCommitAction e: {}", e.getMessage());
            return  WeaResult.fail(500, "OaSrmVendorPunishCommitAction 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;
    }
}
