package com.seeyon.apps.bip.hr.event;

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.event.ACommonWorkflowEvent;
import com.seeyon.apps.ext.workflow.vo.FormDataVo;
import com.seeyon.apps.ext.workflow.vo.WorkflowEventContext;
import com.seeyon.cap4.form.bean.FormDataMasterBean;
import com.seeyon.ctp.common.AppContext;

/**
 * 功能描述:<br>
 * <pre>
 *
 * </pre>
 *
 * @Author: FanGaowei
 * @Date: 2023/2/22
 */
public abstract class HrCommonEvent extends ACommonWorkflowEvent {

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

    public abstract String getMethod();

    @Override
    protected WorkflowEventContext proceed(String request, FormDataVo formDataVo, WorkFlowType workFlowType, FormDataMasterBean formDataMasterBean) throws Exception {
        WorkflowEventContext context = new WorkflowEventContext();
        ConfigVo configVo = cstConfigApi.getConfig(getPluginId());
        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()) {
                context.setSuccess(true);
                context.setErrMsg("推送BIP成功:" + bipResponse.getMsg());
                formDataVo.getNewFieldDataMap().put("BIP返回结果", "推送BIP成功:" + bipResponse.getMsg());
            } else {
                if("当前操作人没有待办任务.".equals(bipResponse.getMsg())){
                    context.setSuccess(true);
                    context.setErrMsg("推送BIP成功:" + bipResponse.getMsg());
                    formDataVo.getNewFieldDataMap().put("BIP返回结果", "推送BIP成功:" + bipResponse.getMsg());
                }else{
                    context.setSuccess(false);
                    context.setErrMsg("推送BIP失败：" + bipResponse.getMsg());
                    formDataVo.getNewFieldDataMap().put("BIP返回结果", "推送BIP失败:" + bipResponse.getMsg());
                }
            }
        } catch (Exception e) {
            context.setSuccess(false);
            context.setErrMsg("推送BIP失败：OA处理异常：" + e.getMessage());
            formDataVo.getNewFieldDataMap().put("BIP返回结果", "推送BIP失败:OA处理异常" + e.getMessage());
        }
        return context;
    }

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

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

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