package com.seeyon.apps.ydctu8c;

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.apps.ydctu8c.constants.U8cConstants;
import com.seeyon.apps.ydctu8c.util.Cap4ReturnUtil;
import com.seeyon.apps.ydctu8c.util.ParamUtil;
import com.seeyon.apps.ydctu8c.util.U8cHttpUtil;
import com.seeyon.apps.ydctu8c.vo.U8cResponse;
import com.seeyon.cap4.form.api.FormDataApi4Cap4;
import com.seeyon.cap4.form.bean.FormDataMasterBean;
import com.seeyon.ctp.common.AppContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 功能描述:<br>
 * <pre>
 *
 * </pre>
 *
 * @Author: HuangZhengguo
 * @Date: 2024/4/28
 */
public abstract class U8CCommonNode extends ACommonSuperNode {

    private static Log log = LogFactory.getLog(U8CCommonNode.class);

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

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

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

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

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


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

        }
        ConfigVo configVo = getU8cConfig();
        context.setNeedSave(true);
        String url = configVo.getParamVal(U8cConstants.u8cUrl.name());
        url += getMethod();
        context.setRequest(param);
        context.setUrl(url);
        try {
            String u8cSystem = configVo.getParamVal(U8cConstants.u8cSystem.name());
            String u8cUsercode = configVo.getParamVal(U8cConstants.u8cUsercode.name());
            String u8cPassword = configVo.getParamVal(U8cConstants.u8cPassword.name());
            String u8cContentType = configVo.getParamVal(U8cConstants.u8cContentType.name());
            String u8cConnection = configVo.getParamVal(U8cConstants.u8cConnection.name());
            log.info(param);
            String response = U8cHttpUtil.doPost(url, param,u8cSystem,u8cUsercode,u8cPassword,u8cConnection,u8cContentType);
            System.out.println(response);
            context.setResponse(response);
            U8cResponse u8cResponse = U8cHttpUtil.parseResponse(response);
            if(u8cResponse.isSuccess()) {
                cap4ReturnUtil.setParamReturn(getMethod(),u8cResponse,formDataVo);
                System.out.println("参数处理完成"+u8cResponse);
                log.info(u8cResponse.getData());
                System.out.println("推送成功显示"+u8cResponse.getStatus());
                context.success("推送U8C成功:" + u8cResponse.getStatus(), false);
                formDataVo.getNewFieldDataMap().put("U8C返回结果", "推送U8C成功:" + u8cResponse.getStatus());
            } else {
                context.setErrMsg(u8cResponse.getMsg());
                context.setException(true);
                context.back("推送U8C失败：" + u8cResponse.getMsg());
                formDataVo.getNewFieldDataMap().put("U8C返回结果", "推送U8C失败:" + u8cResponse.getMsg());
            }
        } catch (Exception e) {
            context.setException(true);
            context.back("推送BIP失败：OA处理异常：" + e.getMessage());
            formDataVo.getNewFieldDataMap().put("BIP返回结果", "推送U8C失败:OA处理异常" + e.getMessage());
        }
        return context;
    }


    public abstract String getMethod();
}
