package com.seeyon.apps.bip;

import com.seeyon.apps.bip.constants.BipConstants;
import com.seeyon.apps.bip.util.BipHttpUtil;
import com.seeyon.apps.bip.vo.BipResponse;
import com.seeyon.apps.common.config.ICstConfigApi;
import com.seeyon.apps.common.plugin.vo.ConfigVo;
import com.seeyon.apps.common.workflow.constants.WorkFlowType;
import com.seeyon.apps.common.workflow.node.ACommonSuperNode;
import com.seeyon.apps.ext.workflow.vo.FieldDataVo;
import com.seeyon.apps.ext.workflow.vo.FormDataVo;
import com.seeyon.apps.ext.workflow.vo.SuperNodeContext;
import com.seeyon.cap4.form.bean.FormDataMasterBean;
import com.seeyon.ctp.common.AppContext;

/**
 * 功能描述:<br>
 * <pre>
 *
 * </pre>
 *
 * @Author: FanGaowei
 * @Date: 2022/11/29
 */
public abstract class BIPCommonNode extends ACommonSuperNode {

    protected ICstConfigApi cstConfigApi = (ICstConfigApi) AppContext.getBean("cstConfigApi");

    @Override
    public String getPluginId() {
        return BipConstants.getPluginId();
    }

    @Override
    public String getFormParse() {
        return "json";
    }

    @Override
    public WorkFlowType[] getTypes() {
        return new WorkFlowType[] {WorkFlowType.superNode};
    }

    public ConfigVo getBipConfig() {
        return cstConfigApi.getConfig(getPluginId());
    }


    @Override
    public SuperNodeContext proceed(String request, FormDataVo formDataVo, FormDataMasterBean formDataMasterBean) throws Exception {
        SuperNodeContext context = new SuperNodeContext();
        try {
            FieldDataVo fhjg = formDataVo.getFieldData("BIP返回结果");
            String value = fhjg.getStringValue();
            if(value.startsWith("推送BIP成功")) {
                return context.success("跳过：" + value);
            }
        } catch (Exception e) {

        }
        ConfigVo configVo = getBipConfig();
        context.setNeedSave(true);
        String url = configVo.getParamVal(BipConstants.bipUrl.name());
        url += getMethod();
        context.setRequest(request);
        context.setUrl(url);
        try {
            String response = BipHttpUtil.doPost(url, request, configVo.getParamVal(BipConstants.clientId.name()), configVo.getParamVal(BipConstants.client_security.name()));
            context.setResponse(response);
            BipResponse bipResponse = BipHttpUtil.parseResponse(response);
            if(bipResponse.isSuccess()) {
                if("updateZhuanzuJinNode".equals(getNodeId())){
                    FieldDataVo fhjg = formDataVo.getFieldData("BIP返回结果");
                    String value = fhjg.getStringValue();
                    String bipReturn = "推送BIP成功:" + value+"；保证金转租金推送："+bipResponse.getMsg();
                    formDataVo.getNewFieldDataMap().put("BIP返回结果", bipReturn);
                }else if("updateBaozhengJinNode".equals(getNodeId())){
                    FieldDataVo fhjg = formDataVo.getFieldData("BIP返回结果");
                    String value = fhjg.getStringValue();
                    String bipReturn = "推送BIP成功:" + value+"；租赁合同收款(浮动租金)回写："+bipResponse.getMsg();
                    formDataVo.getNewFieldDataMap().put("BIP返回结果", bipReturn);
                }else{
                    formDataVo.getNewFieldDataMap().put("BIP返回结果", "推送BIP成功:" + bipResponse.getMsg());
                }
                context.success("推送BIP成功:" + bipResponse.getMsg(), false);
            } else {
                context.setErrMsg(bipResponse.getMsg());
                context.setException(true);
                context.back("推送BIP失败：" + bipResponse.getMsg());
                if(!("updateZhuanzuJinNode".equals(getNodeId()) || "updateBaozhengJinNode".equals(getNodeId()))){
                    formDataVo.getNewFieldDataMap().put("BIP返回结果", "推送BIP失败:" + bipResponse.getMsg());
                }
            }
        } catch (Exception e) {
            context.setException(true);
            context.back("推送BIP失败：OA处理异常：" + e.getMessage());
            if(!("updateZhuanzuJinNode".equals(getNodeId()) || "updateBaozhengJinNode".equals(getNodeId()))){
                formDataVo.getNewFieldDataMap().put("BIP返回结果", "推送BIP失败:OA处理异常" + e.getMessage());
            }

        }
        return context;
    }


    public abstract String getMethod();
}
